table.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import * as React from 'react';
  2. import { PropsWithChildren } from 'react';
  3. import { get } from 'lodash';
  4. import Table, { TableProps } from '../../table';
  5. import { omit } from 'lodash';
  6. const table = (props: PropsWithChildren<TableProps>) => {
  7. const { children } = props;
  8. const toArray = value => Array.isArray(value) ? value : [value];
  9. const columnsFiber = toArray(get(children[0], 'props.children.props.children'));
  10. const dataFiber = toArray(get(children[1], 'props.children'));
  11. const titlesColumns = columnsFiber.map((column, i) => {
  12. return {
  13. dataIndex: String(i),
  14. title: column?.props?.children || ""
  15. };
  16. });
  17. const tableDataSource: any[] = [];
  18. for (let i = 0;i < dataFiber.length;i++) {
  19. let item: Record<string, string> = {
  20. key: String(i)
  21. };
  22. dataFiber[i]?.props.children?.forEach?.((child, index) => {
  23. item[String(index)] = child?.props?.children ?? "";
  24. });
  25. tableDataSource.push(item);
  26. }
  27. return <Table dataSource={tableDataSource} columns={titlesColumns} {...omit(props, 'children')}/>;
  28. };
  29. export default table;