Skip to content

Commit

Permalink
[useTree]: added useSelectAll hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Dec 12, 2023
1 parent 7dee098 commit 9adfad3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
5 changes: 2 additions & 3 deletions uui-core/src/data/processing/views/dataRows/useBuildRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { FoldingService } from '../tree/hooks/services';
import { NodeStats, getDefaultNodeStats, getRowStats, mergeStats } from './stats';

export interface UseBuildRowsProps<TItem, TId, TFilter = any> extends FoldingService<TItem, TId> {

tree: ITree<TItem, TId>;
dataSourceState: DataSourceState<TFilter, TId>;
isPartialLoad?: boolean;
Expand Down Expand Up @@ -37,7 +36,7 @@ export function useBuildRows<TItem, TId, TFilter = any>({
getRowProps,
getLoadingRowProps,
}: UseBuildRowsProps<TItem, TId, TFilter>) {
const rebuildRows = () => {
const buildRows = () => {
const rows: DataRowProps<TItem, TId>[] = [];
const pinned: Record<string, number> = {};
const pinnedByParentId: Record<string, number[]> = {};
Expand Down Expand Up @@ -150,5 +149,5 @@ export function useBuildRows<TItem, TId, TFilter = any>({
};
};

return useMemo(() => rebuildRows(), [tree]);
return useMemo(() => buildRows(), [tree]);
}
28 changes: 8 additions & 20 deletions uui-core/src/data/processing/views/dataRows/useDataRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TItem, TId, TFilter = any> extends FoldingService<TItem, TId>, CheckingService<TItem, TId>, FocusService, SelectingService<TItem, TId> {
tree: ITree<TItem, TId>;
Expand Down Expand Up @@ -85,26 +86,13 @@ export function useDataRows<TItem, TId, TFilter = any>(
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 {
Expand Down
44 changes: 44 additions & 0 deletions uui-core/src/data/processing/views/dataRows/useSelectAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useMemo } from 'react';
import { ITree } from '../tree';
import { NodeStats } from './stats';

export interface UseSelectAllProps<TItem, TId> {
tree: ITree<TItem, TId>;
selectAll?: boolean;
stats: NodeStats;
checked?: TId[];
areCheckboxesVisible: boolean;
handleSelectAll: (isChecked: boolean) => void;
}

export function useSelectAll<TItem, TId>(props: UseSelectAllProps<TItem, TId>) {
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;
}

0 comments on commit 9adfad3

Please sign in to comment.