|
| 1 | +import {createRef} from 'preact'; |
1 | 2 | import PureComponent from '../lib/PureComponent'; |
2 | 3 |
|
3 | 4 | import s from './Dropdown.css'; |
| 5 | + |
4 | 6 | export default class Dropdown extends PureComponent { |
| 7 | + input = createRef(); |
| 8 | + |
| 9 | + state = { |
| 10 | + query: '', |
| 11 | + showOptions: false |
| 12 | + }; |
| 13 | + |
| 14 | + componentDidMount() { |
| 15 | + document.addEventListener('click', this.handleClickOutside, true); |
| 16 | + } |
| 17 | + |
| 18 | + componentWillUnmount() { |
| 19 | + document.removeEventListener('click', this.handleClickOutside, true); |
| 20 | + } |
5 | 21 |
|
6 | 22 | render() { |
7 | | - const {label, defaultOption, onSelectionChange, options} = this.props; |
| 23 | + const {label, options} = this.props; |
| 24 | + |
| 25 | + const filteredOptions = |
| 26 | + this.state.query |
| 27 | + ? options.filter((option) => |
| 28 | + option.toLowerCase().includes(this.state.query.toLowerCase()) |
| 29 | + ) |
| 30 | + : options; |
8 | 31 |
|
9 | 32 | return ( |
10 | 33 | <div className={s.container}> |
11 | | - <div className={s.label}> |
12 | | - {label}: |
13 | | - </div> |
| 34 | + <div className={s.label}>{label}:</div> |
14 | 35 | <div> |
15 | | - <select className={s.select} id={label} name={label} onChange={onSelectionChange}> |
16 | | - <option value={defaultOption}>{defaultOption}</option> |
17 | | - {options.map(option => |
18 | | - <option key={option} value={option}>{option}</option> |
19 | | - )} |
20 | | - </select> |
| 36 | + <input ref={this.input} |
| 37 | + className={s.input} |
| 38 | + type="text" |
| 39 | + value={this.state.query} |
| 40 | + onInput={this.handleInput} |
| 41 | + onFocus={this.handleFocus}/> |
| 42 | + {this.state.showOptions ? ( |
| 43 | + <div className={s.options}> |
| 44 | + {filteredOptions.map((option) => ( |
| 45 | + <div key={option} |
| 46 | + className={s.option} |
| 47 | + onClick={this.getOptionClickHandler(option)}> |
| 48 | + {option} |
| 49 | + </div> |
| 50 | + ))} |
| 51 | + </div> |
| 52 | + ) : null} |
21 | 53 | </div> |
22 | 54 | </div> |
23 | 55 | ); |
24 | 56 | } |
| 57 | + |
| 58 | + handleClickOutside = (event) => { |
| 59 | + const el = this.input.current; |
| 60 | + if (el && event && !el.contains(event.target)) { |
| 61 | + this.setState({showOptions: false}); |
| 62 | + // If the query is not in the options, reset the selection |
| 63 | + if (this.state.query && !this.props.options.some((option) => option === this.state.query)) { |
| 64 | + this.setState({query: ''}); |
| 65 | + this.props.onSelectionChange(undefined); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + handleInput = (event) => { |
| 71 | + const {value} = event.target; |
| 72 | + this.setState({query: value}); |
| 73 | + if (!value) { |
| 74 | + this.props.onSelectionChange(undefined); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + handleFocus = () => { |
| 79 | + // move the cursor to the end of the input |
| 80 | + this.input.current.value = this.state.query; |
| 81 | + this.setState({showOptions: true}); |
| 82 | + } |
| 83 | + |
| 84 | + getOptionClickHandler = (option) => () => { |
| 85 | + this.props.onSelectionChange(option); |
| 86 | + this.setState({query: option, showOptions: false}); |
| 87 | + }; |
25 | 88 | } |
0 commit comments