Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
fix: add disableDateTime api
Browse files Browse the repository at this point in the history
Signed-off-by: dangyuzheng <dang.yuzheng@xsky.com>
  • Loading branch information
dangyuzheng committed Aug 10, 2023
1 parent b3d16f4 commit e3cc26f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/RangePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,17 @@ export default class RangePicker extends React.PureComponent<RangePickerProps, R
});
};
handleOpenChange = (open: boolean) => {
const { onOpenChange } = this.props;
const { onOpenChange, disabledDateTime } = this.props;
const { value } = this.state;

this.setState({ open });
if (onOpenChange) {
onOpenChange(open);
}
if (!open && disabledDateTime) {
const newValue = disabledDateTime(value);
this.setState({ value: newValue });
}
};
clearSelection = (e: React.MouseEvent) => {
e.preventDefault();
Expand Down
4 changes: 3 additions & 1 deletion src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ export interface RangePickerProps {
/**默认时间区间 */
defaultValue?: Moment[];
/** 是否禁用当前日期的选择 */
disabledDate?: (current:Moment) => Boolean;
disabledDate?: (current: Moment) => Boolean;
/** 是否禁用当前时间的选择 */
disabledDateTime?: (value: Moment[] | string[]) => Moment[] | string[];
}

export interface RangePickerState {
Expand Down
32 changes: 32 additions & 0 deletions stories/RangePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ function onOk(date: Moment[]) {
console.log(date);
}

function disabledDate(current: Moment) {
if (!current) return false;
const date = moment();
return current.isAfter(date); // can not select days after today
}

function disabledDateTime(value: Moment[] | string[]): Moment[] | string[] {
const now = moment();
if (moment(value[0]).isAfter(now) || moment(value[1]).isAfter(now)) {
const newValue = [
moment(value[0]).isAfter(now) ? now : moment(value[0]),
moment(value[1]).isAfter(now) ? now : moment(value[1])
];
return newValue
}

return value
}

const meta: Meta<typeof RangePicker> = {
title: 'DATA ENTRY/RangePicker',
component: RangePicker,
Expand All @@ -31,6 +50,19 @@ export const DefaultTimes: Story = {
},
};

export const disabledAfterToday: Story = {
args: {
disabledDate,
},
};

export const disabledAfterTodayTime: Story = {
args: {
disabledDate,
disabledDateTime,
},
};

export const HideDuration: Story = {
args: {
showDuration: false,
Expand Down
4 changes: 4 additions & 0 deletions stories/docs/RangePicker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import * as RangePickerStories from '../RangePicker.stories.tsx';
<Canvas of={RangePickerStories.Basic}/>
### 设置默认时间区间
<Canvas of={RangePickerStories.DefaultTimes}/>
### 禁用当天后的日期
<Canvas of={RangePickerStories.disabledAfterToday}/>
### 禁用当天后的日期及时间
<Canvas of={RangePickerStories.disabledAfterTodayTime}/>
### 设置 `showDuration``false` 隐藏直接选择区间
<Canvas of={RangePickerStories.HideDuration}/>

Expand Down

0 comments on commit e3cc26f

Please sign in to comment.