MCPcopy
hub / github.com/dai-shi/react-tracked / useFormField

Function useFormField

examples/11_form/src/form.ts:38–84  ·  view source on GitHub ↗
(
  name: string,
  initialValue?: Value,
  validate?: (value: Value) => Error | null,
)

Source from the content-addressed store, hash-verified

36};
37
38const useFormField = (
39 name: string,
40 initialValue?: Value,
41 validate?: (value: Value) => Error | null,
42) => {
43 const state = useTrackedState();
44 const setState = useSetState();
45 const updateValue = useCallback(
46 (value: Value) => {
47 const err = validate && validate(value);
48 setState((prev) => {
49 const nextValues = { ...prev.values };
50 const nextErrors = { ...prev.errors };
51 if (err) {
52 delete nextValues[name];
53 nextErrors[name] = err;
54 } else {
55 nextValues[name] = value;
56 delete nextErrors[name];
57 }
58 return { values: nextValues, errors: nextErrors };
59 });
60 },
61 [name, validate, setState],
62 );
63 useLayoutEffect(() => {
64 updateValue(initialValue || '');
65 }, [initialValue, updateValue]);
66 const onChange = useCallback(
67 (event: ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
68 const { target } = event;
69 let value: Value;
70 if (target.type === 'checkbox') {
71 value = (target as HTMLInputElement).checked;
72 } else {
73 value = target.value;
74 }
75 updateValue(value);
76 },
77 [updateValue],
78 );
79 return {
80 value: String(state.values[name] || initialValue),
81 checked: Boolean(state.values[name] || initialValue),
82 onChange,
83 };
84};
85
86export { FormProvider, useFormValues, useFormError, useFormField };

Callers 1

person-form.tsxFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…