Skip to content

Commit

Permalink
fix(Datatable): fix column visibility state
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkl2533 committed Oct 9, 2023
1 parent 9eb8690 commit a3f0b5d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
3 changes: 3 additions & 0 deletions src/components/Datatable/Datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function Datatable<D extends Record<string, unknown>>({
tableConfig = {},
resetSelectionFn,
}: DatatableProps<D>): React.ReactElement {
useEffect(() => {
DatatableStore.update(() => datatableInitialState);
}, []);
const [persistedState, setPersistedState] = useLocalStorageState(
`datatable_${id}`,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';

Check failure on line 1 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

'useEffect' is defined but never used

Check failure on line 1 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

'useEffect' is defined but never used
import PropTypes from 'prop-types';
import { pluck } from 'ramda';
import { noop } from 'ramda-adjunct';
import { useDeepCompareEffect } from 'use-deep-compare';

import { ControlDropdown } from '../../../ControlDropdown';
import { SortableList } from '../../../SortableList';
Expand Down Expand Up @@ -32,31 +33,42 @@ const ColumnsControls: React.FC<ColumnsControlsProps> = ({
resetOrderedColumns,
isInDefaultOrder,
} = useColumnOrder();
const {
hiddenColumns,
setHiddenColumn,
storeVisibleColumns,
reinitializeVisibleColumns,
resetVisisbleColumns,
isInDefaultVisibility,
} = useColumnVisibility();
const { isInDefaultVisibility } = useColumnVisibility();
const allColumns = DatatableStore.useState((s) => s.columns);
const hiddenColumns = DatatableStore.useState((s) => s.hiddenColumns);
console.log(

Check warning on line 39 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement

Check warning on line 39 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement
'🚀 ~ file: ColumnsControls.tsx:38 ~ hiddenColumns:',
hiddenColumns,
);

const [localHiddenColumns, setLocalHiddenColumns] = useState(hiddenColumns);
console.log(

Check warning on line 45 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement

Check warning on line 45 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement
'🚀 ~ file: ColumnsControls.tsx:42 ~ localHiddenColumns:',
localHiddenColumns,
);

useDeepCompareEffect(() => {
setLocalHiddenColumns(hiddenColumns);
}, [hiddenColumns]);

const handleCloseColumnsControl = () => {
onClose();
reinitializeOrderedColumns();
reinitializeVisibleColumns();
};
const handleApplyColumnsControl = () => {
onApply(!isInDefaultOrder || !isInDefaultVisibility);
storeOrderedColumns();
storeVisibleColumns();
DatatableStore.update((s) => {
s.hiddenColumns = localHiddenColumns;
});
};

const handleResetColumnsControl = () => {
onReset();
resetOrderedColumns();
resetVisisbleColumns();
DatatableStore.update((s) => {
s.hiddenColumns = [];
});
};

return (
Expand Down Expand Up @@ -86,12 +98,26 @@ const ColumnsControls: React.FC<ColumnsControlsProps> = ({
>
<Text size={TextSizes.md}>{label}</Text>
<Switch
checked={!hiddenColumns.includes(id)}
checked={!localHiddenColumns.includes(id)}
name={`visibility-${id}`}
size={SwitchSizes.sm}
switchId={`visibility-${id}`}
onChange={(e) => {
setHiddenColumn(id, e.target.checked);
const { checked } = e.target;
setLocalHiddenColumns((prev) => {
console.log(

Check warning on line 108 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement

Check warning on line 108 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement
'🚀 ~ file: ColumnsControls.tsx:101 ~ setLocalHiddenColumns ~ prev:',
prev,
);
const next = checked
? prev.filter((col) => col !== id)
: [...prev, id];
console.log(

Check warning on line 115 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement

Check warning on line 115 in src/components/Datatable/components/ColumnsControls/ColumnsControls.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

Unexpected console statement
'🚀 ~ file: ColumnsControls.tsx:102 ~ next:',
next,
);
return next;
});
}}
/>
</Inline>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,19 @@
import { useEffect, useState } from 'react';
import { isEmptyArray } from 'ramda-adjunct';

import { DatatableStore } from '../../../Datatable.store';

export const useColumnVisibility = (): {
hiddenColumns: string[];
setHiddenColumn: (id: string, value: boolean) => void;
storeVisibleColumns: () => void;
reinitializeVisibleColumns: () => void;
resetVisisbleColumns: () => void;
isInDefaultVisibility: boolean;
} => {
const { hiddenColumns } = DatatableStore.useState((s) => ({
hiddenColumns: s.hiddenColumns,
}));
const [localHiddenColumns, setLocalHiddenColumns] = useState(hiddenColumns);

useEffect(() => {
const unsubscribe = DatatableStore.subscribe(
(s) => ({ hidden: s.hiddenColumns }),
({ hidden }) => {
setLocalHiddenColumns(hidden);
},
);

return () => {
unsubscribe();
};
}, []);
const setHiddenColumn = (id, value) => {
if (value === true) {
setLocalHiddenColumns((prev) => prev.filter((col) => col !== id));
} else {
setLocalHiddenColumns((prev) => [...prev, id]);
}
};
const isInDefaultVisibility = isEmptyArray(localHiddenColumns);
const storeVisibleColumns = () => {
DatatableStore.update((s) => {
s.hiddenColumns = localHiddenColumns;
});
};

const resetVisisbleColumns = () => {
setLocalHiddenColumns([]);
};
const reinitializeVisibleColumns = () => {
setLocalHiddenColumns(hiddenColumns);
};
const isInDefaultVisibility = isEmptyArray(hiddenColumns);

return {
hiddenColumns: localHiddenColumns,
setHiddenColumn,
storeVisibleColumns,
reinitializeVisibleColumns,
resetVisisbleColumns,
hiddenColumns,
isInDefaultVisibility,
};
};

0 comments on commit a3f0b5d

Please sign in to comment.