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

[DatePicker] Reduce coupling of parsing picker input value and props #24319

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/material-ui-lab/src/internal/pickers/date-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RangeInput, NonEmptyDateRange, DateRange } from '../../DateRangePicker/RangeTypes';
import { arrayIncludes } from './utils';
import { ParsableDate } from './constants/prop-types';
import { BasePickerProps } from './typings/BasePicker';
import { DatePickerView } from './typings/Views';
import { MuiPickersAdapter } from './hooks/useUtils';

Expand Down Expand Up @@ -106,18 +105,15 @@ export const getFormatAndMaskByViews = (
};
};

export function parsePickerInputValue(
utils: MuiPickersAdapter,
{ value }: BasePickerProps,
): unknown | null {
export function parsePickerInputValue(utils: MuiPickersAdapter, value: unknown): unknown {
const parsedValue = utils.date(value);

return utils.isValid(parsedValue) ? parsedValue : null;
}

export function parseRangeInputValue<TDate>(
utils: MuiPickersAdapter,
{ value = [null, null] }: BasePickerProps<RangeInput<TDate>, DateRange<TDate>>,
value: RangeInput<TDate> = [null, null],
) {
return value.map((date) =>
!utils.isValid(date) || date === null ? null : utils.startOfDay(utils.date(date)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { WrapperVariant } from '../wrappers/Wrapper';
import { BasePickerProps } from '../typings/BasePicker';
import { useUtils, useNow, MuiPickersAdapter } from './useUtils';

export interface PickerStateValueManager<TInput, TDateValue> {
parseInput: (utils: MuiPickersAdapter, props: BasePickerProps<TInput, TDateValue>) => TDateValue;
export interface PickerStateValueManager<TInputValue, TDateValue> {
parseInput: (utils: MuiPickersAdapter, value: TInputValue) => TDateValue;
emptyValue: TDateValue;
areValuesEqual: (
utils: MuiPickersAdapter,
Expand Down Expand Up @@ -37,23 +37,22 @@ export function usePickerState<TInput, TDateValue>(
const now = useNow();
const utils = useUtils();
const { isOpen, setIsOpen } = useOpenState(props);
const [pickerDate, setPickerDate] = React.useState(valueManager.parseInput(utils, props));
const [pickerDate, setPickerDate] = React.useState(valueManager.parseInput(utils, value));

// Mobile keyboard view is a special case.
// When it's open picker should work like closed, cause we are just showing text field
const [isMobileKeyboardViewOpen, setMobileKeyboardViewOpen] = React.useState(false);

React.useEffect(() => {
const parsedDateValue = valueManager.parseInput(utils, props);
const parsedDateValue = valueManager.parseInput(utils, value);
setPickerDate((currentPickerDate) => {
if (!valueManager.areValuesEqual(utils, currentPickerDate, parsedDateValue)) {
return parsedDateValue;
}

return currentPickerDate;
});
// We need to react only on value change, because `date` could potentially return new Date() on each render
}, [value, utils]); // eslint-disable-line
}, [value, utils, valueManager]);

const acceptDate = React.useCallback(
(acceptedDate: TDateValue, needClosePicker: boolean) => {
Expand Down