Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

html - reactjs - Dropdown select with 2 fields

I'm trying to code a select component with 2 fields, similar to what http://www.apartments.com has. I've looked into antd, bootstrap, semantic etc but did not see a similar component, so I was wondering if anyone knows of a package/library to accomplish this. Or is it my best bet to code it as a dropdown with 2 tabs, each tab as an input field?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure if this is what you are looking for, but you could build your own custom select component like this:

class Select extends React.Component {
  
  state = {
    activeMenu: 'min',
    open: true,
    min: '',
    max: ''
  };
  
  toggleMenu = e => {
    this.setState({
      activeMenu: e.target.name
    });
  };
  
  toggleOpen = () => {
    this.setState( prevState => {
      return { open: !prevState.open }
    });
  };
  
  getMenuOptions = () => {
    let options = [];
    switch(this.state.activeMenu){
      case 'min': {
        options = ['$1000','$2000','$3000','$4000','No Min'];
        break;
      }
      case 'max': {
        options = ['$1000','$2000','$3000','$4000','No Max'];
        break;
      }
    }
    return options.map( (option, i) => {
      return (
        <li key={i} onClick={this.handleSelect.bind(this, this.state.activeMenu, option)}>
          {option}
        </li>
      )
    });
  };
  
  handleSelect = (menu, value) => {
    this.setState({
      [menu]: value
    });
  };

  render() {
    const { open, min, max, activeMenu } = this.state;
    const menuOptions = this.getMenuOptions();
    return(
      <div className="select">
        <span onClick={this.toggleOpen} >Rent Range</span>
        {open && (
          <div>
            <div>
              <input type="text" name="min" value={min} onFocus={this.toggleMenu} autoFocus />
              -
              <input type="text" name="max" value={max} onFocus={this.toggleMenu} />
            </div>
            <div>
              <ul className={activeMenu}>
                {menuOptions}
              </ul>
            </div>
          </div>
        )}
      </div>
    )
  }
}

ReactDOM.render(<Select/>,document.getElementById('root'));
span {
  display: block;
  cursor: pointer;
}
ul {
  list-style-type: none;
  margin: 0;
  padding: 5px;
}
li:hover {
  cursor: pointer;
}
ul.max {
  float: right;
}
.select {
  width: 300px;
  border: 1px solid #222;
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...