Skip to content

Commit

Permalink
feat: introduce disable multiselect (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbach committed Jul 31, 2023
1 parent fa12f13 commit ba56fbc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions next-release-notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Features
- Introduce `disableMultiselect` property on uncontrolled tree environment (#279)

### Bug Fixes and Improvements
- Fixes a bug where a state property is updated when the component is unmounted (#278)
30 changes: 30 additions & 0 deletions packages/core/src/stories/BasicExamples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,33 @@ export const UnitTestTreeOpen = () => (
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
</UncontrolledTreeEnvironment>
);

export const DisableMultiselect = () => (
<UncontrolledTreeEnvironment<string>
canDragAndDrop
canDropOnFolder
canReorderItems
disableMultiselect
dataProvider={
new StaticTreeDataProvider(longTree.items, (item, data) => ({
...item,
data,
}))
}
getItemTitle={item => item.data}
viewState={{
'tree-1': {
expandedItems: [
'Fruit',
'Meals',
'America',
'Europe',
'Asia',
'Desserts',
],
},
}}
>
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
</UncontrolledTreeEnvironment>
);
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export interface UncontrolledTreeEnvironmentProps<
liveDescriptors?: LiveDescriptors;
getItemTitle: (item: TreeItem<T>) => string;
children: JSX.Element | (JSX.Element | null)[] | null;
disableMultiselect?: boolean;
}

export interface TreeConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ export const UncontrolledTreeEnvironment = React.forwardRef<
props.onCollapseItem?.(item, treeId);
}}
onSelectItems={(items, treeId) => {
amendViewState(treeId, old => ({ ...old, selectedItems: items }));
props.onSelectItems?.(items, treeId);
amendViewState(treeId, old => {
if (props.disableMultiselect) {
const newSelected = old.focusedItem ? [old.focusedItem] : [];
props.onSelectItems?.(newSelected, treeId);
return { ...old, selectedItems: newSelected };
}
props.onSelectItems?.(items, treeId);
return { ...old, selectedItems: items };
});
}}
onFocusItem={(item, treeId) => {
amendViewState(treeId, old => ({ ...old, focusedItem: item.index }));
Expand Down

0 comments on commit ba56fbc

Please sign in to comment.