-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(VirtualList): add API jumpIndex
- Loading branch information
Showing
5 changed files
with
157 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# 设置初始位置 | ||
|
||
- order: 0 | ||
|
||
使用 jumpIndex 设置初始位置 | ||
|
||
:::lang=en-us | ||
# Basic | ||
|
||
- order: 0 | ||
|
||
Use jumpIndex to set first item. | ||
|
||
::: | ||
|
||
--- | ||
|
||
````jsx | ||
import { VirtualList } from '@alifd/next'; | ||
|
||
const dataSource = []; | ||
|
||
function generateLi(index) { | ||
if (index % 3 === 0) { | ||
return (<li key={`key-${index}`} style={{lineHeight: '30px', background: '#5f83ff', color: '#fff'}}>key-{index}</li>); | ||
} else { | ||
return (<li key={`key-${index}`} style={{lineHeight: '20px'}}>key-{index}</li>); | ||
} | ||
} | ||
function generateData(len) { | ||
for (let i = 0; i < len; i++) { | ||
dataSource.push(generateLi(i)); | ||
} | ||
} | ||
|
||
class App extends React.Component { | ||
state = { | ||
initial: 20, | ||
dataSource: generateData(1000) | ||
} | ||
componentDidMount() { | ||
setTimeout(()=> { | ||
const instance = this.refs.virtual.getInstance(); | ||
instance.scrollTo(50); | ||
}, 200); | ||
|
||
} | ||
|
||
getHeight(index) { | ||
return index % 3 === 0 ? 30 : 20; | ||
} | ||
onClick() { | ||
this.setState({ | ||
initial: this.state.initial + 20 | ||
}) | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
<button onClick={this.onClick.bind(this)}>jump to {this.state.initial + 20}</button> | ||
<br/> | ||
<br/> | ||
<div className={'virtual-box'}> | ||
<VirtualList ref="virtual" jumpIndex={this.state.initial} itemSizeGetter={this.getHeight.bind(this)}> | ||
{dataSource} | ||
</VirtualList> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render(<App />, mountNode); | ||
```` | ||
|
||
````css | ||
.virtual-box { | ||
height: 200px; | ||
width: 200px; | ||
border: 1px solid #ddd; | ||
overflow: auto; | ||
} | ||
.virtual-box ul { | ||
padding: 0; | ||
margin: 0; | ||
list-style: none; | ||
} | ||
.virtual-box li { | ||
padding-left: 10px; | ||
border-bottom: 1px solid #333; | ||
} | ||
```` |
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