Skip to content

Commit

Permalink
[datetime] fix(DatePicker): disable today btn when out of bounds (#5247)
Browse files Browse the repository at this point in the history
  • Loading branch information
scooobs committed Apr 15, 2022
1 parent cd37cfd commit 749da9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/datetime/src/datePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,17 @@ export class DatePicker extends AbstractPureComponent2<DatePickerProps, IDatePic
);

private renderOptionsBar() {
const { clearButtonText, todayButtonText, canClearSelection } = this.props;
const { clearButtonText, todayButtonText, minDate, maxDate, canClearSelection } = this.props;
const todayEnabled = isTodayEnabled(minDate, maxDate);
return [
<Divider key="div" />,
<div className={Classes.DATEPICKER_FOOTER} key="footer">
<Button minimal={true} onClick={this.handleTodayClick} text={todayButtonText} />
<Button
minimal={true}
disabled={!todayEnabled}
onClick={this.handleTodayClick}
text={todayButtonText}
/>
<Button
disabled={!canClearSelection}
minimal={true}
Expand Down Expand Up @@ -464,3 +470,8 @@ function getInitialMonth(props: DatePickerProps, value: Date | null): Date {
return DateUtils.getDateBetween([props.minDate, props.maxDate]);
}
}

function isTodayEnabled(minDate: Date, maxDate: Date): boolean {
const today = new Date();
return DateUtils.isDayInRange(today, [minDate, maxDate]);
}
38 changes: 38 additions & 0 deletions packages/datetime/test/datePickerTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,43 @@ describe("<DatePicker>", () => {
});
});

describe("today button validation", () => {
const today = new Date();
const MIN_DATE_BEFORE_TODAY = MIN_DATE;
const MAX_DATE_BEFORE_TODAY = MAX_DATE;

const MIN_DATE_AFTER_TODAY = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
const MAX_DATE_AFTER_TODAY = new Date(today.getFullYear() + 2, today.getMonth(), today.getDate());

it("min/max before today has disabled button", () => {
const { getTodayButton } = wrap(
<DatePicker
minDate={MIN_DATE_BEFORE_TODAY}
maxDate={MAX_DATE_BEFORE_TODAY}
showActionsBar={true}
/>,
);

assert.isTrue(getTodayButton().props().disabled);
});

it("min/max after today has disabled button", () => {
const { getTodayButton } = wrap(
<DatePicker minDate={MIN_DATE_AFTER_TODAY} maxDate={MAX_DATE_AFTER_TODAY} showActionsBar={true} />,
);

assert.isTrue(getTodayButton().props().disabled);
});

it("valid min/max today has enabled button", () => {
const { getTodayButton } = wrap(
<DatePicker minDate={MIN_DATE_BEFORE_TODAY} maxDate={MAX_DATE_AFTER_TODAY} showActionsBar={true} />,
);

assert.isFalse(getTodayButton().props().disabled);
});
});

it("only days outside bounds have disabled class", () => {
const minDate = new Date(2000, Months.JANUARY, 10);
const { getDay } = wrap(<DatePicker initialMonth={minDate} minDate={minDate} />);
Expand Down Expand Up @@ -735,6 +772,7 @@ describe("<DatePicker>", () => {
wrapper
.find(`.${Classes.DATEPICKER_DAY}`)
.filterWhere(day => day.text() === "" + dayNumber && !day.hasClass(Classes.DATEPICKER_DAY_OUTSIDE)),
getTodayButton: () => wrapper.find(`.${Classes.DATEPICKER_FOOTER}`).find(Button).first(),
months: wrapper.find(HTMLSelect).filter({ className: Classes.DATEPICKER_MONTH_SELECT }).find("select"),
root: wrapper,
setTimeInput: (precision: TimePrecision | "hour", value: number) =>
Expand Down

1 comment on commit 749da9b

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

[datetime] fix(DatePicker): disable today btn when out of bounds (#5247)

Previews: documentation | landing | table | demo

Please sign in to comment.