MCPcopy
hub / github.com/marmelab/react-admin / useInput

Function useInput

packages/ra-core/src/form/useInput.ts:25–143  ·  view source on GitHub ↗
(
    props: InputProps<ValueType>
)

Source from the content-addressed store, hash-verified

23const defaultParse = (value: string) => (value === '' ? null : value);
24
25export const useInput = <ValueType = any>(
26 props: InputProps<ValueType>
27): UseInputValue => {
28 const {
29 defaultValue,
30 format = defaultFormat,
31 id,
32 isRequired: isRequiredOption,
33 name,
34 onBlur: initialOnBlur,
35 onChange: initialOnChange,
36 parse: parseProp = defaultParse,
37 source,
38 validate,
39 ...options
40 } = props;
41 const finalSource = useWrappedSource(source);
42 const finalName = name || finalSource;
43 const formGroupName = useFormGroupContext();
44 const formGroups = useFormGroups();
45 const record = useRecordContext();
46 // @ts-ignore
47 const parse = useEvent(parseProp);
48 const defaultId = useId();
49
50 if (!finalName && process.env.NODE_ENV === 'development') {
51 console.warn(
52 'Input components require either a source or a name prop.'
53 );
54 }
55
56 useEffect(() => {
57 if (!formGroups || formGroupName == null) {
58 return;
59 }
60
61 formGroups.registerField(finalSource, formGroupName);
62
63 return () => {
64 formGroups.unregisterField(finalSource, formGroupName);
65 };
66 }, [formGroups, formGroupName, finalSource]);
67
68 const sanitizedValidate = Array.isArray(validate)
69 ? composeValidators(validate)
70 : validate;
71
72 // Fetch the defaultValue from the record if available or apply the provided defaultValue.
73 // This ensures dynamically added inputs have their value set correctly (ArrayInput for example).
74 // We don't do this for the form level defaultValues so that it works as it should in react-hook-form
75 // (i.e. field level defaultValue override form level defaultValues for this field).
76 const {
77 field: controllerField,
78 fieldState,
79 formState,
80 } = useController({
81 name: finalName,
82 defaultValue: get(record, finalSource, defaultValue),

Callers 15

CheckboxGroupInputFunction · 0.90
DatagridInputFunction · 0.90
BooleanInputFunction · 0.90
NullableBooleanInputFunction · 0.90
TimeInputFunction · 0.90
TextInputFunction · 0.90
MyComponentFunction · 0.90
SelectInputFunction · 0.90
RadioButtonGroupInputFunction · 0.90
FileInputFunction · 0.90
AutocompleteInputFunction · 0.90
DateInputFunction · 0.90

Calls 11

useWrappedSourceFunction · 0.90
useFormGroupContextFunction · 0.90
useFormGroupsFunction · 0.90
useRecordContextFunction · 0.90
useEventFunction · 0.90
composeValidatorsFunction · 0.90
isRequiredFunction · 0.90
parseFunction · 0.70
formatFunction · 0.70
getFunction · 0.50

Tested by 7

MyComponentFunction · 0.72
ReferenceInputControllerFunction · 0.72
AutocompleteInputFunction · 0.72
InputFunction · 0.72
InputFunction · 0.72
InputFunction · 0.72
InputFunction · 0.68