mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-27 02:18:34 +08:00
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
|
import React from 'react';
|
||
|
import type { DatePickerProps } from 'antd';
|
||
|
import { Button, DatePicker, Flex, Slider, Space, Typography } from 'antd';
|
||
|
import dayjs from 'dayjs';
|
||
|
import type { Dayjs } from 'dayjs';
|
||
|
|
||
|
const onChange: DatePickerProps['onChange'] = (date, dateString) => {
|
||
|
console.log(date, dateString);
|
||
|
};
|
||
|
|
||
|
type DateComponent = Required<NonNullable<DatePickerProps<Dayjs>['components']>>['date'];
|
||
|
type GetProps<T> = T extends React.ComponentType<infer P> ? P : never;
|
||
|
|
||
|
const MyDatePanel = (props: GetProps<DateComponent>) => {
|
||
|
const { value, onSelect, onHover } = props;
|
||
|
|
||
|
// Value
|
||
|
const startDate = React.useMemo(() => dayjs().date(1).month(0), []);
|
||
|
const [innerValue, setInnerValue] = React.useState(value || startDate);
|
||
|
|
||
|
React.useEffect(() => {
|
||
|
if (value) {
|
||
|
setInnerValue(value);
|
||
|
}
|
||
|
}, [value]);
|
||
|
|
||
|
// Range
|
||
|
const dateCount = React.useMemo(() => {
|
||
|
const endDate = startDate.add(1, 'year').add(-1, 'day');
|
||
|
return endDate.diff(startDate, 'day');
|
||
|
}, [startDate]);
|
||
|
|
||
|
const sliderValue = Math.min(Math.max(0, innerValue.diff(startDate, 'day')), dateCount);
|
||
|
|
||
|
// Render
|
||
|
return (
|
||
|
<Flex vertical gap="small" style={{ padding: 16 }}>
|
||
|
<Typography.Title level={4} style={{ margin: 0 }} title="no, it's not">
|
||
|
The BEST Picker Panel
|
||
|
</Typography.Title>
|
||
|
<Slider
|
||
|
min={0}
|
||
|
max={dateCount}
|
||
|
value={sliderValue}
|
||
|
onChange={(nextValue) => {
|
||
|
const nextDate = startDate.add(nextValue, 'day');
|
||
|
setInnerValue(nextDate);
|
||
|
onHover?.(nextDate);
|
||
|
}}
|
||
|
tooltip={{
|
||
|
formatter: (nextValue) => startDate.add(nextValue || 0, 'day').format('YYYY-MM-DD'),
|
||
|
}}
|
||
|
/>
|
||
|
<Button
|
||
|
type="primary"
|
||
|
onClick={() => {
|
||
|
onSelect(innerValue);
|
||
|
}}
|
||
|
>{`That's It!`}</Button>
|
||
|
</Flex>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const App: React.FC = () => (
|
||
|
<Space direction="vertical">
|
||
|
<DatePicker
|
||
|
showNow={false}
|
||
|
onChange={onChange}
|
||
|
components={{
|
||
|
date: MyDatePanel,
|
||
|
}}
|
||
|
/>
|
||
|
</Space>
|
||
|
);
|
||
|
|
||
|
export default App;
|