How to provide Picker a default “Please select…” option?

南楼画角 提交于 2020-01-01 02:45:28

问题


I'd like to make my Picker to show a "default option" on startup. That means: something like "Please select an option".

I've tried to add a option manually with this phrase, but, this way the "default option" can be re-selected after selecting other options, like it was one of the real options. There is some way to do this?

<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>

回答1:


You can add a conditional to your handleChangeOption so that it would only set state when the value is not 0 (the Please select an option... Picker). Something like:

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>



回答2:


Here is how I just achieved it in React Native:

InputPicker Presentational Component:

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 }

Parent Container View (using Formik form handler)

<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>

Parent Container this.getCities()

getCities() {
    const cities = [
        { id: 0, label: 'Nanaimo' },
        { id: 1, label: 'Victoria' },
        { id: 2, label: 'Ladysmith' }
    ]
    return cities
}

It works because it sets value to 0 if "Select your city" is picked, which makes it a falsy value which triggers touched.person_city && errors.person_city and puts it into an error state.




回答3:


just in the default class try to add default value that you wanna display .

exemple :

this.state = {

        type: "House",

    };

in your rander

<Picker
            selectedValue={this.state.type}

          >
            <Picker.Item label="House" value="House" />
            <Picker.Item label="Apartment" value="Apartment" />
            <Picker.Item label="Comercial Space" value="Comercial Space" />
          </Picker>


来源:https://stackoverflow.com/questions/42169272/how-to-provide-picker-a-default-please-select-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!