Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[time picker] Only update time when updating input in TimePicker #4398

Merged
merged 7 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const valueManager: PickerStateValueManager<unknown, unknown> = {
parseInput: parsePickerInputValue,
areValuesEqual: (utils: MuiPickersAdapter<unknown>, a: unknown, b: unknown) =>
utils.isEqual(a, b),
updateValue: (utils, prevValue, newValue) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional method (to be named in a better way) that creates the new value from the previous valud and the new one.

Allows us in TimePicker to only take the time from the new value but keep the date of the old value.

TODO: Do the same for MobileTimePicker (maybe refacto to avoid duplication on the value managers at some point)


date-fns handles this behavior out of the box, you can pass a reference date and when updating a mask it only applies the change of the elements in the mask, the rest is taken from the reference date.
It would be perfect here, but the others date libs do not support it from what I see.

if (prevValue == null) {
return newValue
}

return utils.mergeDateAndTime(prevValue, newValue)
}
};

export interface DesktopTimePickerProps<TDate = unknown>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const KeyboardDateInput = React.forwardRef(function KeyboardDateInput(
const textFieldProps = useMaskedInput(other);
const adornmentPosition = InputAdornmentProps?.position || 'end';
const OpenPickerIcon = components.OpenPickerIcon || Calendar;

return renderInput({
ref,
inputRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const useMaskedInput = ({
setInnerInputValue(finalString);

const date = finalString === null ? null : utils.parse(finalString, inputFormat);

if (ignoreInvalidInputs && !utils.isValid(date)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface PickerStateValueManager<TInputValue, TDateValue> {
) => boolean;
emptyValue: TDateValue;
parseInput: (utils: MuiPickersAdapter<TDateValue>, value: TInputValue) => TDateValue;
updateValue?: (utils: MuiPickersAdapter<TDateValue>, prevValue: TDateValue | null, value: TDateValue) => TDateValue
}

export type PickerSelectionState = 'partial' | 'shallow' | 'finish';
Expand Down Expand Up @@ -141,9 +142,15 @@ export const usePickerState = <TInput, TDateValue>(
[acceptDate, disableCloseOnSelect, isMobileKeyboardViewOpen, draftState.draft],
);

const handleInputChange = (date: TDateValue, keyboardInputValue?: string) => {
console.log(parsedDateValue, date)
const cleanDate = valueManager.updateValue ? valueManager.updateValue(utils, parsedDateValue, date) : date
onChange(cleanDate, keyboardInputValue)
}

const inputProps = React.useMemo(
() => ({
onChange,
onChange: handleInputChange,
open: isOpen,
rawValue: value,
openPicker: () => setIsOpen(true),
Expand Down