|
| 1 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { within, userEvent, expect } from '@storybook/test'; |
| 4 | +import { Progress } from './Progress'; |
| 5 | + |
| 6 | +const meta: Meta<typeof Progress> = { |
| 7 | + title: 'Components/Progress', |
| 8 | + component: Progress, |
| 9 | + tags: ['autodocs'], |
| 10 | + parameters: { |
| 11 | + layout: 'centered', |
| 12 | + docs: { |
| 13 | + description: { |
| 14 | + component: |
| 15 | + '단일 로직을 공유하고 **variant**로 스타일만 분기하는 progress입니다.\n' + |
| 16 | + '- `variant="profile"`: 얇은 트랙 + 단색 인디케이터 (기본)\n' + |
| 17 | + '- `variant="tree"`: 두꺼운 트랙 + 그라데이션 인디케이터\n', |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + argTypes: { |
| 22 | + className: { table: { disable: true } }, |
| 23 | + asChild: { table: { disable: true } }, |
| 24 | + ref: { table: { disable: true } }, |
| 25 | + value: { |
| 26 | + control: { type: 'range', min: 0, max: 100, step: 1 }, |
| 27 | + description: '진행 퍼센트(0–100)', |
| 28 | + }, |
| 29 | + variant: { |
| 30 | + control: { type: 'radio' }, |
| 31 | + options: ['profile', 'tree'], |
| 32 | + description: '스타일 분기', |
| 33 | + }, |
| 34 | + }, |
| 35 | + args: { |
| 36 | + value: 40, |
| 37 | + variant: 'profile', |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +export default meta; |
| 42 | +type Story = StoryObj<typeof Progress>; |
| 43 | + |
| 44 | +const Frame: React.FC<React.ComponentProps<typeof Progress>> = (props) => ( |
| 45 | + <div style={{ width: 320 }}> |
| 46 | + <Progress {...props} /> |
| 47 | + </div> |
| 48 | +); |
| 49 | + |
| 50 | +export const Profile: Story = { |
| 51 | + args: { variant: 'profile', value: 70 }, |
| 52 | + render: (args) => <Frame {...args} />, |
| 53 | +}; |
| 54 | + |
| 55 | +export const Tree: Story = { |
| 56 | + args: { variant: 'tree', value: 70 }, |
| 57 | + render: (args) => <Frame {...args} />, |
| 58 | +}; |
| 59 | + |
| 60 | +export const Zero: Story = { |
| 61 | + args: { value: 0 }, |
| 62 | + render: (args) => <Frame {...args} />, |
| 63 | +}; |
| 64 | + |
| 65 | +export const Full: Story = { |
| 66 | + args: { value: 100 }, |
| 67 | + render: (args) => <Frame {...args} />, |
| 68 | +}; |
| 69 | + |
| 70 | +const AutoProgressDemo: React.FC<{ variant?: 'profile' | 'tree' }> = ({ |
| 71 | + variant = 'profile', |
| 72 | +}) => { |
| 73 | + const [v, setV] = useState(0); |
| 74 | + useEffect(() => { |
| 75 | + const id = setInterval(() => setV((p) => (p >= 100 ? 0 : p + 10)), 250); |
| 76 | + return () => clearInterval(id); |
| 77 | + }, []); |
| 78 | + return <Frame value={v} variant={variant} />; |
| 79 | +}; |
| 80 | + |
| 81 | +export const AutoProgressBoth: StoryObj<typeof Progress> = { |
| 82 | + name: 'Auto progress — profile & tree', |
| 83 | + parameters: { |
| 84 | + controls: { exclude: ['value', 'variant'] }, |
| 85 | + layout: 'centered', |
| 86 | + }, |
| 87 | + render: () => ( |
| 88 | + <div style={{ display: 'flex', gap: 16, width: 760 }}> |
| 89 | + <div> |
| 90 | + <div style={{ fontSize: 14, opacity: 0.7, marginBottom: 8 }}> |
| 91 | + profile |
| 92 | + </div> |
| 93 | + <AutoProgressDemo variant="profile" /> |
| 94 | + </div> |
| 95 | + <div> |
| 96 | + <div style={{ fontSize: 14, opacity: 0.7, marginBottom: 8 }}>tree</div> |
| 97 | + <AutoProgressDemo variant="tree" /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ), |
| 101 | +}; |
| 102 | +const ClickToAdvanceSync: React.FC = () => { |
| 103 | + const [v, setV] = React.useState(0); |
| 104 | + return ( |
| 105 | + <div style={{ width: 320, display: 'grid', gap: 12 }}> |
| 106 | + <Progress value={v} variant="profile" /> |
| 107 | + <Progress value={v} variant="tree" /> |
| 108 | + <div style={{ display: 'flex', gap: 8 }}> |
| 109 | + <button |
| 110 | + type="button" |
| 111 | + onClick={() => setV((p) => Math.min(p + 20, 100))} |
| 112 | + > |
| 113 | + +20% (both) |
| 114 | + </button> |
| 115 | + <button type="button" onClick={() => setV(0)}> |
| 116 | + Reset |
| 117 | + </button> |
| 118 | + <span style={{ marginLeft: 'auto', fontSize: 12, opacity: 0.7 }}> |
| 119 | + {v}% |
| 120 | + </span> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +export const WithInteractionSync: Story = { |
| 127 | + name: 'With interaction — sync both', |
| 128 | + render: () => <ClickToAdvanceSync />, |
| 129 | + play: async ({ canvasElement }) => { |
| 130 | + const canvas = within(canvasElement); |
| 131 | + const btn = await canvas.findByRole('button', { name: /\+20% \(both\)/i }); |
| 132 | + await userEvent.click(btn); |
| 133 | + await userEvent.click(btn); |
| 134 | + |
| 135 | + const roots = canvasElement.querySelectorAll('[data-slot="progress"]'); |
| 136 | + roots.forEach((el) => expect(el.getAttribute('aria-valuenow')).toBe('40')); |
| 137 | + }, |
| 138 | +}; |
0 commit comments