Files
HMDesign/src/components/date-picker.stories.tsx
T
shkim4 a7b56ff23e style(storybook): unify form element story layouts
- Button: Sizes | Variants side by side, States as Brand / Standard Solid /
  Standard Outline matrices (3 / 2 / 1 columns), 8px button gap; the
  Interact row stays only in the verification "Brand States" Story
- Input / Textarea / Select: state name in the field Label slot, repeated
  field Label and group titles removed, aria-label keeps field meaning
  with the visible state name; Select uses the Input field grid
- Combobox / Date Picker: Sizes / States use the same state-name Label
  and Input grid; Usage unchanged
- Toggle: top Variants / States in 2 columns, 8px toggle gap, 단독 toggle
  keeps its own width
- Component code, props, tokens and verification Stories unchanged;
  component docs updated to match
2026-11-12 17:11:56 +09:00

200 lines
8.8 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).
// `fill` lets the field take its grid column (representative page); the verification Stories keep the 320px field.
function Field({ label, id, disabled, fill, children }: { label: string; id: string; disabled?: boolean; fill?: boolean; children: ReactNode }) {
return (
<div className={fill ? "grid gap-2" : "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>
)
}
// Representative page (2026-11-12, same rule as Input / Select): with `state`, the state name takes the field Label slot (no
// repeated field Label under a separate group title) and the trigger keeps the field meaning in its accessible name, which also
// contains the visible state name (Label in Name), e.g. "시작일 (Selected)". Without `state` the verification Stories are unchanged.
const stateName = (state: string | undefined, field: string) => (state ? `${field} (${state})` : undefined)
function Empty({ state }: { state?: string }) {
return (
<Field label={state ?? "시작일"} id="dp-empty" fill={state !== undefined}>
<DatePicker id="dp-empty" aria-label={stateName(state, "시작일")} calendarProps={calendarProps} />
</Field>
)
}
function Selected({ state }: { state?: string }) {
return (
<Field label={state ?? "시작일"} id="dp-selected" fill={state !== undefined}>
<DatePicker id="dp-selected" aria-label={stateName(state, "시작일")} 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, state }: { showValue?: boolean; state?: string }) {
const [date, setDate] = useState<Date | undefined>()
return (
<div className="grid gap-2">
<Field label={state ?? "마감일 (controlled)"} id="dp-controlled" fill={state !== undefined}>
<DatePicker id="dp-controlled" aria-label={stateName(state, "마감일")} 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({ state }: { state?: string }) {
return (
<Field label={state ?? "Medium"} id="dp-medium" fill={state !== undefined}>
<DatePicker id="dp-medium" aria-label={stateName(state, "날짜")} size="medium" defaultValue={new Date(2026, 9, 15)} calendarProps={calendarProps} />
</Field>
)
}
// With `state`, the rule that was the Label stays as helper text (12/18/400 secondary, 8px below the trigger, as Input's Helper)
// and is linked to the trigger with aria-describedby.
const disabledDatesNote = "5일 이전과 일요일은 선택 불가"
function DisabledDates({ state }: { state?: string }) {
return (
<Field label={state ?? disabledDatesNote} id="dp-disabled-dates" fill={state !== undefined}>
<DatePicker
id="dp-disabled-dates"
aria-label={stateName(state, "날짜")}
aria-describedby={state ? "dp-disabled-dates-message" : undefined}
calendarProps={{ ...calendarProps, disabled: [{ before: new Date(2026, 9, 5) }, { dayOfWeek: [0] }] }}
/>
{state ? (
<p id="dp-disabled-dates-message" className="m-0 text-xs leading-[18px] text-muted-foreground">
{disabledDatesNote}
</p>
) : null}
</Field>
)
}
function Disabled({ state }: { state?: string }) {
return (
<Field label={state ?? "비활성"} id="dp-disabled" disabled fill={state !== undefined}>
<DatePicker id="dp-disabled" aria-label={stateName(state, "날짜")} 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>
)
// Sizes / States (2026-11-12): the Input / Select field grid with the common section indent — up to 3 tracks of at least 240px
// within 1120px, narrower widths drop to 2 / 1 columns. Each cell is one field whose Label is the state name.
const fieldColumns =
"grid max-w-[1120px] items-start gap-6 [grid-template-columns:repeat(auto-fill,minmax(max(240px,calc((100%_-_48px)/3)),1fr))]"
function FieldRow({ fields }: { fields: { id: string; render: ReactNode }[] }) {
return (
<div className="sb-page-indent">
<div className={fieldColumns}>
{fields.map(({ id, render }) => (
<div key={id} className="min-w-0" data-overview-state={id}>
{render}
</div>
))}
</div>
</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">
<FieldRow
fields={[
{ id: "empty", render: <Empty state="Default" /> },
{ id: "medium", render: <Medium state="Medium" /> },
]}
/>
</StorySection>
<StorySection title="States">
<FieldRow
fields={[
{ id: "selected", render: <Selected state="Selected" /> },
{ id: "controlled", render: <Controlled showValue={false} state="Controlled" /> },
{ id: "disabled-dates", render: <DisabledDates state="Disabled dates" /> },
{ id: "disabled", render: <Disabled state="Disabled" /> },
]}
/>
</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 /> }