|
| 1 | +import { |
| 2 | + Children, |
| 3 | + isValidElement, |
| 4 | + type ReactElement, |
| 5 | + type ReactNode, |
| 6 | +} from 'react'; |
| 7 | + |
| 8 | +import type { TableColumn, TableData } from '#ui/types'; |
| 9 | + |
| 10 | +const hasChildren = (props: unknown): props is { children: ReactNode } => |
| 11 | + typeof props === 'object' && props !== null && 'children' in props; |
| 12 | + |
| 13 | +const isTableElement = ( |
| 14 | + child: ReactNode, |
| 15 | + tagName: string |
| 16 | +): child is ReactElement<{ children?: ReactNode }> => |
| 17 | + isValidElement(child) && child.type === tagName; |
| 18 | + |
| 19 | +const getTextContent = (node: ReactNode): string => { |
| 20 | + if (typeof node === 'string' || typeof node === 'number') return String(node); |
| 21 | + if (Array.isArray(node)) return node.map(getTextContent).join(''); |
| 22 | + if (isValidElement(node) && hasChildren(node.props)) |
| 23 | + return getTextContent(node.props.children); |
| 24 | + return ''; |
| 25 | +}; |
| 26 | + |
| 27 | +const getColumnKey = (headerText: string, index: number): string => |
| 28 | + headerText |
| 29 | + ? headerText.toLowerCase().trim().replace(/\s+/g, '_') |
| 30 | + : `col_${index}`; |
| 31 | + |
| 32 | +export const extractColumns = (thead: ReactElement): Array<TableColumn> => { |
| 33 | + if (!hasChildren(thead.props) || !thead.props.children) return []; |
| 34 | + |
| 35 | + const headerRows = Children.toArray(thead.props.children); |
| 36 | + const firstRow = headerRows[0]; |
| 37 | + if (!isValidElement(firstRow) || !hasChildren(firstRow.props)) return []; |
| 38 | + |
| 39 | + const headerCells = Children.toArray(firstRow.props.children); |
| 40 | + |
| 41 | + return headerCells |
| 42 | + .map((cell, index) => { |
| 43 | + if (!isValidElement(cell) || !hasChildren(cell.props)) return null; |
| 44 | + |
| 45 | + const headerText = getTextContent(cell.props.children); |
| 46 | + const key = getColumnKey(headerText, index); |
| 47 | + |
| 48 | + return { |
| 49 | + key: key, |
| 50 | + header: headerText, |
| 51 | + }; |
| 52 | + }) |
| 53 | + .filter((col): col is TableColumn => col !== null); |
| 54 | +}; |
| 55 | + |
| 56 | +export const extractData = ( |
| 57 | + tbody: ReactElement, |
| 58 | + columns: Array<TableColumn> |
| 59 | +): Array<TableData> => { |
| 60 | + if (!hasChildren(tbody.props) || !tbody.props.children) return []; |
| 61 | + |
| 62 | + const bodyRows = Children.toArray(tbody.props.children); |
| 63 | + |
| 64 | + return bodyRows |
| 65 | + .map(row => { |
| 66 | + if (!isValidElement(row) || !hasChildren(row.props)) return null; |
| 67 | + |
| 68 | + const cells = Children.toArray(row.props.children); |
| 69 | + const rowData: TableData = {}; |
| 70 | + |
| 71 | + cells.forEach((cell, i) => { |
| 72 | + if (isValidElement(cell) && hasChildren(cell.props) && columns[i]) { |
| 73 | + rowData[columns[i].key] = cell.props.children; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + return rowData; |
| 78 | + }) |
| 79 | + .filter((row): row is TableData => row !== null); |
| 80 | +}; |
| 81 | + |
| 82 | +export const parseTableStructure = ( |
| 83 | + children: ReactNode |
| 84 | +): { columns: Array<TableColumn>; data: Array<TableData> } => { |
| 85 | + if (!children) return { columns: [], data: [] }; |
| 86 | + |
| 87 | + const nodes = Children.toArray(children); |
| 88 | + const thead = nodes.find(node => isTableElement(node, 'thead')); |
| 89 | + const tbody = nodes.find(node => isTableElement(node, 'tbody')); |
| 90 | + |
| 91 | + if (!thead) throw new Error('Thead not found'); |
| 92 | + if (!tbody) throw new Error('Tbody not found'); |
| 93 | + |
| 94 | + const columns = extractColumns(thead); |
| 95 | + const data = extractData(tbody, columns); |
| 96 | + |
| 97 | + return { columns, data }; |
| 98 | +}; |
0 commit comments