Skip to content

Commit

Permalink
Make set today button works properly with autoOk (mui#1555)
Browse files Browse the repository at this point in the history
* Make set today button works properly with autoOk

* Add tests

* Fix autoOK behavior when several views shown
  • Loading branch information
dmtrKovalenko authored Mar 3, 2020
1 parent 3cd58d6 commit d92352a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
20 changes: 20 additions & 0 deletions lib/src/__tests__/e2e/DatePickerProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,24 @@ describe('DatePicker - different props', () => {

expect(component.find('h4[data-mui-test="datepicker-toolbar-date"]').text()).toBe('January');
});

it('showTodayLabel – accept current date when "today" button is clicked', () => {
const onCloseMock = jest.fn();
const onChangeMock = jest.fn();
const component = mount(
<MobileDatePicker
autoOk
showTodayButton
onClose={onCloseMock}
onChange={onChangeMock}
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
/>
);

component.find('input').simulate('click');
component.find('button[data-mui-test="today-action-button"]').simulate('click');

expect(onCloseMock).toHaveBeenCalled();
expect(onChangeMock).toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions lib/src/_shared/ModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export const ModalDialog: React.FC<ModalDialogProps> = ({
})}
>
{clearable && (
<Button color="primary" onClick={onClear}>
<Button data-mui-test="clear-action-button" color="primary" onClick={onClear}>
{clearLabel}
</Button>
)}

{showTodayButton && (
<Button color="primary" onClick={onSetToday}>
<Button data-mui-test="today-action-button" color="primary" onClick={onSetToday}>
{todayLabel}
</Button>
)}
Expand Down
10 changes: 7 additions & 3 deletions lib/src/_shared/hooks/usePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function usePickerState(props: BasePickerProps) {
const { autoOk, disabled, readOnly, onAccept, onChange, onError, value } = props;

const utils = useUtils();
const now = useNow();
const { date, inputFormat } = useDateValues(props);
const [pickerDate, setPickerDate] = useState(date);

Expand Down Expand Up @@ -67,14 +68,17 @@ export function usePickerState(props: BasePickerProps) {

const wrapperProps = useMemo(
() => ({
format: inputFormat,
open: isOpen,
format: inputFormat,
onClear: () => acceptDate(null, true),
onAccept: () => acceptDate(pickerDate, true),
onSetToday: () => setPickerDate(utils.date()),
onDismiss: () => setIsOpen(false),
onSetToday: () => {
setPickerDate(now);
acceptDate(now, Boolean(autoOk));
},
}),
[acceptDate, inputFormat, isOpen, pickerDate, setIsOpen, utils]
[acceptDate, autoOk, inputFormat, isOpen, now, pickerDate, setIsOpen]
);

const pickerProps = useMemo(
Expand Down
8 changes: 4 additions & 4 deletions lib/src/_shared/hooks/useViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ export function useViews({
}, [nextView, setOpenView]);

const handleChangeAndOpenNext = React.useCallback(
(date: MaterialUiPickersDate, isFinish?: boolean | symbol) => {
onChange(date, isFinish);
(date: MaterialUiPickersDate, isFinishedSelectionInCurrentView?: boolean | symbol) => {
onChange(date, Boolean(nextView) ? false : isFinishedSelectionInCurrentView);

if (isFinish) {
if (isFinishedSelectionInCurrentView) {
openNext();
}
},
[onChange, openNext]
[nextView, onChange, openNext]
);

return {
Expand Down

0 comments on commit d92352a

Please sign in to comment.