Skip to content

Commit 94d9448

Browse files
Harry Whorlowharry-whorlow
Harry Whorlow
authored andcommitted
chore: init functionality
1 parent d8f31f1 commit 94d9448

File tree

3 files changed

+135
-14
lines changed

3 files changed

+135
-14
lines changed

packages/form-core/src/FieldApi.ts

+111-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
FieldInfo,
1515
FormApi,
1616
FormAsyncValidateOrFn,
17+
FormState,
1718
FormValidateAsyncFn,
1819
FormValidateFn,
1920
FormValidateOrFn,
@@ -92,6 +93,7 @@ export type FieldValidateFn<
9293
any,
9394
any,
9495
any,
96+
any,
9597
any
9698
>
9799
}) => unknown
@@ -176,6 +178,7 @@ export type FieldValidateAsyncFn<
176178
any,
177179
any,
178180
any,
181+
any,
179182
any
180183
>
181184
signal: AbortSignal
@@ -259,10 +262,39 @@ export type FieldListenerFn<
259262
any,
260263
any,
261264
any,
265+
any,
262266
any
263267
>
264268
}) => void
265269

270+
/**
271+
* @private
272+
*/
273+
export type FieldMetaFn<
274+
TParentData,
275+
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
276+
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
277+
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
278+
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
279+
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
280+
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
281+
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
282+
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
283+
TFieldMetaExtension extends object,
284+
> = (
285+
props: FormState<
286+
TParentData,
287+
TFormOnMount,
288+
TFormOnChange,
289+
TFormOnChangeAsync,
290+
TFormOnBlur,
291+
TFormOnBlurAsync,
292+
TFormOnSubmit,
293+
TFormOnSubmitAsync,
294+
TFormOnServer
295+
>,
296+
) => TFieldMetaExtension
297+
266298
export interface FieldValidators<
267299
TParentData,
268300
TName extends DeepKeys<TParentData>,
@@ -375,6 +407,15 @@ export interface FieldOptions<
375407
TOnSubmitAsync extends
376408
| undefined
377409
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
410+
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
411+
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
412+
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
413+
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
414+
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
415+
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
416+
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
417+
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
418+
TFieldMetaExtension extends object,
378419
> {
379420
/**
380421
* The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.
@@ -428,13 +469,30 @@ export interface FieldOptions<
428469
any,
429470
any,
430471
any,
431-
any
472+
any,
473+
TFieldMetaExtension
432474
>
433475
>
434476
/**
435477
* A list of listeners which attach to the corresponding events
436478
*/
437479
listeners?: FieldListeners<TParentData, TName, TData>
480+
481+
/**
482+
* A list of listeners which attach to the corresponding events
483+
*/
484+
meta?: FieldMetaFn<
485+
TParentData,
486+
TFormOnMount,
487+
TFormOnChange,
488+
TFormOnChangeAsync,
489+
TFormOnBlur,
490+
TFormOnBlurAsync,
491+
TFormOnSubmit,
492+
TFormOnSubmitAsync,
493+
TFormOnServer,
494+
TFieldMetaExtension
495+
>
438496
/**
439497
* Disable the `flat(1)` operation on `field.errors`. This is useful if you want to keep the error structure as is. Not suggested for most use-cases.
440498
*/
@@ -470,6 +528,7 @@ export interface FieldApiOptions<
470528
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
471529
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
472530
TParentSubmitMeta,
531+
TFieldMetaExtension extends object,
473532
> extends FieldOptions<
474533
TParentData,
475534
TName,
@@ -480,7 +539,16 @@ export interface FieldApiOptions<
480539
TOnBlur,
481540
TOnBlurAsync,
482541
TOnSubmit,
483-
TOnSubmitAsync
542+
TOnSubmitAsync,
543+
TFormOnMount,
544+
TFormOnChange,
545+
TFormOnChangeAsync,
546+
TFormOnBlur,
547+
TFormOnBlurAsync,
548+
TFormOnSubmit,
549+
TFormOnSubmitAsync,
550+
TFormOnServer,
551+
TFieldMetaExtension
484552
> {
485553
form: FormApi<
486554
TParentData,
@@ -520,6 +588,7 @@ export type FieldMetaBase<
520588
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
521589
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
522590
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
591+
TFieldMetaExtension extends object = {},
523592
> = {
524593
/**
525594
* A flag indicating whether the field has been touched.
@@ -564,7 +633,7 @@ export type FieldMetaBase<
564633
* A flag indicating whether the field is currently being validated.
565634
*/
566635
isValidating: boolean
567-
}
636+
} & TFieldMetaExtension
568637

569638
export type AnyFieldMetaBase = FieldMetaBase<
570639
any,
@@ -583,6 +652,7 @@ export type AnyFieldMetaBase = FieldMetaBase<
583652
any,
584653
any,
585654
any,
655+
any,
586656
any
587657
>
588658

@@ -610,6 +680,7 @@ export type FieldMetaDerived<
610680
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
611681
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
612682
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
683+
TFieldMetaExtension extends object = {},
613684
> = {
614685
/**
615686
* An array of errors related to the field value.
@@ -656,7 +727,7 @@ export type FieldMetaDerived<
656727
* A flag that is `true` if the field's value has not been modified by the user. Opposite of `isDirty`.
657728
*/
658729
isPristine: boolean
659-
}
730+
} & TFieldMetaExtension
660731

661732
export type AnyFieldMetaDerived = FieldMetaDerived<
662733
any,
@@ -675,6 +746,7 @@ export type AnyFieldMetaDerived = FieldMetaDerived<
675746
any,
676747
any,
677748
any,
749+
any,
678750
any
679751
>
680752

@@ -705,6 +777,7 @@ export type FieldMeta<
705777
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
706778
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
707779
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
780+
TFieldMetaExtension extends object = {},
708781
> = FieldMetaBase<
709782
TParentData,
710783
TName,
@@ -722,7 +795,8 @@ export type FieldMeta<
722795
TFormOnBlur,
723796
TFormOnBlurAsync,
724797
TFormOnSubmit,
725-
TFormOnSubmitAsync
798+
TFormOnSubmitAsync,
799+
TFieldMetaExtension
726800
> &
727801
FieldMetaDerived<
728802
TParentData,
@@ -741,7 +815,8 @@ export type FieldMeta<
741815
TFormOnBlur,
742816
TFormOnBlurAsync,
743817
TFormOnSubmit,
744-
TFormOnSubmitAsync
818+
TFormOnSubmitAsync,
819+
TFieldMetaExtension
745820
>
746821

747822
export type AnyFieldMeta = FieldMeta<
@@ -761,6 +836,7 @@ export type AnyFieldMeta = FieldMeta<
761836
any,
762837
any,
763838
any,
839+
any,
764840
any
765841
>
766842

@@ -791,6 +867,7 @@ export type FieldState<
791867
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
792868
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
793869
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
870+
TFieldMetaExtension extends object,
794871
> = {
795872
/**
796873
* The current value of the field.
@@ -816,7 +893,8 @@ export type FieldState<
816893
TFormOnBlur,
817894
TFormOnBlurAsync,
818895
TFormOnSubmit,
819-
TFormOnSubmitAsync
896+
TFormOnSubmitAsync,
897+
TFieldMetaExtension
820898
>
821899
}
822900

@@ -844,6 +922,7 @@ export type AnyFieldApi = FieldApi<
844922
any,
845923
any,
846924
any,
925+
any,
847926
any
848927
>
849928

@@ -882,6 +961,7 @@ export class FieldApi<
882961
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
883962
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
884963
TParentSubmitMeta,
964+
TFieldMetaExtension extends object,
885965
> {
886966
/**
887967
* A reference to the form API instance.
@@ -905,7 +985,8 @@ export class FieldApi<
905985
TFormOnSubmit,
906986
TFormOnSubmitAsync,
907987
TFormOnServer,
908-
TParentSubmitMeta
988+
TParentSubmitMeta,
989+
TFieldMetaExtension
909990
>['form']
910991
/**
911992
* The field name.
@@ -933,7 +1014,8 @@ export class FieldApi<
9331014
TFormOnSubmit,
9341015
TFormOnSubmitAsync,
9351016
TFormOnServer,
936-
TParentSubmitMeta
1017+
TParentSubmitMeta,
1018+
TFieldMetaExtension
9371019
> = {} as any
9381020
/**
9391021
* The field state store.
@@ -956,7 +1038,8 @@ export class FieldApi<
9561038
TFormOnBlur,
9571039
TFormOnBlurAsync,
9581040
TFormOnSubmit,
959-
TFormOnSubmitAsync
1041+
TFormOnSubmitAsync,
1042+
TFieldMetaExtension
9601043
>
9611044
>
9621045
/**
@@ -990,7 +1073,8 @@ export class FieldApi<
9901073
TFormOnSubmit,
9911074
TFormOnSubmitAsync,
9921075
TFormOnServer,
993-
TParentSubmitMeta
1076+
TParentSubmitMeta,
1077+
TFieldMetaExtension
9941078
>,
9951079
) {
9961080
this.form = opts.form as never
@@ -1037,7 +1121,8 @@ export class FieldApi<
10371121
TFormOnBlur,
10381122
TFormOnBlurAsync,
10391123
TFormOnSubmit,
1040-
TFormOnSubmitAsync
1124+
TFormOnSubmitAsync,
1125+
TFieldMetaExtension
10411126
>
10421127
},
10431128
})
@@ -1110,6 +1195,11 @@ export class FieldApi<
11101195
fieldApi: this,
11111196
})
11121197

1198+
this.setMeta((prev) => ({
1199+
...prev,
1200+
...this.options.meta?.(this.form.state),
1201+
}))
1202+
11131203
return cleanup
11141204
}
11151205

@@ -1136,7 +1226,8 @@ export class FieldApi<
11361226
TFormOnSubmit,
11371227
TFormOnSubmitAsync,
11381228
TFormOnServer,
1139-
TParentSubmitMeta
1229+
TParentSubmitMeta,
1230+
TFieldMetaExtension
11401231
>,
11411232
) => {
11421233
// Default Value
@@ -1183,6 +1274,11 @@ export class FieldApi<
11831274
fieldApi: this,
11841275
})
11851276

1277+
this.setMeta((prev) => ({
1278+
...prev,
1279+
...this.options.meta?.(this.form.state),
1280+
}))
1281+
11861282
this.validate('change')
11871283
}
11881284

@@ -1210,7 +1306,8 @@ export class FieldApi<
12101306
TFormOnBlur,
12111307
TFormOnBlurAsync,
12121308
TFormOnSubmit,
1213-
TFormOnSubmitAsync
1309+
TFormOnSubmitAsync,
1310+
TFieldMetaExtension
12141311
>
12151312
>,
12161313
) => this.form.setFieldMeta(this.name, updater)

packages/form-core/src/FormApi.ts

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ export type FieldInfo<TFormData> = {
380380
any,
381381
any,
382382
any,
383+
any,
383384
any
384385
> | null
385386
/**

packages/form-core/tests/FieldApi.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1850,4 +1850,27 @@ describe('field api', () => {
18501850
expect(field.getMeta().errors).toStrictEqual([])
18511851
expect(form.state.canSubmit).toBe(true)
18521852
})
1853+
1854+
it('should have user defined meta and react to value change', () => {
1855+
const form = new FormApi({
1856+
defaultValues: {
1857+
name: 'Stegosaurus',
1858+
},
1859+
})
1860+
form.mount()
1861+
1862+
const nameField = new FieldApi({
1863+
form,
1864+
name: 'name',
1865+
meta: ({ values }) => ({
1866+
dinosaur: values.name === 'Stegosaurus' ? 'dino' : 'notDino',
1867+
}),
1868+
})
1869+
1870+
nameField.mount()
1871+
expect(nameField.getMeta().dinosaur).toEqual('dino')
1872+
1873+
nameField.handleChange('Cat')
1874+
expect(nameField.getMeta().dinosaur).toEqual('notDino')
1875+
})
18531876
})

0 commit comments

Comments
 (0)