Lui*_*uiz 11 reactjs react-native react-native-android
我想让我的Picker在启动时显示"默认选项".这意味着:像"请选择一个选项"之类的东西.
我试图用这个短语手动添加一个选项,但是,这样在选择其他选项后可以重新选择"默认选项",就像它是真正的选项之一.有一些方法可以做到这一点?
<View style={Styles.row}>
<Picker
selectedValue={this.state.initialOption}
onValueChange={this.handleChangeOption}
prompt='Options'}
>
<Picker.Item label='Please select an option...' value='0' />
<Picker.Item label='option 1' value='1' />
<Picker.Item label='option 2' value='2' />
</Picker>
</View>
Run Code Online (Sandbox Code Playgroud)
Yo *_*ita 19
您可以为handleChangeOption添加一个条件,这样它只会在值不为0(Please select an option...
Picker)时设置状态.就像是:
handleChangeOption(val) {
if (val !== 0) {
this.setState({selectedValue: val});
}
}
...
<View style={Styles.row}>
<Picker
selectedValue={this.state.selectedValue}
onValueChange={this.handleChangeOption}
prompt='Options'}
>
<Picker.Item label='Please select an option...' value='0' />
<Picker.Item label='option 1' value='1' />
<Picker.Item label='option 2' value='2' />
</Picker>
</View>
Run Code Online (Sandbox Code Playgroud)
这是我刚刚在React Native中实现的方法:
InputPicker演示组件:
import React from 'react'
import { StyleSheet, View, Text, Picker } from 'react-native'
const InputPicker = ({
style,
hasError,
title,
enabled,
selectedValue,
onValueChange,
items,
firstItem
}) => {
if (!items || !Array.isArray(items)) return null
const { container, errorContainer, errorText, pickerTitle, pickerItem } = styles
return (
<View style={[container, (hasError) ? errorContainer : null, style]}>
<Text style={[pickerTitle, (hasError) ? errorText : null]}>
{title}
</Text>
<Picker
style={[pickerItem, (hasError) ? errorText : null]}
selectedValue={selectedValue}
onValueChange={onValueChange}
enabled={enabled}>
<Picker.Item key={'unselectable'} label={firstItem} value={0} />
{items.map((c) => <Picker.Item key={c.id} label={c.label} value={c.label} />)}
</Picker>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 0,
flexDirection: 'row',
alignItems: 'center'
},
pickerTitle: {
flex: 1,
textAlign: 'right'
},
pickerItem: {
flex: 2
},
errorContainer: {
backgroundColor: '#F8DEE0',
borderColor: '#DD0426',
borderWidth: 1
},
errorText: {
color: '#DD0426'
}
})
export { InputPicker }
Run Code Online (Sandbox Code Playgroud)
父容器视图(使用Formik表单处理程序)
<CardSection style={{ flex: 0, alignItems: 'center' }}>
<InputPicker
title="City"
enabled
hasError={touched.person_city && errors.person_city}
selectedValue={this.props.person_city}
onValueChange={(value) => {
this.props.setFieldValue('person_city', value)
this.props.updateField({ prop: 'person_city', value })
}}
items={this.getCities()}
firstItem="Select your city"
/>
</CardSection>
Run Code Online (Sandbox Code Playgroud)
父容器 this.getCities()
getCities() {
const cities = [
{ id: 0, label: 'Nanaimo' },
{ id: 1, label: 'Victoria' },
{ id: 2, label: 'Ladysmith' }
]
return cities
}
Run Code Online (Sandbox Code Playgroud)
之所以起作用,是因为如果选择了“选择您的城市”,则它将值设置为0,这使其成为一个错误的值,该值会触发touched.person_city && errors.person_city
并将其置于错误状态。
归档时间: |
|
查看次数: |
14663 次 |
最近记录: |