-
-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow filtering the list of entrypoints (#624)
* filter entrypoints * changelog
- Loading branch information
1 parent
f01056a
commit 4f52add
Showing
4 changed files
with
88 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,88 @@ | ||
import {createRef} from 'preact'; | ||
import PureComponent from '../lib/PureComponent'; | ||
|
||
import s from './Dropdown.css'; | ||
|
||
export default class Dropdown extends PureComponent { | ||
input = createRef(); | ||
|
||
state = { | ||
query: '', | ||
showOptions: false | ||
}; | ||
|
||
componentDidMount() { | ||
document.addEventListener('click', this.handleClickOutside, true); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener('click', this.handleClickOutside, true); | ||
} | ||
|
||
render() { | ||
const {label, defaultOption, onSelectionChange, options} = this.props; | ||
const {label, options} = this.props; | ||
|
||
const filteredOptions = | ||
this.state.query | ||
? options.filter((option) => | ||
option.toLowerCase().includes(this.state.query.toLowerCase()) | ||
) | ||
: options; | ||
|
||
return ( | ||
<div className={s.container}> | ||
<div className={s.label}> | ||
{label}: | ||
</div> | ||
<div className={s.label}>{label}:</div> | ||
<div> | ||
<select className={s.select} id={label} name={label} onChange={onSelectionChange}> | ||
<option value={defaultOption}>{defaultOption}</option> | ||
{options.map(option => | ||
<option key={option} value={option}>{option}</option> | ||
)} | ||
</select> | ||
<input ref={this.input} | ||
className={s.input} | ||
type="text" | ||
value={this.state.query} | ||
onInput={this.handleInput} | ||
onFocus={this.handleFocus}/> | ||
{this.state.showOptions ? ( | ||
<div className={s.options}> | ||
{filteredOptions.map((option) => ( | ||
<div key={option} | ||
className={s.option} | ||
onClick={this.getOptionClickHandler(option)}> | ||
{option} | ||
</div> | ||
))} | ||
</div> | ||
) : null} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
handleClickOutside = (event) => { | ||
const el = this.input.current; | ||
if (el && event && !el.contains(event.target)) { | ||
this.setState({showOptions: false}); | ||
// If the query is not in the options, reset the selection | ||
if (this.state.query && !this.props.options.some((option) => option === this.state.query)) { | ||
this.setState({query: ''}); | ||
this.props.onSelectionChange(undefined); | ||
} | ||
} | ||
}; | ||
|
||
handleInput = (event) => { | ||
const {value} = event.target; | ||
this.setState({query: value}); | ||
if (!value) { | ||
this.props.onSelectionChange(undefined); | ||
} | ||
} | ||
|
||
handleFocus = () => { | ||
// move the cursor to the end of the input | ||
this.input.current.value = this.state.query; | ||
this.setState({showOptions: true}); | ||
} | ||
|
||
getOptionClickHandler = (option) => () => { | ||
this.props.onSelectionChange(option); | ||
this.setState({query: option, showOptions: false}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters