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

docs: number field #932

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
318 changes: 318 additions & 0 deletions plugins/ui/docs/components/number_field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
# Number Field

Number fields allow users to enter a number and increase or decrease the value using stepper buttons.

## Example

```python
from deephaven import ui


my_number_field = ui.number_field(
label="Width",
on_change=lambda value: print(f"Number changed to {value}"),
default_value=1024,
)
```

## Value

A number field's value is empty by default. The `default_value` prop can set an initial, uncontrolled value, or the `value` prop can set a controlled value.

```python
from deephaven import ui


@ui.component
def ui_number_field_value_examples():
value, set_value = ui.use_state(5)
return [
ui.number_field(label="Hours (Uncontrolled)", default_value=5),
ui.number_field(
label="Favorite animal (controlled)", value=value, on_change=set_value
),
]


my_number_field_value_examples = ui_number_field_value_examples()
```

## HTML Forms

Number fields can support a `name` prop for integration with HTML forms, allowing for easy identification of a value on form submission.

```python
from deephaven import ui


my_number_field_name_example = ui.form(
ui.number_field(
label="Withdrawal amount",
name="amount",
default_value=45,
format_options={"currency_sign": "standard"},
)
)
```

## Labeling

To provide a visual label for the text field, use the `label` prop. To indicate that the text field is mandatory, use the `is_required` prop.

```python
from deephaven import ui


@ui.component
def ui_number_field_is_required_examples():
return [
ui.number_field(label="Birth year"),
ui.number_field(label="Birth year", is_required=True),
]


my_number_field_is_required_example = ui_number_field_is_required_examples()
```

By setting `is_required` to True, the `necessity_indicator` is set to "icon" by default, but this can be changed. The `necessity_indicator` can also be used independently to indicate that the text field is optional.

When the `necessity_indicator` prop is set to "label", a localized string will be generated for "(required)" or "(optional)" automatically.

```python
from deephaven import ui


@ui.component
def ui_number_field_necessity_indicator_examples():
return [
ui.number_field(
label="Birth year", is_required=True, necessity_indicator="label"
),
ui.number_field(label="Birth year", necessity_indicator="label"),
]


my_number_field_necessity_indicator_examples = (
ui_number_field_necessity_indicator_examples()
)
```

## Events

The `on_change` property is triggered whenever the value in the text field is edited.

```python
from deephaven import ui


@ui.component
def ui_number_field_on_change_example():
value, set_value = ui.use_state("")
return [
ui.number_field(label="Your age", value=value, on_change=set_value),
ui.text(f"Age has been changed to: {value}"),
]


my_number_field_on_change_example = ui_number_field_on_change_example()
```

## Format Options

The `format_options` prop dictates how the value is displayed and which characters can be inputted. There are 3 styles supported by this parameter: Percentage, Currency, and Units.

Note: This prop is compatible with the option parameter of [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat).

### Percentage

```python
from deephaven import ui


@ui.component
def ui_number_field_percentage_example():

return [
ui.number_field(
label="Percent", default_value="0.5", format_options={"style": "percent"}
),
]


my_number_field_percentage_example = ui_number_field_percentage_example()
```

### Currency

When the style is set to `currency`, a specific currency must be defined through the `currency` prop. Possible values follow the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217#List_of_ISO_4217_currency_codes) currency codes. Examples include "USD" for the US dollar or "EUR" for the euro.

```python
from deephaven import ui


@ui.component
def ui_number_field_currency_example():

return [
ui.number_field(
label="Currency",
default_value="49.99",
format_options={"style": "currency", "currency": "USD"},
),
]


my_number_field_currency_example = ui_number_field_currency_example()
```

### Units
When the style is set to `unit`, a specific unit must be defined through the `unit` prop. Possible values are defined in [UTS #35, Part 2 Section 6](https://unicode.org/reports/tr35/tr35-general.html#Unit_Elements). Examples include "degree", "inch", or "cup".

```python
from deephaven import ui


@ui.component
def ui_number_field_unit_example():

return [
ui.number_field(
label="Unit",
default_value="10",
format_options={"style": "unit", "unit": "inch"},
),
]


my_number_field_unit_example = ui_number_field_unit_example()
```

## Quiet State
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved

The `is_quiet` prop makes number fields "quiet". This can be useful when the input area and its corresponding styling should not distract users from surrounding content.

```python
from deephaven import ui


my_number_field_is_quiet_example = ui.number_field(label="Age", is_quiet=True)
```

## Disabled State
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved

The `is_disabled` prop disables text fields to prevent user interaction. This is useful when the number field should be visible but not available for input.

```python
from deephaven import ui


my_number_field_is_disabled_example = ui.number_field(label="Age", is_disabled=True)
```

## Read only

The `is_read_only` prop makes number fields read-only to prevent user interaction. This is different than setting the `is_disabled` prop since the number field remains focusable, and the contents of the number field remain visible.

```python
from deephaven import ui


my_number_field_is_read_only_example = ui.number_field(
label="Age", default_value=25, is_read_only=True
)
```

## Label position

By default, the position of a number field's label is above the number field, but it can be changed to the side using the `label_position` prop.

While labels can be placed either on top or on the side of the number field, top labels are the default recommendation. Top labels work better with longer copy, localization, and responsive layouts. Side labels are more useful when vertical space is limited.

```python
from deephaven import ui


@ui.component
def ui_number_field_label_position_examples():
return [
ui.number_field(label="Sample Label"),
ui.number_field(
label="Sample Label", label_position="side", label_align="start"
),
]


my_number_field_label_position_examples = ui_number_field_label_position_examples()
```

## Help text

A number field can have both a `description` and an `error_message`. The description remains visible at all times, except when the `validation_state` is set to "invalid" and an error message is present. Use the error message to offer specific guidance on how to correct the input.

```python
from deephaven import ui


@ui.component
def ui_number_field_help_number_examples():
return [
ui.number_field(
label="Comment",
default_value="Awesome!",
validation_state="valid",
description="Enter a comment.",
),
ui.number_field(
label="Comment",
validation_state="invalid",
error_message="Empty input is not allowed.",
),
]


my_number_field_help_number_examples = ui_number_field_help_number_examples()
```

## Contextual Help

Using the `contextual_help` prop, a `ui.contextual_help` can be placed next to the label to provide additional information about the number field.

```python
from deephaven import ui


my_number_field_contextual_help_example = ui.number_field(
label="FPS",
contextual_help=ui.contextual_help(
ui.heading("What is FPS"),
ui.content(
"Frames Per Second (FPS) is a measure of how many individual frames (images) are displayed in one second of video or animation"
),
),
)
```

## Custom width

The `width` prop adjusts the width of a number field, and the `max_width` prop enforces a maximum width.

```python
from deephaven import ui


@ui.component
def ui_number_field_width_examples():
return [
ui.number_field(label="Birth year", width="size-3600"),
ui.number_field(label="Birth year", width="size-3600", max_width="100%"),
]


my_number_field_width_examples = ui_number_field_width_examples()
```

## API Reference

```{eval-rst}
.. dhautofunction:: deephaven.ui.number_field
```
8 changes: 6 additions & 2 deletions plugins/ui/src/deephaven/ui/components/number_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def number_field(
decrement_aria_label: The aria label for the decrement stepper button. If not provided, the default is "Decrement"
increment_aria_label: The aria label for the increment stepper button. If not provided, the default is "Increment"
is_wheel_disabled: Whether the input should change with scroll
format_options: Options for formatting the displayed value, which also restricts input characters.
is_disabled: Whether the input should be disabled
is_read_only: Whether the input scan be selected but not changed by the user
is_required: Whether the input is required before form submission
Expand Down Expand Up @@ -169,12 +170,15 @@ def number_field(
is_hidden: Whether the element is hidden.
id: The unique identifier of the element.
aria_label: The label for the element.
aria_labelled_by: The id of the element that labels the current element.
aria_described_by: The id of the element that describes the current element.
aria_labelledby: The id of the element that labels the current element.
aria_describedby: The id of the element that describes the current element.
aria_details: The id of the element that provides additional information about the current element.
UNSAFE_class_name: A CSS class to apply to the element.
UNSAFE_style: A CSS style to apply to the element.
key: A unique identifier used by React to render elements in a list.

Returns:
The rendered number field element.
"""

return component_element(
Expand Down
Loading