-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dee098
commit 9adfad3
Showing
3 changed files
with
54 additions
and
23 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
44 changes: 44 additions & 0 deletions
44
uui-core/src/data/processing/views/dataRows/useSelectAll.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 |
---|---|---|
@@ -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; | ||
} |