Skip to content

Commit 86208f2

Browse files
committed
docs: renaming, expandall and propmemo
1 parent 2e26332 commit 86208f2

File tree

6 files changed

+87
-8
lines changed

6 files changed

+87
-8
lines changed

packages/docs/docs/features/10-prop-memoization.mdx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,31 @@ hide_title: true
66
---
77

88
import { FeaturePageHeader } from "../../src/components/docs-page/feature-page-header";
9+
import {DemoBox} from "../../src/components/demo/demo-box";
910

1011
<FeaturePageHeader
1112
title="Prop Memoization"
1213
subtitle="Feature making all props created by HT consistent and useable for memoization"
1314
feature="prop-memoization"
1415
/>
1516

16-
TODO
17+
By default, React props generated by `tree.getContainerProps()` and `item.getProps()` will not
18+
be memoized, and might change their reference during each render as they are generated fresh
19+
each time.
20+
21+
If you rely on stable props, or need them to be memoized, you can simply include the `propMemoizationFeature`
22+
and the props will be memoized automatically. The feature doesn't require any configuration or expose any
23+
methods.
24+
25+
## Example
26+
27+
Consider this sample, where the render method of each item is intentionally slowed down, resulting
28+
in a slow usage experience when expanding or collapsing items:
29+
30+
<DemoBox tags={["feature/propmemoization"]} initialStory="react-guides-render-performance-slow-item-renderers--slow-item-renderers" />
31+
32+
Here, the render method is memoized and the propMemoizationFeature is included. Tree items still
33+
render slowly during the initial render, but items that are unchanged during rerender do not retrigger
34+
the slow render function, resulting in a faster experience when expanding or collapsing items:
35+
36+
<DemoBox tags={["feature/propmemoization"]} initialStory="react-guides-render-performance-memoized-slow-item-renderers--memoized-slow-item-renderers" />

packages/docs/docs/features/8-renaming.mdx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@ import { FeaturePageHeader } from "../../src/components/docs-page/feature-page-h
1414
feature="renaming"
1515
/>
1616

17-
<DemoBox tags={["feature/renaming"]} />
17+
<DemoBox tags={["feature/renaming"]} initialStory="react-renaming-basic--basic" />
1818

19-
TODO
19+
The renaming feature allows you to allow users to rename items in the tree. They can either start renaming
20+
an item via the `renameItem` hotkey (defaulting to F2), or when the `item.startRenaming()` method is called.
21+
You can hook this up to e.g. double-clicking an item-name or to a context menu option.
22+
23+
The feature exposes two state variables, `renamingItem` to keep track of the item that is currently being renamed,
24+
and `renamingValue` to keep track of the new name that the user is typing in. When
25+
[maintaining individual states](/guides/state#managing-individual-feature-states), you can hook into changes to
26+
those states via the `setRenamingItem` and `setRenamingValue` config methods.
27+
28+
## Rendering the Rename Input
29+
30+
Similar to other Headless Tree Features, the library only provides the functionality, but not the UI. For any tree
31+
item, you can determine whether it should render a renaming behavior with `item.isRenaming()`. Make sure to
32+
pass `item.getRenameInputProps()` to the input element to hook up the renaming behavior to the input field.
33+
34+
```jsx
35+
if (item.isRenaming()) {
36+
return (
37+
<input {...item.getRenameInputProps()} />
38+
);
39+
}
40+
41+
// otherwise, render the item as usual
42+
```
43+
44+
## Customizing which items can be renamed
45+
46+
You can also customize which items can be renamed by providing a `canRename` function in the tree configuration.
47+
48+
<DemoBox tags={["feature/renaming"]} initialStory="react-renaming-can-rename-configurability--can-rename-configurability" />

packages/docs/docs/features/9-expandall.mdx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,24 @@ import { FeaturePageHeader } from "../../src/components/docs-page/feature-page-h
1616

1717
<DemoBox tags={["feature/expand-all"]} />
1818

19-
TODO
19+
The expand-all feature exposes methods both on any item instance and the tree instance to expand or collapse
20+
all of its items. When called on the tree instance, it will expand or collapse all items in the tree, otherwise
21+
it will only affect all children of the item.
22+
23+
When used in conjunction with the [async data loader](/features/async-dataloader), the expand-all feature will
24+
wait for children to load in during each step, before triggering the expanding of the next level. You can also
25+
programmatically abort the expanding process at any time when this happens. When calling the `expandAll` method,
26+
you can pass an object with a `current` variable. Setting this to `false` during the expanding progress will
27+
cancel the expanding process while it is happening.
28+
29+
```ts
30+
const cancelToken = { current: false };
31+
32+
const expand = () => {
33+
cancelToken.current = false;
34+
tree.expandAll(cancelToken);
35+
};
36+
const cancelExpanding = () => {
37+
cancelToken.current = true;
38+
};
39+
```

packages/docs/docs/guides/0-state.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ category: guide
77

88
import { DemoBox } from "../../src/components/demo/demo-box";
99

10+
# Managing State
11+
1012
With Headless Tree, there are multiple ways how to manage the state of the tree. By default, Headless Tree
1113
manages all parts of the state itself, and you just need to provide the data. Alternatively, you can opt into
1214
managing the entirety of the tree state yourself, which is useful if you want to change the state from outside,
@@ -24,15 +26,15 @@ Another important part to keep in mind is that which parts of the state are expo
2426
are included. The aforementioned `dnd` state property will be completely ignored if the `dragAndDropFeature` is
2527
not included in the tree configuration, as will be dnd-related update handlers.
2628

27-
# Letting headless-tree manage the state
29+
## Letting headless-tree manage the state
2830

2931
The simplest option is to let Headless Tree manage the entirety of the state. This is done by default, so you
3032
do not have to do anything. You can also supply a `initialState` prop in the tree configuration to define
3133
the initial state.
3234

3335
<DemoBox tags={["guides/state"]} initialStory="react-state-internal-state--internal-state" />
3436

35-
# Managing individual feature states
37+
## Managing individual feature states
3638

3739
If you are interested in specific parts of the state, you can opt into managing those parts of the state while
3840
still leaving the rest up to Headless Tree. To manage one part of the state, you need to provide the sub property
@@ -76,7 +78,7 @@ useTree({
7678

7779
<DemoBox tags={["guides/state"]} initialStory="react-state-distinct-state-handlers--distinct-state-handlers" />
7880

79-
# Manage the entire state yourself
81+
## Manage the entire state yourself
8082

8183
As another alternative, you can also choose to manage the entirety of the state yourself. Instead of passing
8284
individual sub properties through the `state` config prop, you can pass the entire state object as that property.

packages/sb-react/src/guides/render-performance/memoized-slow-item-renderers.stories.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Meta } from "@storybook/react";
22
import React, { HTMLProps, forwardRef, memo } from "react";
33
import {
44
hotkeysCoreFeature,
5+
propMemoizationFeature,
56
selectionFeature,
67
syncDataLoaderFeature,
78
} from "@headless-tree/core";
@@ -11,6 +12,7 @@ import cx from "classnames";
1112

1213
const meta = {
1314
title: "React/Guides/Render Performance/Memoized Slow Item Renderers",
15+
tags: ["feature/propmemoization"],
1416
} satisfies Meta;
1517

1618
export default meta;
@@ -59,7 +61,12 @@ export const MemoizedSlowItemRenderers = () => {
5961
`${itemId}-2item`,
6062
],
6163
},
62-
features: [syncDataLoaderFeature, selectionFeature, hotkeysCoreFeature],
64+
features: [
65+
syncDataLoaderFeature,
66+
selectionFeature,
67+
hotkeysCoreFeature,
68+
propMemoizationFeature,
69+
],
6370
});
6471

6572
return (

packages/sb-react/src/guides/render-performance/slow-item-renderers.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import cx from "classnames";
1111

1212
const meta = {
1313
title: "React/Guides/Render Performance/Slow Item Renderers",
14+
tags: ["feature/propmemoization"],
1415
} satisfies Meta;
1516

1617
export default meta;

0 commit comments

Comments
 (0)