Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

Passing data to custom picker

$
0
0

Currently I'm developing an app with a picker on it and I'm using picker component from react native but it doesn't work perfectly on iOS device so I found a custom picker using picker and modal it renders a modal on iOS only. Here is the code

class PickerWrapper extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            type_absen: '',
            modal: false,
        }
    }

    render() {
        let picker;

        let iosPickerModal = (
            <Modal isVisible={this.state.modal} hideModalContentWhileAnimating={true} backdropColor={color.white} backdropOpacity={0.9} animationIn="zoomInDown" animationOut="zoomOutUp" animationInTiming={200} animationOutTiming={200} onBackButtonPress={() => this.setState({ modal: false })} onBackdropPress={() => this.setState({ modal: false })} ><View style={{ backgroundColor: color.white, width: 0.9 * windowWidth(), height: 0.3 * windowHeight(), justifyContent: 'center' }}><Picker
                        selectedValue={this.state.type_absen}
                        onValueChange={(itemValue, itemIndex) => {
                            this.setState({ type_absen: itemValue });
                            this.setState({ modal: false });
                            setTimeout(() => this.props.onSelect(itemValue), 1200);
                        }}>
                        {this.props.items.map((item, key) => <Picker.Item label={item} value={item} key={key} />)}</Picker></View></Modal>);

        if (Platform.OS === 'ios')
            return (<View style={this.props.style}>
                    {iosPickerModal}<TouchableOpacity onPress={() => this.setState({ modal: true })}><View style={{ flexDirection: 'row', height: this.props.height ? this.props.height : normalize(40), width: this.props.width ? this.props.width : 0.68 * windowWidth(), borderWidth: 1, borderColor: color.blue, alignItems: 'center', borderRadius: 5 }}><Text style={{ fontSize: fontSize.regular, marginRight: 30 }}> {this.state.type_absen}</Text><IconWrapper name='md-arrow-dropdown' type='ionicon' color={color.light_grey} size={20} onPress={() => this.setState({ modal: true })} style={{ position: 'absolute', right: 10 }} /></View></TouchableOpacity></View >);
        else
            return (<View style={this.props.style} ><Picker
                        selectedValue={this.state.type_absen}
                        style={{ height: this.props.height ? this.props.height : normalize(20), width: this.props.width ? this.props.width : normalize(150) }}
                        onValueChange={(itemValue, itemIndex) => {
                            this.setState({ type_absen: itemValue });
                            this.props.onSelect(itemValue);
                        }}>
                        {this.props.items.map((item, key) => <Picker.Item label={item} value={item} key={key} />)}</Picker></View>);
    }
}

PickerWrapper.propTypes = {
    onSelect: PropTypes.func.isRequired
}

export default PickerWrapper;

I successfully load the data from api, but I still confused on how to get the value from it, and here is my old code using picker component

<Picker
 selectedValue={this.state.type_absen}
 style={{backgroundColor:'white'}}
 onValueChange={(val) => this.setState({ type_absen: val })}> 
   {
   this.props.schedules ?   this.props.schedules.map((item, key) => { 
     return <Picker.Item value={item.id} label {item.description} key={item.id} />})
 :
  []
   } </Picker>

and this is my new code using the PickerWrapper

export const mapStateToProps = state => ({
  token: state.authReducer.token,
  message: state.authReducer.message,
  schedules: state.authReducer.schedules
});

export const mapDispatchToProps = (dispatch) => ({
  actionsAuth: bindActionCreators(authAction, dispatch)
});

class Change extends Component {
    
  constructor(){
        super();
        this.state={
            type_absen: [],
        }
    }
    
    onPickerValueChange=(value, index)=>{
    this.setState({type_absen: value}, 
      () => {
        Alert.alert("type_absen", this.state.type_absen);
      }
    );
  }
  render() {
      return (<View style={styles.container}><View style={styles.container}><View style={styles.innerContainer}>             <PickerWrapper items={this.props.schedules.map((item) => item.description )} onSelect={this.onPickerValueChange} /></View>  </View></View>
      );
  }
}
 }

what I'm trying to do is get the item.id. How do I do that in my PickerWrapper component?


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>