React semantic-ui Dropdown onChange not working

前端 未结 2 434
忘了有多久
忘了有多久 2021-01-20 22:26

Here\'s the code:

class MenuContainerComponent extends Component {

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

    render()         


        
相关标签:
2条回答
  • 2021-01-20 22:56

    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 />

    0 讨论(0)
  • 2021-01-20 23:02

    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.

    0 讨论(0)
提交回复
热议问题