Skip to content

Commit bcc25cc

Browse files
committed
feat: replace toggle -> setOpen
1 parent 68bb969 commit bcc25cc

13 files changed

+125
-63
lines changed

README.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ function* treeWalker() {
105105

106106
// Node component receives all the data we created in the `treeWalker` +
107107
// internal openness state (`isOpen`), function to change internal openness
108-
// state (`toggle`) and `style` parameter that should be added to the root div.
109-
const Node = ({data: {isLeaf, name}, isOpen, style, toggle}) => (
108+
// state (`setOpen`) and `style` parameter that should be added to the root div.
109+
const Node = ({data: {isLeaf, name}, isOpen, style, setOpen}) => (
110110
<div style={style}>
111111
{!isLeaf && (
112-
<button type="button" onClick={toggle}>
112+
<button type="button" onClick={() => setOpen(!isOpen)}>
113113
{isOpen ? '-' : '+'}
114114
</button>
115115
)}
@@ -297,7 +297,7 @@ tree.recomputeTree({
297297
- `FixedSizeNodePublicState<TData extends FixedSizeNodeData>` - the node state available for the `Node` component and `recomputeTree`'s `subtreeCallback` function. It has the following shape:
298298
- `data: FixedSizeNodeData`.
299299
- `isOpen: boolean` - a current openness status of the node.
300-
- `toggle: function` - a function to change the openness state of the node. It receives no arguments and can be provided directly as an `onClick` handler.
300+
- `setOpen(state: boolean): function` - a function to change the openness state of the node. It receives the new openness state as a `boolean` and opens/closes the node accordingly.
301301
- `FixedSizeTreeProps<TData extends FixedSizeNodeData>` - props that `FixedSizeTree` component receives. Described in the [Props](#props) section.
302302
- `FixedSizeTreeState<TData extends FixedSizeNodeData>` - state that `FixedSizeTree` component has.
303303

@@ -373,10 +373,10 @@ function* treeWalker() {
373373
}
374374

375375
// Node component receives current node height as a prop
376-
const Node = ({data: {isLeaf, name}, height, isOpen, style, toggle}) => (
376+
const Node = ({data: {isLeaf, name}, height, isOpen, style, setOpen}) => (
377377
<div style={style}>
378378
{!isLeaf && (
379-
<button type="button" onClick={toggle}>
379+
<button type="button" onClick={() => setOpen(!isOpen)}>
380380
{isOpen ? '-' : '+'}
381381
</button>
382382
)}
@@ -493,8 +493,7 @@ The `treeWalker` was and is the heart of the `react-vtree`. However, now it look
493493
Old `treeWalker` worked for both initial tree building and changing node openness state:
494494

495495
```js
496-
function* treeWalker( refresh
497-
) {
496+
function* treeWalker(refresh) {
498497
const stack = [];
499498

500499
stack.push({
@@ -586,6 +585,7 @@ The `recomputeTree` method now receives a list of nodes to change (previously, i
586585
The most important change is the introduction of the `subtreeCallback`. It is a function that will be applied to each node in the subtree of the specified node. Among other useful things it also allows imitating the behavior of old `useDefaultOpenness` and `useDefaultHeight` options.
587586

588587
Old `recomputeTree`:
588+
589589
```js
590590
treeInstance.recomputeTree({
591591
opennessState: {
@@ -594,7 +594,7 @@ treeInstance.recomputeTree({
594594
'node-3': false,
595595
},
596596
refreshNodes: true,
597-
useDefaultOpenness: false
597+
useDefaultOpenness: false,
598598
});
599599
```
600600

@@ -609,8 +609,42 @@ treeInstance.recomputeTree({
609609
if (node !== ownerNode) {
610610
node.isOpen = false;
611611
}
612-
}
612+
},
613613
},
614614
'node-3': false,
615615
});
616+
```
617+
618+
### 4. Migrate all your `toggle()` calls to `setOpen(boolean)`
619+
620+
In the `3.x.x` version node provides a `setOpen` function instead of `toggle` that allows more fine-grained control over the openness state.
621+
622+
Old `toggle`:
623+
624+
```javascript
625+
const Node = ({data: {isLeaf, name}, isOpen, style, toggle}) => (
626+
<div style={style}>
627+
{!isLeaf && (
628+
<div>
629+
<button onClick={toggle}>{isOpen ? '-' : '+'}</button>
630+
</div>
631+
)}
632+
<div>{name}</div>
633+
</div>
634+
);
635+
```
636+
637+
New `setOpen`:
638+
```javascript
639+
const Node = ({data: {isLeaf, name}, isOpen, style, setOpen}) => (
640+
<div style={style}>
641+
{!isLeaf && (
642+
<div>
643+
// Imitating the old `toggle` function behavior
644+
<button onClick={() => setOpen(!isOpen)}>{isOpen ? '-' : '+'}</button>
645+
</div>
646+
)}
647+
<div>{name}</div>
648+
</div>
649+
);
616650
```

__stories__/AsyncData.story.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const Node: FC<NodeComponentProps<
9797
data: {download, downloaded, isLeaf, name, nestingLevel},
9898
isOpen,
9999
style,
100-
toggle,
100+
setOpen,
101101
}) => {
102102
const [isLoading, setLoading] = useState(false);
103103

@@ -118,10 +118,10 @@ const Node: FC<NodeComponentProps<
118118
if (!downloaded) {
119119
setLoading(true);
120120
await download();
121-
await toggle();
121+
await setOpen(!isOpen);
122122
setLoading(false);
123123
} else {
124-
await toggle();
124+
await setOpen(!isOpen);
125125
}
126126
}}
127127
style={defaultButtonStyle}

__stories__/AsyncDataIdle.story.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const Node: FC<NodeComponentProps<
118118
data: {download, downloaded, isLeaf, name, nestingLevel},
119119
isOpen,
120120
style,
121-
toggle,
121+
setOpen,
122122
}) => {
123123
const [isLoading, setLoading] = useState(false);
124124
const createBuildingPromise = useBuildingPromise([download]);
@@ -143,12 +143,12 @@ const Node: FC<NodeComponentProps<
143143
setLoading(true);
144144
await Promise.all([
145145
download(),
146-
toggle(),
146+
setOpen(!isOpen),
147147
createBuildingPromise(),
148148
]);
149149
setLoading(false);
150150
} else {
151-
await toggle();
151+
await setOpen(!isOpen);
152152
}
153153
}
154154
: undefined

__stories__/BigData.story.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const Node: FC<NodeComponentProps<
7373
isOpen,
7474
resize,
7575
style,
76-
toggle,
76+
setOpen,
7777
treeData: itemSize,
7878
}) => {
7979
const canOpen = height <= itemSize;
@@ -96,7 +96,11 @@ const Node: FC<NodeComponentProps<
9696
>
9797
{!isLeaf && (
9898
<div>
99-
<button type="button" onClick={toggle} style={defaultButtonStyle}>
99+
<button
100+
type="button"
101+
onClick={() => setOpen(!isOpen)}
102+
style={defaultButtonStyle}
103+
>
100104
{isOpen ? '-' : '+'}
101105
</button>
102106
</div>

__stories__/FixedSizeTree.story.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function* treeWalker(): ReturnType<TreeWalker<TreeData, NodeMeta>> {
9999
const Node: FC<NodeComponentProps<
100100
TreeData,
101101
FixedSizeNodePublicState<TreeData>
102-
>> = ({data: {isLeaf, name, nestingLevel}, isOpen, style, toggle}) => (
102+
>> = ({data: {isLeaf, name, nestingLevel}, isOpen, style, setOpen}) => (
103103
<div
104104
style={{
105105
...style,
@@ -110,7 +110,11 @@ const Node: FC<NodeComponentProps<
110110
>
111111
{!isLeaf && (
112112
<div>
113-
<button type="button" onClick={toggle} style={defaultButtonStyle}>
113+
<button
114+
type="button"
115+
onClick={() => setOpen(!isOpen)}
116+
style={defaultButtonStyle}
117+
>
114118
{isOpen ? '-' : '+'}
115119
</button>
116120
</div>

__stories__/MultipleRoots.story.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function* treeWalker(): ReturnType<TreeWalker<TreeData, NodeMeta>> {
102102
const Node: FC<NodeComponentProps<
103103
TreeData,
104104
FixedSizeNodePublicState<TreeData>
105-
>> = ({data: {isLeaf, name, nestingLevel}, isOpen, style, toggle}) => (
105+
>> = ({data: {isLeaf, name, nestingLevel}, isOpen, style, setOpen}) => (
106106
<div
107107
style={{
108108
...style,
@@ -113,7 +113,11 @@ const Node: FC<NodeComponentProps<
113113
>
114114
{!isLeaf && (
115115
<div>
116-
<button type="button" onClick={toggle} style={defaultButtonStyle}>
116+
<button
117+
type="button"
118+
onClick={() => setOpen(!isOpen)}
119+
style={defaultButtonStyle}
120+
>
117121
{isOpen ? '-' : '+'}
118122
</button>
119123
</div>

__stories__/VariableSizeTree.story.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const Node: FC<NodeComponentProps<
7373
isOpen,
7474
resize,
7575
style,
76-
toggle,
76+
setOpen,
7777
treeData: itemSize,
7878
}) => {
7979
const canOpen = height <= itemSize;
@@ -96,7 +96,11 @@ const Node: FC<NodeComponentProps<
9696
>
9797
{!isLeaf && (
9898
<div>
99-
<button type="button" onClick={toggle} style={defaultButtonStyle}>
99+
<button
100+
type="button"
101+
onClick={() => setOpen(!isOpen)}
102+
style={defaultButtonStyle}
103+
>
100104
{isOpen ? '-' : '+'}
101105
</button>
102106
</div>

__tests__/FixedSizeTree.spec.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('FixedSizeTree', () => {
138138
nestingLevel: 0,
139139
},
140140
isOpen: true,
141-
toggle: expect.any(Function),
141+
setOpen: expect.any(Function),
142142
},
143143
{
144144
data: {
@@ -148,7 +148,7 @@ describe('FixedSizeTree', () => {
148148
nestingLevel: 1,
149149
},
150150
isOpen: true,
151-
toggle: expect.any(Function),
151+
setOpen: expect.any(Function),
152152
},
153153
{
154154
data: {
@@ -158,7 +158,7 @@ describe('FixedSizeTree', () => {
158158
nestingLevel: 2,
159159
},
160160
isOpen: true,
161-
toggle: expect.any(Function),
161+
setOpen: expect.any(Function),
162162
},
163163
{
164164
data: {
@@ -168,7 +168,7 @@ describe('FixedSizeTree', () => {
168168
nestingLevel: 2,
169169
},
170170
isOpen: true,
171-
toggle: expect.any(Function),
171+
setOpen: expect.any(Function),
172172
},
173173
{
174174
data: {
@@ -178,7 +178,7 @@ describe('FixedSizeTree', () => {
178178
nestingLevel: 1,
179179
},
180180
isOpen: true,
181-
toggle: expect.any(Function),
181+
setOpen: expect.any(Function),
182182
},
183183
{
184184
data: {
@@ -188,7 +188,7 @@ describe('FixedSizeTree', () => {
188188
nestingLevel: 2,
189189
},
190190
isOpen: true,
191-
toggle: expect.any(Function),
191+
setOpen: expect.any(Function),
192192
},
193193
{
194194
data: {
@@ -198,7 +198,7 @@ describe('FixedSizeTree', () => {
198198
nestingLevel: 2,
199199
},
200200
isOpen: true,
201-
toggle: expect.any(Function),
201+
setOpen: expect.any(Function),
202202
},
203203
]);
204204
});
@@ -245,11 +245,11 @@ describe('FixedSizeTree', () => {
245245
});
246246

247247
it('allows preserving previous state on the new tree building', async () => {
248-
const [, {toggle}]: ReadonlyArray<FixedSizeNodePublicState<
248+
const [, {setOpen}]: ReadonlyArray<FixedSizeNodePublicState<
249249
ExtendedData
250250
>> = extractReceivedRecords(component.find(FixedSizeList));
251251

252-
await toggle();
252+
await setOpen(false);
253253
component.update();
254254

255255
expect(
@@ -384,7 +384,7 @@ describe('FixedSizeTree', () => {
384384
nestingLevel: 0,
385385
},
386386
isOpen: false,
387-
toggle: expect.any(Function),
387+
setOpen: expect.any(Function),
388388
},
389389
]);
390390
});
@@ -533,17 +533,23 @@ describe('FixedSizeTree', () => {
533533
});
534534
});
535535

536-
it('provides a toggle function that changes openness state of the specific node', async () => {
537-
const [{toggle}] = extractReceivedRecords(component.find(FixedSizeList));
536+
it('provides a setOpen function that changes openness state of the specific node', async () => {
537+
const [{setOpen}] = extractReceivedRecords(component.find(FixedSizeList));
538538

539-
await toggle();
539+
await setOpen(false);
540540
component.update(); // Update the wrapper to get the latest changes
541541

542-
const list = component.find(FixedSizeList);
542+
let list = component.find(FixedSizeList);
543543
expect(list.prop('itemCount')).toBe(1);
544544
expect(extractReceivedRecords(list).map(({data: {id}}) => id)).toEqual([
545545
'foo-1',
546546
]);
547+
548+
await setOpen(true);
549+
component.update(); // Update the wrapper to get the latest changes
550+
551+
list = component.find(FixedSizeList);
552+
expect(list.prop('itemCount')).toBe(7);
547553
});
548554
});
549555
});

0 commit comments

Comments
 (0)