Skip to content

Commit

Permalink
[mui#1111] Properly rely on views order when switching views (mui#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko authored Jun 14, 2019
1 parent 41c49c7 commit 2aff556
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
12 changes: 6 additions & 6 deletions lib/src/Picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const Picker: React.FunctionComponent<PickerProps> = props => {
{openView === 'year' && (
<YearSelection
date={date}
onChange={handleChangeAndOpenNext('month')}
onChange={handleChangeAndOpenNext}
minDate={minDate}
maxDate={maxDate}
disablePast={disablePast}
Expand All @@ -132,7 +132,7 @@ export const Picker: React.FunctionComponent<PickerProps> = props => {
{openView === 'month' && (
<MonthSelection
date={date}
onChange={handleChangeAndOpenNext('date')}
onChange={handleChangeAndOpenNext}
minDate={minDate}
maxDate={maxDate}
disablePast={disablePast}
Expand All @@ -144,7 +144,7 @@ export const Picker: React.FunctionComponent<PickerProps> = props => {
{openView === 'date' && (
<Calendar
date={date}
onChange={handleChangeAndOpenNext('hours')}
onChange={handleChangeAndOpenNext}
onMonthChange={onMonthChange}
disablePast={disablePast}
disableFuture={disableFuture}
Expand All @@ -167,9 +167,9 @@ export const Picker: React.FunctionComponent<PickerProps> = props => {
ampm={ampm}
type={openView}
minutesStep={minutesStep}
onHourChange={handleChangeAndOpenNext('minutes')}
onMinutesChange={handleChangeAndOpenNext('seconds')}
onSecondsChange={handleChangeAndOpenNext(null)}
onHourChange={handleChangeAndOpenNext}
onMinutesChange={handleChangeAndOpenNext}
onSecondsChange={handleChangeAndOpenNext}
/>
)}
</div>
Expand Down
32 changes: 10 additions & 22 deletions lib/src/_shared/hooks/useViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,19 @@ export function useViews(
openTo && views.includes(openTo) ? openTo : views[0]
);

const getNextAvailableView = React.useCallback(
(nextView: PickerView) => {
if (views.includes(nextView)) {
return nextView;
}
return views[views.indexOf(openView!) + 1];
},
[openView, views]
);

const handleChangeAndOpenNext = React.useCallback(
(nextView: PickerView | null) => {
return (date: MaterialUiPickersDate, isFinish?: boolean) => {
const nextViewToOpen = nextView && getNextAvailableView(nextView);
if (isFinish && nextViewToOpen) {
// do not close picker if needs to show next view
onChange(date, false);
setOpenView(nextViewToOpen);
return;
}
(date: MaterialUiPickersDate, isFinish?: boolean) => {
const nextViewToOpen = views[views.indexOf(openView!) + 1];
if (isFinish && nextViewToOpen) {
// do not close picker if needs to show next view
onChange(date, false);
setOpenView(nextViewToOpen);
return;
}

onChange(date, Boolean(isFinish));
};
onChange(date, Boolean(isFinish));
},
[getNextAvailableView, onChange]
[onChange, openView, views]
);

return { handleChangeAndOpenNext, openView, setOpenView };
Expand Down

0 comments on commit 2aff556

Please sign in to comment.