-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(react-tree): deprecate flattenTree
- Loading branch information
1 parent
82cc27f
commit 26be764
Showing
6 changed files
with
109 additions
and
50 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-tree-9ce25cd9-37b5-4f07-8832-c69f0b2667b7.json
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,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "chore: deprecate flattenTree", | ||
"packageName": "@fluentui/react-tree", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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
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
84 changes: 84 additions & 0 deletions
84
...components/react-tree/stories/src/Tree/UseHeadlessFlatTreeWithNestedStructure.stories.tsx
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,84 @@ | ||
import * as React from 'react'; | ||
import { | ||
FlatTree, | ||
TreeItem, | ||
TreeItemLayout, | ||
useHeadlessFlatTree_unstable, | ||
HeadlessFlatTreeItemProps, | ||
TreeItemValue, | ||
} from '@fluentui/react-components'; | ||
|
||
type FlatTreeItem = HeadlessFlatTreeItemProps & { content: React.ReactNode }; | ||
type NestedTreeItem = FlatTreeItem & { subtree?: NestedTreeItem[] }; | ||
|
||
const flattenTree = (items: NestedTreeItem[], parentValue?: TreeItemValue | undefined): FlatTreeItem[] => | ||
items.flatMap<FlatTreeItem>(({ subtree, ...item }) => | ||
subtree ? [{ ...item, parentValue }, ...flattenTree(subtree, item.value)] : { parentValue, ...item }, | ||
); | ||
|
||
const flatTreeItems = flattenTree([ | ||
{ | ||
value: '1', | ||
content: <>level 1, item 1</>, | ||
subtree: [ | ||
{ | ||
value: '1-1', | ||
content: <>level 2, item 1</>, | ||
}, | ||
{ | ||
value: '1-2', | ||
content: <>level 2, item 2</>, | ||
}, | ||
{ | ||
value: '1-3', | ||
content: <>level 2, item 3</>, | ||
}, | ||
], | ||
}, | ||
{ | ||
value: '2', | ||
content: <>level 1, item 2</>, | ||
subtree: [ | ||
{ | ||
value: '2-1', | ||
content: <>level 2, item 1</>, | ||
subtree: [ | ||
{ | ||
value: '2-1-1', | ||
content: <>level 3, item 1</>, | ||
subtree: [ | ||
{ | ||
value: '2-1-1-1', | ||
content: <>level 4, item 1</>, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]); | ||
|
||
export const UseHeadlessFlatTreeWithNestedStructure = () => { | ||
const flatTree = useHeadlessFlatTree_unstable(flatTreeItems); | ||
return ( | ||
<FlatTree {...flatTree.getTreeProps()} aria-label="Flat Tree"> | ||
{Array.from(flatTree.items(), flatTreeItem => { | ||
const { content, ...treeItemProps } = flatTreeItem.getTreeItemProps(); | ||
return ( | ||
<TreeItem key={treeItemProps.value} {...treeItemProps}> | ||
<TreeItemLayout>{content}</TreeItemLayout> | ||
</TreeItem> | ||
); | ||
})} | ||
</FlatTree> | ||
); | ||
}; | ||
|
||
UseHeadlessFlatTreeWithNestedStructure.parameters = { | ||
docs: { | ||
description: { | ||
story: `If your data structure is nested, it can be converted to a flat structure before providing it to \`useHeadlessFlatTree\`. Here's an example using recursion and [\`Array.flatMap\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) to convert a nested array of items (nested by the property \`subtree\`) into a flat array of items:`, | ||
}, | ||
}, | ||
}; |
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