8000 Pydom add better support for select element by fpliger · Pull Request #1887 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Pydom add better support for select element #1887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 6, 2023
commit 14f9c5d438fa0c836f238f2b8b77adb75eb8f73f
26 changes: 17 additions & 9 deletions pyscript.core/src/stdlib/pyweb/pydom.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ def id(self, value):

@property
def options(self):
if 'options' in self._proxies:
return self._proxies['options']
if "options" in self._proxies:
return self._proxies["options"]

if not self._js.tagName.lower() in {"select", "datalist", "optgroup"}:
raise AttributeError(
f"Element {self._js.tagName} has no options attribute."
)
self._proxies['options'] = OptionsProxy(self)
return self._proxies['options']
self._proxies["options"] = OptionsProxy(self)
return self._proxies["options"]

@property
def value(self):
Expand Down Expand Up @@ -209,25 +209,33 @@ class OptionsProxy:
"""This class represents the options of a select element. It
allows to access to add and remove options by using the `add` and `remove` methods.
"""

def __init__(self, element: Element) -> None:
self._element = element
if self._element._js.tagName.lower() != "select":
raise AttributeError(
f"Element {self._element._js.tagName} has no options attribute."
)

def add(self, value: Any=None, html: str=None, text: str=None, before: Element|int=None, **kws) -> None:
def add(
self,
value: Any = None,
html: str = None,
text: str = None,
before: Element | int = None,
**kws,
) -> None:
"""Add a new option to the select element"""
# create the option element and set the attributes
option = document.createElement('option')
option = document.createElement("option")
if value is not None:
kws['value'] = value
kws["value"] = value
if html is not None:
option.innerHTML = html
if text is not None:
kws['text'] = text
kws["text"] = text

for (key, value) in kws.items():
for key, value in kws.items():
option.setAttribute(key, value)

if before:
Expand Down
15 changes: 8 additions & 7 deletions pyscript.core/test/pyscript_dom/tests/test_dom.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class TestSelect:
def test_select_options_iter(self):
select = pydom[f"#test_select_element_w_options"][0]

for i, option in enumerate(select.options, 1):
for i, option in enumerate(select.options, 1):
assert option.value == f"{i}"
assert option.html == f"Option {i}"

Expand All @@ -327,7 +327,7 @@ def test_select_element_add(self):
assert len(select.options) == 0

# WHEN we add an option
select.options.add(value = "1", html = "Option 1")
select.options.add(value="1", html="Option 1")

# EXPECT the select element to have 1 option matching the attributes
# we passed in
Expand All @@ -337,7 +337,7 @@ def test_select_element_add(self):

# WHEN we add another option (blank this time)
select.options.add()

# EXPECT the select element to have 2 options
assert len(select.options) == 2

Expand All @@ -347,7 +347,7 @@ def test_select_element_add(self):

# WHEN we add another option (this time adding it in between the other 2
# options by using an integer index)
select.options.add(value = "2", html = "Option 2", before = 1)
select.options.add(value="2", html="Option 2", before=1)

# EXPECT the select element to have 3 options
assert len(select.options) == 3
Expand All @@ -362,7 +362,9 @@ def test_select_element_add(self):

# WHEN we add another option (this time adding it in between the other 2
# options but using the option itself)
select.options.add(value = "3", html = "Option 3", before = select.options[2], selected = True)
select.options.add(
value="3", html="Option 3", before=select.options[2], selected=True
)

# EXPECT the select element to have 3 options
assert len(select.options) == 4
Expand All @@ -381,7 +383,7 @@ def test_select_element_add(self):

# WHEN we add another option (this time adding it in between the other 2
# options but using the JS element of the option itself)
select.options.add(value = "2a", html = "Option 2a", before = select.options[2]._js)
select.options.add(value="2a", html="Option 2a", before=select.options[2]._js)

54A5 # EXPECT the select element to have 3 options
assert len(select.options) == 5
Expand Down Expand Up @@ -431,4 +433,3 @@ def test_select_get_selected_option(self):
assert selected_option.value == "2"
assert selected_option.html == "Option 2"
assert selected_option.selected == selected_option._js.selected == True

0