Skip to content

Commit

Permalink
fix(datepicker): reset state properly when clearOnSameDateClick is fa…
Browse files Browse the repository at this point in the history
…lse (#706)
  • Loading branch information
arthurdenner committed Jan 13, 2022
1 parent 5603059 commit 263fb37
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
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

1 comment on commit 263fb37

@vercel
Copy link

@vercel vercel bot commented on 263fb37 Jan 13, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.