Skip to content

Commit

Permalink
Fix(input) Refactor date and time input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
arimet committed Dec 22, 2023
1 parent e32f946 commit 355d240
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
30 changes: 21 additions & 9 deletions packages/ra-ui-materialui/src/input/DateInput.tsx
Expand Up @@ -65,17 +65,22 @@ export const DateInput = ({
});
const [renderCount, setRenderCount] = React.useState(1);

const initialDefaultValueRef = React.useRef(defaultValue);
const initialDefaultValueRef = React.useRef(field.value);

React.useEffect(() => {
if (initialDefaultValueRef.current !== defaultValue) {
const initialDateValue =
new Date(initialDefaultValueRef.current).getTime() || null;

const fieldDateValue = new Date(field.value).getTime() || null;

if (initialDateValue !== fieldDateValue) {
setRenderCount(r => r + 1);
parse
? field.onChange(parse(defaultValue))
: field.onChange(defaultValue);
initialDefaultValueRef.current = defaultValue;
? field.onChange(parse(field.value))
: field.onChange(field.value);
initialDefaultValueRef.current = field.value;
}
}, [defaultValue, setRenderCount, parse, field]);
}, [setRenderCount, parse, field]);

const { onBlur: onBlurFromField } = field;
const hasFocus = React.useRef(false);
Expand All @@ -99,11 +104,17 @@ export const DateInput = ({
!isNaN(new Date(target.valueAsDate).getTime())
? parse
? parse(target.valueAsDate)
: target.valueAsDate
: getStringFromDate(target.valueAsDate)
: parse
? parse(target.value)
: getStringFromDate(target.value);

// if value empty, set it to null
if (newValue === '') {
field.onChange(null);
return;
}

field.onChange(newValue);
};

Expand All @@ -130,7 +141,7 @@ export const DateInput = ({
<TextField
id={id}
inputRef={ref}
defaultValue={format(defaultValue)}
defaultValue={format(initialDefaultValueRef.current)}
key={renderCount}
type="date"
onChange={handleChange}
Expand Down Expand Up @@ -199,7 +210,8 @@ const getStringFromDate = (value: string | Date) => {
// null, undefined and empty string values should not go through dateFormatter
// otherwise, it returns undefined and will make the input an uncontrolled one.
if (value == null || value === '') {
return '';
console.log('toto');
return null;
}

if (value instanceof Date) {
Expand Down
19 changes: 12 additions & 7 deletions packages/ra-ui-materialui/src/input/DateTimeInput.tsx
Expand Up @@ -56,17 +56,22 @@ export const DateTimeInput = ({
});
const [renderCount, setRenderCount] = React.useState(1);

const initialDefaultValueRef = React.useRef(defaultValue);
const initialDefaultValueRef = React.useRef(field.value);

React.useEffect(() => {
if (initialDefaultValueRef.current !== defaultValue) {
const initialDateValue =
new Date(initialDefaultValueRef.current).getTime() || null;

const fieldDateValue = new Date(field.value).getTime() || null;

if (initialDateValue !== fieldDateValue) {
setRenderCount(r => r + 1);
parse
? field.onChange(parse(defaultValue))
: field.onChange(defaultValue);
initialDefaultValueRef.current = defaultValue;
? field.onChange(parse(field.value))
: field.onChange(field.value);
initialDefaultValueRef.current = field.value;
}
}, [defaultValue, setRenderCount, parse, field]);
}, [setRenderCount, parse, field]);

const { onBlur: onBlurFromField } = field;
const hasFocus = React.useRef(false);
Expand Down Expand Up @@ -121,7 +126,7 @@ export const DateTimeInput = ({
<TextField
id={id}
inputRef={ref}
defaultValue={format(defaultValue)}
defaultValue={format(initialDefaultValueRef.current)}
key={renderCount}
type="datetime-local"
onChange={handleChange}
Expand Down

0 comments on commit 355d240

Please sign in to comment.