diff --git a/packages/ui/components/name-input.tsx b/packages/ui/components/name-input.tsx index 650d8211d8..bc55a62047 100644 --- a/packages/ui/components/name-input.tsx +++ b/packages/ui/components/name-input.tsx @@ -2,38 +2,92 @@ import { InputField, InputFieldComponent } from './input-field'; import { useAddressTemplates } from '@karrio/hooks/address'; import { formatAddress, isNone } from '@karrio/lib'; import { AddressType } from '@karrio/types'; -import React, { ChangeEvent } from 'react'; +import React, { ChangeEvent, RefObject } from 'react'; +import { Combobox } from '@headlessui/react'; interface NameInputComponent extends InputFieldComponent { onValueChange: (value: Partial, refresh?: boolean) => void; defaultValue?: string; disableSuggestion?: boolean; + label?: string; + fieldClass?: string; + controlClass?: string; + addonRight?: JSX.Element; + addonLeft?: JSX.Element; + ref?: RefObject; } -export const NameInput: React.FC = ({ disableSuggestion, onValueChange, ...props }) => { - const { query, filter, setFilter } = useAddressTemplates(); +export const NameInput: React.FC = ({ disableSuggestion, value, onValueChange, label, required, className, fieldClass, controlClass, children, ref, addonLeft, addonRight, ...props }) => { + const { query: { data: { address_templates } = {} }, filter, setFilter } = useAddressTemplates(); + const [query, setQuery] = React.useState(value as string || ""); - const onInput = (e: ChangeEvent) => { - e.preventDefault(); - const template = (query.data?.address_templates?.edges || []) - .find(t => t.node.address?.person_name === e.target.value) - ?.node; - let value = template?.address || { person_name: e.target.value }; - setFilter({ ...filter, keyword: e.target.value }) - onValueChange(value as Partial, !isNone(template)); - }; + const setSeltected = (e: AddressType | ChangeEvent) => { + if (e.hasOwnProperty('address_line1')) { + setQuery((e as any).address_line1) + onValueChange(e as AddressType, true); + } else { + (e as any).preventDefault(); + setQuery((e as any).target.value) + setFilter({ ...filter, keyword: (e as any).target.value }) + onValueChange({ person_name: (e as any).target.value } as Partial, false); + } + } return ( - - {!disableSuggestion && - {(query.data?.address_templates?.edges || []) - .map(({ node: template }) => ( - - )) - } - } - + <> + +
+ {label !== undefined && } +
+ {addonLeft && addonLeft} + + + {({ open }) => ( +
0) ? "is-active" : ""}`}> + (_ as any).person_name || query} + /> + + {!disableSuggestion && <> + + } + +
+ )} +
+ + {addonRight ? addonRight : <>} +
+ {children} +
+ + + ) };