LoginSignup
1
1

More than 3 years have passed since last update.

Ant Design で Table の columns の dataIndex の候補を表示する型

Last updated at Posted at 2020-08-12

Ant Design で Table の columns の dataIndex の候補を表示する型

antd の Tablecolumns には表示する対象を指定する dataIndex がありますが、
これは手動で指定する必要があります。

no-suggestion
type Data = {
  id: number;
  name: string;
};

export const columns: TableProps<Data>['columns'] = [
  {
    title: 'ID',
    key: 'id',
    // これだと候補がでない
    dataIndex: '',
  }
];

no-suggestion.png

そこで以下のような型を定義することで dataIndex の型を指定してみます。

columns-type
import { ColumnGroupType, ColumnType } from 'antd/lib/table/interface';

type AnyData = Record<string, unknown>;

type RenderReturn<TRecord = AnyData> = ReturnType<
  NonNullable<ColumnType<TRecord>['render']>
>;

type Column<TRecord = AnyData> =
  | (Omit<ColumnGroupType<TRecord>, 'render'> & {
      render?: (
        value: TRecord,
        record: TRecord,
        index: number
      ) => RenderReturn<TRecord>;
    })
  | (ColumnType<TRecord> & {
      dataIndex?: keyof TRecord;
      render?: <T = TRecord>(
        value: T,
        record: TRecord,
        index: number
      ) => RenderReturn<TRecord>;
    });

type Columns<TRecord = AnyData> = Column<TRecord>[];

type Data = {
  id: number;
  name: string;
};

export const columns: Columns<Data> = [
  {
    title: 'ID',
    key: 'id',
    dataIndex: '',
  },
];

定義した Columns を指定することで dataIndex に候補が出るようになりました。

suggestion.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1