1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-17 08:48:05 +00:00
thatmattlove-hyperglass/hyperglass/ui/components/form/queryGroup.tsx
2021-09-10 23:13:27 -07:00

40 lines
1.1 KiB
TypeScript

import { useMemo } from 'react';
import { Select } from '~/components';
import { useLGMethods, useLGState } from '~/hooks';
import type { TSelectOption } from '~/types';
import type { TQueryGroup } from './types';
export const QueryGroup: React.FC<TQueryGroup> = (props: TQueryGroup) => {
const { onChange, label } = props;
const { selections, availableGroups, queryLocation } = useLGState();
const { exportState } = useLGMethods();
const options = useMemo<TSelectOption[]>(
() => availableGroups.map(g => ({ label: g.value, value: g.value })),
// eslint-disable-next-line react-hooks/exhaustive-deps
[availableGroups, queryLocation],
);
function handleChange(e: TSelectOption | TSelectOption[]): void {
let value = '';
if (!Array.isArray(e) && e !== null) {
selections.queryGroup.set(e);
value = e.value;
} else {
selections.queryGroup.set(null);
}
onChange({ field: 'queryGroup', value });
}
return (
<Select
size="lg"
name="queryGroup"
options={options}
aria-label={label}
onChange={handleChange}
value={exportState(selections.queryGroup.value)}
/>
);
};