|
| 1 | +# Form |
| 2 | + |
| 3 | +Forms allow users to enter data that can be submitted while providing alignment and styling for form fields. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +```python |
| 8 | +from deephaven import ui |
| 9 | + |
| 10 | + |
| 11 | +@ui.component |
| 12 | +def ui_form(): |
| 13 | + return ui.form( |
| 14 | + ui.text_field(name="name", label="Enter name"), |
| 15 | + ui.button("Submit", type="submit"), |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +my_form = ui_form() |
| 20 | +``` |
| 21 | + |
| 22 | +## Events |
| 23 | + |
| 24 | +Forms can handle three events: |
| 25 | + |
| 26 | +1. **Submit**: Triggered when the form is submitted. Users can define a callback function to handle the form data upon submission. |
| 27 | +2. **Reset**: Triggered when the form is reset. This event can be used to clear the form fields or reset them to their initial values. |
| 28 | +3. **Invalid**: Triggered when the form validation fails. This event allows users to handle validation errors and provide feedback. |
| 29 | + |
| 30 | + |
| 31 | +### Submit |
| 32 | + |
| 33 | +```python |
| 34 | +from deephaven import ui |
| 35 | + |
| 36 | + |
| 37 | +@ui.component |
| 38 | +def ui_form_submit(): |
| 39 | + return ui.form( |
| 40 | + ui.text_field(name="name", label="Enter name"), |
| 41 | + ui.number_field(name="age", label="Enter age"), |
| 42 | + ui.button("Submit", type="submit"), |
| 43 | + on_submit=lambda e: print(f"Form submitted: {e}"), |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +my_form_submit = ui_form_submit() |
| 48 | +``` |
| 49 | + |
| 50 | +### Reset |
| 51 | + |
| 52 | +```python |
| 53 | +from deephaven import ui |
| 54 | + |
| 55 | + |
| 56 | +@ui.component |
| 57 | +def ui_form_submit(): |
| 58 | + return ui.form( |
| 59 | + ui.text_field(name="name", label="Enter name"), |
| 60 | + ui.number_field(name="age", label="Enter age"), |
| 61 | + ui.button("Reset", type="reset"), |
| 62 | + on_reset=lambda e: print(f"Form reset"), |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +my_form_submit = ui_form_submit() |
| 67 | +``` |
| 68 | + |
| 69 | +### Invalid |
| 70 | + |
| 71 | +```python |
| 72 | +from deephaven import ui |
| 73 | + |
| 74 | + |
| 75 | +@ui.component |
| 76 | +def ui_form_invalid(): |
| 77 | + value, set_value = ui.use_state("") |
| 78 | + return ui.form( |
| 79 | + ui.text_field( |
| 80 | + name="name", |
| 81 | + label="Enter name", |
| 82 | + value=value, |
| 83 | + on_change=set_value, |
| 84 | + is_required=True, |
| 85 | + ), |
| 86 | + ui.number_field(name="age", label="Enter age"), |
| 87 | + ui.button("Submit", type="submit"), |
| 88 | + on_invalid=lambda e: print(f"Form invalid"), |
| 89 | + validation_behavior="native", |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +my_form_invalid = ui_form_invalid() |
| 94 | +``` |
| 95 | + |
| 96 | +## Action |
| 97 | + |
| 98 | +The `action` prop enables forms to be sent to a URL. The example below communicates with a 3rd party server and displays the content received from the form. |
| 99 | + |
| 100 | +```python |
| 101 | +from deephaven import ui |
| 102 | + |
| 103 | + |
| 104 | +@ui.component |
| 105 | +def ui_form_action(): |
| 106 | + return ui.form( |
| 107 | + ui.text_field(name="first_name", default_value="Mickey", label="First Name"), |
| 108 | + ui.text_field(name="last_name", default_value="Mouse", label="Last Name"), |
| 109 | + ui.button("Submit", type="submit"), |
| 110 | + action="https://postman-echo.com/get", |
| 111 | + method="get", |
| 112 | + target="_blank", |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +my_form_action = ui_form_action() |
| 117 | +``` |
| 118 | + |
| 119 | +## Validation Behavior |
| 120 | + |
| 121 | +By default, validation errors will be displayed in real-time as the fields are edited, but form submission is not blocked. To enable this and native HTML form validation, set `validation_behavior` to "native". |
| 122 | + |
| 123 | +```python |
| 124 | +from deephaven import ui |
| 125 | + |
| 126 | + |
| 127 | +@ui.component |
| 128 | +def ui_form_validation_behavior(): |
| 129 | + return ui.form( |
| 130 | + ui.text_field(name="email", label="Email", type="email", is_required=True), |
| 131 | + ui.button("Submit", type="submit"), |
| 132 | + validation_behavior="native", |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +my_form_validation_behavior = ui_form_validation_behavior() |
| 137 | +``` |
| 138 | + |
| 139 | + |
| 140 | +## Quiet |
| 141 | + |
| 142 | +The `is_quiet` prop makes form fields "quiet". This can be useful when its corresponding styling should not distract users from surrounding content. |
| 143 | + |
| 144 | +```python |
| 145 | +from deephaven import ui |
| 146 | + |
| 147 | + |
| 148 | +@ui.component |
| 149 | +def ui_form_quiet(): |
| 150 | + |
| 151 | + return ui.form( |
| 152 | + ui.text_field(name="name", label="Enter name"), |
| 153 | + is_quiet=True, |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +my_form_quiet = ui_form_quiet() |
| 158 | +``` |
| 159 | + |
| 160 | +## Emphasized |
| 161 | + |
| 162 | +The `is_emphasized` prop adds visual prominence to the form fields. This can be useful when its corresponding styling should catch the user's attention. |
| 163 | + |
| 164 | +```python |
| 165 | +from deephaven import ui |
| 166 | + |
| 167 | + |
| 168 | +@ui.component |
| 169 | +def ui_form_emphasized(): |
| 170 | + |
| 171 | + return ui.form( |
| 172 | + ui.text_field(name="name", label="Enter name"), |
| 173 | + ui.radio_group( |
| 174 | + ui.radio("Video games", value="games"), |
| 175 | + ui.radio("Reading", value="reading"), |
| 176 | + ui.radio("Sports", value="sports"), |
| 177 | + label="Favorite hobby", |
| 178 | + default_value="games", |
| 179 | + ), |
| 180 | + is_emphasized=True, |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +my_form = ui_form_emphasized() |
| 185 | +``` |
| 186 | + |
| 187 | +## Disabled |
| 188 | + |
| 189 | +The `is_disabled` prop disables form fields to prevent user interaction. This is useful when the fields should be visible but not available for input. |
| 190 | + |
| 191 | +```python |
| 192 | +from deephaven import ui |
| 193 | + |
| 194 | + |
| 195 | +@ui.component |
| 196 | +def ui_form_disabled(): |
| 197 | + return ui.form( |
| 198 | + ui.text_field(name="name", label="Enter name"), |
| 199 | + is_disabled=True, |
| 200 | + ) |
| 201 | + |
| 202 | + |
| 203 | +my_form_disabled = ui_form_disabled() |
| 204 | +``` |
| 205 | + |
| 206 | +## Necessity Indicator |
| 207 | + |
| 208 | +The `necessity_indicator` prop dictates whether form labels will use an icon or a label to outline which form fields are required. The default is "icon". |
| 209 | + |
| 210 | +```python |
| 211 | +from deephaven import ui |
| 212 | + |
| 213 | + |
| 214 | +@ui.component |
| 215 | +def ui_form_indicator(): |
| 216 | + def icon_indicator(): |
| 217 | + return ui.form( |
| 218 | + ui.text_field(name="name", label="Name", is_required=True), |
| 219 | + ui.text_field(name="age", label="Age"), |
| 220 | + is_required=True, |
| 221 | + ) |
| 222 | + |
| 223 | + def label_indicator(): |
| 224 | + return ui.form( |
| 225 | + ui.text_field(name="name", label="Name", is_required=True), |
| 226 | + ui.text_field(name="age", label="Age"), |
| 227 | + is_required=True, |
| 228 | + necessity_indicator="label", |
| 229 | + ) |
| 230 | + |
| 231 | + return [icon_indicator(), label_indicator()] |
| 232 | + |
| 233 | + |
| 234 | +my_form_required = ui_form_indicator() |
| 235 | +``` |
| 236 | + |
| 237 | +## Read only |
| 238 | + |
| 239 | +The `is_read_only` prop makes form fields read-only to prevent user interaction. This is different than setting the `is_disabled` prop since the fields remains focusable, and the contents of the fields remain visible. |
| 240 | + |
| 241 | +```python |
| 242 | +from deephaven import ui |
| 243 | + |
| 244 | + |
| 245 | +@ui.component |
| 246 | +def ui_form_read_only(): |
| 247 | + return ui.form( |
| 248 | + ui.text_field(name="name", label="Name"), |
| 249 | + is_read_only=True, |
| 250 | + ) |
| 251 | + |
| 252 | + |
| 253 | +my_form_read_only = ui_form_read_only() |
| 254 | +``` |
| 255 | + |
| 256 | + |
| 257 | +## API Reference |
| 258 | + |
| 259 | +```{eval-rst} |
| 260 | +.. dhautofunction:: deephaven.ui.form |
| 261 | +``` |
0 commit comments