blakeblackshear.frigate/web/src/components/Select.jsx

257 lines
7.4 KiB
React
Raw Normal View History

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 DatePicker from './DatePicker';
2022-02-26 20:11:00 +01:00
import Calendar from './Calendar';
2021-02-02 05:28:25 +01:00
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
export default function Select({
type,
label,
onChange,
paramName,
options: inputOptions = [],
selected: propSelected,
}) {
2021-02-02 05:28:25 +01:00
const options = useMemo(
() =>
typeof inputOptions[0] === 'string' ? inputOptions.map((opt) => ({ value: opt, label: opt })) : inputOptions,
[inputOptions]
);
2021-02-02 05:28:25 +01:00
const [showMenu, setShowMenu] = useState(false);
const [selected, setSelected] = useState();
const [datePickerValue, setDatePickerValue] = useState();
// Reset the state if the prop value changes
useEffect(() => {
const selectedIndex = Math.max(
2021-02-02 05:28:25 +01:00
options.findIndex(({ value }) => value === propSelected),
0
);
if (propSelected && selectedIndex !== selected) {
setSelected(selectedIndex);
setFocused(selectedIndex);
}
// DO NOT include `selected`
}, [options, propSelected]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (type === 'datepicker') {
if ('after' && 'before' in propSelected) {
if (!propSelected.before || !propSelected.after) return setDatePickerValue('all');
2021-02-02 05:28:25 +01:00
for (let i = 0; i < inputOptions.length; i++) {
if (
inputOptions[i].value &&
Object.entries(inputOptions[i].value).sort().toString() === Object.entries(propSelected).sort().toString()
) {
setDatePickerValue(inputOptions[i]?.label);
break;
} else {
setDatePickerValue(
`${new Date(propSelected.after * 1000).toLocaleDateString()} -> ${new Date(
propSelected.before * 1000 - 1
).toLocaleDateString()}`
);
}
}
}
}
if (type === 'dropdown') {
setSelected(
Math.max(
options.findIndex(({ value }) => Object.values(propSelected).includes(value)),
0
)
);
}
}, [type, options, inputOptions, propSelected, setSelected]);
const [focused, setFocused] = useState(null);
2022-02-26 20:11:00 +01:00
const [showCalendar, setShowCalendar] = useState(false);
const calendarRef = useRef(null);
2021-02-02 05:28:25 +01:00
const ref = useRef(null);
const handleSelect = useCallback(
(value) => {
setSelected(options.findIndex(({ value }) => Object.values(propSelected).includes(value)));
setShowMenu(false);
2022-02-26 20:11:00 +01:00
//show calendar date range picker
if (value === 'custom_range') return setShowCalendar(true);
onChange && onChange(value);
},
[onChange, options, propSelected, setSelected]
);
const handleDateRange = useCallback(
(range) => {
onChange && onChange(range);
2021-02-02 05:28:25 +01:00
setShowMenu(false);
},
[onChange]
2021-02-02 05:28:25 +01:00
);
const handleClick = useCallback(() => {
setShowMenu(true);
}, [setShowMenu]);
const handleKeydownDatePicker = useCallback(
2021-02-02 05:28:25 +01:00
(event) => {
switch (event.key) {
2021-02-11 16:16:35 +01:00
case 'Enter': {
if (!showMenu) {
setShowMenu(true);
setFocused(selected);
} else {
setSelected(focused);
if (options[focused].value === 'custom_range') {
setShowMenu(false);
2022-02-26 20:11:00 +01:00
return setShowCalendar(true);
}
onChange && onChange(options[focused].value);
2021-02-11 16:16:35 +01:00
setShowMenu(false);
}
break;
2021-02-02 05:28:25 +01:00
}
2021-02-11 16:16:35 +01:00
case 'ArrowDown': {
event.preventDefault();
2021-02-11 16:16:35 +01:00
const newIndex = focused + 1;
newIndex < options.length && setFocused(newIndex);
break;
}
2021-02-02 05:28:25 +01:00
2021-02-11 16:16:35 +01:00
case 'ArrowUp': {
event.preventDefault();
2021-02-11 16:16:35 +01:00
const newIndex = focused - 1;
newIndex > -1 && setFocused(newIndex);
break;
}
// no default
2021-02-02 05:28:25 +01:00
}
},
[onChange, options, showMenu, setShowMenu, setFocused, focused, selected]
2021-02-02 05:28:25 +01:00
);
const handleKeydown = useCallback(
(event) => {
switch (event.key) {
case 'Enter': {
if (!showMenu) {
setShowMenu(true);
setFocused(selected);
} else {
setSelected(focused);
onChange && onChange({ [paramName]: options[focused].value });
setShowMenu(false);
}
break;
}
case 'ArrowDown': {
event.preventDefault();
const newIndex = focused + 1;
newIndex < options.length && setFocused(newIndex);
break;
}
case 'ArrowUp': {
event.preventDefault();
const newIndex = focused - 1;
newIndex > -1 && setFocused(newIndex);
break;
}
// no default
}
},
[onChange, options, showMenu, setShowMenu, setFocused, focused, selected, paramName]
);
2021-02-02 05:28:25 +01:00
const handleDismiss = useCallback(() => {
setShowMenu(false);
}, [setShowMenu]);
const findDOMNodes = (component) => {
return (component && (component.base || (component.nodeType === 1 && component))) || null;
};
2021-02-02 05:28:25 +01:00
useEffect(() => {
const addBackDrop = (e) => {
2022-02-26 20:11:00 +01:00
if (showCalendar && !findDOMNodes(calendarRef.current).contains(e.target)) {
setShowCalendar(false);
}
};
window.addEventListener('click', addBackDrop);
2021-02-02 05:28:25 +01:00
return function cleanup() {
window.removeEventListener('click', addBackDrop);
};
2022-02-26 20:11:00 +01:00
}, [showCalendar]);
switch (type) {
case 'datepicker':
return (
<Fragment>
<DatePicker
inputRef={ref}
label={label}
onchange={onChange}
onclick={handleClick}
onkeydown={handleKeydownDatePicker}
trailingIcon={showMenu ? ArrowDropup : ArrowDropdown}
value={datePickerValue}
/>
2022-02-26 20:11:00 +01:00
{showCalendar && (
<Menu className="rounded-t-none" onDismiss={handleDismiss} relativeTo={ref}>
2022-02-26 20:11:00 +01:00
<Calendar onChange={handleDateRange} calendarRef={calendarRef} close={() => setShowCalendar(false)} />
</Menu>
)}
{showMenu ? (
<Menu className="rounded-t-none" onDismiss={handleDismiss} relativeTo={ref} widthRelative>
{options.map(({ value, label }, i) => (
<MenuItem key={value} label={label} focus={focused === i} onSelect={handleSelect} value={value} />
))}
</Menu>
) : null}
</Fragment>
);
2022-02-26 20:11:00 +01:00
// case 'dropdown':
default:
return (
<Fragment>
<TextField
inputRef={ref}
label={label}
onchange={onChange}
onclick={handleClick}
onkeydown={handleKeydown}
readonly
trailingIcon={showMenu ? ArrowDropup : ArrowDropdown}
value={options[selected]?.label}
/>
{showMenu ? (
<Menu className="rounded-t-none" onDismiss={handleDismiss} relativeTo={ref} widthRelative>
{options.map(({ value, label }, i) => (
<MenuItem
key={value}
label={label}
focus={focused === i}
onSelect={handleSelect}
value={{ [paramName]: value }}
/>
))}
</Menu>
) : null}
</Fragment>
);
}
2021-02-02 05:28:25 +01:00
}