Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(design): add
DateRangePicker
(#1510)
* feat(design): add `DateRangePicker` * fix: lint
- Loading branch information
Showing
14 changed files
with
895 additions
and
78 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
46 changes: 46 additions & 0 deletions
46
packages/design/src/components/date-range-picker/DateRangePicker.stories.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { Meta } from '@storybook/react'; | ||
import React, { useState } from 'react'; | ||
|
||
import type { Dayjs } from 'dayjs'; | ||
import dayjs from 'dayjs'; | ||
import type { NoUndefinedRangeValueType } from 'rc-picker/lib/PickerInput/RangePicker'; | ||
import { DateRangePicker } from './DateRangePicker'; | ||
|
||
const meta: Meta<typeof DateRangePicker> = { | ||
title: 'Components / DateRangePicker', | ||
component: DateRangePicker, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
|
||
export const DateRangePickerBasic = { | ||
render() { | ||
const [value, setValue] = useState<NoUndefinedRangeValueType<Dayjs>>([dayjs(), dayjs().add(7, 'day')]); | ||
|
||
return ( | ||
<> | ||
<DateRangePicker value={value} onChange={setValue} /> | ||
</> | ||
); | ||
}, | ||
}; |
60 changes: 60 additions & 0 deletions
60
packages/design/src/components/date-range-picker/DateRangePicker.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React, { useContext } from 'react'; | ||
import { RangePicker } from 'rc-picker'; | ||
import generateConfig from 'rc-picker/lib/generate/dayjs'; | ||
import { CalendarSingle, GuideSingle } from '@univerjs/icons'; | ||
import type { Dayjs } from 'dayjs'; | ||
import type { NoUndefinedRangeValueType } from 'rc-picker/lib/PickerInput/RangePicker'; | ||
import { ConfigContext } from '../config-provider/ConfigProvider'; | ||
import styles from './index.module.less'; | ||
|
||
export interface IDateRangePickerProps { | ||
/** | ||
* The value of the date picker. | ||
*/ | ||
value: NoUndefinedRangeValueType<Dayjs>; | ||
|
||
/** | ||
* Callback when the value of the date picker changes. | ||
*/ | ||
onChange: (date: NoUndefinedRangeValueType<Dayjs>, dateString: [string, string]) => void; | ||
} | ||
|
||
export function DateRangePicker(props: IDateRangePickerProps) { | ||
const { value, onChange } = props; | ||
|
||
const { locale } = useContext(ConfigContext); | ||
|
||
function handleChange(date: NoUndefinedRangeValueType<Dayjs>, dateString: [string, string]) { | ||
if (Array.isArray(date) && Array.isArray(dateString)) { | ||
onChange(date, dateString); | ||
} | ||
} | ||
|
||
return ( | ||
<RangePicker<Dayjs> | ||
value={value} | ||
prefixCls={styles.dateRangePicker} | ||
generateConfig={generateConfig} | ||
locale={locale.design.Picker} | ||
separator={<GuideSingle />} | ||
suffixIcon={<CalendarSingle className={styles.dateRangePickerSuffixIcon} />} | ||
onChange={handleChange} | ||
/> | ||
); | ||
} |
Oops, something went wrong.