在外部单击或滚动时隐藏下拉列表

时间:2018-11-22 08:15:28

标签: css reactjs autocomplete

在窗格外单击或滚动后,我想关闭下拉列表。当我们在下拉框外滚动时,下拉框始终保持打开状态。这是我的代码。

static defaultProps = { // <-- DEFAULT PROPS
          wrapperStyle: {
            display: 'inline',

             }, 
          menuStyle: {
                borderRadius: '3px',
                boxShadow: '0 2px 12px rgba(0, 0, 0, 0.1)',
                padding: '2px 0',
                fontSize: '90%',
                position: 'fixed',
                minWidth: '300px',
                overflow: 'auto',
                maxHeight: '250px',
                display: 'inline', 
              }  
    }

............................................... ......

<ReactAutocomplete
                                name="ReferredBy"

                                items = {patientsMasterData.ReferredBy && patientsMasterData.ReferredBy.map(referredObj =>(
                                    {options:referredObj.RefName, 
                                   values:referredObj.RefID}
                                  ))
                                }

                                shouldItemRender={(item, value) => item.options.toLowerCase().indexOf(value.toLowerCase()) > -1}
                                getItemValue={(item) => item.options}
                                renderItem={(item, highlighted) =>
                                    <div
                                    key={item.values}
                                    style={{ backgroundColor: highlighted ? '#3db4e5' : '#FFFFFF',cursor:'pointer', border:'1px solid lighten($grey-element,30%)',padding: '5px}}
                                    {item.options}</div>}
                 inputProps={{placeholder:'Select...'}}
                                menuStyle={this.props.menuStyle}
                                wrapperStyle={this.props.wrapperStyle}
                                value={this.state.value}
                          onChange{e=>this.setState({value:e.target.value})}
                                onSelect={value => this.setState({ value })}

                       />

&css部分

&_value1 {
              flex:2;
              white-space: normal;
              width: 100%;
              // overflow-y: auto;
              font-size: 14px;
              position: relative;
              z-index: 2;
              display: inline-block;
              input, textarea {
                width: 100%;
                min-width: 200px;
                height: 25px;
                border: 1px solid $grey-element;
                padding: 0 8px;
                font-size: 12px;   
              }
             &::after {
                position: absolute;
                right: 9px;
                top: 10px;
                content: '';
                width: 0;
                height: 0;
                border-style: solid;
                border-width: 6px 3px 0 3px;
                border-color: $black transparent transparent transparent;
              }   }

在外部滚动时如何隐藏下拉框?

2 个答案:

答案 0 :(得分:0)

简而言之:您需要在下拉列表打开时添加事件侦听器,并在下拉列表中添加引用以避免在下拉列表中单击事件,但是在其他位置单击时会触发事件(并在此处删除事件监听器)。您也可以收听滚动事件。这是实现示例:

import React, {Component} from 'react';
import { CSSTransition } from 'react-transition-group';

class Dropdown extends Component {
    constructor(props) {
        super(props);
        this.setWrapperRef = this.setWrapperRef.bind(this);
        this.handleClickOutside = this.handleClickOutside.bind(this);
    };

    setWrapperRef(node) {
        this.wrapperRef = node;
    };

    handleClickOutside(e) {
        e.stopPropagation();
        if (this.wrapperRef && !this.wrapperRef.contains(e.target) && this.props.isOpen){
            this.props.onClose();
        }
    };

    componentDidUpdate(){
        if(this.props.isOpen){
            document.addEventListener('mousedown', this.handleClickOutside);
        } else {
            document.removeEventListener('mousedown', this.handleClickOutside);
        }
    }

   render(){
       return (
            <div className={"dropdown " + (this.props.isOpen ? "show" : "hide")} ref={this.setWrapperRef}>
                <CSSTransition in={this.props.isOpen} timeout={300} classNames="fadeIndown" unmountOnExit={true}>
                        {this.props.children}
                </CSSTransition>
            </div>
        )
    }
}

export default Dropdown;

答案 1 :(得分:0)

const toggleDropdown = () => this.setState({ isDropdownOpen: !this.state.isDropdownOpen });

const closeDropdownThen = fn => (...params) => {
  this.setState({ isDropdownOpen: false });
  return fn(...params);
};

在渲染下,您应该像上面定义的那样定义常量。而当您使用

       <Dropdown
            isOpen={isDropdownOpen}
            toggleDropdown={toggleDropdown}
            className={s.dropDownContainer}
            label="Export"
          >
            <DropdownItem onClick={closeDropdownThen(this.abcFunction)}>
               CSV
            </DropdownItem>

这是我的dropDown组件,可能会对您有所帮助。最好的问候