Skip to content
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

[REF-526] debounce_input should respect child ref #1717

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions integration/test_form_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def index():
rx.radio_group(["option1", "option2"], id="radio_input"),
rx.select(["option1", "option2"], id="select_input"),
rx.text_area(id="text_area_input"),
rx.input(
id="debounce_input",
debounce_timeout=0,
on_change=rx.console_log,
),
rx.button("Submit", type_="submit"),
),
on_submit=FormState.form_submit,
Expand Down Expand Up @@ -119,6 +124,9 @@ def test_submit(driver, form_submit: AppHarness):
textarea_input = driver.find_element(By.CLASS_NAME, "chakra-textarea")
textarea_input.send_keys("Some", Keys.ENTER, "Text")

debounce_input = driver.find_element(By.ID, "debounce_input")
debounce_input.send_keys("bar baz")

time.sleep(1)

submit_input = driver.find_element(By.CLASS_NAME, "chakra-button")
Expand All @@ -139,3 +147,4 @@ def test_submit(driver, form_submit: AppHarness):
assert backend_state.form_data["radio_input"] == "option2"
assert backend_state.form_data["select_input"] == "option1"
assert backend_state.form_data["text_area_input"] == "Some\nText"
assert backend_state.form_data["debounce_input"] == "bar baz"
6 changes: 6 additions & 0 deletions reflex/components/forms/debounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ def _render(self) -> Tag:

Raises:
RuntimeError: unless exactly one child element is provided.
ValueError: if the child element does not have an on_change handler.
"""
child, props = _collect_first_child_and_props(self)
if isinstance(child, type(self)) or len(self.children) > 1:
raise RuntimeError(
"Provide a single child for DebounceInput, such as rx.input() or "
"rx.text_area()",
)
if "on_change" not in child.event_triggers:
raise ValueError("DebounceInput child requires an on_change handler")
child_ref = child.get_ref()
if child_ref and not props.get("ref"):
props["input_ref"] = Var.create(child_ref, is_local=False)
self.children = []
tag = super()._render()
tag.add_props(
Expand Down