4

Here's the code:

class MenuContainerComponent extends Component {

    onInputWidgetMenuChange(event, data) {
        console.log(data);
    }

    render() {
        var inputWidgets = [];
        for (var i = 0; i < this.props.cdata.widgets.inputWidgets.length; i++) {
            var componentName = getComponentNameFromType(this.props.cdata.widgets.inputWidgets[i]);
            var key = "inputWidget" + i;
            inputWidgets.push(<Dropdown.Item key={key}>{componentName}</Dropdown.Item>);
        }

        return (
        <Dropdown style={childStyle} text='Input widgets' icon='keyboard' floating labeled button className='icon' onChange={this.onInputWidgetMenuChange}>
            <Dropdown.Menu>
                <Dropdown.Header icon='tags' content='Select a widget to add to canvas' />
                <Dropdown.Divider />
                {inputWidgets}
            </Dropdown.Menu>
        </Dropdown>
        )
}

I am trying to get an event on menu selection. 'onClick' is working in similar fashion but there is no event on menu selection.

1
  • Have you tried onChange={(e) => this.onInputWidgetMenuChange(e)} Commented Jan 14, 2018 at 18:36

2 Answers 2

8

AFAIK, since you're using Dropdown.Menu inside this Dropdown, the onChange won't work. It's for normal Drodowns (like selecting a value etc). Try creating a generic onClick and assign it to <Dropdown.Item />

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

4 Comments

Tried doing "displayWidgets.push(<Dropdown.Item key={key} onClick={this.onInputWidgetMenuChange}>{componentName}</Dropdown.Item>);". This also does not work.
What's the error? Doesn't onInputWidgetMenuChange get called? If not, check for this.
footnote: Use Array.map() instead of for. Less clutter :)
Yeah, this works. (solution is obvious after you read it :O) Should be the accepted answer
1

Giri's answer is correct. Change this line

inputWidgets.push(<Dropdown.Item key={key}>{componentName}</Dropdown.Item>);

to

inputWidgets.push(<Dropdown.Item key={key} value={componentId} onClick={this.onInputWidgetMenuChange}>{componentName}</Dropdown.Item>);

Where componentId is the actual value of the Dropdown.Item, (as opposed to the text displayed). Given the right circumstances componentId can be the same as the componentName too.

Another thing is that since you're using Dropdown.Menu inside the Dropdown, clicking the items on the menu won't automatically change the value of the Dropdown. (which is why the onChange of event the Dropdown component isn't fired). You need to save the current value of the Dropdown in the react state and manually set the trigger prop of Dropdown to make it look like the selected item.

Comments

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.