Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add datepicker example #408

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 123 additions & 2 deletions src/components/data-entry/DatePicker/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,134 @@
import { DatePicker } from 'src/components/data-entry/DatePicker/DatePicker'
import { DatePicker, Tooltip } from 'src/components'
import dayjs, { type Dayjs } from 'dayjs'
import React, { useState } from 'react'
import { type Meta, type StoryObj } from '@storybook/react'
import { type CellRender as RcPickerCellRender, type CellRenderInfo } from 'rc-picker/lib/interface'

const DatePickerWithDisabledYears: React.FC = () => {
const [selectedDate, setSelectedDate] = useState<Dayjs | null>(null)
const disabledDate = (current: Dayjs): boolean => current && current.year() < 2023
const handleChange = (date: Dayjs | null) => {
if (date && disabledDate(date)) {
setSelectedDate(null)
} else {
setSelectedDate(date)
}
}

const cellRender: RcPickerCellRender<Dayjs> = (current, info: CellRenderInfo<Dayjs>) => {
if (!dayjs.isDayjs(current)) {
return info.originNode
}

const isDisabled = disabledDate(current)

const handleInteraction = (e: React.SyntheticEvent) => {
if (isDisabled) {
e.preventDefault()
e.stopPropagation()
}
}
if (info.type === 'year') {
return isDisabled ? (
<Tooltip title="Data is only available for years 2023 and onwards.">
<div
className="ant-picker-cell-inner"
style={{ color: 'rgba(0, 0, 0, 0.25)', cursor: 'not-allowed' }}
onClick={handleInteraction}
onMouseDown={handleInteraction}
onKeyDown={handleInteraction}>
{current.year()}
</div>
</Tooltip>
) : (
info.originNode
)
}

return info.originNode
}

return <DatePicker picker="year" cellRender={cellRender} value={selectedDate} onChange={handleChange} />
}

const meta: Meta<typeof DatePicker> = {
title: 'Aquarium/Data Entry/Date Picker',
component: DatePicker,

args: {},
}
export default meta

type Story = StoryObj<typeof DatePicker>

export const Primary: Story = {}

export const WithDatePickerWithDisabledYears: Story = {
render: () => <DatePickerWithDisabledYears />,
}

WithDatePickerWithDisabledYears.parameters = {
docs: {
source: {
code: `
import React from 'react';
import { DatePicker } from 'antd';
import dayjs, { Dayjs } from 'dayjs';
import { Tooltip } from 'src/components';

const DatePickerWithDisabledYears: React.FC = () => {
const [selectedDate, setSelectedDate] = React.useState<Dayjs | null>(null);

const disabledDate = (current: Dayjs): boolean => current && current.year() < 2023;

const handleChange = (date: Dayjs | null) => {
if (date && disabledDate(date)) {
setSelectedDate(null);
} else {
setSelectedDate(date);
}
};

const cellRender = (current: Dayjs, info: any) => {
const isDisabled = disabledDate(current);

const handleInteraction = (e: React.SyntheticEvent) => {
if (isDisabled) {
e.preventDefault();
e.stopPropagation();
}
};

if (info.type === 'year') {
return isDisabled ? (
<Tooltip title="Data is only available for years 2023 and onwards.">
<div
className="ant-picker-cell-inner"
style={{ color: 'rgba(0, 0, 0, 0.25)', cursor: 'not-allowed' }}
onClick={handleInteraction}
onMouseDown={handleInteraction}
onKeyDown={handleInteraction}
>
{current.year()}
</div>
</Tooltip>
) : (
info.originNode
);
}

return info.originNode;
};

return (
<DatePicker
picker="year"
cellRender={cellRender}
value={selectedDate}
onChange={handleChange}
/>
);
};
`,
},
},
}
Loading