Skip to content

Commit fb84fb2

Browse files
Harry Whorlowharry-whorlow
Harry Whorlow
authored andcommitted
chore: init functionality
1 parent f55b193 commit fb84fb2

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
@@ -20,6 +20,7 @@ import type {
2020
FieldInfo,
2121
FormApi,
2222
FormAsyncValidateOrFn,
23+
FormState,
2324
FormValidateAsyncFn,
2425
FormValidateFn,
2526
FormValidateOrFn,
@@ -100,6 +101,7 @@ export type FieldValidateFn<
100101
any,
101102
any,
102103
any,
104+
any,
103105
any
104106
>
105107
}) => unknown
@@ -183,6 +185,7 @@ export type FieldValidateAsyncFn<
183185
any,
184186
any,
185187
any,
188+
any,
186189
any
187190
>
188191
signal: AbortSignal
@@ -265,10 +268,39 @@ export type FieldListenerFn<
265268
any,
266269
any,
267270
any,
271+
any,
268272
any
269273
>
270274
}) => void
271275

276+
/**
277+
* @private
278+
*/
279+
export type FieldMetaFn<
280+
TParentData,
281+
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
282+
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
283+
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
284+
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
285+
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
286+
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
287+
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
288+
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
289+
TFieldMetaExtension extends object,
290+
> = (
291+
props: FormState<
292+
TParentData,
293+
TFormOnMount,
294+
TFormOnChange,
295+
TFormOnChangeAsync,
296+
TFormOnBlur,
297+
TFormOnBlurAsync,
298+
TFormOnSubmit,
299+
TFormOnSubmitAsync,
300+
TFormOnServer
301+
>,
302+
) => TFieldMetaExtension
303+
272304
export interface FieldValidators<
273305
TParentData,
274306
TName extends DeepKeys<TParentData>,
@@ -383,6 +415,15 @@ export interface FieldOptions<
383415
TOnSubmitAsync extends
384416
| undefined
385417
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
418+
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
419+
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
420+
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
421+
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
422+
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
423+
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
424+
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
425+
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
426+
TFieldMetaExtension extends object,
386427
> {
387428
/**
388429
* The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.
@@ -436,13 +477,30 @@ export interface FieldOptions<
436477
any,
437478
any,
438479
any,
439-
any
480+
any,
481+
TFieldMetaExtension
440482
>
441483
>
442484
/**
443485
* A list of listeners which attach to the corresponding events
444486
*/
445487
listeners?: FieldListeners<TParentData, TName, TData>
488+
489+
/**
490+
* A list of listeners which attach to the corresponding events
491+
*/
492+
meta?: FieldMetaFn<
493+
TParentData,
494+
TFormOnMount,
495+
TFormOnChange,
496+
TFormOnChangeAsync,
497+
TFormOnBlur,
498+
TFormOnBlurAsync,
499+
TFormOnSubmit,
500+
TFormOnSubmitAsync,
501+
TFormOnServer,
502+
TFieldMetaExtension
503+
>
446504
/**
447505
* 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.
448506
*/
@@ -492,6 +550,7 @@ export interface FieldApiOptions<
492550
| FormAsyncValidateOrFn<TParentData>,
493551
in out TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
494552
in out TParentSubmitMeta,
553+
in out TFieldMetaExtension extends object,
495554
> extends FieldOptions<
496555
TParentData,
497556
TName,
@@ -502,7 +561,16 @@ export interface FieldApiOptions<
502561
TOnBlur,
503562
TOnBlurAsync,
504563
TOnSubmit,
505-
TOnSubmitAsync
564+
TOnSubmitAsync,
565+
TFormOnMount,
566+
TFormOnChange,
567+
TFormOnChangeAsync,
568+
TFormOnBlur,
569+
TFormOnBlurAsync,
570+
TFormOnSubmit,
571+
TFormOnSubmitAsync,
572+
TFormOnServer,
573+
TFieldMetaExtension
506574
> {
507575
form: FormApi<
508576
TParentData,
@@ -542,6 +610,7 @@ export type FieldMetaBase<
542610
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
543611
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
544612
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
613+
TFieldMetaExtension extends object = {},
545614
> = {
546615
/**
547616
* A flag indicating whether the field has been touched.
@@ -575,7 +644,7 @@ export type FieldMetaBase<
575644
* A flag indicating whether the field is currently being validated.
576645
*/
577646
isValidating: boolean
578-
}
647+
} & TFieldMetaExtension
579648

580649
export type AnyFieldMetaBase = FieldMetaBase<
581650
any,
@@ -594,6 +663,7 @@ export type AnyFieldMetaBase = FieldMetaBase<
594663
any,
595664
any,
596665
any,
666+
any,
597667
any
598668
>
599669

@@ -621,6 +691,7 @@ export type FieldMetaDerived<
621691
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
622692
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
623693
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
694+
TFieldMetaExtension extends object = {},
624695
> = {
625696
/**
626697
* An array of errors related to the field value.
@@ -660,7 +731,7 @@ export type FieldMetaDerived<
660731
* A flag indicating whether the field's current value is the default value
661732
*/
662733
isDefaultValue: boolean
663-
}
734+
} & TFieldMetaExtension
664735

665736
export type AnyFieldMetaDerived = FieldMetaDerived<
666737
any,
@@ -679,6 +750,7 @@ export type AnyFieldMetaDerived = FieldMetaDerived<
679750
any,
680751
any,
681752
any,
753+
any,
682754
any
683755
>
684756

@@ -709,6 +781,7 @@ export type FieldMeta<
709781
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
710782
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
711783
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
784+
TFieldMetaExtension extends object = {},
712785
> = FieldMetaBase<
713786
TParentData,
714787
TName,
@@ -726,7 +799,8 @@ export type FieldMeta<
726799
TFormOnBlur,
727800
TFormOnBlurAsync,
728801
TFormOnSubmit,
729-
TFormOnSubmitAsync
802+
TFormOnSubmitAsync,
803+
TFieldMetaExtension
730804
> &
731805
FieldMetaDerived<
732806
TParentData,
@@ -745,7 +819,8 @@ export type FieldMeta<
745819
TFormOnBlur,
746820
TFormOnBlurAsync,
747821
TFormOnSubmit,
748-
TFormOnSubmitAsync
822+
TFormOnSubmitAsync,
823+
TFieldMetaExtension
749824
>
750825

751826
export type AnyFieldMeta = FieldMeta<
@@ -765,6 +840,7 @@ export type AnyFieldMeta = FieldMeta<
765840
any,
766841
any,
767842
any,
843+
any,
768844
any
769845
>
770846

@@ -795,6 +871,7 @@ export type FieldState<
795871
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
796872
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
797873
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
874+
TFieldMetaExtension extends object,
798875
> = {
799876
/**
800877
* The current value of the field.
@@ -820,7 +897,8 @@ export type FieldState<
820897
TFormOnBlur,
821898
TFormOnBlurAsync,
822899
TFormOnSubmit,
823-
TFormOnSubmitAsync
900+
TFormOnSubmitAsync,
901+
TFieldMetaExtension
824902
>
825903
}
826904

@@ -848,6 +926,7 @@ export type AnyFieldApi = FieldApi<
848926
any,
849927
any,
850928
any,
929+
any,
851930
any
852931
>
853932

@@ -900,6 +979,7 @@ export class FieldApi<
900979
| FormAsyncValidateOrFn<TParentData>,
901980
in out TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
902981
in out TParentSubmitMeta,
982+
in out TFieldMetaExtension extends object,
903983
> {
904984
/**
905985
* A reference to the form API instance.
@@ -923,7 +1003,8 @@ export class FieldApi<
9231003
TFormOnSubmit,
9241004
TFormOnSubmitAsync,
9251005
TFormOnServer,
926-
TParentSubmitMeta
1006+
TParentSubmitMeta,
1007+
TFieldMetaExtension
9271008
>['form']
9281009
/**
9291010
* The field name.
@@ -951,7 +1032,8 @@ export class FieldApi<
9511032
TFormOnSubmit,
9521033
TFormOnSubmitAsync,
9531034
TFormOnServer,
954-
TParentSubmitMeta
1035+
TParentSubmitMeta,
1036+
TFieldMetaExtension
9551037
> = {} as any
9561038
/**
9571039
* The field state store.
@@ -974,7 +1056,8 @@ export class FieldApi<
9741056
TFormOnBlur,
9751057
TFormOnBlurAsync,
9761058
TFormOnSubmit,
977-
TFormOnSubmitAsync
1059+
TFormOnSubmitAsync,
1060+
TFieldMetaExtension
9781061
>
9791062
>
9801063
/**
@@ -1012,7 +1095,8 @@ export class FieldApi<
10121095
TFormOnSubmit,
10131096
TFormOnSubmitAsync,
10141097
TFormOnServer,
1015-
TParentSubmitMeta
1098+
TParentSubmitMeta,
1099+
TFieldMetaExtension
10161100
>,
10171101
) {
10181102
this.form = opts.form as never
@@ -1052,7 +1136,8 @@ export class FieldApi<
10521136
TFormOnBlur,
10531137
TFormOnBlurAsync,
10541138
TFormOnSubmit,
1055-
TFormOnSubmitAsync
1139+
TFormOnSubmitAsync,
1140+
TFieldMetaExtension
10561141
>
10571142
},
10581143
})
@@ -1136,6 +1221,11 @@ export class FieldApi<
11361221
fieldApi: this,
11371222
})
11381223

1224+
this.setMeta((prev) => ({
1225+
...prev,
1226+
...this.options.meta?.(this.form.state),
1227+
}))
1228+
11391229
return cleanup
11401230
}
11411231

@@ -1162,7 +1252,8 @@ export class FieldApi<
11621252
TFormOnSubmit,
11631253
TFormOnSubmitAsync,
11641254
TFormOnServer,
1165-
TParentSubmitMeta
1255+
TParentSubmitMeta,
1256+
TFieldMetaExtension
11661257
>,
11671258
) => {
11681259
this.options = opts as never
@@ -1211,6 +1302,11 @@ export class FieldApi<
12111302

12121303
this.triggerOnChangeListener()
12131304

1305+
this.setMeta((prev) => ({
1306+
...prev,
1307+
...this.options.meta?.(this.form.state),
1308+
}))
1309+
12141310
this.validate('change')
12151311
}
12161312

@@ -1238,7 +1334,8 @@ export class FieldApi<
12381334
TFormOnBlur,
12391335
TFormOnBlurAsync,
12401336
TFormOnSubmit,
1241-
TFormOnSubmitAsync
1337+
TFormOnSubmitAsync,
1338+
TFieldMetaExtension
12421339
>
12431340
>,
12441341
) => this.form.setFieldMeta(this.name, updater)

packages/form-core/src/FormApi.ts

+1
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ export type FieldInfo<TFormData> = {
484484
any,
485485
any,
486486
any,
487+
any,
487488
any
488489
> | null
489490
/**

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

+23
Original file line numberDiff line numberDiff line change
@@ -2457,4 +2457,27 @@ describe('field api', () => {
24572457

24582458
expect(field.getMeta().errorSourceMap.onChange).toEqual('field')
24592459
})
2460+
2461+
it('should have user defined meta and react to value change', () => {
2462+
const form = new FormApi({
2463+
defaultValues: {
2464+
name: 'Stegosaurus',
2465+
},
2466+
})
2467+
form.mount()
2468+
2469+
const nameField = new FieldApi({
2470+
form,
2471+
name: 'name',
2472+
meta: ({ values }) => ({
2473+
dinosaur: values.name === 'Stegosaurus' ? 'dino' : 'notDino',
2474+
}),
2475+
})
2476+
2477+
nameField.mount()
2478+
expect(nameField.getMeta().dinosaur).toEqual('dino')
2479+
2480+
nameField.handleChange('Cat')
2481+
expect(nameField.getMeta().dinosaur).toEqual('notDino')
2482+
})
24602483
})

0 commit comments

Comments
 (0)