From 9adfad3fe08e26d77443cb701242ee1e57b43598 Mon Sep 17 00:00:00 2001 From: Yaroslav Kuznietsov Date: Tue, 12 Dec 2023 10:13:52 +0200 Subject: [PATCH] [useTree]: added useSelectAll hook. --- .../processing/views/dataRows/useBuildRows.ts | 5 +-- .../processing/views/dataRows/useDataRows.ts | 28 ++++-------- .../processing/views/dataRows/useSelectAll.ts | 44 +++++++++++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 uui-core/src/data/processing/views/dataRows/useSelectAll.ts diff --git a/uui-core/src/data/processing/views/dataRows/useBuildRows.ts b/uui-core/src/data/processing/views/dataRows/useBuildRows.ts index abf11ec620..d1788b8166 100644 --- a/uui-core/src/data/processing/views/dataRows/useBuildRows.ts +++ b/uui-core/src/data/processing/views/dataRows/useBuildRows.ts @@ -6,7 +6,6 @@ import { FoldingService } from '../tree/hooks/services'; import { NodeStats, getDefaultNodeStats, getRowStats, mergeStats } from './stats'; export interface UseBuildRowsProps extends FoldingService { - tree: ITree; dataSourceState: DataSourceState; isPartialLoad?: boolean; @@ -37,7 +36,7 @@ export function useBuildRows({ getRowProps, getLoadingRowProps, }: UseBuildRowsProps) { - const rebuildRows = () => { + const buildRows = () => { const rows: DataRowProps[] = []; const pinned: Record = {}; const pinnedByParentId: Record = {}; @@ -150,5 +149,5 @@ export function useBuildRows({ }; }; - return useMemo(() => rebuildRows(), [tree]); + return useMemo(() => buildRows(), [tree]); } diff --git a/uui-core/src/data/processing/views/dataRows/useDataRows.ts b/uui-core/src/data/processing/views/dataRows/useDataRows.ts index 47c7257886..19dba65cfc 100644 --- a/uui-core/src/data/processing/views/dataRows/useDataRows.ts +++ b/uui-core/src/data/processing/views/dataRows/useDataRows.ts @@ -5,6 +5,7 @@ import { idToKey } from '../helpers'; import { FoldingService, CheckingService, FocusService, SelectingService } from '../tree/hooks/services'; import { useDataRowProps } from './useDataRowProps'; import { useBuildRows } from './useBuildRows'; +import { useSelectAll } from './useSelectAll'; export interface UseDataRowsProps extends FoldingService, CheckingService, FocusService, SelectingService { tree: ITree; @@ -85,26 +86,13 @@ export function useDataRows( getLoadingRowProps, }); - const isSelectAllEnabled = useMemo(() => props.selectAll === undefined ? true : props.selectAll, [props.selectAll]); - - const selectAll = useMemo(() => { - if (stats.isSomeCheckable && isSelectAllEnabled) { - return { - value: stats.isSomeCheckboxEnabled ? stats.isAllChecked : false, - onValueChange: handleSelectAll, - indeterminate: dataSourceState.checked && dataSourceState.checked.length > 0 && !stats.isAllChecked, - }; - } else if (tree.getRootIds().length === 0 && rowOptions?.checkbox?.isVisible && isSelectAllEnabled) { - // Nothing loaded yet, but we guess that something is checkable. Add disabled checkbox for less flicker. - return { - value: false, - onValueChange: () => {}, - isDisabled: true, - indeterminate: dataSourceState.checked?.length > 0, - }; - } - return null; - }, [tree, rowOptions, dataSourceState.checked, stats, isSelectAllEnabled, handleSelectAll]); + const selectAll = useSelectAll({ + tree, + checked: dataSourceState.checked, + stats, + areCheckboxesVisible: rowOptions?.checkbox?.isVisible, + handleSelectAll, + }); const getListProps = useCallback((): DataSourceListProps => { return { diff --git a/uui-core/src/data/processing/views/dataRows/useSelectAll.ts b/uui-core/src/data/processing/views/dataRows/useSelectAll.ts new file mode 100644 index 0000000000..4d8c83977d --- /dev/null +++ b/uui-core/src/data/processing/views/dataRows/useSelectAll.ts @@ -0,0 +1,44 @@ +import { useMemo } from 'react'; +import { ITree } from '../tree'; +import { NodeStats } from './stats'; + +export interface UseSelectAllProps { + tree: ITree; + selectAll?: boolean; + stats: NodeStats; + checked?: TId[]; + areCheckboxesVisible: boolean; + handleSelectAll: (isChecked: boolean) => void; +} + +export function useSelectAll(props: UseSelectAllProps) { + const isSelectAllEnabled = useMemo(() => props.selectAll === undefined ? true : props.selectAll, [props.selectAll]); + + const selectAll = useMemo(() => { + if (props.stats.isSomeCheckable && isSelectAllEnabled) { + return { + value: props.stats.isSomeCheckboxEnabled ? props.stats.isAllChecked : false, + onValueChange: props.handleSelectAll, + indeterminate: props.checked && props.checked.length > 0 && !props.stats.isAllChecked, + }; + } else if (props.tree.getRootIds().length === 0 && props.areCheckboxesVisible && isSelectAllEnabled) { + // Nothing loaded yet, but we guess that something is checkable. Add disabled checkbox for less flicker. + return { + value: false, + onValueChange: () => {}, + isDisabled: true, + indeterminate: props.checked?.length > 0, + }; + } + return null; + }, [ + props.tree, + props.areCheckboxesVisible, + props.checked, + props.stats, + isSelectAllEnabled, + props.handleSelectAll, + ]); + + return selectAll; +}