Files
HMDesign/src/components/date-picker.stories.tsx
T

160 lines
6.2 KiB
TypeScript

import { useState, type ReactNode } from "react"
import type { Meta, StoryObj } from "@storybook/react-vite"
import { DatePicker } from "@hm-design/ui"
import { StateBoard } from "@/components/story-board"
import { StoryPage, StoryPageHeader, StorySection } from "@/components/story-page"
// DatePicker is a Popover + Calendar composition packaged as a component (value handling, trigger text, close on select).
// The representative Story shows real pickers; the same specimens also exist as Stories tagged `verification` (hidden from the
// sidebar). The Label is Story composition (the way a consumer would); `today` is pinned through Calendar's own prop so the
// Story looks the same on any day.
const today = new Date(2026, 9, 2)
const calendarProps = { today, startMonth: new Date(2026, 0), endMonth: new Date(2027, 11) }
// Label 14/20/500 in the default text color; a Disabled field keeps the secondary color at 0.6 (same as the Form pages).
function Field({ label, id, disabled, children }: { label: string; id: string; disabled?: boolean; children: ReactNode }) {
return (
<div className="grid max-w-xs gap-2">
<label htmlFor={id} className={`justify-self-start text-sm font-medium ${disabled ? "text-muted-foreground opacity-60" : "text-foreground"}`}>
{label}
</label>
{children}
</div>
)
}
function Empty() {
return (
<Field label="시작일" id="dp-empty">
<DatePicker id="dp-empty" calendarProps={calendarProps} />
</Field>
)
}
function Selected() {
return (
<Field label="시작일" id="dp-selected">
<DatePicker id="dp-selected" defaultValue={new Date(2026, 9, 6)} calendarProps={calendarProps} />
</Field>
)
}
function DefaultFormat() {
return (
<Field label="기본 형식 · YYYY.MM.DD" id="dp-padded">
<DatePicker id="dp-padded" defaultValue={new Date(2026, 0, 9)} calendarProps={calendarProps} />
</Field>
)
}
function CustomFormat() {
return (
<Field label="Custom format override" id="dp-custom">
<DatePicker id="dp-custom" defaultValue={new Date(2026, 9, 6)} format={(date) => date.toLocaleDateString("ko-KR")} calendarProps={calendarProps} />
</Field>
)
}
// `showValue={false}` hides the value readout on the representative page; the verification Stories keep it.
function Controlled({ showValue = true }: { showValue?: boolean }) {
const [date, setDate] = useState<Date | undefined>()
return (
<div className="grid gap-2">
<Field label="마감일 (controlled)" id="dp-controlled">
<DatePicker id="dp-controlled" controlled value={date} onValueChange={setDate} calendarProps={calendarProps} />
</Field>
{showValue ? <span className="text-sm text-muted-foreground" data-testid="controlled">{`value = ${date ? date.toLocaleDateString("ko-KR") : "undefined"}`}</span> : null}
</div>
)
}
function Medium() {
return (
<Field label="Medium" id="dp-medium">
<DatePicker id="dp-medium" size="medium" defaultValue={new Date(2026, 9, 15)} calendarProps={calendarProps} />
</Field>
)
}
function DisabledDates() {
return (
<Field label="5일 이전과 일요일은 선택 불가" id="dp-disabled-dates">
<DatePicker
id="dp-disabled-dates"
calendarProps={{ ...calendarProps, disabled: [{ before: new Date(2026, 9, 5) }, { dayOfWeek: [0] }] }}
/>
</Field>
)
}
function Disabled() {
return (
<Field label="비활성" id="dp-disabled" disabled>
<DatePicker id="dp-disabled" disabled defaultValue={new Date(2026, 9, 15)} />
</Field>
)
}
// Same 3-column track as the Form pages (1 column on narrow screens). Each cell is a one-specimen StateBoard so subtitles, dividers and
// fields line up; sections with fewer specimens leave the remaining tracks empty.
const Grid = ({ children }: { children: ReactNode }) => (
<div className="grid items-start gap-6 [grid-template-columns:repeat(auto-fill,minmax(min(100%,300px),1fr))] min-[1000px]:grid-cols-3">{children}</div>
)
const Cell = ({ id, label, children }: { id: string; label: string; children: ReactNode }) => (
<div className="min-w-0">
<StateBoard min={10000} states={[{ id, label, children }]} />
</div>
)
const meta = { title: "Components/Search & Date/Date Picker", component: DatePicker, parameters: { layout: "padded" } } satisfies Meta<typeof DatePicker>
export default meta
type Story = StoryObj<typeof meta>
// Representative Story: the common Story page; the sections group the same specimens.
export const All: Story = {
name: "Date Picker",
parameters: { layout: "fullscreen", controls: { disable: true } },
render: () => (
<StoryPage>
<StoryPageHeader title="Date Picker" />
<StorySection title="Sizes">
<Grid>
<Cell id="empty" label="Default"><Empty /></Cell>
<Cell id="medium" label="Medium"><Medium /></Cell>
</Grid>
</StorySection>
<StorySection title="States">
<Grid>
<Cell id="selected" label="Selected"><Selected /></Cell>
<Cell id="controlled" label="Controlled"><Controlled showValue={false} /></Cell>
<Cell id="disabled-dates" label="Disabled dates"><DisabledDates /></Cell>
<Cell id="disabled" label="Disabled"><Disabled /></Cell>
</Grid>
</StorySection>
<StorySection title="Usage">
<Grid>
<Cell id="format-default" label="Default format"><DefaultFormat /></Cell>
<Cell id="format-custom" label="Custom format"><CustomFormat /></Cell>
</Grid>
</StorySection>
</StoryPage>
),
}
export const EmptyPicker: Story = { tags: ["verification"], name: "Empty", render: () => <Empty /> }
export const InvalidFocus: Story = {
tags: ["verification"],
render: () => (
<div className="grid max-w-xs gap-6">
<button type="button">Before field</button>
<Field label="Invalid Date Picker" id="dp-invalid">
<DatePicker id="dp-invalid" aria-invalid defaultValue={new Date(2026, 9, 6)} calendarProps={calendarProps} />
</Field>
<button type="button">After field</button>
</div>
),
}
export const SelectedPicker: Story = { tags: ["verification"], name: "Selected", render: () => <Selected /> }
export const ControlledPicker: Story = { tags: ["verification"], name: "Controlled", render: () => <Controlled /> }