UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines • 15.9 kB
{"version":3,"file":"autocomplete.cjs","names":["createSlotComponent","autocompleteStyle","XIcon","useGroupItemProps","useAutocomplete","createComboboxItem","createComboboxChildren","useInputBorder","ComboboxDescendantsContext","ComboboxContext","AutocompleteContext","useInputPropsContext","ChevronDownIcon","useComboboxGroupContext","useComboboxGroup","ComboboxGroupContext","styled","useAutocompleteOption","CheckIcon","MinusIcon"],"sources":["../../../../src/components/autocomplete/autocomplete.tsx"],"sourcesContent":["\"use client\"\n\nimport type { ReactElement, ReactNode } from \"react\"\nimport type {\n GenericsComponent,\n HTMLProps,\n HTMLStyledProps,\n ThemeProps,\n} from \"../../core\"\nimport type {\n ComboboxItem,\n UseComboboxGroupProps,\n} from \"../../hooks/use-combobox\"\nimport type { UseInputBorderProps } from \"../input\"\nimport type { PopupAnimationProps } from \"../popover\"\nimport type { AutocompleteStyle } from \"./autocomplete.style\"\nimport type {\n UseAutocompleteOptionProps,\n UseAutocompleteProps,\n UseAutocompleteReturn,\n} from \"./use-autocomplete\"\nimport { useMemo } from \"react\"\nimport { createSlotComponent, styled } from \"../../core\"\nimport {\n ComboboxContext,\n ComboboxDescendantsContext,\n ComboboxGroupContext,\n createComboboxChildren,\n createComboboxItem,\n useComboboxGroup,\n useComboboxGroupContext,\n} from \"../../hooks/use-combobox\"\nimport { cast, isArray } from \"../../utils\"\nimport { useGroupItemProps } from \"../group\"\nimport { CheckIcon, ChevronDownIcon, MinusIcon, XIcon } from \"../icon\"\nimport { InputGroup, useInputBorder, useInputPropsContext } from \"../input\"\nimport { Popover } from \"../popover\"\nimport { autocompleteStyle } from \"./autocomplete.style\"\nimport {\n AutocompleteContext,\n useAutocomplete,\n useAutocompleteOption,\n} from \"./use-autocomplete\"\n\ninterface ComponentContext\n extends Pick<UseAutocompleteReturn, \"getInputProps\" | \"getSeparatorProps\">,\n Pick<\n AutocompleteRootProps,\n \"emptyIcon\" | \"emptyProps\" | \"groupProps\" | \"inputProps\" | \"optionProps\"\n > {}\n\nexport interface AutocompleteRootProps<Multiple extends boolean = false>\n extends Omit<\n HTMLStyledProps,\n \"defaultValue\" | \"filter\" | \"offset\" | \"onChange\" | \"ref\" | \"value\"\n >,\n UseAutocompleteProps<Multiple>,\n PopupAnimationProps,\n ThemeProps<AutocompleteStyle>,\n UseInputBorderProps {\n /**\n * If `true`, display the clear icon.\n *\n * @default true\n */\n clearable?: boolean\n /**\n * The icon to be used in the clear button.\n */\n clearIcon?: ReactNode\n /**\n * The icon to be used in the empty element.\n */\n emptyIcon?: ReactNode\n /**\n * The icon to be used in the autocomplete.\n */\n icon?: ReactNode\n /**\n * Props for content element.\n */\n contentProps?: AutocompleteContentProps\n /**\n * The props for the end element.\n */\n elementProps?: InputGroup.ElementProps\n /**\n * Props for empty element.\n */\n emptyProps?: AutocompleteEmptyProps\n /**\n * Props for group element.\n */\n groupProps?: Omit<AutocompleteGroupProps, \"children\" | \"label\">\n /**\n * Props for icon element.\n */\n iconProps?: AutocompleteIconProps\n /**\n * The props for the input element.\n */\n inputProps?: HTMLStyledProps<\"input\">\n /**\n * Props for option element.\n */\n optionProps?: Omit<AutocompleteOptionProps, \"children\" | \"value\">\n /**\n * Props for root element.\n */\n rootProps?: InputGroup.RootProps\n}\n\nconst {\n ComponentContext,\n PropsContext: AutocompletePropsContext,\n useComponentContext,\n usePropsContext: useAutocompletePropsContext,\n withContext,\n withProvider,\n} = createSlotComponent<\n AutocompleteRootProps,\n AutocompleteStyle,\n ComponentContext\n>(\"autocomplete\", autocompleteStyle)\n\nexport { AutocompletePropsContext, useAutocompletePropsContext }\n\n/**\n * `Autocomplete` is a component used to display suggestions in response to user text input.\n *\n * @see https://yamada-ui.com/docs/components/autocomplete\n */\nexport const AutocompleteRoot = withProvider(\n <Multiple extends boolean = false>(\n props: AutocompleteRootProps<Multiple>,\n ) => {\n const [\n groupItemProps,\n {\n className,\n css,\n colorScheme,\n animationScheme = \"block-start\",\n children,\n clearable = true,\n clearIcon = <XIcon />,\n duration,\n emptyIcon,\n errorBorderColor,\n focusBorderColor,\n icon,\n items: itemsProp,\n contentProps,\n elementProps,\n emptyProps,\n groupProps,\n iconProps,\n inputProps,\n optionProps,\n rootProps,\n ...rest\n },\n ] = useGroupItemProps(props)\n const items = useMemo<ComboboxItem[]>(() => {\n if (itemsProp) return itemsProp\n\n return createComboboxItem(children, {\n Group: AutocompleteGroup,\n Label: AutocompleteLabel,\n Option: AutocompleteOption,\n })\n }, [itemsProp, children])\n const {\n children: fieldChildren,\n descendants,\n items: computedItems,\n max,\n value,\n getClearIconProps,\n getContentProps,\n getFieldProps,\n getIconProps,\n getInputProps,\n getRootProps,\n getSeparatorProps,\n popoverProps,\n onActiveDescendant,\n onClose,\n onSelect,\n } = useAutocomplete({ items, ...rest })\n const mergedPopoverProps = useMemo<Popover.RootProps>(\n () => ({\n animationScheme,\n duration,\n ...popoverProps,\n }),\n [animationScheme, duration, popoverProps],\n )\n const computedChildren = useMemo(\n () =>\n createComboboxChildren(computedItems, {\n Empty: AutocompleteEmpty,\n Group: AutocompleteGroup,\n Option: AutocompleteOption,\n }),\n [computedItems],\n )\n const varProps = useInputBorder({ errorBorderColor, focusBorderColor })\n const comboboxContext = useMemo(\n () => ({ onActiveDescendant, onClose, onSelect }),\n [onActiveDescendant, onClose, onSelect],\n )\n const autocompleteContext = useMemo(() => ({ max, value }), [value, max])\n const componentContext = useMemo(\n () => ({\n emptyIcon,\n emptyProps,\n getInputProps,\n getSeparatorProps,\n groupProps,\n inputProps,\n optionProps,\n }),\n [\n emptyIcon,\n emptyProps,\n getInputProps,\n getSeparatorProps,\n groupProps,\n inputProps,\n optionProps,\n ],\n )\n const hasValue = isArray(value) ? !!value.length : !!value\n\n return (\n <ComboboxDescendantsContext value={descendants}>\n <ComboboxContext value={comboboxContext}>\n <AutocompleteContext value={autocompleteContext}>\n <ComponentContext value={componentContext}>\n <Popover.Root {...mergedPopoverProps}>\n <InputGroup.Root\n className={className}\n css={css}\n colorScheme={colorScheme}\n {...getRootProps({ ...groupItemProps, ...rootProps })}\n >\n <Popover.Trigger>\n <AutocompleteField {...getFieldProps(varProps)}>\n {fieldChildren}\n </AutocompleteField>\n </Popover.Trigger>\n\n <InputGroup.Element\n {...{ clickable: clearable && hasValue, ...elementProps }}\n >\n {clearable && hasValue ? (\n <AutocompleteIcon\n icon={clearIcon}\n {...getClearIconProps(iconProps)}\n />\n ) : (\n <AutocompleteIcon\n icon={icon}\n {...getIconProps(iconProps)}\n />\n )}\n </InputGroup.Element>\n </InputGroup.Root>\n\n <AutocompleteContent\n {...cast<AutocompleteContentProps>(\n getContentProps(cast<HTMLProps>(contentProps)),\n )}\n >\n {computedChildren}\n </AutocompleteContent>\n </Popover.Root>\n </ComponentContext>\n </AutocompleteContext>\n </ComboboxContext>\n </ComboboxDescendantsContext>\n )\n },\n \"root\",\n)((props) => {\n const context = useInputPropsContext()\n\n return { ...context, ...props }\n}) as GenericsComponent<{\n <Multiple extends boolean = false>(\n props: AutocompleteRootProps<Multiple>,\n ): ReactElement\n}>\n\ninterface AutocompleteFieldProps extends HTMLStyledProps {}\n\nconst AutocompleteField = withContext<\"div\", AutocompleteFieldProps>(\n \"div\",\n \"field\",\n)({ \"data-group-propagate\": \"\" }, ({ children, ...rest }) => {\n const { getInputProps, inputProps } = useComponentContext()\n\n return {\n ...rest,\n children: (\n <>\n {children}\n <AutocompleteInput {...getInputProps(inputProps)} />\n </>\n ),\n }\n})\n\ninterface AutocompleteInputProps extends HTMLStyledProps<\"input\"> {}\n\nconst AutocompleteInput = withContext<\"input\", AutocompleteInputProps>(\n \"input\",\n \"input\",\n)()\n\ninterface AutocompleteIconProps extends HTMLStyledProps {\n icon?: ReactNode\n}\n\nconst AutocompleteIcon = withContext<\"div\", AutocompleteIconProps>(\n \"div\",\n \"icon\",\n)(undefined, ({ children, icon, ...rest }) => ({\n children: icon || children || <ChevronDownIcon />,\n ...rest,\n}))\n\ninterface AutocompleteContentProps extends Popover.ContentProps {}\n\nconst AutocompleteContent = withContext<\"div\", AutocompleteContentProps>(\n Popover.Content,\n \"content\",\n)()\n\nexport interface AutocompleteLabelProps extends HTMLStyledProps<\"span\"> {}\n\nexport const AutocompleteLabel = withContext<\"span\", AutocompleteLabelProps>(\n \"span\",\n \"label\",\n)(undefined, (props) => {\n const { getLabelProps } = useComboboxGroupContext()\n\n return getLabelProps(props)\n})\n\nexport interface AutocompleteGroupProps\n extends UseComboboxGroupProps,\n HTMLStyledProps {\n /**\n * The label of the group.\n */\n label?: ReactNode\n /**\n * Props for the label component.\n */\n labelProps?: AutocompleteLabelProps\n}\n\nexport const AutocompleteGroup = withContext<\"div\", AutocompleteGroupProps>(\n ({ children, label, labelProps, ...rest }) => {\n const { groupProps } = useComponentContext()\n const { getGroupProps, getLabelProps } = useComboboxGroup({\n ...groupProps,\n ...rest,\n })\n const context = useMemo(() => ({ getLabelProps }), [getLabelProps])\n\n return (\n <ComboboxGroupContext value={context}>\n <styled.div {...getGroupProps()}>\n {label ? (\n <AutocompleteLabel {...labelProps}>{label}</AutocompleteLabel>\n ) : null}\n {children}\n </styled.div>\n </ComboboxGroupContext>\n )\n },\n \"group\",\n)()\n\nexport interface AutocompleteOptionProps\n extends UseAutocompleteOptionProps,\n HTMLStyledProps {\n /**\n * The icon to be used in the autocomplete option.\n */\n icon?: ReactNode\n}\n\nexport const AutocompleteOption = withContext<\"div\", AutocompleteOptionProps>(\n ({ children, icon: iconProp, ...rest }) => {\n const { optionProps: { icon, ...optionProps } = {} } = useComponentContext()\n const { getIndicatorProps, getOptionProps } = useAutocompleteOption({\n ...optionProps,\n ...rest,\n })\n\n return (\n <styled.div {...getOptionProps()}>\n <AutocompleteIndicator {...getIndicatorProps()}>\n {iconProp ?? icon ?? <CheckIcon />}\n </AutocompleteIndicator>\n {children}\n </styled.div>\n )\n },\n \"option\",\n)()\n\ninterface AutocompleteEmptyProps extends HTMLStyledProps {\n /**\n * The icon to be used in the autocomplete option.\n */\n icon?: ReactNode\n}\n\nconst AutocompleteEmpty = withContext<\"div\", AutocompleteEmptyProps>(\n ({ children, icon, ...rest }) => {\n const { emptyIcon, emptyProps } = useComponentContext()\n\n return (\n <styled.div {...emptyProps} {...rest}>\n <AutocompleteIndicator>\n {icon ?? emptyIcon ?? <MinusIcon />}\n </AutocompleteIndicator>\n {children}\n </styled.div>\n )\n },\n \"empty\",\n)()\n\ninterface AutocompleteIndicatorProps extends HTMLStyledProps {}\n\nconst AutocompleteIndicator = withContext<\"div\", AutocompleteIndicatorProps>(\n \"div\",\n \"indicator\",\n)()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAgHA,MAAM,EACJ,kBACA,cAAc,0BACd,qBACA,iBAAiB,6BACjB,aACA,iBACEA,6CAIF,gBAAgBC,6CAAkB;;;;;;AASpC,MAAa,mBAAmB,cAE5B,UACG;CACH,MAAM,CACJ,gBACA,EACE,WACA,KACA,aACA,kBAAkB,eAClB,UACA,YAAY,MACZ,YAAY,2CAACC,yBAAQ,EACrB,UACA,WACA,kBACA,kBACA,MACA,OAAO,WACP,cACA,cACA,YACA,YACA,WACA,YACA,aACA,UACA,GAAG,UAEHC,oCAAkB,MAAM;CAU5B,MAAM,EACJ,UAAU,eACV,aACA,OAAO,eACP,KACA,OACA,mBACA,iBACA,eACA,cACA,eACA,cACA,mBACA,cACA,oBACA,SACA,aACEC,yCAAgB;EAAE,gCA1BsB;AAC1C,OAAI,UAAW,QAAO;AAEtB,UAAOC,oDAAmB,UAAU;IAClC,OAAO;IACP,OAAO;IACP,QAAQ;IACT,CAAC;KACD,CAAC,WAAW,SAAS,CAAC;EAkBI,GAAG;EAAM,CAAC;CACvC,MAAM,+CACG;EACL;EACA;EACA,GAAG;EACJ,GACD;EAAC;EAAiB;EAAU;EAAa,CAC1C;CACD,MAAM,4CAEFC,wDAAuB,eAAe;EACpC,OAAO;EACP,OAAO;EACP,QAAQ;EACT,CAAC,EACJ,CAAC,cAAc,CAChB;CAC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