Skip to content

Commit d9289e9

Browse files
committed
chore: improve docs on custom tree items in relation to renaming
1 parent 867524c commit d9289e9

File tree

2 files changed

+62
-45
lines changed

2 files changed

+62
-45
lines changed

packages/core/src/stories/Theming.stories.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@ export const MinimalRenderers = () => (
4343
)
4444
) : null
4545
}
46-
renderItem={({ title, arrow, context, children }) => (
47-
<li {...context.itemContainerWithChildrenProps}>
48-
<button
49-
type="button"
50-
{...context.itemContainerWithoutChildrenProps}
51-
{...(context.interactiveElementProps as any)}
52-
>
53-
{arrow}
54-
{title}
55-
</button>
56-
{children}
57-
</li>
58-
)}
46+
renderItem={({ title, arrow, context, children }) => {
47+
const InteractiveComponent = context.isRenaming ? 'div' : 'button';
48+
return (
49+
<li {...context.itemContainerWithChildrenProps}>
50+
<InteractiveComponent
51+
type="button"
52+
{...context.itemContainerWithoutChildrenProps}
53+
{...(context.interactiveElementProps as any)}
54+
>
55+
{arrow}
56+
{title}
57+
</InteractiveComponent>
58+
{children}
59+
</li>
60+
);
61+
}}
5962
renderTreeContainer={({ children, containerProps }) => (
6063
<div {...containerProps}>{children}</div>
6164
)}

packages/docs/docs/guides/rendering.mdx

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,26 @@ function App() {
4444
renderItemArrow={({ item, context }) =>
4545
item.isFolder ? <span {...context.arrowProps}>{context.isExpanded ? 'v ' : '> '}</span> : null
4646
}
47-
renderItem={({ title, arrow, depth, context, children }) => (
48-
<li
49-
{...context.itemContainerWithChildrenProps}
50-
style={{
51-
margin: 0,
52-
display: 'flex',
53-
flexDirection: 'column',
54-
alignItems: 'flex-start',
55-
}}
56-
>
57-
<button {...context.itemContainerWithoutChildrenProps} {...context.interactiveElementProps}>
58-
{arrow}
59-
{title}
60-
</button>
61-
{children}
62-
</li>
63-
)}
47+
renderItem={({ title, arrow, depth, context, children }) => {
48+
const InteractiveComponent = context.isRenaming ? 'div' : 'button';
49+
return (
50+
<li
51+
{...context.itemContainerWithChildrenProps}
52+
style={{
53+
margin: 0,
54+
display: 'flex',
55+
flexDirection: 'column',
56+
alignItems: 'flex-start',
57+
}}
58+
>
59+
<InteractiveComponent {...context.itemContainerWithoutChildrenProps} {...context.interactiveElementProps}>
60+
{arrow}
61+
{title}
62+
</InteractiveComponent>
63+
{children}
64+
</li>
65+
);
66+
}}
6467
renderTreeContainer={({ children, containerProps }) => <div {...containerProps}>{children}</div>}
6568
renderItemsContainer={({ children, containerProps }) => <ul {...containerProps}>{children}</ul>}
6669
>
@@ -105,22 +108,25 @@ for example because you want to implement a virtualized list, you can do so by r
105108
item container:
106109

107110
```jsx
108-
renderItem={({ title, arrow, depth, context, children }) => (
109-
<>
110-
<li
111-
{...context.itemContainerWithChildrenProps}
112-
>
113-
<button
114-
{...context.itemContainerWithoutChildrenProps}
115-
{...context.interactiveElementProps}
111+
renderItem={({ title, arrow, depth, context, children }) => {
112+
const InteractiveComponent = context.isRenaming ? 'div' : 'button';
113+
return (
114+
<>
115+
<li
116+
{...context.itemContainerWithChildrenProps}
116117
>
117-
{ arrow }
118-
{ title }
119-
</button>
120-
</li>
121-
{children}
122-
</>
123-
)}
118+
<button
119+
{...context.itemContainerWithoutChildrenProps}
120+
{...context.interactiveElementProps}
121+
>
122+
{ arrow }
123+
{ title }
124+
</button>
125+
</li>
126+
{children}
127+
</>
128+
);
129+
}}
124130
```
125131

126132
Make sure to provide the props-objects `context.itemContainerWithoutChildrenProps` and
@@ -138,3 +144,11 @@ Note that, if you want to customize the way how mouse clicks interact with the t
138144
clicking on a parent node should expand it or just focus it) should not be changed by providing custom
139145
DOM hooks, but by choosing a different interaction mode.
140146
[Read more on interaction modes here](/docs/guides/interaction-modes).
147+
148+
:::caution
149+
150+
The `InteractiveComponent = context.isRenaming ? 'div' : 'button'` is important in case you want to
151+
support renaming items. If the tree item is always rendered as button, it's focusing behavior will
152+
cause a blur event when the rename starts, and revert the item back to the non-renaming state.
153+
154+
:::

0 commit comments

Comments
 (0)