ComponentsDemoPage.tsx 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
import { useState } from 'react';
import { SearchIcon } from 'tdesign-icons-react';
import {
  Alert,
  Avatar,
  AvatarGroup,
  Badge,
  Button,
  Card,
  Checkbox,
  Collapse,
  ColorPicker,
  DatePicker,
  DateRangePicker,
  Dialog,
  Divider,
  Drawer,
  Form,
  Input,
  Menu,
  Popconfirm,
  Progress,
  Radio,
  Select,
  Skeleton,
  Steps,
  Switch,
  Tabs,
  Tag,
  Textarea,
  Timeline,
  Tooltip,
  TreeSelect,
  Upload,
} from 'tdesign-react';

import {
  BusinessForm,
  BusinessTable,
  MediaWall,
  SearchFilter,
  StatePlaceholder,
  type BusinessFormField,
  type BusinessFormValues,
  type BusinessTableColumn,
} from '@gl/business-components';
import styles from './ComponentsDemoPage.module.css';

const { FormItem } = Form;
const { MenuItem, SubMenu } = Menu;
const { Panel: CollapsePanel } = Collapse;
const { TabPanel } = Tabs;

interface DemoTableRow {
  id: string;
  name: string;
  status: string;
}

export function ComponentsDemoPage() {
  const [form] = Form.useForm();
  const tableRows: DemoTableRow[] = [{ id: '1', name: '项目A', status: '启用' }];
  const tableColumns: BusinessTableColumn<DemoTableRow>[] = [
    { title: '名称', colKey: 'name' },
    { title: '状态', colKey: 'status', cell: ({ row }) => <Tag theme="success">{row.status}</Tag> },
  ];
  const mediaImage =
    'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2296%22 height=%2296%22 viewBox=%220 0 96 96%22%3E%3Crect width=%2296%22 height=%2296%22 fill=%22%23dbeafe%22/%3E%3Ccircle cx=%2232%22 cy=%2235%22 r=%2210%22 fill=%22%232468f2%22/%3E%3Cpath d=%22M12 78 39 52l14 13 12-17 19 30Z%22 fill=%22%231f5bd6%22/%3E%3C/svg%3E';
  const mediaVideo =
    'data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2296%22 height=%2296%22 viewBox=%220 0 96 96%22%3E%3Crect width=%2296%22 height=%2296%22 fill=%22%231f2633%22/%3E%3Cpath d=%22M18 72 38 45l13 12 11-18 16 33Z%22 fill=%22%23667085%22/%3E%3Ccircle cx=%2270%22 cy=%2226%22 r=%2210%22 fill=%22%23f59e0b%22/%3E%3C/svg%3E';
  const [checkboxChecked, setCheckboxChecked] = useState(true);
  const [switchChecked, setSwitchChecked] = useState(true);
  const [treeValue, setTreeValue] = useState<string>();
  const [multiSelectValue, setMultiSelectValue] = useState<string[]>(['zhang', 'li']);
  const [dateValue, setDateValue] = useState('2026-05-16');
  const [dateRangeValue, setDateRangeValue] = useState<string[]>([]);
  const [showMessage, setShowMessage] = useState(false);
  const [dialogOpen, setDialogOpen] = useState(true);
  const [searchFilterSnapshot, setSearchFilterSnapshot] = useState('点击搜索查看配置结果');
  const [businessFormValues, setBusinessFormValues] = useState<BusinessFormValues>({
    title: '合同审批',
    mode: 'hide',
    detail: '审批说明',
    locked: '锁定内容',
    readonlyNote: '只读内容',
    enabled: 'on',
    scenarios: ['approval', 'archive'],
    coverMedia: { name: '封面图.png', url: mediaImage, kind: 'image' },
    attachment: { name: '项目资料.pdf', url: '/files/project.pdf', kind: 'file' },
    summary: '标题:合同审批',
  });
  const businessFormFields: BusinessFormField[] = [
    {
      key: 'title',
      label: '标题',
      type: 'input',
      required: true,
      maxLength: 20,
      onChange: (nextValue) => ({ summary: `标题:${String(nextValue ?? '')}` }),
    },
    {
      key: 'mode',
      label: '展示模式',
      type: 'input',
      helpText: '切换可见、禁用和只读状态',
    },
    {
      key: 'detail',
      label: '可见说明',
      type: 'input',
      visible: true
    },
    {
      key: 'locked',
      label: '锁定项',
      type: 'input',
      disabled: true,
    },
    {
      key: 'readonlyNote',
      label: '只读项',
      type: 'input',
      readonly: true
    },
    {
      key: 'category',
      label: '分类',
      type: 'select',
      placeholder: '请选择分类',
      options: [
        { label: '基础', value: 'basic' },
        { label: '高级', value: 'advanced' },
      ],
    },
    {
      key: 'scenarios',
      label: '适用场景',
      type: 'select',
      multiple: true,
      placeholder: '请选择适用场景',
      helpText: '多选 Select 示例,字段值以数组写回',
      options: [
        { label: '审批', value: 'approval' },
        { label: '归档', value: 'archive' },
        { label: '通知', value: 'notice' },
      ],
    },
    {
      key: 'enabled',
      label: '启用状态',
      type: 'switch',
      checkedValue: 'on',
      uncheckedValue: 'off',
      checkedLabel: '已启用',
      uncheckedLabel: '已停用',
    },
    {
      key: 'summary',
      label: '摘要',
      type: 'displayText',
      render: (value) => String(value ?? '请填写标题'),
    },
    {
      key: 'customAction',
      label: '自定义操作',
      type: 'custom',
      render: ({ value, values, setValue, setValues }) => (
        <div className={styles.businessFormActions}>
          <span>当前状态:{String(value ?? '未执行')}</span>
          <Button
            size="small"
            onClick={() => {
              setValues(
                {
                  ...values,
                  summary: `摘要:${String(values.title ?? '')}`,
                },
                'summary',
              );
            }}
          >
            填充摘要
          </Button>
          <Button
            size="small"
            variant="outline"
            onClick={() => {
              setValue('已执行');
            }}
          >
            标记完成
          </Button>
        </div>
      ),
    },
    {
      key: 'coverMedia',
      label: '展示素材',
      type: 'upload',
      required: true,
      uploadType: 'media',
      kinds: ['image', 'gif', 'video'],
      accept: '.png,.gif,.mp4,.mov',
      maxSize: 20 * 1024 * 1024,
      // maxCount: 3,
      multiple: true,
      placeholder: '点击上传',
      helperText: '支持图片、视频、GIF,推荐 1:1,最大 20MB',
      upload: async (file) => `https://static.example.com/business-form/media/${encodeURIComponent(file.name)}`,
    },
    {
      key: 'attachment',
      label: '附件文件',
      type: 'upload',
      uploadType: 'file',
      kinds: ['file'],
      accept: '.pdf,.doc,.zip',
      maxSize: 20 * 1024 * 1024,
      actionText: '上传附件',
      upload: async (file) => `https://static.example.com/business-form/file/${encodeURIComponent(file.name)}`,
    },
  ];
  return (
    <section className={styles.page}>
      <div className={styles.heading}>
        <p className={styles.eyebrow}>Component Library</p>
        <h2>组件库 Demo</h2>
        <p>TDesign 基础控件与全局业务组合组件展示入口</p>
      </div>

      <section className={styles.group}>
        <h3>基础交互</h3>
        <div className={styles.grid}>
          <div className={styles.buttonDemo}>
            <Button theme="primary">主按钮</Button>
            <Button>普通按钮</Button>
            <Button variant="outline">描边按钮</Button>
            <Button variant="text">文字按钮</Button>
            <Button theme="danger">危险按钮</Button>
          </div>
          <div className={styles.buttonDemo}>
            <Button size="small">小按钮</Button>
            <Button size="medium">中按钮</Button>
            <Button size="large">大按钮</Button>
            <Button icon={<SearchIcon />}>图标按钮</Button>
            <Button disabled>禁用按钮</Button>
            <Button loading>加载中</Button>
          </div>
          <Input aria-label="名称" defaultValue="项目名称" clearable showLimitNumber maxlength={20} />
          <Textarea aria-label="备注" defaultValue="补充说明" count maxlength={120} />
          <Input aria-label="搜索" defaultValue="合同" prefixIcon={<SearchIcon />} />
          <Checkbox checked={checkboxChecked} onChange={setCheckboxChecked}>Demo 启用</Checkbox>
          <Radio checked value="project">按项目</Radio>
          <Switch aria-label="Demo 启用审批" value={switchChecked} onChange={setSwitchChecked} />
        </div>
      </section>

      <section className={styles.group}>
        <h3>搜索区域</h3>
        <div className={styles.searchFilterDemo}>
          <SearchFilter
            fields={[
              { key: 'keyword', label: '项目名称', type: 'input', defaultValue: '智能审批' },
              { key: 'code', label: '项目编号', type: 'numberInput', defaultValue: '10a20' },
              {
                key: 'status',
                label: '状态',
                type: 'select',
                options: [
                  { label: '启用', value: 'enabled' },
                  { label: '停用', value: 'disabled' },
                ],
              },
              {
                key: 'owner',
                label: '负责人',
                type: 'select',
                options: [
                  { label: '张三', value: 'zhang' },
                  { label: '李四', value: 'li' },
                  { label: '王五', value: 'wang' },
                ],
              },
              { key: 'dept', label: '所属部门', type: 'input', defaultValue: '华东区' },
              { key: 'createdAt', label: '创建日期', type: 'datePicker', defaultValue: '2026-05-16' },
              { key: 'effectiveAt', label: '生效区间', type: 'datePicker', range: true },
              { key: 'remark', label: '备注说明', type: 'input', defaultValue: '用于展示展开收起' },
            ]}
            onReset={() => setSearchFilterSnapshot('已重置,等待下一次搜索')}
            onSearch={(values) => setSearchFilterSnapshot(JSON.stringify(values, null, 2))}
          />
          <pre className={styles.searchFilterResult}>{searchFilterSnapshot}</pre>
        </div>
      </section>

      <section className={styles.group}>
        <h3>选择与日期</h3>
        <div className={styles.grid}>
          <Select aria-label="负责人" options={[{ label: '张三', value: 'zhang' }, { label: '李四', value: 'li' }]} />
          <Select
            multiple
            aria-label="负责人多选"
            clearable
            placeholder="请选择负责人"
            value={multiSelectValue}
            options={[
              { label: '张三', value: 'zhang' },
              { label: '李四', value: 'li' },
              { label: '王五', value: 'wang' },
            ]}
            onChange={(value) => setMultiSelectValue(Array.isArray(value) ? value.map(String) : [])}
          />
          <TreeSelect
            aria-label="组织"
            value={treeValue}
            onChange={(value) => setTreeValue(value === undefined ? undefined : String(value))}
            data={[{ label: '总部', value: 'root', children: [{ label: '财务部', value: 'finance' }] }]}
          />
          <DatePicker aria-label="计划日期" value={dateValue} onChange={(value) => setDateValue(String(value))} />
          <DateRangePicker aria-label="周期" value={dateRangeValue} onChange={(value) => setDateRangeValue(value.map(String))} />
        </div>
      </section>

      <section className={styles.group}>
        <h3>表格与分页</h3>
        <div className={styles.tableStateDemo}>
          <div className={styles.tableStateItem}>
            <h4>有数据</h4>
            <BusinessTable
              rowKey="id"
              columns={tableColumns}
              data={tableRows}
              pagination={{ current: 1, pageSize: 10, total: 42 }}
            />
          </div>
          <div className={styles.tableStateItem}>
            <h4>普通空态</h4>
            <BusinessTable rowKey="id" columns={tableColumns} data={[]} pagination={false} />
          </div>
          <div className={styles.tableStateItem}>
            <h4>筛选无结果</h4>
            <BusinessTable rowKey="id" columns={tableColumns} data={[]} hasFilter pagination={false} />
          </div>
          <div className={styles.tableStateItem}>
            <h4>请求失败</h4>
            <BusinessTable rowKey="id" columns={tableColumns} data={[]} error pagination={false} />
          </div>
          <div className={styles.tableStateItem}>
            <h4>权限受限</h4>
            <BusinessTable
              rowKey="id"
              columns={tableColumns}
              data={[]}
              error
              errorState={{ kind: 'forbidden', onRetry: () => undefined }}
              pagination={false}
            />
          </div>
          <div className={styles.tableStateItem}>
            <h4>加载中</h4>
            <BusinessTable
              rowKey="id"
              columns={tableColumns}
              data={tableRows}
              loading
              loadingProps={{ delay: 0 }}
              pagination={false}
            />
          </div>
        </div>
      </section>

      <section className={styles.group}>
        <h3>反馈与浮层</h3>
        <div className={styles.grid}>
          <Form form={form} className={styles.formDemo} labelAlign="left" initialData={{ projectName: '', role: '驻店销售岗', remark: '' }}>
            <FormItem label="项目名称" name="projectName" rules={[{ required: true, message: '请输入项目名称' }]}>
              <Input aria-label="项目名称" />
            </FormItem>
            <FormItem label="所属岗位" name="role" help="从 Agent 管理自动带入,不可修改">
              <Input aria-label="所属岗位" disabled />
            </FormItem>
            <FormItem label="备注" name="remark">
              <Input aria-label="表单备注" />
            </FormItem>
            <Button theme="primary" onClick={() => void form.validate()}>提交表单</Button>
          </Form>
          <Alert theme="info" message="提示:保存后立即生效" />
          <Button theme="primary" onClick={() => setShowMessage(true)}>展示消息提示</Button>
          {showMessage ? (
            <div aria-label="消息提示示例" className={styles.messageDemo}>
              <Alert theme="success" message="保存成功" close onClose={() => setShowMessage(false)} />
              <Alert theme="warning" message="请补充审批人" />
              <Alert theme="error" message="删除失败" />
            </div>
          ) : null}
          <Tooltip content="补充说明"><Button>帮助</Button></Tooltip>
          <Popconfirm content="确认删除?"><Button theme="danger">删除</Button></Popconfirm>
          <Dialog
            visible={dialogOpen}
            header="编辑项目"
            onCancel={() => setDialogOpen(false)}
            onClose={() => setDialogOpen(false)}
          >
            内容
          </Dialog>
          <Drawer visible={false} header="详情">内容</Drawer>
        </div>
      </section>

      <section className={styles.group}>
        <h3>业务表单</h3>
        <div className={styles.businessFormDemo}>
          <BusinessForm
            fieldWidth={360}
            fields={businessFormFields}
            values={businessFormValues}
            onChange={(nextValues) => setBusinessFormValues(nextValues)}
          />
          <pre aria-label="业务表单快照" className={styles.businessFormSnapshot}>
            {JSON.stringify(businessFormValues, null, 2)}
          </pre>
        </div>
      </section>

      <section className={styles.group}>
        <h3>数据展示</h3>
        <div className={styles.grid}>
          <Card header="概览" actions={<Button size="small">刷新</Button>}>卡片内容</Card>
          <Badge count={12}><Button>消息</Button></Badge>
          <AvatarGroup max={2}><Avatar>张</Avatar><Avatar>李</Avatar><Avatar>王</Avatar></AvatarGroup>
          <Progress percentage={68} label />
          <Timeline>
            <Timeline.Item label="09:00">提交</Timeline.Item>
            <Timeline.Item label="10:00">审批:处理中</Timeline.Item>
          </Timeline>
        </div>
      </section>

      <section className={styles.group}>
        <h3>导航与占位</h3>
        <div className={styles.grid}>
          <Menu defaultValue="agent-list" defaultExpanded={['agent-management']}>
            <SubMenu value="agent-management" title="Agent管理">
              <MenuItem value="agent-list">Agent列表</MenuItem>
            </SubMenu>
          </Menu>
          <Tabs defaultValue="base">
            <TabPanel value="base" label="基础信息">基础内容</TabPanel>
            <TabPanel value="advanced" label="高级配置">高级内容</TabPanel>
          </Tabs>
          <Steps current={1} options={[{ title: '提交' }, { title: '审批' }, { title: '完成' }]} />
          <Divider />
          <StatePlaceholder
            state="empty"
            title="暂无内容"
            description="该区域当前没有可展示内容"
          />
          <Skeleton loading />
          <Collapse defaultValue={['one']}>
            <CollapsePanel value="one" header="基础设置">设置内容</CollapsePanel>
          </Collapse>
        </div>
      </section>

      <section className={styles.group}>
        <h3>颜色能力</h3>
        <div className={styles.grid}>
          <ColorPicker value="#2468f2" />
          <Input aria-label="主题色" value="#2468f2" />
        </div>
      </section>

      <section className={styles.group}>
        <h3>上传</h3>
        <div className={styles.uploadDemo}>
          <Upload
            autoUpload={false}
            draggable
            multiple
            theme="file-flow"
            files={[
              { name: 'product_design_v2.fig', status: 'success' },
              { name: '需求文档.pdf', status: 'progress', percent: 42 },
              { name: '素材包.zip', status: 'fail' },
            ]}
            tips="支持拖拽业务资料到此处,单文件上限 1GB"
          />
          <MediaWall
            items={[
              { id: 'image', type: 'image', src: mediaImage, alt: '宣传图' },
              { id: 'video', type: 'video', src: mediaVideo, alt: '演示视频' },
            ]}
            helperText="支持 JPG、PNG、MP4、MOV,图片单张不超过 10MB,视频单个不超过 500MB"
            onPreview={() => undefined}
          />
        </div>
      </section>
    </section>
  );
}