Repository navigation
docs: move React form binding to Flow #6621
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| title: Form API | ||
| page-title: Hooks and types of form binding in React views | Vaadin | ||
| description: The hooks that bind forms in React views, their results, and the types for validation. | ||
| meta-description: API reference for Vaadin form binding in React views - useForm, useFormPart, useFormArrayPart, the field function, validators, errors, and form models. | ||
| order: 50 | ||
| --- | ||
|
|
||
|
|
||
| = Form API | ||
| :toclevels: 2 | ||
|
|
||
| The hooks of form binding are in the `@vaadin/hilla-react-form` package. The validation types are in `@vaadin/hilla-lit-form`, and the model helpers in `@vaadin/hilla-models`. | ||
|
|
||
| section_outline::[] | ||
|
|
||
|
|
||
| [[use-form]] | ||
| == useForm | ||
|
|
||
| `useForm(model, config?)` creates a form for the given model, and returns its state and functions. The optional configuration object has these properties: | ||
|
|
||
| `onSubmit(value)`:: | ||
| An asynchronous function that [methodname]`submit()` calls with the value of the form. See <<index#submitting,Submitting>>. | ||
|
|
||
| `onChange(oldValue)`:: | ||
| A function that the form calls when it updates its value, with the value before the update. | ||
|
|
||
| The result has the state properties described in <<form-state#properties,Form State>>, and these functions: | ||
|
|
||
| `field(model)`:: | ||
| Returns the properties that bind a component to the field of the given model. Spread them on the component. See <<field-components#,Field Components>>. | ||
|
|
||
| `read(value)`:: | ||
| Sets the value and the default value of the form, and clears the validation errors. With `null` or `undefined`, it clears the form. | ||
|
|
||
| `reset()`:: | ||
| Sets the value back to the default value. | ||
|
|
||
| `clear()`:: | ||
| Sets the value and the default value to the default values of the model. | ||
|
|
||
| `submit()`:: | ||
| Validates the form, and calls `onSubmit`. Returns a promise that resolves with the result of `onSubmit`, or rejects with a [classname]`ValidationError`. | ||
|
|
||
| `setValue(value)`:: | ||
| Sets the value of the form. | ||
|
|
||
| `setDefaultValue(value)`:: | ||
| Sets the default value of the form, which affects the `dirty` state and [methodname]`reset()`. | ||
|
|
||
| `validate()`:: | ||
| Validates the form, and returns a promise of its errors. | ||
|
|
||
| `addValidator(validator)`:: | ||
| Adds a validator to the form. See <<validation#form-validators,Form Validators>>. | ||
|
|
||
| `setValidators(validators)`:: | ||
| Replaces the validators of the form. | ||
|
|
||
| `setVisited(visited)`:: | ||
| Sets the `visited` state. | ||
|
|
||
| `update()`:: | ||
| Re-renders the component. | ||
|
|
||
|
|
||
| [[use-form-part]] | ||
| == useFormPart | ||
|
|
||
| `useFormPart(model)` returns the state and functions of a part of a form, such as a field or a nested object. The model must belong to a form that a [methodname]`useForm()` call in the same or a parent component created. | ||
|
|
||
| The result has the state properties described in <<form-state#properties,Form State>>, except `submitting`, and the `field()`, `setValue()`, `validate()`, `addValidator()`, `setValidators()`, and `setVisited()` functions of [methodname]`useForm()`. They apply to the part. | ||
|
|
||
| The validators of a field include the validators of its Bean Validation constraints. Calling [methodname]`setValidators()` replaces them, too. | ||
|
|
||
|
|
||
| [[use-form-array-part]] | ||
| == useFormArrayPart | ||
|
|
||
| `useFormArrayPart(model)` returns the state and functions of a list or an array in a form, like [methodname]`useFormPart()`, but without `field()`. Its `items` property contains a model for each item. The model must be non-nullable. See <<nested-objects-arrays#arrays,Arrays>>. | ||
|
|
||
|
|
||
| [[validation-types]] | ||
| == Validation Types | ||
|
|
||
| The `@vaadin/hilla-lit-form` package has these types for validation: | ||
|
|
||
| [classname]`Validator`:: | ||
| An object with a `message`, a [methodname]`validate(value)` function, and an optional `impliesRequired` flag. See <<validation#validators,Validators in the View>>. | ||
|
|
||
| [classname]`ValueError`:: | ||
| A validation error, with the `property` that identifies the field, the `message`, the `value`, and the `validator` that failed. | ||
|
|
||
| [classname]`ValidationError`:: | ||
| The error that [methodname]`submit()` rejects with when the validation fails. Its `errors` property contains the [classname]`ValueError` objects. | ||
|
|
||
| [classname]`BinderRoot`:: | ||
| Its static `interpolateMessageCallback` property replaces the placeholders in the messages of constraints. See <<validation#messages,Error Messages>>. | ||
|
|
||
| The package also has a validator class for each Bean Validation constraint that the browser checks, such as [classname]`NotBlank`, [classname]`Size`, and [classname]`Pattern`. They take an options object with a `message` and the attributes of the constraint, such as `new Size({ min: 3, max: 20 })`. | ||
|
|
||
|
|
||
| [[models]] | ||
| == Form Models | ||
|
|
||
| A generated form model, such as `PersonModel`, is both a value and a type with the same name. Pass the value to the hooks, and use the type for the properties of components that get a part of the form. | ||
|
|
||
| The `$defaultValue` symbol from `@vaadin/hilla-models` gives the default value of a model, such as `PersonModel[$defaultValue]`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| title: Field Components | ||
| page-title: Field components that bind to forms in React views | Vaadin | ||
| description: The Vaadin components that the field() function binds, and the Java types of the properties that they edit. | ||
| meta-description: Reference for Vaadin forms in React views - which Vaadin field components the field() function binds, and the Java property types that each of them edits. | ||
| order: 40 | ||
| --- | ||
|
|
||
|
|
||
| = Field Components | ||
|
|
||
| The [methodname]`field()` function binds the following Vaadin components. Each component edits a property of one of the listed Java types: | ||
|
|
||
| [cols="2,3"] | ||
| |=== | ||
| |Component |Java type of the property | ||
|
|
||
| |<<{articles}/components/text-field#,Text Field>>, <<{articles}/components/text-area#,Text Area>>, <<{articles}/components/password-field#,Password Field>>, <<{articles}/components/email-field#,Email Field>> | ||
| |`String` | ||
|
|
||
| |<<{articles}/components/number-field#,Integer Field, Number Field>> | ||
| |A number type, such as `int`, `Integer`, `double`, or `BigDecimal` | ||
|
|
||
| |<<{articles}/components/checkbox#,Checkbox>> | ||
| |`boolean` | ||
|
|
||
| |<<{articles}/components/date-picker#,Date Picker>> | ||
| |`LocalDate` | ||
|
|
||
| |<<{articles}/components/time-picker#,Time Picker>> | ||
| |`LocalTime` | ||
|
|
||
| |<<{articles}/components/date-time-picker#,Date Time Picker>> | ||
| |`LocalDateTime` | ||
|
|
||
| |<<{articles}/components/select#,Select>>, <<{articles}/components/radio-button#,Radio Button Group>> | ||
| |`String` or an enum, matched to the values of the items | ||
|
|
||
| |<<{articles}/components/combo-box#,Combo Box>> | ||
| |`String` or an enum, matched to the values of the items, or an object, which is the selected item | ||
|
|
||
| |<<{articles}/components/checkbox#,Checkbox Group>> | ||
| |A list or a set of `String` | ||
|
|
||
| |<<{articles}/components/multi-select-combo-box#,Multi-Select Combo Box>> | ||
| |A list or a set of the items | ||
|
|
||
| |<<{articles}/components/custom-field#,Custom Field>> | ||
| |`String`. By default, the value combines the values of the fields inside the custom field, separated by tab characters. | ||
|
|
||
| |<<{articles}/components/rich-text-editor#,Rich Text Editor>> | ||
| |`String`, in the Delta format of the editor | ||
| |=== | ||
|
|
||
| For example, a combo box can edit a `Country` property of a form, when its items are `Country` objects: | ||
|
|
||
| [source,tsx] | ||
| ---- | ||
| <ComboBox label="Country" items={countries} itemLabelPath="name" itemValuePath="name" {...field(model.country)} /> | ||
| ---- | ||
|
|
||
| See <<form-state#changing-values,Changing Values from Code>> for the whole example. | ||
|
|
||
| When the user empties a number field, the value of a nullable number becomes `undefined`. A non-nullable number becomes `NaN`, and the field shows the error "must be a number". | ||
|
|
||
| The [methodname]`field()` function throws an error for a component that isn't a field, such as a <<{articles}/components/list-box#,List Box>>. It doesn't read the state of a <<{articles}/components/switch#,Switch>>, so use a checkbox for a `boolean` property instead. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.