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

[datetime] feat: support highlightCurrentDay prop in more components #4314

Merged
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
6 changes: 6 additions & 0 deletions packages/datetime/src/dateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export interface IDateInputProps extends IDatePickerBaseProps, IDateFormatProps,
*/
fill?: boolean;

/**
* Whether the current day should be highlighted in the calendar.
* @default false
*/
highlightCurrentDay?: boolean;
ejose19 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Props to pass to the [input group](#core/components/text-inputs.input-group).
* `disabled` and `value` will be ignored in favor of the top-level props on this component.
Expand Down
6 changes: 6 additions & 0 deletions packages/datetime/src/dateRangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export interface IDateRangeInputProps extends IDatePickerBaseProps, IDateFormatP
*/
endInputProps?: HTMLInputProps & IInputGroupProps;

/**
* Whether the current day should be highlighted in the calendar.
* @default false
*/
highlightCurrentDay?: boolean;

/**
* Called when the user selects a day.
* If no days are selected, it will pass `[null, null]`.
Expand Down
34 changes: 33 additions & 1 deletion packages/datetime/src/dateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export interface IDateRangePickerProps extends IDatePickerBaseProps, IProps {
*/
defaultValue?: DateRange;

ejose19 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Whether the current day should be highlighted in the calendar.
* @default false
*/
highlightCurrentDay?: boolean;

/**
* Called when the user selects a day.
* If no days are selected, it will pass `[null, null]`.
Expand Down Expand Up @@ -295,6 +301,29 @@ export class DateRangePicker extends AbstractPureComponent2<IDateRangePickerProp
}
}

private isToday = (date: Date) => DateUtils.areSameDay(date, new Date());
ejose19 marked this conversation as resolved.
Show resolved Hide resolved

private shouldHighlightCurrentDay = (date: Date) => {
const { highlightCurrentDay } = this.props;

return highlightCurrentDay && this.isToday(date);
};

private getDateRangePickerModifiers = () => {
const { modifiers } = this.props;

return combineModifiers(this.modifiers, {
isToday: this.shouldHighlightCurrentDay,
...modifiers,
});
};

private renderDay = (day: Date) => {
const date = day.getDate();

return <div className={DateClasses.DATEPICKER_DAY_WRAPPER}>{date}</div>;
};

private disabledDays = (day: Date) => !DateUtils.isDayInRange(day, [this.props.minDate, this.props.maxDate]);

private getDisabledDaysModifier = () => {
Expand Down Expand Up @@ -382,7 +411,7 @@ export class DateRangePicker extends AbstractPureComponent2<IDateRangePickerProp
const dayPickerBaseProps: DayPickerProps = {
locale,
localeUtils,
modifiers: combineModifiers(this.modifiers, this.props.modifiers),
modifiers: this.getDateRangePickerModifiers(),
showOutsideDays: true,
...dayPickerProps,
disabledDays: this.getDisabledDaysModifier(),
Expand All @@ -403,6 +432,7 @@ export class DateRangePicker extends AbstractPureComponent2<IDateRangePickerProp
numberOfMonths={1}
onMonthChange={this.handleLeftMonthChange}
toMonth={maxDate}
renderDay={dayPickerProps?.renderDay ?? this.renderDay}
/>
);
} else {
Expand All @@ -418,6 +448,7 @@ export class DateRangePicker extends AbstractPureComponent2<IDateRangePickerProp
numberOfMonths={1}
onMonthChange={this.handleLeftMonthChange}
toMonth={DateUtils.getDatePreviousMonth(maxDate)}
renderDay={dayPickerProps?.renderDay ?? this.renderDay}
/>,
<DayPicker
key="right"
Expand All @@ -430,6 +461,7 @@ export class DateRangePicker extends AbstractPureComponent2<IDateRangePickerProp
numberOfMonths={1}
onMonthChange={this.handleRightMonthChange}
toMonth={maxDate}
renderDay={dayPickerProps?.renderDay ?? this.renderDay}
/>,
];
}
Expand Down