Skip to content

Commit

Permalink
fix(@clayui/core): fixes columns visibility initial state behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
matuzalemsteles committed Mar 5, 2024
1 parent 89a8f07 commit 34614f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
11 changes: 6 additions & 5 deletions packages/clay-core/src/table/Head.tsx
Expand Up @@ -54,7 +54,6 @@ function HeadInner<T extends Record<string, any>>(
) {
const {
columnsVisibility,
headCellsCount,
hiddenColumns,
messages,
onHeadCellsChange,
Expand All @@ -69,6 +68,9 @@ function HeadInner<T extends Record<string, any>>(
});

useMemo(() => {
if (hiddenColumns.size === 0) {
onHiddenColumnsChange(collection.getItems(), 0);
}
onHeadCellsChange(collection.getSize());
}, []);

Expand Down Expand Up @@ -151,12 +153,11 @@ function HeadInner<T extends Record<string, any>>(
},
}}
disabled={
!hiddenColumns.has(
hiddenColumns.has(
item
) &&
headCellsCount -
1 ===
hiddenColumns.size
hiddenColumns.size ===
1
}
onChange={(event) => {
event.stopPropagation();
Expand Down
21 changes: 20 additions & 1 deletion packages/clay-core/src/table/Table.tsx
Expand Up @@ -212,7 +212,26 @@ export const Table = React.forwardRef<HTMLDivElement, Props>(
onExpandedChange: setExpandedKeys,
onHeadCellsChange: setHeadCellsCount,
onHiddenColumnsChange: useCallback(
(column: React.Key, index: number) => {
(
column: React.Key | Array<React.Key>,
index: number
) => {
if (Array.isArray(column)) {
const columns = new Map(hidden);

column.forEach((value, index) => {
if (columns.has(value)) {
columns.delete(value);
} else {
columns.set(value, index);
}
});

setHidden(columns);

return;
}

const columns = new Map(hidden);

if (columns.has(column)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/clay-core/src/table/context.tsx
Expand Up @@ -21,7 +21,10 @@ type Context = {
onHeadCellsChange: (value: number) => void;
onLoadMore?: (item: unknown) => Promise<Array<any> | undefined>;
onSortChange: (sorting: Sorting | null, textValue: string) => void;
onHiddenColumnsChange: (column: React.Key, index: number) => void;
onHiddenColumnsChange: (
column: React.Key | Array<React.Key>,
index: number
) => void;
sort: Sorting | null;
sortDescriptionId: string;
treegrid: boolean;
Expand Down

0 comments on commit 34614f8

Please sign in to comment.