【问题标题】:Scrollable drop down lists in react-bootstrapreact-bootstrap 中的可滚动下拉列表
【发布时间】:2020-06-11 22:13:26
【问题描述】:

我正在尝试创建一些简单的出生日期下拉列表,并希望在下拉列表中显示固定数量的项目的滚动条。我怎样才能用 react-bootstrap 做到这一点?我目前的下拉列表离开屏幕并在整个页面上触发滚动条。

这是我的代码:

<FormGroup>
  <Col componentClass={ControlLabel} sm={3}>
    Birth Date
  </Col>
  <Col sm={9}>
    <DropdownButton title="Month" id="birth-month">
      {createMonthSelectItems()}
    </DropdownButton>
    <DropdownButton title="Day" id="birth-day">
      {createDayOfMonthSelectItems()}
    </DropdownButton>
    <DropdownButton title="Year" id="birth-year">
      {createYearSelectItems()}
    </DropdownButton>
  </Col>
</FormGroup>

另外,请告知这是否是一个好主意。我需要这个 UI 在移动设备和桌面设备上都能很好地工作。

【问题讨论】:

    标签: reactjs react-bootstrap


    【解决方案1】:

    您可以通过为“.dropdown-menu”元素应用固定高度并设置“overflow-y:scroll;”来创建可滚动的下拉菜单

    反应代码:

    import React, { Component } from 'react'
    import { DropdownButton, MenuItem } from 'react-bootstrap'
    import './QuantityInput.css'
    
    export default class QuantityInput extends Component {
      render() {
        return (
          <DropdownButton
            bsStyle="default"
            bsSize="small"
            style={{ maxHeight: "28px" }}
            title={"Qty"}
            key={1}
            id="dropdown-size-small"
          >
            <MenuItem eventKey="1">Action</MenuItem>
            <MenuItem eventKey="2">Another action</MenuItem>
            <MenuItem eventKey="3" active>
              Active Item
            </MenuItem>
            <MenuItem divider />
            <MenuItem eventKey="4">Separated link</MenuItem>
          </DropdownButton>
        )
      }
    }
    

    数量输入.css

    .dropdown-menu {
      height: 70px;
      overflow-y: scroll;
    }
    

    【讨论】:

    • 或者,如果您想在较小的尺寸下保持菜单尺寸的动态特性,您可以使用 max-height。
    【解决方案2】:

    将此样式用于可滚动的下拉按钮

    ul.dropdown-menu {
        max-height: 500px;
        overflow-y: scroll;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个可能的解决方案,它将根据视口高度动态缩放元素的最大高度:

      import React, { Component } from 'react'
      import { Button, Dropdown, MenuItem } from 'react-bootstrap'
      
      export default class CustomDropdown extends Component {
          constructor(props) {
              super(props);
              this.state = {
                  open: false,
              };
          }
          toggle = () => {
              this.setState({ open: !this.state.open });
          }
          onToggle = (isOpen, e, source) => {
              //This closes the menu on toggling the dropdown or hitting esc.
              if (source.source === 'click' || source.source === 'rootClose') {
                  this.toggle();
              }
          }
      
          render(){
              <div ref={(ref) => this.myRef = ref} className='CustomDropdown'>
                  <Dropdown open={this.state.open}
                          onToggle={this.onToggle}
                          id={'Dropdown'}
                      >
                      <Button onClick={this.toggle}
                      >{this.props.myTitle ? this.props.myTitle : 'Click Me'}</Button>
                       <Dropdown.Toggle style={{ textAlign: right, paddingBottom: 5 }} />
                      <Dropdown.Menu style={{overflowY: 'scroll', maxHeight: (window.innerHeight - (this.myRef ? (this.myRef.getBoundingClientRect().top + this.myRef.getBoundingClientRect().height + 100) : 200))}}>
                          ... add menu items here
                      </Dropdown.Menu>
                  </Dropdown>
              </div>
          }
      }
      

      【讨论】:

      • 作为一项改进,您可能希望向窗口添加一个调整大小的侦听器,并让高度与状态相关,并在 render() 中使用它而不是 window.innerHeight。
      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 2021-11-26
      • 2018-08-25
      • 1970-01-01
      • 2023-03-11
      • 2015-08-28
      • 1970-01-01
      • 2015-10-09
      相关资源
      最近更新 更多