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

Function DateInput

packages/ra-ui-materialui/src/input/DateInput.tsx:54–242  ·  view source on GitHub ↗
(props: DateInputProps)

Source from the content-addressed store, hash-verified

52 * <DateInput source="published_at" parse={val => new Date(val)} />
53 */
54export const DateInput = (props: DateInputProps) => {
55 const {
56 className,
57 defaultValue,
58 format = defaultFormat,
59 label,
60 source,
61 resource,
62 helperText,
63 margin,
64 onChange,
65 onFocus,
66 validate,
67 variant,
68 disabled,
69 readOnly,
70 ...rest
71 } = useThemeProps({
72 props: props,
73 name: PREFIX,
74 });
75
76 const { field, fieldState, id, isRequired } = useInput({
77 defaultValue,
78 resource,
79 source,
80 validate,
81 disabled,
82 readOnly,
83 format,
84 ...rest,
85 });
86 const localInputRef = React.useRef<HTMLInputElement>();
87 // DateInput is not a really controlled input to ensure users can start entering a date, go to another input and come back to complete it.
88 // This ref stores the value that is passed to the input defaultValue prop to solve this issue.
89 const initialDefaultValueRef = React.useRef(field.value);
90 // As the defaultValue prop won't trigger a remount of the HTML input, we will force it by changing the key.
91 const [inputKey, setInputKey] = React.useState(1);
92 // This ref let us track that the last change of the form state value was made by the input itself
93 const wasLastChangedByInput = React.useRef(false);
94
95 // This effect ensures we stays in sync with the react-hook-form state when the value changes from outside the input
96 // for instance by using react-hook-form reset or setValue methods.
97 React.useEffect(() => {
98 // Ignore react-hook-form state changes if it came from the input itself
99 if (wasLastChangedByInput.current) {
100 // Resets the flag to ensure futures changes are handled
101 wasLastChangedByInput.current = false;
102 return;
103 }
104
105 const hasNewValueFromForm =
106 localInputRef.current?.value !== field.value &&
107 !(localInputRef.current?.value === '' && field.value == null);
108
109 if (hasNewValueFromForm) {
110 // The value has changed from outside the input, we update the input value
111 initialDefaultValueRef.current = field.value;

Callers

nothing calls this directly

Calls 5

useInputFunction · 0.90
useEventFunction · 0.90
sanitizeInputRestPropsFunction · 0.90
onChangeFunction · 0.50
formatFunction · 0.50

Tested by

no test coverage detected