0 // 当前级指定选中第几项,默认为第一项
items.push({
defaultIndex: childIndex,
values: arr //当前级的选项列表
})
if (arr[childIndex] && arr[childIndex].children) {
const nextIndex = index + 1
re(arr[childIndex].children, nextIndex)
}
}
}
re(options, 0) // re为一个递归函数
return items.map((obj, index) => {
return ( // PickerItem为单个选择项,list为选项列表,defaultIndex为指定选择第几项,onChange选中选项改变时回调函数
<PickerItem
key={index.toString()}
list={obj.values}
defaultIndex={obj.defaultIndex}
onChange={(itemIndex) => { this.onChange(itemIndex, index)}}
/>
)
})
}
render() {
return (
<View
style={styles.box}>
<TouchableOpacity
onPress={this.close}
style={styles.bg}>
<TouchableOpacity
activeOpacity={1}
style={styles.dialogBox}>
<View style={styles.pickerBox}>
{this.renderItems()}
</View>
<View style={styles.btnBox}>
<TouchableOpacity
onPress={this.close}
style={styles.cancelBtn}>
<Text
numberOfLines={1}
ellipsizeMode={"tail"}
style={styles.cancelBtnText}>取消</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={this.ok}
style={styles.okBtn}>
<Text
numberOfLines={1}
ellipsizeMode={"tail"}
style={styles.okBtnText}>确认</Text>
</TouchableOpacity>
</View>
</TouchableOpacity>
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
box: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 9999,
},
bg: {
flex: 1,
backgroundColor: 'rgba(0,0,0,0.4)',
justifyContent: 'center',
alignItems: 'center'
},
dialogBox: {
width: 260,
flexDirection: "column",
backgroundColor: '#fff',
},
pickerBox: {
flexDirection: "row",
},
btnBox: {
flexDirection: "row",
height: 45,
},
cancelBtn: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
borderColor: '#4A90E2',
borderWidth: 1,
},
cancelBtnText: {
fontSize: 15,
color: '#4A90E2'
},
okBtn: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#4A90E2',
},
okBtnText: {
fontSize: 15,
color: '#fff'
},
})
export default Pickers
以上就是如何封装一个React Native多级联动(代码实现)的详细内容,更多请关注php中文网其它相关文章!
网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。
关键词:如何封装一个React Native多级联动(代码完成)