8000 Value property to PyDom.Element and ElementCollection (#1828) · shivashankarv/pyscript@e81830a · GitHub
[go: up one dir, main page]

Skip to content

Commit e81830a

Browse files
Value property to PyDom.Element and ElementCollection (pyscript#1828)
* add base test for input value field * add value property to Element * add test for non supported element * prevent users to set value attribute on elements that do not support it * add test for setting value on collections * add value property to collection and add more tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 54df717 commit e81830a

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

pyscript.core/src/stdlib/pyweb/pydom.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ def id(self):
131131
def id(self, value):
132132
self._js.id = value
133133

134+
@property
135+
def value(self):
136+
return self._js.value
137+
138+
@value.setter
139+
def value(self, value):
140+
# in order to avoid confusion to the user, we don't allow setting the
141+
# value of elements that don't have a value attribute
142+
if not hasattr(self._js, "value"):
143+
raise AttributeError(
144+
f"Element {self._js.tagName} has no value attribute. If you want to "
145+
"force a value attribute, set it directly using the `_js.value = <value>` "
146+
"javascript API attribute instead."
147+
)
148+
self._js.value = value
149+
134150
def clone(self, new_id=None):
135151
clone = Element(self._js.cloneNode(True))
136152
clone.id = new_id
@@ -264,6 +280,14 @@ def html(self):
264280
def html(self, value):
265281
self._set_attribute("html", value)
266282

283< 8000 /td>+
@property
284+
def value(self):
285+
return self._get_attribute("value")
286+
287+
@value.setter
288+
def value(self, value):
289+
self._set_attribute("value", value)
290+
267291
@property
268292
def children(self):
269293
return self._elements

pyscript.core/test/pyscript_dom/index.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ <h3 id="test_rr_h3">Content test_rr_h3</h3>
6464
<h2 id="multi-elem-h2" class="multi-elems">Content multi-elem-h2</h2>
6565

6666
<form>
67-
<input id="test_rr_input_txt" type="text" value="Content test_rr_input_txt">
68-
<input id="test_rr_input_btn" type="button" value="Content test_rr_input_btn">
67+
<input id="test_rr_input_text" type="text" value="Content test_rr_input_text">
68+
<input id="test_rr_input_button" type="button" value="Content test_rr_input_button">
6969
<input id="test_rr_input_email" type="email" value="Content test_rr_input_email">
7070
<input id="test_rr_input_password" type="password" value="Content test_rr_input_password">
7171
</form>
@@ -89,7 +89,6 @@ <h3 class="collection"></h3>
8989
const log = console.log.bind(console)
9090
let testsStarted = false;
9191
console.log = (...args) => {
92-
log("---IN---");
9392
let txt = args.join(" ");
9493
let token = "<br>";
9594
if (txt.endsWith("FAILED"))

pyscript.core/test/pyscript_dom/tests/test_dom.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,51 @@ def test_create_element_child(self):
244244
assert new_el.parent == parent_div
245245

246246
assert pydom[selector][0].children[0] == new_el
247+
248+
249+
class TestInput:
250+
input_ids = [
251+
"test_rr_input_text",
252+
"test_rr_input_button",
253+
"test_rr_input_email",
254+
"test_rr_input_password",
255+
]
256+
257+
def test_value(self):
258+
for id_ in self.input_ids:
259+
expected_type = id_.split("_")[-1]
260+
result = pydom[f"#{id_}"]
261+
input_el = result[0]
262+
assert input_el._js.type == expected_type
263+
assert input_el.value == f"Content {id_}" == input_el._js.value
264+
265+
# Check that we can set the value
266+
new_value = f"New Value {expected_type}"
267+
input_el.value = new_value
268+
assert input_el.value == new_value
269+
270+
# Check that we can set the value back to the original using
271+
# the collection
272+
new_value = f"Content {id_}"
273+
result.value = new_value
274+
assert input_el.value == new_value
275+
276+
def test_set_value_collection(self):
277+
for id_ in self.input_ids:
278+
input_el = pydom[f"#{id_}"]
279+
280+
assert input_el.value[0] == f"Content {id_}" == input_el[0].value
281+
282+
new_value = f"New Value {id_}"
283+
input_el.value = new_value
284+
assert input_el.value[0] == new_value == input_el[0].value
285+
286+
def test_element_without_value(self):
287+
result = pydom[f"#tests-terminal"][0]
288+
with pytest.raises(AttributeError):
289+
result.value = "some value"
290+
291+
def test_element_without_collection(self):
292+
result = pydom[f"#tests-terminal"]
293+
with pytest.raises(AttributeError):
294+
result.value = "some value"
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
declare namespace _default {
2-
let pyscript: {
1+
declare const _default: {
2+
pyscript: {
33
"__init__.py": string;
44
"display.py": string;
55
"event_handling.py": string;
66
"magic_js.py": string;
77
"util.py": string;
88
};
9-
let pyweb: {
9+
"pyscript.py": string;
10+
pyweb: {
1011
"pydom.py": string;
1112
};
12-
}
13+
};
1314
export default _default;

0 commit comments

Comments
 (0)
0