Skip to content

fix(datepicker): reset state properly when clearOnSameDateClick is false #706

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

Merged
merged 1 commit into from
Jan 7, 2022
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
17 changes: 1 addition & 16 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,26 +296,11 @@ class SemanticDatepicker extends React.Component<
format,
keepOpenOnSelect,
onChange,
clearOnSameDateClick,
formatOptions,
} = this.props;

if (!newDate) {
// if clearOnSameDateClick is true (this is the default case)
// then reset the state. This is what was previously the default
// behavior, without a specific prop.
if (clearOnSameDateClick) {
this.resetState(event);
} else {
// Don't reset the state. Instead, close or keep open the
// datepicker according to the value of keepOpenOnSelect.
// Essentially, follow the default behavior of clicking a date
// but without changing the value in state.
this.setState({
isVisible: keepOpenOnSelect,
});
}

this.resetState(event);
return;
}

Expand Down
8 changes: 6 additions & 2 deletions src/pickers/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ class DatePicker extends React.Component<BasicDatePickerProps> {
{ selectable, date },
event: React.SyntheticEvent
) => {
const { selected: selectedDate, onChange } = this.props;
const { clearOnSameDateClick, selected: selectedDate, onChange } = this.props;

if (!selectable) {
return;
}

let newDate = date;
if (selectedDate && selectedDate.getTime() === date.getTime()) {
if (
selectedDate &&
selectedDate.getTime() === date.getTime() &&
clearOnSameDateClick
) {
newDate = null;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export type BaseDatePickerProps = DayzedProps & {
};

export interface BasicDatePickerProps extends BaseDatePickerProps {
clearOnSameDateClick?: boolean
onChange: (event: React.SyntheticEvent, date: Date | null) => void;
selected: Date;
}
Expand Down