0
} else if (index === currentIndex) { // 当前级等于发生变化的层级, 选中项是传来的itemIndex
childIndex = itemIndex
} else { // 当前级大于发生变化的层级, 选中项应该置为默认0,因为下级的选项会随着上级的变化而变化
childIndex = 0
}
indexArr[index] = childIndex
if (arr[childIndex] && arr[childIndex].children) {
const nextIndex = index + 1
re(arr[childIndex].children, nextIndex)
}
}
}
re(options, 0)
this.setState(
{
indexs: indexArr // 重置所有选中项,重新渲染
},
() => {
this.props.onChange && this.props.onChange(indexArr)
}
)
}
renderItems () { // 拼装选择项组
const items = []
const { options = [], indexs = [] } = this.state
const re = (arr, index) => { // index为第几级
if (arr && arr.length > 0) {
const childIndex = indexs[index]
关键词:如何封装一个React Native多级联动(代码完成)