Skip to content

Commit 6715027

Browse files
LukaFilipovic99Felix Beceic
and
Felix Beceic
authored
#240 Add support for multiple optional columns in DataTable component (#244)
* #240 Add support for multiple optional columns in DataTable component * Add support for optional columns with '&&' operator * Add support for optional column array with '&&' operator * Improve code --------- Co-authored-by: Felix Beceic <[email protected]>
1 parent 0ba8ca2 commit 6715027

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

libs/data-display/src/DataTable.tsx

+30-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ import { Checkbox } from "@tiller-ds/form-elements";
3636
import { ComponentTokens, cx, TokenProps, useIcon, useTokens } from "@tiller-ds/theme";
3737
import { createNamedContext, findChild } from "@tiller-ds/util";
3838

39-
type DataTableChild<T extends object> = React.ReactElement<DataTableColumnProps<T> | DataTableExpanderProps<T>>;
39+
type DataTableChild<T extends object> =
40+
| React.ReactElement<DataTableColumnProps<T> | DataTableExpanderProps<T>>
41+
| React.ReactElement<DataTableColumnProps<T> | DataTableExpanderProps<T>>[]
42+
| undefined;
4043

4144
export type DataTableProps<T extends object> = {
4245
/**
@@ -490,13 +493,37 @@ function DataTable<T extends object>({
490493
const primaryRow = findChild("DataTablePrimaryRow", children);
491494
const secondaryRow = findChild("DataTableSecondaryRow", children);
492495
const hasSecondaryColumns = React.isValidElement(secondaryRow);
493-
const primaryColumnChildren = React.isValidElement(primaryRow) ? primaryRow.props.children : children;
496+
497+
const primaryColumnChildren = React.useMemo(() => {
498+
return mapChildren(React.isValidElement(primaryRow) ? primaryRow.props.children : children);
499+
}, [primaryRow, children]);
500+
494501
const primaryColumnChildrenArray = React.Children.toArray(primaryColumnChildren).filter(Boolean);
495502
const columnChildrenSize = primaryColumnChildrenArray.length;
496503

497-
const secondaryColumnChildren = React.isValidElement(secondaryRow) ? secondaryRow.props.children : null;
504+
const secondaryColumnChildren = React.useMemo(() => {
505+
return React.isValidElement(secondaryRow) ? mapChildren(secondaryRow.props.children) : null;
506+
}, [secondaryRow, children]);
507+
498508
const secondaryColumnChildrenArray = React.Children.toArray(secondaryColumnChildren).filter(Boolean);
499509

510+
function mapChildren(children: DataTableChild<T>[]) {
511+
return children.map((child) => {
512+
if (!child) {
513+
return undefined;
514+
}
515+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
516+
// @ts-ignore
517+
if (Array.isArray(child.props?.children)) {
518+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
519+
// @ts-ignore
520+
return mapChildren(child.props?.children);
521+
} else {
522+
return child;
523+
}
524+
});
525+
}
526+
500527
const totalColumnChildrenSize = hasSecondaryColumns
501528
? secondaryColumnChildrenArray.length + columnChildrenSize
502529
: columnChildrenSize;

0 commit comments

Comments
 (0)