Skip to content

Commit

Permalink
* datetime-picker: support for setting minDate and maxDate with callb…
Browse files Browse the repository at this point in the history
…ack.
  • Loading branch information
catouse committed Jan 29, 2024
1 parent b276f07 commit 9b74e0a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/datetime-picker/src/component/date-picker-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export class DatePickerMenu extends Component<DatePickerMenuProps, DatePickerMen
yearText = i18n.getLang('yearFormat') || '{0}',
weekNames = i18n.getLang('weekNames'),
monthNames = i18n.getLang('monthNames'),
minDate: minDateSetting = '1970-1-1',
maxDate: maxDateSetting = '2099-1-1',
weekStart,
} = props;
const currentDate = date ? new Date(date) : undefined;
Expand All @@ -121,8 +123,8 @@ export class DatePickerMenu extends Component<DatePickerMenuProps, DatePickerMen
select,
} = state;
const isSelectDay = select === 'day';
const minDate = createDate(props.minDate || '1970-1-1');
const maxDate = createDate(props.maxDate || '2099-12-1');
const minDate = createDate(typeof minDateSetting === 'function' ? minDateSetting(currentDate) : minDateSetting);
const maxDate = createDate(typeof maxDateSetting === 'function' ? maxDateSetting(currentDate) : maxDateSetting);
return (
<div className="date-picker-menu row" ref={this.#ref} onClick={this.#handleClick}>
{this.#renderMenu(props)}
Expand Down
4 changes: 2 additions & 2 deletions lib/datetime-picker/src/component/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export class DatePicker<T extends DatePickerOptions = DatePickerOptions> extends
}
let date = createDate(value);
if (minDate) {
const min = createDate(minDate);
const min = createDate(typeof minDate === 'function' ? minDate(date) : minDate);
if (date < min) {
date = min;
}
}
if (maxDate) {
const max = createDate(maxDate);
const max = createDate(typeof maxDate === 'function' ? maxDate(date) : maxDate);
if (date > max) {
date = max;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/datetime-picker/src/types/date-picker-menu-props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {DateLike} from '@zui/helpers/src/date-helper';
import type {NavItemOptions, NavOptions} from '@zui/nav';
import type {ListitemProps} from '@zui/list';
import type {NavOptions} from '@zui/nav';
import type {ToolbarItemOptions, ToolbarOptions} from '@zui/toolbar';

export type DatePickerMenuProps = {
Expand All @@ -11,8 +12,8 @@ export type DatePickerMenuProps = {
todayText?: string;
clearText?: string;
weekStart?: number;
menu?: NavItemOptions[] | NavOptions;
menu?: ListitemProps[] | NavOptions;
actions?: ToolbarItemOptions[] | ToolbarOptions;
minDate?: DateLike;
maxDate?: DateLike;
minDate?: DateLike | ((date?: Date) => DateLike);
maxDate?: DateLike | ((date?: Date) => DateLike);
};
4 changes: 2 additions & 2 deletions lib/datetime-picker/src/types/date-picker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export interface DatePickerOptions extends PickOptions {
todayText?: string;
clearText?: string;
weekStart?: number;
minDate?: DateLike;
maxDate?: DateLike;
minDate?: DateLike | ((date?: Date) => DateLike);
maxDate?: DateLike | ((date?: Date) => DateLike);
menu?: Item[] | NavOptions;
actions?: ToolbarItemOptions[] | ToolbarOptions;
onInvalid?: (value: string) => void;
Expand Down

0 comments on commit 9b74e0a

Please sign in to comment.