-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Tree): support virtual scroll (#1746)
Co-authored-by: jerryyxu <jerryyxu@IT-C02ZQ0R7LVDL.local>
- Loading branch information
Showing
11 changed files
with
763 additions
and
341 deletions.
There are no files selected for viewing
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
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,99 @@ | ||
# 虚拟滚动 | ||
|
||
- order: 10 | ||
|
||
当树的节点数比较多的时候,设置虚拟滚动提高性能,注意设置高度,且允许滚动。 | ||
|
||
:::lang=en-us | ||
# Virtual Tree | ||
|
||
- order: 10 | ||
|
||
Setting useVirtual property to improve performance when there are many nodes in the tree,pay attention to set height and allow to scroll. | ||
|
||
::: | ||
|
||
--- | ||
|
||
````jsx | ||
import { Tree } from '@alifd/next'; | ||
|
||
function createDataSource(level=3, count=5) { | ||
const dataSource = []; | ||
let num = 0; | ||
|
||
const drill = (children, _level, _count) => { | ||
children.forEach((child, i) => { | ||
child.children = new Array(_count).fill(null).map((item, k) => { | ||
const key = `${child.key}-${k}`; | ||
num++; | ||
return { | ||
key, | ||
label: key, | ||
}; | ||
}); | ||
_level > 0 && drill(child.children, _level - 1, count); | ||
}); | ||
}; | ||
|
||
dataSource.push({ | ||
label: '0-0', | ||
key: '0-0', | ||
}); | ||
drill(dataSource, level, count); | ||
console.log('node num:', num + 1); | ||
return dataSource; | ||
} | ||
|
||
class Demo extends React.Component { | ||
constructor() { | ||
super(); | ||
|
||
this.state = { | ||
dataSource: [], | ||
}; | ||
} | ||
|
||
onSelect(keys, info) { | ||
console.log('onSelect', keys, info); | ||
} | ||
|
||
onCheck(keys, info) { | ||
console.log('onCheck', keys, info); | ||
} | ||
|
||
onEditFinish(key, label, node) { | ||
console.log('onEditFinish', key, label, node); | ||
} | ||
|
||
onRightClick(info) { | ||
console.log('onRightClick', info); | ||
} | ||
|
||
componentDidMount() { | ||
this.setState({ | ||
dataSource: createDataSource(), | ||
}); | ||
} | ||
|
||
render() { | ||
const dataSource = this.state.dataSource; | ||
|
||
return ( | ||
dataSource.length && <Tree checkable editable focusable showLine | ||
useVirtual | ||
animation | ||
style={{ maxHeight: '480px', overflow: 'auto' }} | ||
defaultExpandAll | ||
defaultCheckedKeys={['0-0-1', '0-0-2']} | ||
onSelect={this.onSelect} | ||
onCheck={this.onCheck} | ||
dataSource={dataSource} | ||
onEditFinish={this.onEditFinish} | ||
onRightClick={this.onRightClick} /> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render(<Demo />, mountNode); | ||
```` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
|
||
export default class TreeNodeIndent extends Component { | ||
static propTypes = { | ||
prefix: PropTypes.string, | ||
level: PropTypes.number, | ||
isLastChild: PropTypes.arrayOf(PropTypes.bool), | ||
showLine: PropTypes.bool, | ||
}; | ||
|
||
static defaultProps = { | ||
isLastChild: [], | ||
}; | ||
|
||
render() { | ||
const { prefix, level, isLastChild, showLine } = this.props; | ||
const indents = []; | ||
|
||
for (let i = 1; i < level; i++) { | ||
const classNames = cx(`${prefix}tree-node-indent-unit`, { | ||
[`${prefix}line`]: !isLastChild[i - 1] && showLine, | ||
}); | ||
|
||
indents.push(<span className={classNames} key={i - 1} />); | ||
} | ||
|
||
return indents; | ||
} | ||
} |
Oops, something went wrong.