-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addable select box for selecting context
- Loading branch information
1 parent
c79e61a
commit 5c47480
Showing
4 changed files
with
206 additions
and
6 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
87 changes: 87 additions & 0 deletions
87
src/popup/common-components/inputs/SearchableSelectBox.module.scss
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
.selectBoxContainer { | ||
width: 100%; | ||
height: 3rem; | ||
|
||
.selectBox { | ||
background-color: rgba(255, 255, 255, 0.9); | ||
width: 100%; | ||
height: 100%; | ||
border: 1px solid #f2f2f2; | ||
border-radius: 2px; | ||
font-size: 14px; | ||
display: block; | ||
line-height: 2.15; | ||
margin: 0; | ||
position: relative; | ||
|
||
.selectedOption { | ||
user-select: none; | ||
padding: 5px; | ||
cursor: pointer; | ||
|
||
.placeholder { | ||
color: gray; | ||
} | ||
} | ||
|
||
ul.options { | ||
max-height: 0; | ||
position: absolute; | ||
top: 3rem; | ||
left: 0; | ||
right: 0; | ||
overflow: hidden; | ||
transition: .2s ease-in; | ||
margin: 0; | ||
background: white; | ||
box-shadow: 0 0 3px silver; | ||
border-radius: 0 0 4px 4px; | ||
z-index: 1; | ||
display: flex; | ||
|
||
li.option { | ||
user-select: none; | ||
padding: 5px; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background: whitesmoke; | ||
} | ||
} | ||
|
||
li.noResults { | ||
user-select: none; | ||
padding: 5px; | ||
color: gray; | ||
text-align: center; | ||
} | ||
|
||
.searchInputContainer { | ||
input { | ||
margin: 0; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
&.open { | ||
.selectBox { | ||
ul.options { | ||
max-height: 200px; | ||
} | ||
} | ||
} | ||
|
||
&.reversed { | ||
.selectBox { | ||
|
||
ul.options { | ||
border-radius: 4px 4px 0 0; | ||
top: unset; | ||
bottom: 3rem; | ||
flex-direction: column-reverse; | ||
} | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/popup/common-components/inputs/SearchableSelectBox.tsx
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import styles from "./SearchableSelectBox.module.scss"; | ||
import { cx } from '@/popup/utils'; | ||
|
||
interface ISelectOption { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
interface ISelectBoxProps { | ||
defaultValue?: ISelectOption; | ||
placeholder?: string; | ||
onChange?: (item: ISelectOption) => void; | ||
options: ISelectOption[]; | ||
addable?: boolean; | ||
onAdd?: (term: string) => void; | ||
reversed?: boolean; | ||
addText?: string; | ||
} | ||
|
||
const SearchableSelectBox: React.FC<ISelectBoxProps> = (props) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [searchTerm, setSearchTerm] = useState(""); | ||
const searchRef = useRef<HTMLInputElement>(null); | ||
const [isMouseIn, setIsMouseIn] = useState(false); | ||
|
||
const optionChanged = (option: ISelectOption) => { | ||
setIsOpen(false); | ||
if(props.onChange) | ||
props.onChange(option); | ||
} | ||
|
||
const addOptionClicked = (searchTerm: string) => { | ||
setSearchTerm(""); | ||
setIsOpen(false); | ||
if(props.onAdd) | ||
props.onAdd(searchTerm); | ||
} | ||
|
||
useEffect(() => { | ||
setSearchTerm(""); | ||
}, [isOpen]); | ||
|
||
const selectBoxClicked = () => { | ||
if(!isOpen) { | ||
searchRef.current?.focus(); | ||
} | ||
setIsOpen(!isOpen); | ||
} | ||
|
||
let searchResults = props.options.filter(option => option.label.toLowerCase().includes(searchTerm.trim()) || option.value.toLowerCase().includes(searchTerm.trim())); | ||
|
||
return ( | ||
<div className={cx(styles.selectBoxContainer, isOpen ? styles.open : "", props.reversed ? styles.reversed : "")} onMouseEnter={(e) => setIsMouseIn(true)} onMouseLeave={(e) => setIsMouseIn(false)}> | ||
<div className={styles.selectBox}> | ||
<div className={styles.selectedOption} onClick={() => selectBoxClicked()}>{props.defaultValue?.label || (<span className={styles.placeholder}>{props.placeholder || "Select an item"}</span>)}</div> | ||
<ul className={styles.options}> | ||
<li className={styles.searchInputContainer}> | ||
<input type="text" ref={searchRef} placeholder="Filter results" value={searchTerm} onBlur={(e) => setIsOpen(isMouseIn)} onChange={(e) => setSearchTerm(e.target.value)} /> | ||
</li> | ||
{props.addable && searchTerm.trim().length ? ( | ||
<li className={styles.option} onClick={(e) => addOptionClicked(searchTerm)}>{props.addText ? props.addText.replaceAll("{term}", searchTerm) : `Add "${searchTerm}"`}</li> | ||
) : !searchResults.length ? ( | ||
<li className={styles.noResults}>No results</li> | ||
) : null} | ||
{searchResults.map(option => ( | ||
<li key={option.label + option.value} className={styles.option} onClick={(e) => optionChanged(option)}>{option.label}</li> | ||
))} | ||
</ul> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchableSelectBox; |
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