Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropdownButton, MenuItem from "react-bootstrap";

Here where to put the onSelect into the MenuItem or into the DropdownButton.

In this simple example when user select menuitem its not reflected into the dropdownbutton.

Is there any example on onSelect or onChange ?

<div className="select_option">
    <label htmlFor="type">Document Desc</label>
    <DropdownButton title="--Select One--" id="document-type">
        <MenuItem>Item 1</MenuItem>
        <MenuItem>Item 2</MenuItem>
        <MenuItem>Item 3</MenuItem>
        <MenuItem>Item 4</MenuItem>
    </DropdownButton>
</div>
like image 901
amitpowerpeace Avatar asked Mar 07 '23 06:03

amitpowerpeace


1 Answers

So you try to create a controlled select from a dropdown. You will need a options list and a state.

Here is an example:

const options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"];

class MySampleComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedOption: options[0] // default selected value
    };
  }

  handleSelect(eventKey, event) {
    this.setState({ selectedOption: options[eventKey] });
  }

  render() {
    <div className="select_option">
      <label htmlFor="type">Document Desc</label>
      <DropdownButton
        title={this.state.selectedOption}
        id="document-type"
        onSelect={this.handleSelect.bind(this)}
      >
        {options.map((opt, i) => (
          <MenuItem key={i} eventKey={i}>
            {opt}
          </MenuItem>
        ))}
      </DropdownButton>
    </div>;
  }
}
like image 83
Tomasz Mularczyk Avatar answered Mar 15 '23 18:03

Tomasz Mularczyk