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

Fix 'enter_key_submit=True' on 'rx.text_area' by carrying custom_code on debounce #4142

Merged
merged 2 commits into from
Oct 24, 2024
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
6 changes: 5 additions & 1 deletion reflex/components/core/debounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def create(cls, *children: Component, **props: Any) -> Component:
_var_type=Type[Component],
_var_data=VarData(
imports=child._get_imports(),
hooks=child._get_hooks_internal(),
hooks=child._get_all_hooks(),
),
),
)
Expand All @@ -128,6 +128,10 @@ def create(cls, *children: Component, **props: Any) -> Component:
component.event_triggers.update(child.event_triggers)
component.children = child.children
component._rename_props = child._rename_props
outer_get_all_custom_code = component._get_all_custom_code
component._get_all_custom_code = lambda: outer_get_all_custom_code().union(
child._get_all_custom_code()
)
return component

def _render(self):
Expand Down
58 changes: 36 additions & 22 deletions reflex/components/el/elements/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,42 @@ class Textarea(BaseHTML):
# Fired when a key is released
on_key_up: EventHandler[lambda e0: [e0.key]]

@classmethod
def create(cls, *children, **props):
"""Create a textarea component.

Args:
*children: The children of the textarea.
**props: The properties of the textarea.

Returns:
The textarea component.

Raises:
ValueError: when `enter_key_submit` is combined with `on_key_down`.
"""
enter_key_submit = props.get("enter_key_submit")
auto_height = props.get("auto_height")
custom_attrs = props.setdefault("custom_attrs", {})

if enter_key_submit is not None:
enter_key_submit = Var.create(enter_key_submit)
if "on_key_down" in props:
raise ValueError(
"Cannot combine `enter_key_submit` with `on_key_down`.",
)
custom_attrs["on_key_down"] = Var(
_js_expr=f"(e) => enterKeySubmitOnKeyDown(e, {str(enter_key_submit)})",
_var_data=VarData.merge(enter_key_submit._get_all_var_data()),
)
if auto_height is not None:
auto_height = Var.create(auto_height)
custom_attrs["on_input"] = Var(
_js_expr=f"(e) => autoHeightOnInput(e, {str(auto_height)})",
_var_data=VarData.merge(auto_height._get_all_var_data()),
)
return super().create(*children, **props)

def _exclude_props(self) -> list[str]:
return super()._exclude_props() + [
"auto_height",
Expand All @@ -619,28 +655,6 @@ def _get_all_custom_code(self) -> Set[str]:
custom_code.add(ENTER_KEY_SUBMIT_JS)
return custom_code

def _render(self) -> Tag:
tag = super()._render()
if self.enter_key_submit is not None:
if "on_key_down" in self.event_triggers:
raise ValueError(
"Cannot combine `enter_key_submit` with `on_key_down`.",
)
tag.add_props(
on_key_down=Var(
_js_expr=f"(e) => enterKeySubmitOnKeyDown(e, {str(self.enter_key_submit)})",
_var_data=VarData.merge(self.enter_key_submit._get_all_var_data()),
)
)
if self.auto_height is not None:
tag.add_props(
on_input=Var(
_js_expr=f"(e) => autoHeightOnInput(e, {str(self.auto_height)})",
_var_data=VarData.merge(self.auto_height._get_all_var_data()),
)
)
return tag


button = Button.create
fieldset = Fieldset.create
Expand Down
11 changes: 7 additions & 4 deletions reflex/components/el/elements/forms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1658,10 +1658,10 @@ class Textarea(BaseHTML):
] = None,
**props,
) -> "Textarea":
"""Create the component.
"""Create a textarea component.

Args:
*children: The children of the component.
*children: The children of the textarea.
auto_complete: Whether the form control should have autocomplete enabled
auto_focus: Automatically focuses the textarea when the page loads
auto_height: Automatically fit the content height to the text (use min-height with this prop)
Expand Down Expand Up @@ -1701,10 +1701,13 @@ class Textarea(BaseHTML):
class_name: The class name for the component.
autofocus: Whether the component should take the focus once the page is loaded
custom_attrs: custom attribute
**props: The props of the component.
**props: The properties of the textarea.

Returns:
The component.
The textarea component.

Raises:
ValueError: when `enter_key_submit` is combined with `on_key_down`.
"""
...

Expand Down
Loading