Skip to content

Commit

Permalink
* datetime-picker: add callback to set date allowed to select.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Oct 13, 2024
1 parent ae88a53 commit 4ce5c90
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/datetime-picker/src/component/date-picker-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class DatePickerMenu extends Component<DatePickerMenuProps, DatePickerMen
month={month}
selections={currentDate || []}
onClickDate={this.changeDate}
isDateAllowed={props.isDateAllowed}
/>
)
: null
Expand Down
9 changes: 7 additions & 2 deletions lib/datetime-picker/src/component/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class DatePicker<T extends DatePickerOptions = DatePickerOptions> extends

_parseDate(value: string): Date | null {
const date = getDate(value);
return date ? campDate(date, ...this._getDateRange(value)) : null;
return (date && this._isDateAllowed(date)) ? campDate(date, ...this._getDateRange(value)) : null;
}

_afterSetDate() {
Expand Down Expand Up @@ -147,8 +147,12 @@ export class DatePicker<T extends DatePickerOptions = DatePickerOptions> extends
};
}

protected _isDateAllowed = (date: Date): boolean => {
return this.props.isDateAllowed?.call(this, date) ?? true;
};

_renderPop(props: T, state: PickState): ComponentChildren {
const {weekNames, monthNames, weekStart, yearText, todayText, clearText, menu, actions, required} = props;
const {weekNames, monthNames, weekStart, yearText, todayText, clearText, menu, actions, required, isDateAllowed} = props;
const [minDate, maxDate] = this._getDateRange(state.value);
return (
<DatePickerMenu
Expand All @@ -164,6 +168,7 @@ export class DatePicker<T extends DatePickerOptions = DatePickerOptions> extends
actions={actions}
minDate={minDate}
maxDate={maxDate}
isDateAllowed={isDateAllowed ? this._isDateAllowed : undefined}
/>
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/datetime-picker/src/component/mini-calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class MiniCalendar extends Component<MiniCalendarProps> {
selections = [],
maxDate,
minDate,
isDateAllowed,
} = props;
const weekNamesView: ComponentChild[] = [];
const btnClass = 'btn ghost square rounded-full';
Expand Down Expand Up @@ -79,7 +80,7 @@ export class MiniCalendar extends Component<MiniCalendarProps> {
'is-out-month': !isInMonth,
'is-today': isSameDay(day, now),
'is-weekend': weekDay === 0 || weekDay === 6,
disabled: !isSameDay(day, maxDateTime) && !isSameDay(day, minDateTime) && (time > maxDateTime || time < minDateTime),
disabled: (isDateAllowed && !isDateAllowed(day)) || ((time > maxDateTime || time < minDateTime) && !isSameDay(day, maxDateTime) && !isSameDay(day, minDateTime)),
});
rowDays.push(
<div className={className} key={dateStr} data-date={dateStr}>
Expand Down
1 change: 1 addition & 0 deletions lib/datetime-picker/src/types/date-picker-menu-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export type DatePickerMenuProps = {
actions?: ToolbarSetting;
minDate?: Date | null;
maxDate?: Date | null;
isDateAllowed?: (date: Date) => boolean;
};
3 changes: 2 additions & 1 deletion lib/datetime-picker/src/types/date-picker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface DatePickerOptions extends PickOptions {
readonly?: boolean;
placeholder?: string;
format?: string | ((date: Date) => string);
display?: (value: string, date: Date | null) => string;
display?: (value: string, date: Date | undefined | null) => string;
icon?: IconType | boolean;
weekNames?: string[];
monthNames?: string[];
Expand All @@ -19,6 +19,7 @@ export interface DatePickerOptions extends PickOptions {
weekStart?: number;
minDate?: DateLike | ((value?: string) => DateLike);
maxDate?: DateLike | ((value?: string) => DateLike);
isDateAllowed?: (date: Date) => boolean;
menu?: NavSetting;
actions?: ToolbarSetting;
allowInvalid?: boolean;
Expand Down
1 change: 1 addition & 0 deletions lib/datetime-picker/src/types/mini-calendar-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type MiniCalendarProps = {
monthNames?: string[];
minDate?: DateLike;
maxDate?: DateLike;
isDateAllowed?: (date: Date) => boolean;
year?: number;
month?: number;
selections?: DateLike | DateLike[];
Expand Down

0 comments on commit 4ce5c90

Please sign in to comment.