2021-02-02 05:28:25 +01:00
|
|
|
import { h, Fragment } from 'preact';
|
|
|
|
import ArrowDropdown from '../icons/ArrowDropdown';
|
|
|
|
import ArrowDropup from '../icons/ArrowDropup';
|
|
|
|
import Menu, { MenuItem } from './Menu';
|
|
|
|
import TextField from './TextField';
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
|
|
|
|
|
|
export default function Select({ label, onChange, options: inputOptions = [], selected: propSelected }) {
|
|
|
|
const options = useMemo(
|
|
|
|
() =>
|
|
|
|
typeof inputOptions[0] === 'string' ? inputOptions.map((opt) => ({ value: opt, label: opt })) : inputOptions,
|
|
|
|
[inputOptions]
|
|
|
|
);
|
|
|
|
const [showMenu, setShowMenu] = useState(false);
|
|
|
|
const [selected, setSelected] = useState(
|
|
|
|
Math.max(
|
|
|
|
options.findIndex(({ value }) => value === propSelected),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const [focused, setFocused] = useState(null);
|
|
|
|
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
const handleSelect = useCallback(
|
|
|
|
(value, label) => {
|
|
|
|
setSelected(options.findIndex((opt) => opt.value === value));
|
|
|
|
onChange && onChange(value, label);
|
|
|
|
setShowMenu(false);
|
|
|
|
},
|
2021-02-09 20:35:33 +01:00
|
|
|
[onChange, options]
|
2021-02-02 05:28:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
setShowMenu(true);
|
|
|
|
}, [setShowMenu]);
|
|
|
|
|
|
|
|
const handleKeydown = useCallback(
|
|
|
|
(event) => {
|
|
|
|
switch (event.key) {
|
2021-02-09 20:35:33 +01:00
|
|
|
case 'Enter': {
|
|
|
|
if (!showMenu) {
|
|
|
|
setShowMenu(true);
|
|
|
|
setFocused(selected);
|
|
|
|
} else {
|
|
|
|
setSelected(focused);
|
|
|
|
onChange && onChange(options[focused].value, options[focused].label);
|
|
|
|
setShowMenu(false);
|
2021-02-02 05:28:25 +01:00
|
|
|
}
|
2021-02-09 20:35:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-02-02 05:28:25 +01:00
|
|
|
|
2021-02-09 20:35:33 +01:00
|
|
|
case 'ArrowDown': {
|
|
|
|
const newIndex = focused + 1;
|
|
|
|
newIndex < options.length && setFocused(newIndex);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-02 05:28:25 +01:00
|
|
|
|
2021-02-09 20:35:33 +01:00
|
|
|
case 'ArrowUp': {
|
|
|
|
const newIndex = focused - 1;
|
|
|
|
newIndex > -1 && setFocused(newIndex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no default
|
2021-02-02 05:28:25 +01:00
|
|
|
}
|
|
|
|
},
|
2021-02-09 20:35:33 +01:00
|
|
|
[onChange, options, showMenu, setShowMenu, setFocused, focused, selected]
|
2021-02-02 05:28:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleDismiss = useCallback(() => {
|
|
|
|
setShowMenu(false);
|
|
|
|
}, [setShowMenu]);
|
|
|
|
|
|
|
|
// Reset the state if the prop value changes
|
|
|
|
useEffect(() => {
|
|
|
|
const selectedIndex = Math.max(
|
|
|
|
options.findIndex(({ value }) => value === propSelected),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (propSelected && selectedIndex !== selected) {
|
|
|
|
setSelected(selectedIndex);
|
|
|
|
setFocused(selectedIndex);
|
|
|
|
}
|
2021-02-09 20:35:33 +01:00
|
|
|
// DO NOT include `selected`
|
|
|
|
}, [options, propSelected]); // eslint-disable-line react-hooks/exhaustive-deps
|
2021-02-02 05:28:25 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<TextField
|
|
|
|
inputRef={ref}
|
|
|
|
label={label}
|
|
|
|
onchange={onChange}
|
|
|
|
onclick={handleClick}
|
|
|
|
onkeydown={handleKeydown}
|
|
|
|
readonly
|
|
|
|
trailingIcon={showMenu ? ArrowDropup : ArrowDropdown}
|
|
|
|
value={options[selected]?.label}
|
|
|
|
/>
|
|
|
|
{showMenu ? (
|
2021-02-07 05:59:11 +01:00
|
|
|
<Menu className="rounded-t-none" onDismiss={handleDismiss} relativeTo={ref} widthRelative>
|
2021-02-02 05:28:25 +01:00
|
|
|
{options.map(({ value, label }, i) => (
|
|
|
|
<MenuItem key={value} label={label} focus={focused === i} onSelect={handleSelect} value={value} />
|
|
|
|
))}
|
|
|
|
</Menu>
|
|
|
|
) : null}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|