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] Don't run onChange if same date selected #1967

Merged
merged 7 commits into from
Jul 15, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions docs/prop-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/src/DateRangePicker/DateRangePickerDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const PureDateRangeDay = ({
{...other}
day={day}
selected={selected}
allowSameDateSelection={true}
inCurrentMonth={inCurrentMonth}
data-mui-test="DateRangeDay"
className={clsx(
Expand Down
19 changes: 19 additions & 0 deletions lib/src/__tests__/DatePickerTestingLib.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ describe('<DatePicker />', () => {

expect(screen.queryByRole('dialog')).toBeInTheDocument();
});

it('does not call onChange if same date selected', async () => {
const onChangeMock = jest.fn();
todor-a marked this conversation as resolved.
Show resolved Hide resolved

render(
<DesktopDatePicker
TransitionComponent={FakeTransitionComponent}
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
onChange={onChangeMock}
renderInput={props => <TextField {...props} />}
/>
);

fireEvent.click(screen.getByLabelText('Choose date, selected date is Jan 1, 2018'));
Copy link
Member

Choose a reason for hiding this comment

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

I side note, I wonder if the asserting here isn't too brittle. What happens if we change the formation or the value? Would it be possible to query an element with role button instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I too think that this approach to querying can be brittle. But also fixing it should probably part of a separate issue/pull request?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, right, a different pull request would make sense

await waitFor(() => screen.getByRole('dialog'));

fireEvent.click(screen.getByLabelText('Jan 1, 2018'));
expect(onChangeMock).not.toHaveBeenCalled();
});
});
1 change: 0 additions & 1 deletion lib/src/_shared/hooks/usePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export function usePickerState<TInput, TDateValue>(
selectionState: PickerSelectionState = 'partial'
) => {
setPickerDate(newDate);

if (selectionState === 'partial') {
acceptDate(newDate, false);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/views/Calendar/Day.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,18 @@ export interface DayProps extends ExtendMui<ButtonBaseProps> {
* @default false
*/
disableHighlightToday?: boolean;
/**
* Allow selecting the same date (fire onChange even if same date is selected).
* @default false
Copy link
Member

@oliviertassinari oliviertassinari Jul 14, 2020

Choose a reason for hiding this comment

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

@eps1lon This change makes me wonder. On the main repository, we don't mention the default value in the TypeScript definitions, we have a tool that extracts them from the source.

Here, the upside is that we get the information right in the editor:

Capture d’écran 2020-07-14 à 20 28 03

But it can get quickly out-of-sync. What do you think about the tradeoff?

Copy link
Member

Choose a reason for hiding this comment

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

Great idea. I think we could statically verify if the default value matches. Though I wouldn't worry about it too much at first. In the end default values will rarely change in SemVer patches or minors because they're very likely breaking changes.

I don't remember how we extract default values but if we extract them statically then we can also match them against TS types.

*/
allowSameDateSelection?: boolean;
onDayFocus: (day: unknown) => void;
onDaySelect: (day: unknown, isFinish: PickerSelectionState) => void;
}

const PureDay: React.FC<DayProps> = ({
allowKeyboardControl,
allowSameDateSelection = false,
className,
day,
disabled,
Expand Down Expand Up @@ -179,6 +185,8 @@ const PureDay: React.FC<DayProps> = ({
};

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
if (!allowSameDateSelection && selected) return;

if (!disabled) {
onDaySelect(day, 'finish');
}
Expand Down