table.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 titles: string[] = columnsFiber.map(item=>item?.props?.children || "");
  12. const tableDataSource: any[] = [];
  13. for (let i=0;i<dataFiber.length;i++) {
  14. let item: Record<string, string> = {
  15. key: String(i)
  16. };
  17. dataFiber[i].props.children.forEach((child, index)=>{
  18. item[titles[index]] = child?.props?.children ?? "";
  19. });
  20. tableDataSource.push(item);
  21. }
  22. return <Table dataSource={tableDataSource} columns={titles.map(title=>{
  23. return {
  24. title,
  25. dataIndex: title
  26. };
  27. })} {...omit(props, 'children')}/>;
  28. };
  29. export default table;