diff --git a/hyperglass/ui/components/looking-glass-form.tsx b/hyperglass/ui/components/looking-glass-form.tsx index 5d8a106..fc879be 100644 --- a/hyperglass/ui/components/looking-glass-form.tsx +++ b/hyperglass/ui/components/looking-glass-form.tsx @@ -149,8 +149,11 @@ export const LookingGlassForm = (): JSX.Element => { } else if (e.field === 'queryTarget') { if (isString(e.value)) { setFormValue('queryTarget', [e.value]); - } else if (Array.isArray(e.value)) { + setValue('queryTarget', [e.value]); + } + if (Array.isArray(e.value)) { setFormValue('queryTarget', e.value); + setValue('queryTarget', e.value); } } } diff --git a/hyperglass/ui/components/query-target.tsx b/hyperglass/ui/components/query-target.tsx index 8d17aa9..1d452f9 100644 --- a/hyperglass/ui/components/query-target.tsx +++ b/hyperglass/ui/components/query-target.tsx @@ -1,17 +1,16 @@ -import { useMemo } from 'react'; import { Input, InputGroup, InputRightElement, Text } from '@chakra-ui/react'; +import { useMemo } from 'react'; import { components } from 'react-select'; -import { If, Then, Else } from 'react-if'; import { Select } from '~/components'; import { isSingleValue } from '~/components/select'; -import { useColorValue, useDirective, useFormState } from '~/hooks'; +import { useDirective, useFormState } from '~/hooks'; import { isSelectDirective } from '~/types'; import { UserIP } from './user-ip'; -import type { OptionProps, GroupBase } from 'react-select'; -import type { UseFormRegister } from 'react-hook-form'; +import { type UseFormRegister, useForm } from 'react-hook-form'; +import type { GroupBase, OptionProps } from 'react-select'; import type { SelectOnChange } from '~/components/select'; -import type { Directive, SingleOption, OnChangeArgs, FormData } from '~/types'; +import type { Directive, FormData, OnChangeArgs, SingleOption } from '~/types'; type OptionWithDescription = SingleOption<{ description: string | null }>; @@ -49,16 +48,13 @@ const Option = (props: OptionProps) => { export const QueryTarget = (props: QueryTargetProps): JSX.Element => { const { name, register, onChange, placeholder } = props; - const bg = useColorValue('white', 'blackSolid.800'); - const color = useColorValue('gray.400', 'whiteAlpha.800'); - const border = useColorValue('gray.100', 'whiteAlpha.50'); - const placeholderColor = useColorValue('gray.600', 'whiteAlpha.700'); const displayTarget = useFormState(s => s.target.display); const setTarget = useFormState(s => s.setTarget); - const form = useFormState(s => s.form); + const queryTarget = useFormState(s => s.form.queryTarget); const directive = useDirective(); const options = useMemo(() => buildOptions(directive), [directive]); + const isSelect = useMemo(() => directive !== null && isSelectDirective(directive), [directive]); function handleInputChange(e: React.ChangeEvent): void { setTarget({ display: e.target.value }); @@ -72,43 +68,46 @@ export const QueryTarget = (props: QueryTargetProps): JSX.Element => { } }; + const handleUserIPChange = (target: string): void => { + setTarget({ display: target }); + onChange({ field: name, value: target }); + }; + return ( <> - - - - - name={name} - options={options} - components={{ Option }} - onChange={handleSelectChange} + + {isSelect ? ( + + name={name} + options={options} + components={{ Option }} + onChange={handleSelectChange} + /> + ) : ( + + - - - - - - { - setTarget({ display: target }); - onChange({ field: name, value: target }); - }} - /> - - - - + + + + + )} ); };