有没有熟悉 Antd 可编辑表格的,自定义的数据类型应该怎么修改 Column 定义才能通过 ts 检查?

2023-04-25 22:18:59 +08:00
 steins2628

如题,自定义的数据结构大概长这样

interface MyLabel {
    key: string;
    name: string;
    color?: string;
}

interface MyData {
    id: React.Key,
    group: number,   
    category: number 
    state: number,  
    keyword: string,
    create_at: number,
    edit_at: number,

    author?: string,
    level?: number,
    labels?: MyLabel[];
}

因为要做可编辑除了默认的类型还会有 render, filter...,现在的 column 类型大概是这样的

  {
    title: string,
    dataIndex: string,
    editable: boolean,
    filters?: undefined,
    filterSearch?: undefined,
    onFilter?: undefined,
    render?: undefined,
    ellipsis?: undefined,
    sorter?: undefined,
  }

然后 ts 检查就会告诉我

... not assignable to type ColumnType<MyData> | ColumnGroupType<MyData>

直接给 column 赋类型 ColumnsType<MyData> 也是不行的,有点迷茫不知道要怎么改了,官网教程也没说

同时 render 的时候,默认的类型应该是 string | number | undefined, 实际上是个枚举,但直接给枚举类型也是报错的,用断言 as number 也是不行的

有没有好兄弟分享下经验的,这种时候应该怎么定义类型?

附上完整的 Column 定义

  const edit_tabel_column = [
    {
        title: 'level',
        dataIndex: 'level',
        editable: true,
    },
    {
        title: 'group',
        dataIndex: 'group',
        editable: true,
        filters: [
          {
            text: "Blacklist",
            value: CleanGroup.Blacklist
          },
          {
            text: "Whitelist",
            value: CleanGroup.Whitelist
          }
        ],
        filterSearch: false,
        onFilter: (value: CleanGroup, record: any) => record.group === value,
        render: (value: CleanGroup) => (
          <SelectCell group={value}/>
        ),
    },
    {
      title: 'category',
      dataIndex: 'category',
      editable: true,
      filters: [
        {
          text: 'Quick Access',
          value: CleanCategory.QuickAccess,
        },
        {
          text: 'Frequent Folder',
          value: CleanCategory.FrequentFolders,
        },
        {
          text: 'Recent File',
          value: CleanCategory.RecentFiles,
        },
      ],
      filterSearch: false,
      onFilter: (value: CleanCategory, record: any) => record.category === value,
      render: (value: CleanCategory) => (
          <SelectCell category={value}/>
        ),
    },
    {
      title: 'state',
      dataIndex: 'state',
      editable: true,
      filters: [
        {
          text: 'enabled',
          value: CleanState.Enabled
        },
        {
          text: 'disabled',
          value: CleanState.Disabled
        }
      ],
      filterSearch: false,
      onFilter: (value: CleanState, record: any) => record.state === value,
      render: (value: CleanState) => (
        <SelectCell badge state={value}/>
      ),
    },
    {
        title: 'keyword',
        dataIndex: 'keyword',
        editable: true,
        ellipsis: {
            showTitle: false,
        },
    },
    {
        title: 'create_at',
        dataIndex: 'create_at',
        ellipsis: {
            showTitle: false,
        },
        editable: false,
        sorter: (a: any, b: any) => a.create_at - b.create_at,
        render: (create_at: number) => (
        <Tooltip title={time.get_date_string_from_timestamp(create_at, "YYYY-MM-DD")} placement='top'>
            {time.get_date_string_from_timestamp(create_at, "YYYY-MM-DD")}
        </Tooltip>
        ),
    },
    {
        title: 'edit_at',
        dataIndex: 'edit_at',
        ellipsis: {
            showTitle: false,
        },
        editable: false,
        sorter: (a: any, b: any) => a.edit_at - b.edit_at,
        render: (edit_at: number) => (
        <Tooltip title={time.get_date_string_from_timestamp(edit_at, "YYYY-MM-DD")}>
            {time.get_date_string_from_timestamp(edit_at, "YYYY-MM-DD")}
        </Tooltip>
        ),
    },
    {
        title: 'operation',
        dataIndex: 'operation',
        render: (_: any, record: MyData) => {
          const editable = isEditing(record);
          return editable ? (
            <span>
              <Typography.Link onClick={() => saveEditingRow(record.id)} style={{ marginRight: 8 }}>
              {'save'}
              </Typography.Link>
              <Popconfirm title={'sure to cancel?')} onConfirm={cancelEditTableRow}>
                <a>{'cancel'}</a>
              </Popconfirm>
            </span>
          ) : (
            <div>
              <Typography.Link disabled={editingKey !== ''} onClick={() => editTableRow(record)}>
                {'edit'}
              </Typography.Link>
              <Divider type="vertical" />
              <Typography.Link disabled={editingKey !== ''} onClick={() => removeRow(record)}>
              {'delete'}
              </Typography.Link>              
            </div>
          );
        },
    }
  ]

  const mergedEditColumns = edit_tabel_column.map((col) => {
    if (!col.editable) {
      return col;
    }
    return {
      ...col,
      onCell: (record: MyData) => ({
        record,
        inputType: col.dataIndex,
        dataIndex: col.dataIndex,
        title: col.title,
        editing: isEditing(record),
      }),
    };
  })
698 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/935485

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX