MCPcopy
hub / github.com/final-form/react-final-form / useField

Function useField

src/useField.ts:30–394  ·  view source on GitHub ↗
(name: string, config: UseFieldConfig = {})

Source from the content-addressed store, hash-verified

28 value === "" ? undefined : value;
29
30function useField<
31 FieldValue = any,
32 T extends HTMLElement = HTMLElement,
33 FormValues = Record<string, any>,
34>(name: string, config: UseFieldConfig = {}): FieldRenderProps<FieldValue, T> {
35 const {
36 afterSubmit,
37 allowNull,
38 component,
39 data,
40 defaultValue,
41 format = defaultFormat,
42 formatOnBlur,
43 initialValue,
44 multiple,
45 parse = defaultParse,
46 subscription = all,
47 type,
48 validateFields,
49 value: _value,
50 } = config;
51 const form: FormApi<FormValues> = useForm<FormValues>("useField");
52
53 const configRef = useLatest(config);
54
55 const register = (
56 callback: (state: FieldState<any>) => void,
57 silent: boolean,
58 ) =>
59 // avoid using `state` const in any closures created inside `register`
60 // because they would refer `state` from current execution context
61 // whereas actual `state` would defined in the subsequent `useField` hook
62 // execution
63 // (that would be caused by `setState` call performed in `register` callback)
64 form.registerField(name as keyof FormValues, callback, subscription, {
65 afterSubmit,
66 beforeSubmit: () => {
67 const {
68 beforeSubmit,
69 formatOnBlur,
70 format = defaultFormat,
71 } = configRef.current;
72
73 if (formatOnBlur) {
74 const fieldState = form.getFieldState(name as keyof FormValues);
75 if (fieldState) {
76 const { value } = fieldState;
77 const formatted = format(value, name);
78
79 if (formatted !== value) {
80 form.change(name as keyof FormValues, formatted);
81 }
82 }
83 }
84
85 return beforeSubmit && beforeSubmit();
86 },
87 data,

Callers 15

NumberFieldValueFunction · 0.85
NumberInputValueFunction · 0.85
MyComponentFunction · 0.85
MyTypedComponentFunction · 0.85
Field1Function · 0.85
Field2Function · 0.85
ParentWithEffectFunction · 0.85
MyFieldComponentFunction · 0.85
MyFieldListenerFunction · 0.85
MyFieldFunction · 0.85
MySelectFieldFunction · 0.85

Calls 10

addLazyFieldMetaStateFunction · 0.90
useFormFunction · 0.85
useLatestFunction · 0.85
registerFunction · 0.85
shallowEqualFunction · 0.85
useConstantCallbackFunction · 0.85
formatFunction · 0.85
getValueFunction · 0.85
parseFunction · 0.85
isEqualFunction · 0.70

Tested by 13

NumberFieldValueFunction · 0.68
NumberInputValueFunction · 0.68
MyComponentFunction · 0.68
MyTypedComponentFunction · 0.68
Field1Function · 0.68
Field2Function · 0.68
ParentWithEffectFunction · 0.68
MyFieldComponentFunction · 0.68
MyFieldListenerFunction · 0.68
MyFieldFunction · 0.68
MySelectFieldFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…