Rakesh Adoni
Rakesh Adoni

Reputation: 23

React bootstrap select dropdown

I am using ReactJS with BootStrap and is unable to get dropdown data that I am passing, in the event handler. The html is not built with option lists only input field is coming.

My code looks like:

import  React from 'react';
import PropTypes from 'prop-types';
import { FormWithConstraints, FieldFeedback } from 'react-form-with-constraints';
import { FieldFeedbacks, FormGroup, FormControlLabel, FormControlInput } from 'react-form-with-constraints-bootstrap4';

class Select extends  React.Component{

    handleChange(event) {
        //event.target.value
        //do required changes

    }

    render(){
        return(
                <FormGroup for={this.props.name}>
                    <FormControlLabel htmlFor={this.props.name} style={styles.lableStyle}>{this.props.label}{this.props.required?<b
                        style={{color: "#2c3d4d"}}>*</b>:null}</FormControlLabel>
                    <FormControlInput
                        id={"Source"}
                        label={this.props.label}
                        placeholder="Select Country"
                        type="text"
                        // value={this.props.value}
                        onChange={this.handleChange}
                            // (event)=>{
                            // this.handleChange(event);
                            // this.props.onChange(event);

                        // }}
                        options={this.props.dropDownList}
                        required={this.props.required}
                        className="form-control"
                        optionsKey="name"
                        optionsValue="id"
                        // readOnly={this.props.readOnly}
                        // onClick={this.props.onClick}
                    />
                    <FieldFeedbacks for={this.props.name} className="form-control-feedback"
                                    style={styles.errorStyle}>
                        <FieldFeedback when="valueMissing" />
                        {(this.props.required)?
                        <FieldFeedback when={value => /^ *$/.test(value)}>Please enter valid text</FieldFeedback>:null}
                    </FieldFeedbacks>
                </FormGroup>

        );
    }
}

and the drop data looks like

dropDownList=[ {name: "NAme",id:1,key:"Key"}];

Upvotes: 0

Views: 12756

Answers (2)

Hardik Kondhiya
Hardik Kondhiya

Reputation: 51

Any of these two possibilities are there.

Either you have to add an attribute as="select" to <FormControlInput> and remove type attribute, or Change type="text" to type="select"

Upvotes: 0

Bobz
Bobz

Reputation: 2614

Check the documentation of the widget you are using - FormControlInput https://github.com/tkrotoff/react-form-with-constraints ?

Usually onChange callback gets only the event as input, and you can get the value from the event. eg:

event.target.value

More info:

React: Forms https://react-bootstrap.github.io/components/forms/

React: Handling Events https://reactjs.org/docs/handling-events.html

Upvotes: 1

Related Questions