Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Datatable): fix column visibility state #832

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
tableConfig = {},
resetSelectionFn,
}: DatatableProps<D>): React.ReactElement {
useEffect(() => {
DatatableStore.update(() => datatableInitialState);
}, []);
const [persistedState, setPersistedState] = useLocalStorageState(
`datatable_${id}`,
);
Expand All @@ -72,7 +75,7 @@
);
// eslint-disable-next-line
return unsubscribe;
}, []);

Check warning on line 78 in src/components/Datatable/Datatable.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

React Hook useEffect has missing dependencies: 'id' and 'setPersistedState'. Either include them or remove the dependency array

Check warning on line 78 in src/components/Datatable/Datatable.tsx

View workflow job for this annotation

GitHub Actions / 🧹 Run lint

React Hook useEffect has missing dependencies: 'id' and 'setPersistedState'. Either include them or remove the dependency array

// Set canceled signal to prevent data fetch when unmounting
useEffect(
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 @@
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 @@
>
<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,
};
};
Loading