diff --git a/nicegui/elements/input.py b/nicegui/elements/input.py index 2f94cc7eb..b2bdba0b6 100644 --- a/nicegui/elements/input.py +++ b/nicegui/elements/input.py @@ -17,6 +17,8 @@ def __init__(self, value: str = '', password: bool = False, password_toggle_button: bool = False, + prefix: Optional[str] = None, + suffix: Optional[str] = None, on_change: Optional[Handler[ValueChangeEventArguments]] = None, autocomplete: Optional[list[str]] = None, validation: Optional[Union[ValidationFunction, ValidationDict]] = None, @@ -46,6 +48,8 @@ def __init__(self, :param value: the current value of the text input :param password: whether to hide the input (default: False) :param password_toggle_button: whether to show a button to toggle the password visibility (default: False) + :param prefix: a prefix to prepend to the displayed value + :param suffix: a suffix to append to the displayed value :param on_change: callback to execute when the value changes :param autocomplete: optional list of strings for autocompletion :param validation: dictionary of validation rules or a callable that returns an optional error message (default: None for no validation) @@ -66,10 +70,31 @@ def toggle_type(_): self._props['_autocomplete'] = autocomplete or [] + if prefix is not None: + self._props['prefix'] = prefix + if suffix is not None: + self._props['suffix'] = suffix + def set_autocomplete(self, autocomplete: Optional[list[str]]) -> None: """Set the autocomplete list.""" self._props['_autocomplete'] = autocomplete + def set_prefix(self, text: Optional[str]) -> None: + """ + Set the prefix. + + :param text: the content of the prefix. + """ + self._props['prefix'] = text + + def set_suffix(self, text: Optional[str]) -> None: + """ + Set the suffix. + + :param text: the content of the suffix. + """ + self._props['suffix'] = text + def _handle_value_change(self, value: Any) -> None: super()._handle_value_change(value) if self._send_update_on_value_change: diff --git a/nicegui/elements/number.py b/nicegui/elements/number.py index 3f741617d..eb7266a58 100644 --- a/nicegui/elements/number.py +++ b/nicegui/elements/number.py @@ -116,6 +116,22 @@ def sanitize(self) -> None: self.value = float(self.format % value) if self.format else value self.update() + def set_prefix(self, text: Optional[str]) -> None: + """ + Set the prefix. + + :param text: the content of the prefix. + """ + self._props['prefix'] = text + + def set_suffix(self, text: Optional[str]) -> None: + """ + Set the suffix. + + :param text: the content of the suffix. + """ + self._props['suffix'] = text + def _event_args_to_value(self, e: GenericEventArguments) -> Any: if not e.args: return None diff --git a/tests/test_input.py b/tests/test_input.py index 0d6d8764c..3b549babc 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -216,3 +216,22 @@ def page(): screen.click('focus 2') screen.wait(0.3) assert elements[1] == screen.selenium.switch_to.active_element + + +def test_input_with_prefix_suffix(screen: Screen): + @ui.page('/') + def page(): + n = ui.input(prefix='MyPrefix', suffix='MySuffix') + + def change_prefix_suffix(): + n.set_prefix('NewPrefix') + n.set_suffix('NewSuffix') + + ui.button('Change', on_click=change_prefix_suffix) + + screen.open('/') + screen.should_contain('MyPrefix') + screen.should_contain('MySuffix') + screen.click('Change') + screen.should_contain('NewPrefix') + screen.should_contain('NewSuffix') diff --git a/tests/test_number.py b/tests/test_number.py index 406cb8c42..ac0445afe 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -176,3 +176,22 @@ def page(): screen.should_contain_input('1') screen.should_contain('model: 1') screen.should_contain('event: 1') + + +def test_number_with_prefix_suffix(screen: Screen): + @ui.page('/') + def page(): + n = ui.number(prefix='MyPrefix', suffix='MySuffix') + + def change_prefix_suffix(): + n.set_prefix('NewPrefix') + n.set_suffix('NewSuffix') + + ui.button('Change', on_click=change_prefix_suffix) + + screen.open('/') + screen.should_contain('MyPrefix') + screen.should_contain('MySuffix') + screen.click('Change') + screen.should_contain('NewPrefix') + screen.should_contain('NewSuffix')