This commit is contained in:
HuaYu 2023-01-19 07:40:02 +08:00
parent f47616a7b8
commit 7a09798e5b
1 changed files with 25 additions and 9 deletions

View File

@ -71,14 +71,20 @@ const back = () => { }
// ); // );
// } // }
export default class CityList extends React.Component { export default class CityList extends React.Component {
state = {
constructor(props){
super(props)
this.state = {
cityList: {}, cityList: {},
cityIndex: [], cityIndex: [],
activeIndex: 0 activeIndex: 0
} }
componentDidMount() { const cityListComponent = React.createRef()
this.getCityList() }
console.info("componentDidMount") async componentDidMount() {
await this.getCityList()
//提前计算list高度
this.cityListComponent.current.measureAllRows()
} }
async getCityList() { async getCityList() {
const res = await axios.get('http://localhost:8080/area/city?level=1') const res = await axios.get('http://localhost:8080/area/city?level=1')
@ -134,11 +140,19 @@ export default class CityList extends React.Component {
renderCityIndex() { renderCityIndex() {
const { cityIndex, activeIndex } = this.state const { cityIndex, activeIndex } = this.state
return cityIndex.map((item, index) => { return cityIndex.map((item, index) => {
<li className="city-index-item" key={item}> <li className="city-index-item" key={item} onClick={()=>{this.cityListComponent.current.scrollToRow(index)}}>
<span className={activeIndex === index ? "index-active" : ''}>{item === 'hot' ? '热' : item.toUpperCase()}</span> <span className={activeIndex === index ? "index-active" : ''}>{item === 'hot' ? '热' : item.toUpperCase()}</span>
</li> </li>
}) })
} }
// 用户获取list组件渲染行数据
onRowsRendered=({startIndex})=>{
if(this.state.activeIndex!=startIndex){
this.setState({
activeIndex:startIndex
})
}
}
render() { render() {
return <div className="citylist"><NavBar className="citylist-navbar" back='返回' onBack={back}>城市选择</NavBar> return <div className="citylist"><NavBar className="citylist-navbar" back='返回' onBack={back}>城市选择</NavBar>
@ -147,10 +161,12 @@ export default class CityList extends React.Component {
<List <List
width={width} width={width}
height={height} height={height}
ref={this.cityListComponent}
rowCount={this.state.cityIndex.length} rowCount={this.state.cityIndex.length}
rowHeight={this.getRowHeight} rowHeight={this.getRowHeight}
rowRenderer={this.rowRenderer} rowRenderer={this.rowRenderer}
/> onRowsRendered={this.onRowsRendered}
scrollToAlignment="start" />
)} )}
</AutoSizer> </AutoSizer>
<ul className="city-index">{this.renderCityIndex()}</ul> <ul className="city-index">{this.renderCityIndex()}</ul>