import { useMemo } from 'react'; import { Input, Text } from '@chakra-ui/react'; import { components } from 'react-select'; import { If, Select } from '~/components'; import { useColorValue } from '~/context'; import { useLGState, useDirective } from '~/hooks'; import { isSelectDirective } from '~/types'; import type { OptionProps } from 'react-select'; import type { TSelectOption, Directive } from '~/types'; import type { TQueryTarget } from './types'; function buildOptions(directive: Nullable): TSelectOption[] { if (directive !== null && isSelectDirective(directive)) { return directive.options.map(o => ({ value: o.value, label: o.name, description: o.description, })); } return []; } const Option = (props: OptionProps) => { const { label, data } = props; return ( {label}
{data.description}
); }; export const QueryTarget: React.FC = (props: TQueryTarget) => { const { name, register, onChange, placeholder } = props; const bg = useColorValue('white', 'whiteAlpha.100'); const color = useColorValue('gray.400', 'whiteAlpha.800'); const border = useColorValue('gray.100', 'whiteAlpha.50'); const placeholderColor = useColorValue('gray.600', 'whiteAlpha.700'); const { queryTarget, displayTarget } = useLGState(); const directive = useDirective(); const options = useMemo(() => buildOptions(directive), [directive]); function handleInputChange(e: React.ChangeEvent): void { displayTarget.set(e.target.value); onChange({ field: name, value: e.target.value }); } function handleSelectChange(e: TSelectOption | TSelectOption[]): void { if (!Array.isArray(e) && e !== null) { onChange({ field: name, value: e.value }); displayTarget.set(e.value); } } return ( <> ); };