import type { Meta, StoryObj } from "@storybook/react"; import { useState } from "react"; import DragFill from "./drag-fill"; import type { DragFillProps } from "./drag-fill.types"; import { cn } from "@/lib/utils"; const meta: Meta = { title: "Components/Quiz/DragFill", component: DragFill, tags: ["autodocs"], argTypes: { options: { control: { type: "object" }, description: "可拖拽的选项列表", }, blanks: { control: { type: "object" }, description: "空白区域列表", }, value: { control: { type: "object" }, description: "当前填充结果(受控模式)", }, defaultValue: { control: { type: "object" }, description: "默认填充结果", }, disabled: { control: { type: "boolean" }, description: "是否禁用整个组件", }, allowDuplicate: { control: { type: "boolean" }, description: "是否允许重复选择同一选项", }, showSubmitButton: { control: { type: "boolean" }, description: "是否显示提交按钮", }, submitText: { control: { type: "text" }, description: "提交按钮文本", }, }, }; export default meta; type Story = StoryObj; // 基础示例 export const Default: Story = { args: { options: ["选项A", "选项B", "选项C", "选项D"], blanks: ["空白1", "空白2", "空白3"], onSubmit: (results) => { console.log("填充结果:", results); }, }, }; // 使用选项和空白对象 export const WithObjects: Story = { args: { options: [ { value: "opt1", label: "选项一" }, { value: "opt2", label: "选项二" }, { value: "opt3", label: "选项三(禁用)", disabled: true }, { value: "opt4", label: "选项四" }, ], blanks: [ { id: "blank1", label: "第一个空白", required: true }, { id: "blank2", label: "第二个空白" }, { id: "blank3", label: "第三个空白(必填)", required: true }, ], onSubmit: (results) => { console.log("填充结果:", results); }, }, }; // 受控模式 export const Controlled: Story = { render: () => { const [value, setValue] = useState([]); return (
{ alert(`提交的结果: ${JSON.stringify(results)}`); }} />
当前填充: {JSON.stringify(value)}
); }, }; // 允许重复选择 export const AllowDuplicate: Story = { args: { options: ["选项A", "选项B", "选项C"], blanks: ["空白1", "空白2", "空白3"], allowDuplicate: true, onSubmit: (results) => { console.log("填充结果:", results); }, }, }; // 禁用状态 export const Disabled: Story = { args: { options: ["选项A", "选项B", "选项C"], blanks: ["空白1", "空白2"], disabled: true, defaultValue: [{ blankId: "blank1", optionValue: "选项A" }], }, }; // 不显示提交按钮 export const WithoutSubmitButton: Story = { args: { options: ["选项A", "选项B", "选项C"], blanks: ["空白1", "空白2"], showSubmitButton: false, onChange: (results) => { console.log("填充变化:", results); }, }, }; // 自定义样式 export const CustomStyled: Story = { args: { options: ["选项A", "选项B", "选项C", "选项D"], blanks: ["空白1", "空白2"], className: "max-w-2xl mx-auto p-6 bg-gray-50 rounded-xl", optionClassName: "text-lg font-semibold", blankClassName: "min-h-[100px]", submitText: "确认答案", onSubmit: (results) => { console.log("填充结果:", results); }, }, }; // 自定义渲染 export const CustomRender: Story = { render: () => { return ( (
📦 {option.label}
)} renderBlank={(blank, filledValue) => (
{filledValue ? (
✓ {filledValue}
) : (
⬇️
{blank.label || "拖拽到这里"}
)}
)} onSubmit={(results) => { console.log("填充结果:", results); }} /> ); }, };