1

I have a Material UI dropdown component and a label. How to open this dropdown clicking the label?

<label>Dash style</label>
<DropDownMenu
    value={this.props.seriesOptions.dashStyle}
    onChange={this.handleDashChange} >
    <MenuItem key={1} value={"Solid"} label="Solid" primaryText="Solid" />
    <MenuItem key={2} value={"ShortDash"} label="ShortDash" primaryText="ShortDash" />
    <MenuItem key={3} value={"ShortDot"} label="ShortDot" primaryText="ShortDot" />
</DropDownMenu>
2
  • You're talking about the default behavior. Did you import react-tap-event-plugin? Commented May 16, 2017 at 9:30
  • @LeeHanKyeol, No. How can it help me? Is there a onClick attribute that I need to add to the label? Commented May 16, 2017 at 10:02

1 Answer 1

3

As I know, the DropDownMenu component don't have a property that let you control his state. However, you can use Popover with Menu component to do this.

You can write something like

        <label onClick={(event) => {
          this.setState({
            open: true,
            anchorEl: event.currentTarget,
          });
        }}>Dash style</label>
        <Popover
          open={this.state.open}
          anchorEl={this.state.anchorEl}
          anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
          targetOrigin={{horizontal: 'left', vertical: 'top'}}
          onRequestClose={this.handleRequestClose}
          animation={PopoverAnimationVertical}
        >
          <Menu
            value={this.state.dashStyle}
            onChange={this.handleDashChange.bind(this)}
          >
            <MenuItem key={1} value="Solid" primaryText="Solid"/>
            <MenuItem key={2} value="ShortDash" primaryText="ShortDash"/>
            <MenuItem key={3} value="ShortDot" primaryText="ShortDot"/>
          </Menu>
        </Popover>

with these two components.

Hope this help.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I used for now another workaround: absolutely positioned dropdown with a padding going over the label, so that the user has impression to click the label but actually it is the dropdown itself that is clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.