-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Datatable): fix column visibility state
- Loading branch information
Showing
3 changed files
with
45 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 2 additions & 43 deletions
45
src/components/Datatable/components/ColumnsControls/hooks/useColumnVisibility.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |