2021-02-16 01:48:10 +00:00
|
|
|
import { debounce } from 'debounce';
|
2021-07-25 21:51:39 +00:00
|
|
|
import React, { createRef, ReactElement, useEffect, useState } from 'react';
|
2021-07-22 17:15:27 +00:00
|
|
|
import tw, { styled } from 'twin.macro';
|
2021-02-05 16:44:31 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
2021-07-25 21:51:39 +00:00
|
|
|
import Label from '@/components/elements/Label';
|
2021-02-05 16:44:31 +00:00
|
|
|
|
|
|
|
const Dropdown = styled.div<{ expanded: boolean }>`
|
2021-02-16 20:03:14 +00:00
|
|
|
${tw`absolute z-10 w-full mt-1 rounded-md shadow-lg bg-neutral-900`};
|
2021-02-05 16:44:31 +00:00
|
|
|
${props => !props.expanded && tw`hidden`};
|
|
|
|
`;
|
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
interface OptionProps<T> {
|
|
|
|
selectId: string;
|
|
|
|
id: number;
|
|
|
|
item: T;
|
|
|
|
active: boolean;
|
|
|
|
|
|
|
|
isHighlighted?: boolean;
|
|
|
|
onClick?: (item: T) => (e: React.MouseEvent) => void;
|
|
|
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IdObj {
|
|
|
|
id: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Option = <T extends IdObj>({ selectId, id, item, active, isHighlighted, onClick, children }: OptionProps<T>) => {
|
|
|
|
if (isHighlighted === undefined) {
|
|
|
|
isHighlighted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should never be true, but just in-case we set it to an empty function to make sure shit doesn't blow up.
|
|
|
|
if (onClick === undefined) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
onClick = () => () => {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
return (
|
|
|
|
<li id={selectId + '-select-item-' + id} role="option" css={[ tw`relative py-2 pl-3 cursor-pointer select-none text-neutral-200 pr-9 hover:bg-neutral-700`, isHighlighted ? tw`bg-neutral-700` : null ]} onClick={onClick(item)}>
|
|
|
|
<div css={tw`flex items-center`}>
|
|
|
|
<span css={tw`block font-medium truncate`}>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<span css={tw`absolute inset-y-0 right-0 flex items-center pr-4`}>
|
|
|
|
<svg css={tw`w-5 h-5 text-primary-400`} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
|
|
|
|
<path clipRule="evenodd" fillRule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"/>
|
|
|
|
</svg>
|
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<li id={selectId + 'select-item-' + id} role="option" css={[ tw`relative py-2 pl-3 cursor-pointer select-none text-neutral-200 pr-9 hover:bg-neutral-700`, isHighlighted ? tw`bg-neutral-700` : null ]} onClick={onClick(item)}>
|
|
|
|
<div css={tw`flex items-center`}>
|
|
|
|
<span css={tw`block font-normal truncate`}>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-02-16 01:48:10 +00:00
|
|
|
interface SearchableSelectProps<T> {
|
2021-02-05 16:44:31 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
2021-07-25 21:51:39 +00:00
|
|
|
label: string;
|
|
|
|
placeholder?: string;
|
|
|
|
nullable?: boolean;
|
2021-02-05 16:44:31 +00:00
|
|
|
|
2021-02-16 01:48:10 +00:00
|
|
|
selected: T | null;
|
2021-02-17 22:17:37 +00:00
|
|
|
setSelected: (item: T | null) => void;
|
2021-02-16 01:48:10 +00:00
|
|
|
|
2021-02-16 20:23:24 +00:00
|
|
|
items: T[] | null;
|
|
|
|
setItems: (items: T[] | null) => void;
|
2021-02-05 16:44:31 +00:00
|
|
|
|
|
|
|
onSearch: (query: string) => Promise<void>;
|
2021-02-16 20:03:14 +00:00
|
|
|
onSelect: (item: T | null) => void;
|
2021-02-05 16:44:31 +00:00
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
getSelectedText: (item: T | null) => string | undefined;
|
2021-02-16 01:48:10 +00:00
|
|
|
|
2021-02-05 16:44:31 +00:00
|
|
|
children: React.ReactNode;
|
2021-07-25 21:51:39 +00:00
|
|
|
className?: string;
|
2021-02-05 16:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
export const SearchableSelect = <T extends IdObj>({ id, name, label, placeholder, selected, setSelected, items, setItems, onSearch, onSelect, getSelectedText, children, className }: SearchableSelectProps<T>) => {
|
2021-02-05 16:44:31 +00:00
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const [ expanded, setExpanded ] = useState(false);
|
|
|
|
|
|
|
|
const [ inputText, setInputText ] = useState('');
|
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
const [ highlighted, setHighlighted ] = useState<number | null>(null);
|
|
|
|
|
2021-02-16 05:41:19 +00:00
|
|
|
const searchInput = createRef<HTMLInputElement>();
|
|
|
|
const itemsList = createRef<HTMLDivElement>();
|
|
|
|
|
2021-02-05 16:44:31 +00:00
|
|
|
const onFocus = () => {
|
|
|
|
setInputText('');
|
2021-02-16 20:23:24 +00:00
|
|
|
setItems(null);
|
2021-02-05 16:44:31 +00:00
|
|
|
setExpanded(true);
|
2021-02-17 22:17:37 +00:00
|
|
|
setHighlighted(null);
|
2021-02-05 16:44:31 +00:00
|
|
|
};
|
|
|
|
|
2021-02-16 05:41:19 +00:00
|
|
|
const onBlur = () => {
|
|
|
|
setInputText(getSelectedText(selected) || '');
|
2021-02-17 22:17:37 +00:00
|
|
|
setItems(null);
|
2021-02-16 05:41:19 +00:00
|
|
|
setExpanded(false);
|
2021-02-17 22:17:37 +00:00
|
|
|
setHighlighted(null);
|
2021-02-16 05:41:19 +00:00
|
|
|
};
|
|
|
|
|
2021-02-05 16:44:31 +00:00
|
|
|
const search = debounce((query: string) => {
|
|
|
|
if (!expanded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query === '' || query.length < 2) {
|
2021-02-16 20:23:24 +00:00
|
|
|
setItems(null);
|
2021-02-17 22:17:37 +00:00
|
|
|
setHighlighted(null);
|
2021-02-05 16:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
onSearch(query).then(() => setLoading(false));
|
2021-02-16 20:23:24 +00:00
|
|
|
}, 250);
|
2021-02-05 16:44:31 +00:00
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
const handleInputKeydown = (e: React.KeyboardEvent) => {
|
|
|
|
if (e.key === 'Tab' || e.key === 'Escape') {
|
|
|
|
onBlur();
|
|
|
|
return;
|
|
|
|
}
|
2021-02-05 16:44:31 +00:00
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
if (!items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
|
|
|
|
// Prevent up and down arrows from moving the cursor in the input.
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (highlighted === null) {
|
|
|
|
setHighlighted(items[0].id);
|
2021-02-16 05:41:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
const item = items.find(i => i.id === highlighted);
|
2021-02-17 22:17:37 +00:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = items.indexOf(item);
|
|
|
|
if (e.key === 'ArrowUp') {
|
|
|
|
if (--index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (++index >= items.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 05:41:19 +00:00
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
setHighlighted(items[index].id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent the form from being submitted if the user accidentally hits enter
|
|
|
|
// while focused on the select.
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
const item = items.find(i => i.id === highlighted);
|
2021-02-17 22:17:37 +00:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelected(item);
|
2021-10-29 05:48:07 +00:00
|
|
|
onSelect(item);
|
2021-02-17 22:17:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-16 05:41:19 +00:00
|
|
|
const clickHandler = (e: MouseEvent) => {
|
|
|
|
const input = searchInput.current;
|
|
|
|
const menu = itemsList.current;
|
|
|
|
|
|
|
|
if (e.button === 2 || !expanded || !input || !menu) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target === input || input.contains(e.target as Node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target === menu || menu.contains(e.target as Node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target === input || input.contains(e.target as Node)) {
|
2021-02-05 16:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-16 05:41:19 +00:00
|
|
|
if (e.target === menu || menu.contains(e.target as Node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlur();
|
2021-02-05 16:44:31 +00:00
|
|
|
};
|
|
|
|
|
2021-02-16 05:41:19 +00:00
|
|
|
const contextmenuHandler = () => {
|
|
|
|
onBlur();
|
|
|
|
};
|
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
window.addEventListener('mousedown', clickHandler);
|
2021-02-16 05:41:19 +00:00
|
|
|
window.addEventListener('contextmenu', contextmenuHandler);
|
2021-02-05 16:44:31 +00:00
|
|
|
return () => {
|
2021-07-25 21:51:39 +00:00
|
|
|
window.removeEventListener('mousedown', clickHandler);
|
2021-02-16 05:41:19 +00:00
|
|
|
window.removeEventListener('contextmenu', contextmenuHandler);
|
2021-02-05 16:44:31 +00:00
|
|
|
};
|
|
|
|
}, [ expanded ]);
|
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
const onClick = (item: T) => () => {
|
2021-02-16 01:48:10 +00:00
|
|
|
onSelect(item);
|
2021-02-17 22:17:37 +00:00
|
|
|
|
|
|
|
setExpanded(false);
|
|
|
|
setInputText(getSelectedText(selected) || '');
|
2021-02-16 01:48:10 +00:00
|
|
|
};
|
|
|
|
|
2021-02-17 22:17:37 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (expanded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setInputText(getSelectedText(selected) || '');
|
|
|
|
}, [ selected ]);
|
|
|
|
|
2021-02-16 01:48:10 +00:00
|
|
|
// This shit is really stupid but works, so is it really stupid?
|
|
|
|
const c = React.Children.map(children, child => React.cloneElement(child as ReactElement, {
|
2021-02-17 22:17:37 +00:00
|
|
|
isHighlighted: ((child as ReactElement).props as OptionProps<T>).id === highlighted,
|
2021-02-16 01:48:10 +00:00
|
|
|
onClick: onClick.bind(child),
|
|
|
|
}));
|
|
|
|
|
2021-02-05 16:44:31 +00:00
|
|
|
return (
|
2021-07-25 21:51:39 +00:00
|
|
|
<div className={className}>
|
2021-11-17 21:26:23 +00:00
|
|
|
<div css={tw`flex flex-row`}>
|
|
|
|
<Label htmlFor={id + '-select-label'}>{label}</Label>
|
|
|
|
</div>
|
2021-02-05 16:44:31 +00:00
|
|
|
|
2021-11-17 21:26:23 +00:00
|
|
|
<div css={tw`relative`}>
|
2021-02-05 16:44:31 +00:00
|
|
|
<InputSpinner visible={loading}>
|
2021-02-17 22:17:37 +00:00
|
|
|
<Input
|
|
|
|
ref={searchInput}
|
2021-07-25 21:51:39 +00:00
|
|
|
type={'search'}
|
2021-02-17 22:17:37 +00:00
|
|
|
id={id}
|
2021-07-25 21:51:39 +00:00
|
|
|
name={name}
|
2021-02-17 22:17:37 +00:00
|
|
|
value={inputText}
|
|
|
|
readOnly={!expanded}
|
|
|
|
onFocus={onFocus}
|
|
|
|
onChange={e => {
|
|
|
|
setInputText(e.currentTarget.value);
|
|
|
|
search(e.currentTarget.value);
|
|
|
|
}}
|
|
|
|
onKeyDown={handleInputKeydown}
|
2021-07-25 21:51:39 +00:00
|
|
|
className={'ignoreReadOnly'}
|
|
|
|
placeholder={placeholder}
|
2021-02-05 16:44:31 +00:00
|
|
|
/>
|
|
|
|
</InputSpinner>
|
|
|
|
|
2021-07-25 21:51:39 +00:00
|
|
|
<div css={[ tw`absolute inset-y-0 right-0 flex items-center pr-2 ml-3`, !expanded && tw`pointer-events-none` ]}>
|
|
|
|
{inputText !== '' && expanded &&
|
|
|
|
<svg css={tw`w-5 h-5 text-neutral-400 cursor-pointer`} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"
|
|
|
|
onMouseDown={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
setInputText('');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<path clipRule="evenodd" fillRule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"/>
|
|
|
|
</svg>
|
|
|
|
}
|
|
|
|
<svg css={tw`w-5 h-5 text-neutral-400 pointer-events-none`} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
|
2021-02-05 16:44:31 +00:00
|
|
|
<path clipRule="evenodd" fillRule="evenodd" d="M10 3a1 1 0 01.707.293l3 3a1 1 0 01-1.414 1.414L10 5.414 7.707 7.707a1 1 0 01-1.414-1.414l3-3A1 1 0 0110 3zm-3.707 9.293a1 1 0 011.414 0L10 14.586l2.293-2.293a1 1 0 011.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z"/>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
|
2021-02-16 05:41:19 +00:00
|
|
|
<Dropdown ref={itemsList} expanded={expanded}>
|
2021-07-25 21:51:39 +00:00
|
|
|
{items === null || items.length < 1 ?
|
2021-02-16 20:23:24 +00:00
|
|
|
items === null || inputText.length < 2 ?
|
2021-02-16 20:03:14 +00:00
|
|
|
<div css={tw`flex flex-row items-center h-10 px-3`}>
|
2021-02-05 16:44:31 +00:00
|
|
|
<p css={tw`text-sm`}>Please type 2 or more characters.</p>
|
|
|
|
</div>
|
|
|
|
:
|
2021-02-16 20:03:14 +00:00
|
|
|
<div css={tw`flex flex-row items-center h-10 px-3`}>
|
2021-02-05 16:44:31 +00:00
|
|
|
<p css={tw`text-sm`}>No results found.</p>
|
|
|
|
</div>
|
|
|
|
:
|
2021-02-16 05:41:19 +00:00
|
|
|
<ul
|
|
|
|
tabIndex={-1}
|
|
|
|
role={id + '-select'}
|
|
|
|
aria-labelledby={id + '-select-label'}
|
2021-02-17 22:17:37 +00:00
|
|
|
aria-activedescendant={id + '-select-item-' + selected?.id}
|
|
|
|
css={tw`py-2 overflow-auto text-base rounded-md max-h-56 ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm`}
|
2021-02-16 05:41:19 +00:00
|
|
|
>
|
2021-02-16 01:48:10 +00:00
|
|
|
{c}
|
2021-02-05 16:44:31 +00:00
|
|
|
</ul>
|
|
|
|
}
|
|
|
|
</Dropdown>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2021-02-17 22:17:37 +00:00
|
|
|
};
|
2021-02-16 01:48:10 +00:00
|
|
|
|
2021-02-05 16:44:31 +00:00
|
|
|
export default SearchableSelect;
|