123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- """Interactive components provided by @radix-ui/themes."""
- from typing import Any, Dict, Literal
- from reflex.components import el
- from reflex.components.component import Component, ComponentNamespace
- from reflex.components.core.debounce import DebounceInput
- from reflex.constants import EventTriggers
- from reflex.vars import Var
- from ..base import (
- LiteralAccentColor,
- LiteralRadius,
- RadixThemesComponent,
- )
- LiteralTextFieldSize = Literal["1", "2", "3"]
- LiteralTextFieldVariant = Literal["classic", "surface", "soft"]
- class TextFieldRoot(el.Div, RadixThemesComponent):
- """Captures user input with an optional slot for buttons and icons."""
- tag = "TextField.Root"
- # Text field size "1" - "3"
- size: Var[LiteralTextFieldSize]
- # Variant of text field: "classic" | "surface" | "soft"
- variant: Var[LiteralTextFieldVariant]
- # Override theme color for text field
- color_scheme: Var[LiteralAccentColor]
- # Override theme radius for text field: "none" | "small" | "medium" | "large" | "full"
- radius: Var[LiteralRadius]
- class TextFieldInput(el.Input, TextFieldRoot):
- """The input part of a TextField, may be used by itself."""
- tag = "TextField.Input"
- @classmethod
- def create(cls, *children, **props) -> Component:
- """Create an Input component.
- Args:
- *children: The children of the component.
- **props: The properties of the component.
- Returns:
- The component.
- """
- if props.get("value") is not None and props.get("on_change"):
- # create a debounced input if the user requests full control to avoid typing jank
- return DebounceInput.create(super().create(*children, **props))
- return super().create(*children, **props)
- def get_event_triggers(self) -> Dict[str, Any]:
- """Get the event triggers that pass the component's value to the handler.
- Returns:
- A dict mapping the event trigger to the var that is passed to the handler.
- """
- return {
- **super().get_event_triggers(),
- EventTriggers.ON_CHANGE: lambda e0: [e0.target.value],
- EventTriggers.ON_FOCUS: lambda e0: [e0.target.value],
- EventTriggers.ON_BLUR: lambda e0: [e0.target.value],
- EventTriggers.ON_KEY_DOWN: lambda e0: [e0.key],
- EventTriggers.ON_KEY_UP: lambda e0: [e0.key],
- }
- class TextFieldSlot(RadixThemesComponent):
- """Contains icons or buttons associated with an Input."""
- tag = "TextField.Slot"
- # Override theme color for text field slot
- color_scheme: Var[LiteralAccentColor]
- class Input(RadixThemesComponent):
- """High level wrapper for the Input component."""
- # Text field size "1" - "3"
- size: Var[LiteralTextFieldSize]
- # Variant of text field: "classic" | "surface" | "soft"
- variant: Var[LiteralTextFieldVariant]
- # Override theme color for text field
- color_scheme: Var[LiteralAccentColor]
- # Override theme radius for text field: "none" | "small" | "medium" | "large" | "full"
- radius: Var[LiteralRadius]
- # Whether the input should have autocomplete enabled
- auto_complete: Var[bool]
- # The value of the input when initially rendered.
- default_value: Var[str]
- # Disables the input
- disabled: Var[bool]
- # Specifies the maximum number of characters allowed in the input
- max_length: Var[str]
- # Specifies the minimum number of characters required in the input
- min_length: Var[str]
- # Name of the input, used when sending form data
- name: Var[str]
- # Placeholder text in the input
- placeholder: Var[str]
- # Indicates whether the input is read-only
- read_only: Var[bool]
- # Indicates that the input is required
- required: Var[bool]
- # Specifies the type of input
- type: Var[str]
- # Value of the input
- value: Var[str]
- @classmethod
- def create(cls, **props):
- """Create an Input component.
- Args:
- **props: The properties of the component.
- Returns:
- The component.
- """
- return TextFieldInput.create(**props)
- def get_event_triggers(self) -> Dict[str, Any]:
- """Get the event triggers that pass the component's value to the handler.
- Returns:
- A dict mapping the event trigger to the var that is passed to the handler.
- """
- return {
- **super().get_event_triggers(),
- EventTriggers.ON_CHANGE: lambda e0: [e0.target.value],
- EventTriggers.ON_FOCUS: lambda e0: [e0.target.value],
- EventTriggers.ON_BLUR: lambda e0: [e0.target.value],
- EventTriggers.ON_KEY_DOWN: lambda e0: [e0.key],
- EventTriggers.ON_KEY_UP: lambda e0: [e0.key],
- }
- class TextField(ComponentNamespace):
- """TextField components namespace."""
- root = staticmethod(TextFieldRoot.create)
- input = staticmethod(TextFieldInput.create)
- slot = staticmethod(TextFieldSlot.create)
- __call__ = staticmethod(Input.create)
- text_field = TextField()
|