Skip to content

Commit bf9751d

Browse files
committed
feat: Support virtual prop
ref react-component/select#460
1 parent 9e973e5 commit bf9751d

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ see examples
8585
| onRightClick | select current treeNode and show customized contextmenu | function({event,node}) | - |
8686
| onSelect | click the treeNode to fire | function(selectedKeys, e:{selected: bool, selectedNodes, node, event, nativeEvent}) | - |
8787
| switcherIcon | specific the switcher icon. | ReactNode / (props: TreeNodeAttribute) => ReactNode | - |
88+
| virtual | Disable virtual scroll when `false` | boolean | - |
8889

8990
### TreeNode props
9091

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"prop-types": "^15.5.8",
6868
"rc-animate": "^2.9.2",
6969
"rc-util": "^4.11.0",
70-
"rc-virtual-list": "^1.0.0",
70+
"rc-virtual-list": "^1.1.0",
7171
"react-lifecycles-compat": "^3.0.4"
7272
}
7373
}

src/NodeList.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ interface NodeListProps {
7878
// Virtual list
7979
height: number;
8080
itemHeight: number;
81+
virtual?: boolean;
8182

8283
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
8384
onFocus?: React.FocusEventHandler<HTMLDivElement>;
@@ -120,7 +121,10 @@ function getAccessibilityPath(item: FlattenNode): string {
120121
return path;
121122
}
122123

123-
const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (props, ref) => {
124+
const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (
125+
props,
126+
ref,
127+
) => {
124128
const {
125129
prefixCls,
126130
data,
@@ -142,6 +146,7 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
142146

143147
height,
144148
itemHeight,
149+
virtual,
145150

146151
focusable,
147152
activeItem,
@@ -170,7 +175,9 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
170175
const [prevData, setPrevData] = React.useState(data);
171176
const [transitionData, setTransitionData] = React.useState(data);
172177
const [transitionRange, setTransitionRange] = React.useState([]);
173-
const [motionType, setMotionType] = React.useState<'show' | 'hide' | null>(null);
178+
const [motionType, setMotionType] = React.useState<'show' | 'hide' | null>(
179+
null,
180+
);
174181

175182
function onMotionEnd() {
176183
setPrevData(data);
@@ -188,7 +195,9 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
188195

189196
if (diffExpanded.key !== null) {
190197
if (diffExpanded.add) {
191-
const keyIndex = prevData.findIndex(({ data: { key } }) => key === diffExpanded.key);
198+
const keyIndex = prevData.findIndex(
199+
({ data: { key } }) => key === diffExpanded.key,
200+
);
192201

193202
if (motion) setDisableVirtual(true);
194203
const rangeNodes = getMinimumRangeTransitionRange(
@@ -204,7 +213,9 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
204213
setTransitionRange(rangeNodes);
205214
setMotionType('show');
206215
} else {
207-
const keyIndex = data.findIndex(({ data: { key } }) => key === diffExpanded.key);
216+
const keyIndex = data.findIndex(
217+
({ data: { key } }) => key === diffExpanded.key,
218+
);
208219

209220
if (motion) setDisableVirtual(true);
210221
const rangeNodes = getMinimumRangeTransitionRange(
@@ -276,6 +287,7 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
276287
itemKey={itemKey}
277288
height={height}
278289
fullHeight={false}
290+
virtual={virtual}
279291
itemHeight={itemHeight}
280292
onSkipRender={onMotionEnd}
281293
prefixCls={`${prefixCls}-list`}
@@ -291,7 +303,10 @@ const RefNodeList: React.RefForwardingComponent<NodeListRef, NodeListProps> = (p
291303
const mergedKey = getKey(key, pos);
292304
delete restProps.children;
293305

294-
const treeNodeProps = getTreeNodeProps(mergedKey, treeNodeRequiredProps);
306+
const treeNodeProps = getTreeNodeProps(
307+
mergedKey,
308+
treeNodeRequiredProps,
309+
);
295310

296311
return (
297312
<MotionTreeNode

src/Tree.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export interface TreeProps {
161161
// Virtual List
162162
height?: number;
163163
itemHeight?: number;
164+
virtual?: boolean;
164165
}
165166

166167
interface TreeState {
@@ -1140,6 +1141,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
11401141
filterTreeNode,
11411142
height,
11421143
itemHeight,
1144+
virtual,
11431145
} = this.props;
11441146
const domProps: React.HTMLAttributes<HTMLDivElement> = getDataAndAria(
11451147
this.props,
@@ -1198,6 +1200,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
11981200
dragging={dragging}
11991201
height={height}
12001202
itemHeight={itemHeight}
1203+
virtual={virtual}
12011204
focusable={focusable}
12021205
focused={focused}
12031206
tabIndex={tabIndex}

tests/Tree.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,4 +1429,20 @@ describe('Tree Basic', () => {
14291429
.simulate('click');
14301430
expect(wrapper.find('List').props().data).toHaveLength(2);
14311431
});
1432+
1433+
it('support virtual', () => {
1434+
const data = [];
1435+
for (let i = 0; i < 99; i += 1) {
1436+
data.push({
1437+
key: i,
1438+
title: i,
1439+
});
1440+
}
1441+
1442+
const wrapper = mount(
1443+
<Tree itemHeight={10} height={100} treeData={data} virtual={false} />,
1444+
);
1445+
1446+
expect(wrapper.find('List').props().virtual).toBe(false);
1447+
});
14321448
});

0 commit comments

Comments
 (0)