如何为Picker提供默认的"请选择..."选项?

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)

  • 这样,在第一次按下Picker时,"请选择一个选项"看起来就像是一个可选择的选项.我想在用户按下它之前显示它.像标题一样,不是选项本身. (3认同)
  • 谢谢.似乎react-native不提供确切的功能,当用户弹出Picker时,甚至不会显示"Please select and option".但是你的解决方案有助于我解决这个问题. (2认同)

agm*_*984 9

这是我刚刚在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并将其置于错误状态。