我设法通过自定义 onToggle 来使其工作,如果单击来自输入元素,则该下拉菜单不会执行任何操作。我基本上结束了这样的事情:
所以是这样的:
var React = require('react');
var ReactBootstrap = require('react-bootstrap'),
Dropdown = ReactBootstrap.Dropdown,
DropdownToggle = Dropdown.Toggle,
DropdownMenu = Dropdown.Menu,
Input = ReactBootstrap.Input,
MenuItem = ReactBootstrap.MenuItem;
module.exports = React.createClass({
displayName: 'DropdownWithInput',
setValue: function(e) {
var value = e.target.value;
this.setState({value: value});
},
onSelect: function(event, value) {
this.setState({value: value});
},
inputWasClicked: function() {
this._inputWasClicked = true;
},
onToggle: function(open) {
if (this._inputWasClicked) {
this._inputWasClicked = false;
return;
}
this.setState({open: open});
},
render: function() {
return (
<Dropdown id={this.props.id} open={this.state.open} onToggle={this.onToggle}
className="btn-group-xs btn-group-default">
<DropdownToggle bsStyle="default" bsSize="xs" className="dropdown-with-input dropdown-toggle">
Dropdown with input
</DropdownToggle>
<DropdownMenu>
<Input
type="text"
ref="inputElem"
autoComplete="off"
name={this.props.name}
placeholder="Type something here"
onSelect={this.inputWasClicked}
onChange={this.setValue}
/>
<MenuItem divider key={this.props.id + '-dropdown-input-divider'}/>
<MenuItem eventKey={1} onSelect={this.onSelect}>One</MenuItem>
<MenuItem eventKey={2} onSelect={this.onSelect}>Two</MenuItem>
<MenuItem eventKey={3} onSelect={this.onSelect}>Three</MenuItem>
</DropdownMenu>
</Dropdown>
);
}
});
希望这也适用于你。