UNPKG

phx-react

Version:

PHX REACT

106 lines 3.92 kB
export const getMaxDepth = (headers) => { let max = 1; for (const item of headers) { if (item.children && item.children.length > 0) { max = Math.max(max, 1 + getMaxDepth(item.children)); } } return max; }; export const buildHeaderRows = (headers, level = 0, rows = [], maxDepth = 1, startIndex = 0) => { if (!rows[level]) rows[level] = []; let currentIndex = startIndex; for (const item of headers) { const hasChildren = item.children && item.children.length > 0; const colSpan = hasChildren ? countLeaf(item) : 1; const rowSpan = hasChildren ? 1 : maxDepth - level; rows[level].push({ name: item.name, colSpan, rowSpan, colIndex: currentIndex, }); if (hasChildren) { buildHeaderRows(item.children, level + 1, rows, maxDepth, currentIndex); } currentIndex += colSpan; } return rows; }; export const countLeaf = (header) => { if (!header.children || header.children.length === 0) return 1; return header.children.reduce((t, c) => t + countLeaf(c), 0); }; export const renderCell = (data, columnKey, thComponent) => { const value = data[columnKey]; if (thComponent && (thComponent === null || thComponent === void 0 ? void 0 : thComponent[columnKey])) { const componentCell = thComponent[columnKey](value, data); return { value: componentCell.value, row: componentCell.row || 1, children: (componentCell === null || componentCell === void 0 ? void 0 : componentCell.children) || null, }; } return { value: value !== null && value !== void 0 ? value : '', row: 1, children: null }; }; export const findColumnKeyMerge = (mergeConfig, columnIndex) => { const { columnPosition } = mergeConfig; const currentKey = columnPosition.find((item) => item.position === columnIndex + 1); return (currentKey === null || currentKey === void 0 ? void 0 : currentKey.key) || ''; }; export function buildRowSpan(tableData, mergeConfig) { const firstRowMap = {}; const dataLength = tableData.length; for (let index = 0; index < dataLength; index++) { const data = tableData[index]; for (const config of mergeConfig.columnPosition) { const { key } = config; const value = data[key]; if (!firstRowMap[`${key}${value || `idx${index}`}`]) { firstRowMap[`${key}${value || `idx${index}`}`] = true; data[`${key}_first_row`] = true; } else { data[`${key}_first_row`] = false; } } } return tableData; } export const applyUrlQuery = { get: (searchParams, key) => { const params = new URLSearchParams(searchParams.toString()); return params.get(key); }, set: (routerPush, searchParams, data) => { const params = new URLSearchParams(searchParams.toString()); for (const item of data) { const { key, value } = item; params.set(key, value); } routerPush(`?${params.toString()}`); }, delete: (routerPush, searchParams, key) => { const params = new URLSearchParams(searchParams.toString()); for (const item of key) { params.delete(item); } routerPush(`?${params.toString()}`); }, multipleMethod: (routerPush, searchParams, data) => { const params = new URLSearchParams(searchParams.toString()); for (const item of data) { const { key, value, method } = item; if (method === 'delete') { params[method](key); } else { params[method](key, value); } } routerPush(`?${params.toString()}`); }, }; //# sourceMappingURL=utils.js.map