-
-
Notifications
You must be signed in to change notification settings - Fork 542
feat(core): add field listeners #801
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,24 @@ export type FieldAsyncValidateOrFn< | |
| TData | ||
| > | ||
|
|
||
| /** | ||
| * @private | ||
| */ | ||
| export type FieldListenerFn< | ||
| TParentData, | ||
| TName extends DeepKeys<TParentData>, | ||
| TFieldValidator extends | ||
| | Validator<DeepValue<TParentData, TName>, unknown> | ||
| | undefined = undefined, | ||
| TFormValidator extends | ||
| | Validator<TParentData, unknown> | ||
| | undefined = undefined, | ||
| TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>, | ||
| > = (props: { | ||
| value: TData | ||
| fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData> | ||
| }) => void | ||
|
|
||
| export interface FieldValidators< | ||
| TParentData, | ||
| TName extends DeepKeys<TParentData>, | ||
|
|
@@ -251,6 +269,40 @@ export interface FieldValidators< | |
| > | ||
| } | ||
|
|
||
| export interface FieldListeners< | ||
| TParentData, | ||
| TName extends DeepKeys<TParentData>, | ||
| TFieldValidator extends | ||
| | Validator<DeepValue<TParentData, TName>, unknown> | ||
| | undefined = undefined, | ||
| TFormValidator extends | ||
| | Validator<TParentData, unknown> | ||
| | undefined = undefined, | ||
| TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>, | ||
| > { | ||
| onChange?: FieldListenerFn< | ||
| TParentData, | ||
| TName, | ||
| TFieldValidator, | ||
| TFormValidator, | ||
| TData | ||
| > | ||
| onHandleChange?: FieldListenerFn< | ||
| TParentData, | ||
| TName, | ||
| TFieldValidator, | ||
| TFormValidator, | ||
| TData | ||
| > | ||
| onBlur?: FieldListenerFn< | ||
| TParentData, | ||
| TName, | ||
| TFieldValidator, | ||
| TFormValidator, | ||
| TData | ||
| > | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it would give more versatility to the feature, and maybe onSubmit should allow for transformation before sending out the data ? |
||
| } | ||
|
|
||
| /** | ||
| * An object type representing the options for a field in a form. | ||
| */ | ||
|
|
@@ -299,6 +351,16 @@ export interface FieldOptions< | |
| * An optional object with default metadata for the field. | ||
| */ | ||
| defaultMeta?: Partial<FieldMeta> | ||
| /** | ||
| * A list of listeners which attach to the corresponding events | ||
| */ | ||
| listeners?: FieldListeners< | ||
| TParentData, | ||
| TName, | ||
| TFieldValidator, | ||
| TFormValidator, | ||
| TData | ||
| > | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -479,6 +541,11 @@ export class FieldApi< | |
|
|
||
| this.prevState = state | ||
| this.state = state | ||
|
|
||
| this.options.listeners?.onChange?.({ | ||
| value: state.value, | ||
| fieldApi: this, | ||
| }) | ||
| }, | ||
| }, | ||
| ) | ||
|
|
@@ -949,6 +1016,11 @@ export class FieldApi< | |
| */ | ||
| handleChange = (updater: Updater<TData>) => { | ||
| this.setValue(updater, { touch: true }) | ||
|
|
||
| this.options.listeners?.onHandleChange?.({ | ||
| value: this.state.value, | ||
| fieldApi: this, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -961,6 +1033,11 @@ export class FieldApi< | |
| this.validate('change') | ||
| } | ||
| this.validate('blur') | ||
|
|
||
| this.options.listeners?.onBlur?.({ | ||
| value: this.state.value, | ||
| fieldApi: this, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced we need a different function for programmatic vs user input. If the user needs this distinction, they can do so outside of our
listenersIt also opens a naming can of worms and adds another one when we need an
onBlurChangekinda deal