Skip to content

Commit c974e7b

Browse files
committed
fix(date-input): ability to update date input externally when it is passed a valid date
1 parent a765408 commit c974e7b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

packages/ui-components/src/lib/inputs/InputDate/InputDate.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
77
import { useFormContext } from '../../hooks';
88
import { InputText } from '../InputText/InputText';
99
import { InputProps } from '../InputBase';
10+
import { useEffect } from 'react';
1011

1112
export type InputDateProps = Omit<InputProps, 'onChange'> & {
1213
value?: Date | string;
@@ -33,8 +34,13 @@ export const InputDate: FC<InputDateProps> = ({
3334
const formContext = useFormContext();
3435
if (!formikProps && formContext) formikProps = formContext;
3536
const fieldValue = formikProps ? _get(formikProps?.values, props.name, '') : value;
37+
const fieldDateTime = valueFormat ? DateTime.fromFormat(fieldValue, valueFormat) : DateTime.fromISO(fieldValue);
3638
const [inputValue, setInputValue] = React.useState<DateTime | null>(fieldValue);
3739

40+
useEffect(() => {
41+
if (fieldDateTime.isValid) setInputValue(fieldDateTime);
42+
}, [fieldValue]);
43+
3844
return (
3945
<LocalizationProvider dateAdapter={AdapterLuxon}>
4046
<DatePicker

0 commit comments

Comments
 (0)