Skip to content

Commit ba56fbc

Browse files
committed
feat: introduce disable multiselect (#279)
1 parent fa12f13 commit ba56fbc

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

next-release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### Features
2+
- Introduce `disableMultiselect` property on uncontrolled tree environment (#279)
23

34
### Bug Fixes and Improvements
45
- Fixes a bug where a state property is updated when the component is unmounted (#278)

packages/core/src/stories/BasicExamples.stories.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,33 @@ export const UnitTestTreeOpen = () => (
495495
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
496496
</UncontrolledTreeEnvironment>
497497
);
498+
499+
export const DisableMultiselect = () => (
500+
<UncontrolledTreeEnvironment<string>
501+
canDragAndDrop
502+
canDropOnFolder
503+
canReorderItems
504+
disableMultiselect
505+
dataProvider={
506+
new StaticTreeDataProvider(longTree.items, (item, data) => ({
507+
...item,
508+
data,
509+
}))
510+
}
511+
getItemTitle={item => item.data}
512+
viewState={{
513+
'tree-1': {
514+
expandedItems: [
515+
'Fruit',
516+
'Meals',
517+
'America',
518+
'Europe',
519+
'Asia',
520+
'Desserts',
521+
],
522+
},
523+
}}
524+
>
525+
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
526+
</UncontrolledTreeEnvironment>
527+
);

packages/core/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ export interface UncontrolledTreeEnvironmentProps<
345345
liveDescriptors?: LiveDescriptors;
346346
getItemTitle: (item: TreeItem<T>) => string;
347347
children: JSX.Element | (JSX.Element | null)[] | null;
348+
disableMultiselect?: boolean;
348349
}
349350

350351
export interface TreeConfiguration {

packages/core/src/uncontrolledEnvironment/UncontrolledTreeEnvironment.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ export const UncontrolledTreeEnvironment = React.forwardRef<
102102
props.onCollapseItem?.(item, treeId);
103103
}}
104104
onSelectItems={(items, treeId) => {
105-
amendViewState(treeId, old => ({ ...old, selectedItems: items }));
106-
props.onSelectItems?.(items, treeId);
105+
amendViewState(treeId, old => {
106+
if (props.disableMultiselect) {
107+
const newSelected = old.focusedItem ? [old.focusedItem] : [];
108+
props.onSelectItems?.(newSelected, treeId);
109+
return { ...old, selectedItems: newSelected };
110+
}
111+
props.onSelectItems?.(items, treeId);
112+
return { ...old, selectedItems: items };
113+
});
107114
}}
108115
onFocusItem={(item, treeId) => {
109116
amendViewState(treeId, old => ({ ...old, focusedItem: item.index }));

0 commit comments

Comments
 (0)