import * as React from 'react'; import { Box as ReakitBox } from 'reakit'; import ConditionalWrap from 'conditional-wrap'; import { useClassName, createComponent, createElement, createHook } from '../utils'; import { Box, BoxProps } from '../Box'; import { Group, GroupProps } from '../Group'; import { FieldWrapper, FieldWrapperProps } from '../FieldWrapper'; import { Autosuggest, AutosuggestProps } from './Autosuggest'; import * as styles from './Autosuggest.styles'; export type LocalAutosuggestFieldProps = { /** Addon component to the input (before). Similar to the addon components in Input. */ addonBefore?: React.ReactElement; /** Addon component to the input (after). Similar to the addon components in Input. */ addonAfter?: React.ReactElement; /** Additional props for the Autosuggest component */ autosuggestProps?: Partial; /** If addonBefore or addonAfter exists, then the addons will render vertically. */ orientation?: 'vertical' | 'horizontal'; }; export type AutosuggestFieldProps = BoxProps & FieldWrapperProps & AutosuggestProps & LocalAutosuggestFieldProps; const useProps = createHook( (props, { themeKey }) => { const { addonAfter, addonBefore, automaticSelection, autosuggestProps, cacheKey, containLabel, children, defaultValue, defer, description, disabled, errorText, emptyText, hint, isLoading, isOptional, isRequired, orientation, label, limit, loadingText, loadingMoreText, loadOptions, loadVariables, options, pagination, placeholder, restrictToOptions, renderError, renderClearButton, renderEmpty, renderLoading, renderLoadingMore, renderOption, clearButtonProps, inputProps, itemProps, popoverProps, dropdownMenuInitialState, state, tooltip, tooltipTriggerComponent, value, onChange, overrides, validationText, variant, ...restProps } = props; const boxProps = Box.useProps(restProps); const className = useClassName({ style: styles.AutosuggestField, styleProps: props, themeKey, prevClassName: boxProps.className, }); return { ...boxProps, className, children: ( {({ elementProps }) => ( ( {children} )} > {addonBefore} {addonAfter} )} ), }; }, { themeKey: 'AutosuggestField' } ); export const AutosuggestField = createComponent( (props) => { const AutosuggestFieldProps = useProps(props); return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: AutosuggestFieldProps, }); }, { attach: { useProps, displayName: 'Autosuggest.Field', }, themeKey: 'AutosuggestField', } );