-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DateRangePicker): add new DateRangePicker
- Loading branch information
1 parent
758f44a
commit 4f0b523
Showing
15 changed files
with
236 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
import { usePrefixCls, useControlledState } from '@gio-design/utils'; | ||
import format from 'date-fns/format'; | ||
import { CalendarOutlined } from '@gio-design/icons'; | ||
import Popover from '../popover'; | ||
import { InputButton } from '../input'; | ||
import StaticDateRangePicker from '../static-date-range-picker'; | ||
import { DateRangePickerProps, NullableDate, NullableString } from './interfaces'; | ||
|
||
export const DateRangePicker: React.FC<DateRangePickerProps> = (props: DateRangePickerProps) => { | ||
const { | ||
onVisibleChange: onPopoverVisibleChange, | ||
overlayClassName, | ||
visible: popoverVisible, | ||
trigger, | ||
disabled, | ||
value, | ||
defaultValue, | ||
onSelect, | ||
disabledDate, | ||
placeholder, | ||
allowClear, | ||
format: formatString, | ||
prefix, | ||
suffix, | ||
hidePrefix, | ||
size, | ||
...restProps | ||
} = props; | ||
|
||
const prefixCls = usePrefixCls('date-range-picker-new'); | ||
const overlayCls = classnames(`${prefixCls}-overlay`, overlayClassName); | ||
|
||
const formatDates = (dates: [NullableDate, NullableDate]): NullableString => { | ||
const strongFormat = (date: NullableDate) => (date ? format(date, formatString ?? 'yyyy/MM/dd') : undefined); | ||
return `${strongFormat(dates[0]) || ''} - ${strongFormat(dates[1]) || ''}`; | ||
}; | ||
|
||
const [visible, setVisible] = useControlledState(popoverVisible, false); | ||
|
||
const [controlledValue, setControlledValue] = useControlledState<[NullableDate, NullableDate] | undefined>( | ||
value, | ||
defaultValue | ||
); | ||
|
||
const handleVisibleChange = (current: boolean) => { | ||
setVisible(current); | ||
onPopoverVisibleChange?.(current); | ||
}; | ||
|
||
const handleOnSelect = (currentValue: [Date, Date], index: number) => { | ||
setControlledValue(currentValue); | ||
if (index) { | ||
setVisible(false); | ||
onSelect?.(currentValue, formatDates(currentValue)); | ||
} | ||
}; | ||
|
||
const content = ( | ||
<StaticDateRangePicker | ||
onSelect={handleOnSelect} | ||
value={controlledValue as [Date, Date]} | ||
disabledDate={disabledDate} | ||
/> | ||
); | ||
|
||
function renderTrigger() { | ||
if (trigger) { | ||
return <div>{trigger}</div>; | ||
} | ||
return ( | ||
<InputButton | ||
prefix={prefix || <CalendarOutlined />} | ||
placeholder={placeholder} | ||
disabled={disabled} | ||
allowClear={allowClear} | ||
value={controlledValue && formatDates(controlledValue)} | ||
size={size} | ||
suffix={suffix} | ||
hidePrefix={hidePrefix} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Popover | ||
content={content} | ||
trigger={['click', 'focus']} | ||
visible={visible} | ||
placement="bottomLeft" | ||
overlayClassName={overlayCls} | ||
onVisibleChange={handleVisibleChange} | ||
{...restProps} | ||
> | ||
{renderTrigger()} | ||
</Popover> | ||
); | ||
}; | ||
|
||
export default DateRangePicker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { Meta, Story } from '@storybook/react/types-6-0'; | ||
import { action } from '@storybook/addon-actions'; | ||
|
||
import DateRangePicker from '../index'; | ||
|
||
import { DateRangePickerProps } from '../interfaces'; | ||
|
||
import '../style'; | ||
|
||
export default { | ||
title: 'Upgraded/DateRangePicker', | ||
component: DateRangePicker, | ||
parameters: { | ||
docs: {}, | ||
}, | ||
} as Meta; | ||
|
||
const defaultPlaceholder = '选择日期范围'; | ||
|
||
const Template: Story<DateRangePickerProps> = (args) => ( | ||
<div style={{ width: 280 }}> | ||
<DateRangePicker {...args} /> | ||
</div> | ||
); | ||
|
||
export const Basic = Template.bind({}); | ||
Basic.args = { | ||
placeholder: defaultPlaceholder, | ||
onSelect: action('selected:'), | ||
onClear: action('onClear:'), | ||
}; | ||
|
||
export const DisbaledDate = Template.bind({}); | ||
DisbaledDate.args = { | ||
placeholder: defaultPlaceholder, | ||
onSelect: action('selected:'), | ||
disabledDate: (current: Date) => current.getTime() > new Date().getTime(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import DateRangePicker from './Picker'; | ||
|
||
export type { DateRangePickerProps } from './interfaces'; | ||
|
||
export default DateRangePicker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { CommonProps } from '@gio-design/utils'; | ||
import { PopoverProps } from '../popover'; | ||
import { StaticDateRangePickerProps } from '../static-date-range-picker'; | ||
import { InputButtonProps } from '../input'; | ||
|
||
export type NullableDate = Date | undefined; | ||
export type NullableString = string | undefined; | ||
|
||
export interface DateRangePickerProps | ||
extends CommonProps, | ||
Omit<InputButtonProps, 'value' | 'onSelect' | 'defaultValue'>, | ||
Omit<StaticDateRangePickerProps, 'onSelect' | 'value' | 'defaultValue'>, | ||
Omit<PopoverProps, 'trigger' | 'placement' | 'prefixCls' | 'children' | 'content'> { | ||
/** | ||
* 自定义的触发器 | ||
*/ | ||
trigger?: React.ReactNode; | ||
/** | ||
* 日期展示格式 | ||
*/ | ||
format?: string; | ||
/** | ||
* 选择日期时的回调 | ||
* | ||
* @param dates - 选择的日期 `[Date, Date]` | ||
* @param dateStrings - 格式化后的日期 `[string, string]` | ||
*/ | ||
onSelect?: (dates: [NullableDate, NullableDate], dateStrings: NullableString) => void; | ||
/** | ||
* 选择的日期 | ||
*/ | ||
value?: [NullableDate, NullableDate]; | ||
defaultValue?: [NullableDate, NullableDate]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@import '../../stylesheet/index.less'; | ||
@import '../../stylesheet/mixin/index.less'; | ||
@import '../../stylesheet/variables/index.less'; | ||
|
||
@picker-prefix-cls: ~'@{component-prefix}-date-range-picker-new'; | ||
|
||
.@{picker-prefix-cls}-overlay { | ||
height: fit-content; | ||
background: @gray-0; | ||
border-radius: 4px; | ||
.elevation(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './index.less'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/static-date-range-picker/__test__/DateRangePicker.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.