Skip to content

Commit

Permalink
fix: pull request comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Nov 23, 2024
1 parent a88f993 commit 084e8da
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/framework/react/guides/basic-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ Example:
/>
```

More information can be found at [Listeners](./listeners.md)

## Array Fields

Array fields allow you to manage a list of values within a form, such as a list of hobbies. You can create an array field using the `form.Field` component with the `mode="array"` prop.
Expand Down
14 changes: 7 additions & 7 deletions docs/framework/react/guides/listeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ For situations where you want to "affect" or "react" to triggers, there's the li
Imagine the following user flow:

- User selects a country from a drop-down.
- User then selects a county from another drop-down.
- User then selects a province from another drop-down.
- User changes the selected country to a different one.

In this example, when the user changes the country, the selected county needs to be reset as it's no longer valid. With the listener API, we can subscribe to the onChange event and dispatch a reset to the field "county" when the listener is fired.
In this example, when the user changes the country, the selected province needs to be reset as it's no longer valid. With the listener API, we can subscribe to the onChange event and dispatch a reset to the field "province" when the listener is fired.

Other events that can be "listened" to are:
Events that can be "listened" to are:

- onChange
- onBlur
Expand All @@ -25,7 +25,7 @@ function App() {
const form = useForm({
defaultValues: {
country: '',
county: '',
province: '',
},
// ...
})
Expand All @@ -41,18 +41,18 @@ function App() {
onChange={(e) => field.handleChange(e.target.value)}
listener={{
onChange: ({ value }) => {
form.reset({county: ''})
form.reset({province: ''})
}
}}
/>
</label>
)}
</form.Field>

<form.Field name="county">
<form.Field name="province">
{(field) => (
<label>
<div>County</div>
<div>Province</div>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
Expand Down
2 changes: 1 addition & 1 deletion packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ export class FieldApi<
this.form.setFieldValue(this.name, updater as never, options)

this.options.listeners?.onChange?.({
value: updater as never,
value: this.state.value,
fieldApi: this,
})

Expand Down
2 changes: 1 addition & 1 deletion packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ export class FormApi<

this.store.batch(() => {
void (
Object.values(this.fieldInfo) as FieldInfo<any, TFormValidator>[]
Object.values(this.fieldInfo) as FieldInfo<TFormData, TFormValidator>[]
).forEach((field) => {
field.instance?.options.listeners?.onSubmit?.({
value: field.instance.state.value,
Expand Down
1 change: 0 additions & 1 deletion packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,6 @@ describe('form api', () => {
listeners: {
onSubmit: ({ value }) => {
triggered = value
console.log(value)
},
},
})
Expand Down

0 comments on commit 084e8da

Please sign in to comment.