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

Disable today button if today's date cannot be chosen #5247

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/datetime/src/common/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const DATEPICKER_DAY_IS_TODAY = `${DATEPICKER_DAY}--isToday`;
export const DATEPICKER_DAY_WRAPPER = `${DATEPICKER}-day-wrapper`;
export const DATEPICKER_FOOTER = `${DATEPICKER}-footer`;
export const DATEPICKER_MONTH_SELECT = `${DATEPICKER}-month-select`;
export const DATEPICKER_TODAY_BUTTON_DISABLED = `${NS}-disabled`;
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
export const DATEPICKER_YEAR_SELECT = `${DATEPICKER}-year-select`;
export const DATEPICKER_NAVBAR = `${DATEPICKER}-navbar`;
export const DATEPICKER_NAVBUTTON = `DayPicker-NavButton`;
Expand Down
15 changes: 13 additions & 2 deletions packages/datetime/src/datePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,17 @@ export class DatePicker extends AbstractPureComponent2<DatePickerProps, IDatePic
);

private renderOptionsBar() {
const { clearButtonText, todayButtonText } = this.props;
const { clearButtonText, todayButtonText, minDate, maxDate } = 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 minimal={true} onClick={this.handleClearClick} text={clearButtonText} />
</div>,
];
Expand Down Expand Up @@ -458,3 +464,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 @@ -724,6 +761,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