Use Downshift instead of react-select
parent
e5a4b82db4
commit
8e2601d76f
@ -1,97 +0,0 @@
|
||||
.filter-list-text:hover ~ .filter-list-edit {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.filter-list-text:hover ~ .filter-list-remove {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.filter-select__control:focus-within {
|
||||
box-shadow: rgb(255, 255, 255) 0px 0px 0px 0px, rgb(99, 102, 241) 0px 0px 0px 1px, rgba(0, 0, 0, 0) 0px 0px 0px 0px; /* indigo-500 */
|
||||
}
|
||||
|
||||
.filter-select__value-container {
|
||||
padding-left: 1rem !important;
|
||||
}
|
||||
|
||||
.filter-select__control {
|
||||
@apply dark:bg-gray-900 border-gray-300 dark:border-gray-700 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 pr-8;
|
||||
border-radius: 0.375rem !important;
|
||||
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
|
||||
background-position: right 0.5rem center;
|
||||
background-repeat: no-repeat;
|
||||
background-size: 1.35rem 1.35rem;
|
||||
}
|
||||
|
||||
.filter-select__control:hover {
|
||||
@apply cursor-pointer;
|
||||
border-color: rgb(156, 163, 175) !important; /* gray-400 */
|
||||
}
|
||||
|
||||
.filter-select__indicator-separator {
|
||||
@apply hidden;
|
||||
}
|
||||
|
||||
.filter-select__dropdown-indicator {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.filter-select__input {
|
||||
@apply w-full text-base sm:text-sm;
|
||||
margin: 0 !important;
|
||||
}
|
||||
|
||||
.filter-select__input input {
|
||||
width: 100% !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
|
||||
.dark .filter-select__input input {
|
||||
color: rgb(209, 213, 219) !important; /* gray-300 */
|
||||
}
|
||||
|
||||
.filter-select__value-container > :not(.filter-select__single-value) {
|
||||
@apply w-full;
|
||||
padding-top: 0 !important;
|
||||
}
|
||||
|
||||
.filter-select__loading-indicator {
|
||||
color: rgb(79, 70, 229) !important; /* indigo-600 */
|
||||
}
|
||||
|
||||
.filter-select__loading-indicator > span {
|
||||
height: 1.5em;
|
||||
width: 1.5em;
|
||||
}
|
||||
|
||||
.filter-select__single-value {
|
||||
@apply dark:text-gray-400 text-base sm:text-sm;
|
||||
}
|
||||
|
||||
.filter-select__menu-list {
|
||||
@apply dark:bg-gray-900;
|
||||
}
|
||||
|
||||
.filter-select__option {
|
||||
@apply dark:text-gray-300;
|
||||
font-size: 1rem !important;
|
||||
line-height: 1.5rem !important;
|
||||
cursor: pointer !important;
|
||||
}
|
||||
|
||||
@media (min-width: 640px) {
|
||||
.filter-select__option {
|
||||
font-size: 0.875rem !important;
|
||||
line-height: 1.25rem !important;
|
||||
}
|
||||
}
|
||||
|
||||
.filter-select__option--is-focused {
|
||||
background-color: rgb(99, 102, 241) !important; /* indigo-500 */
|
||||
color: white !important;
|
||||
}
|
||||
|
||||
.dark .filter-select__option--is-focused {
|
||||
background-color: rgb(79, 70, 229) !important; /* indigo-600 */
|
||||
}
|
||||
|
||||
@ -0,0 +1,115 @@
|
||||
import React, {useState, useCallback} from 'react'
|
||||
import {useCombobox} from 'downshift'
|
||||
import classNames from 'classnames'
|
||||
import debounce from 'debounce-promise'
|
||||
|
||||
function selectInputText(e) {
|
||||
e.target.select()
|
||||
}
|
||||
|
||||
function ChevronDown() {
|
||||
return (
|
||||
<svg className="text-indigo-500 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<polyline points="6 9 12 15 18 9"></polyline>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
function Spinner() {
|
||||
return (
|
||||
<svg className="animate-spin h-4 w-4 text-indigo-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default function SearchSelect(props) {
|
||||
const [items, setItems] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [initialLoadComplete, setInitialLoadComplete] = useState(false)
|
||||
|
||||
function fetchOptions({inputValue, isOpen}) {
|
||||
setLoading(isOpen)
|
||||
|
||||
return props.fetchOptions(inputValue).then((loadedItems) => {
|
||||
setLoading(false)
|
||||
setItems(loadedItems)
|
||||
})
|
||||
}
|
||||
|
||||
const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), [])
|
||||
|
||||
const {
|
||||
isOpen,
|
||||
inputValue,
|
||||
getToggleButtonProps,
|
||||
getLabelProps,
|
||||
getMenuProps,
|
||||
getInputProps,
|
||||
getComboboxProps,
|
||||
highlightedIndex,
|
||||
getItemProps,
|
||||
selectItem,
|
||||
closeMenu,
|
||||
setInputValue
|
||||
} = useCombobox({
|
||||
items: items,
|
||||
onInputValueChange: (changes) => {
|
||||
debouncedFetchOptions(changes)
|
||||
props.onInput(changes.inputValue)
|
||||
},
|
||||
initialSelectedItem: props.initialSelectedItem,
|
||||
onIsOpenChange: ({inputValue}) => {
|
||||
if (!initialLoadComplete) {
|
||||
fetchOptions({inputValue: inputValue, isOpen: true}).then(() => {
|
||||
setInitialLoadComplete(true)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
function keydown(e) {
|
||||
if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return
|
||||
|
||||
if (e.key == 'Enter' && isOpen && highlightedIndex === -1) {
|
||||
closeMenu()
|
||||
e.preventDefault()
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-1 relative">
|
||||
<div className="relative rounded-md shadow-sm" {...getToggleButtonProps()} {...getComboboxProps()}>
|
||||
<input {...getInputProps({onKeyDown: keydown})} onFocus={selectInputText} placeholder="Enter a filter value" type="text" className={classNames('focus:ring-indigo-500 focus:border-indigo-500 block w-full pr-10 sm:text-sm border-gray-300 rounded-md', {'cursor-pointer': inputValue === '' && !isOpen})} />
|
||||
<div className="absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none">
|
||||
{ !loading && <ChevronDown /> }
|
||||
{ loading && <Spinner /> }
|
||||
</div>
|
||||
</div>
|
||||
<div {...getMenuProps()}>
|
||||
{ isOpen &&
|
||||
<ul className="absolute z-10 mt-1 w-full bg-white shadow-lg max-h-60 rounded-md py-1 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm">
|
||||
{ !loading && items.length == 0 &&
|
||||
<li className="cursor-default select-none relative py-2 pl-3 pr-9">No results found</li>
|
||||
}
|
||||
{ loading && items.length == 0 &&
|
||||
<li className="cursor-default select-none relative py-2 pl-3 pr-9">Loading options...</li>
|
||||
}
|
||||
|
||||
{
|
||||
items.map((item, index) => (
|
||||
<li className={classNames("cursor-pointer select-none relative py-2 pl-3 pr-9", {'text-white bg-indigo-600': highlightedIndex === index, 'text-gray-900': highlightedIndex !== index})}
|
||||
key={`${item}${index}`}
|
||||
{...getItemProps({ item, index })}
|
||||
>
|
||||
{item}
|
||||
</li>
|
||||
))
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in New Issue