【问题标题】:Dropdown with input field带有输入字段的下拉菜单
【发布时间】:2016-11-28 08:36:36
【问题描述】:

我正在使用react-bootstrapbootstrap 元素添加到我的应用程序中。我需要添加一个下拉按钮,其中包含用户可以用来标记其项目的“标签”列表。

我的问题是添加一个输入字段,以便他们可以添加“自定义”标签。这是它目前的样子(蓝色方块是复选框):

首先我尝试过:

<Dropdown id="myDropdown">
    <Dropdown.Toggle bsStyle="warning">Label as...</Dropdown.Toggle>
    <Dropdown.Menu>
        <li onSelect={...}>
            <Checkbox onClick={...} checked={...} inline>Some label</Checkbox>
        </li>
        <li onSelect={...}>
            <Checkbox onClick={...} checked={...} inline>Some other label</Checkbox>
        </li>
        <li><input /></li>
    </Dropdown.Menu>
</Dropdown>

但是,当您单击 input 元素时,这会关闭 dropdown

因此,我尝试对官方文档中显示的示例进行轻微改动(搜索“Custom Dropdown Components”并查看代码)。这允许我单击并输入input,但现在当我在元素外部单击时下拉菜单不会关闭。


TL;DR

如何实现允许我单击input 字段的下拉菜单,当您在菜单外单击时该字段也会关闭?

编辑:

为我的第二个实现添加生成的 html:

<div class="dropdown btn-group">
  <button type="button" class="dropdown-toggle btn btn-warning"><i class="fa fa-check" aria-hidden="true"></i><!-- react-text: 153 --> <!-- /react-text --><!-- react-text: 154 -->Label as...<!-- /react-text --><!-- react-text: 155 --> <!-- /react-text --><span class="caret"></span></button>
   <ul class="dropdown-menu">
     <li><label class="checkbox-inline"><input type="checkbox" value="on"><span></span><!-- react-text: 162 --> Some label<!-- /react-text --></label></li>
     <li><label class="checkbox-inline"><input type="checkbox" value="on"><span></span><!-- react-text: 167 --> Some other label<!-- /react-text --></label></li>
     <li><label class="checkbox-inline"><input type="checkbox" value="on"><span></span><input type="text" value="" style="height: 17px;font-size: .9em;"></label></li>
     <li role="separator" class="divider"></li>
     <li role="presentation" class=""><a role="menuitem" tabindex="-1" href="#">Clear labels</a></li>
   </ul>
 </div>

【问题讨论】:

  • 你能把编译后的 HTML 粘贴进去吗?
  • @JoshuaTerrill,当然。我尝试了两种实现中的哪一种?
  • 第二个,您可以在其中单击并输入输入,但下拉菜单不会关闭。
  • @JoshuaTerrill,是的,刚刚添加了它。 :)
  • 我对 bootstrap 的常规版本(不是反应版本)做了一个改动,一旦我添加了 data-toggle 属性,它就起作用了。 jsfiddle.net/v7gk7e76 不确定反应版本。如果您可以在 fiddle 上运行某种工作的 sn-p 或运行 react 的 plunkr,我可以尝试提供更多帮助。

标签: javascript twitter-bootstrap reactjs react-bootstrap


【解决方案1】:

所以我几天前找到了答案,我会在这里发布,以防将来有人偶然发现这篇文章。

你需要安装react-overlays并导入ReactRootWrapper。修改示例代码(来自文档中的 Custom Dropdown Component),组件现在应该类似于:

<RootCloseWrapper onRootClose={this.setOpenStateToFalse}>
  <Dropdown id="dropdown-custom-menu" open={this.state.open} onToggle={this.toggleOpenState}>
    <CustomToggle bsRole="toggle">Custom toggle</CustomToggle>
    <CustomMenu bsRole="menu">
      <MenuItem eventKey="1">Red</MenuItem>
      <MenuItem eventKey="2">Blue</MenuItem>
      <MenuItem eventKey="3" active>Orange</MenuItem>
      <MenuItem eventKey="1">Red-Orange</MenuItem>
    </CustomMenu>
  </Dropdown>
</RootCloseWrapper>

我没有添加示例中的函数,但它们的名称说明了它们的作用。基本上setOpenStateToFalse()需要始终将this.state.open设置为false,而toggleOpenState()需要将this.state.open的当前布尔值取反。

希望有人觉得这很有用。

【讨论】:

  • 我没有在菜单中看到输入。
  • RootCloseWrapper 似乎从 react-overlays 中消失了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2018-04-28
  • 2022-01-01
  • 2021-05-25
  • 2014-06-24
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多