|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Autocomplete } from '@base-ui/react/autocomplete'; |
| 4 | +import { cx, useThemeMode } from 'antd-style'; |
| 5 | +import { XIcon } from 'lucide-react'; |
| 6 | +import { memo, useMemo, useRef } from 'react'; |
| 7 | + |
| 8 | +import { inputStyles, inputVariants } from '@/base-ui/Input'; |
| 9 | +import Icon from '@/Icon'; |
| 10 | +import { useAppElement } from '@/ThemeProvider'; |
| 11 | + |
| 12 | +import { styles } from './style'; |
| 13 | +import type { AutoCompleteOption, AutoCompleteProps } from './type'; |
| 14 | + |
| 15 | +const AutoComplete = memo<AutoCompleteProps>( |
| 16 | + ({ |
| 17 | + className, |
| 18 | + classNames, |
| 19 | + styles: customStyles, |
| 20 | + style, |
| 21 | + variant, |
| 22 | + shadow, |
| 23 | + size = 'middle', |
| 24 | + options = [], |
| 25 | + onChange, |
| 26 | + onSearch, |
| 27 | + allowClear, |
| 28 | + disabled, |
| 29 | + placeholder, |
| 30 | + prefix, |
| 31 | + suffix, |
| 32 | + emptyText, |
| 33 | + ...rest |
| 34 | + }) => { |
| 35 | + const { isDarkMode } = useThemeMode(); |
| 36 | + const appElement = useAppElement(); |
| 37 | + const anchorRef = useRef<HTMLDivElement>(null); |
| 38 | + const mergedVariant = variant || (isDarkMode ? 'filled' : 'outlined'); |
| 39 | + |
| 40 | + const items = useMemo<AutoCompleteOption[]>( |
| 41 | + () => options.map((option) => (typeof option === 'string' ? { value: option } : option)), |
| 42 | + [options], |
| 43 | + ); |
| 44 | + |
| 45 | + return ( |
| 46 | + <Autocomplete.Root |
| 47 | + openOnInputClick |
| 48 | + disabled={disabled} |
| 49 | + itemToStringValue={(item) => (item as AutoCompleteOption).value} |
| 50 | + items={items} |
| 51 | + onValueChange={(value) => { |
| 52 | + onChange?.(value); |
| 53 | + onSearch?.(value); |
| 54 | + }} |
| 55 | + {...rest} |
| 56 | + > |
| 57 | + <div |
| 58 | + className={cx(inputVariants({ shadow, size, variant: mergedVariant }), className)} |
| 59 | + data-disabled={disabled ? '' : undefined} |
| 60 | + ref={anchorRef} |
| 61 | + style={style} |
| 62 | + > |
| 63 | + {prefix && <span className={inputStyles.slot}>{prefix}</span>} |
| 64 | + <Autocomplete.Input |
| 65 | + className={cx(inputStyles.input, classNames?.input)} |
| 66 | + placeholder={placeholder} |
| 67 | + style={customStyles?.input} |
| 68 | + /> |
| 69 | + {allowClear && ( |
| 70 | + <Autocomplete.Clear aria-label={'Clear'} className={styles.clear}> |
| 71 | + <Icon icon={XIcon} size={14} /> |
| 72 | + </Autocomplete.Clear> |
| 73 | + )} |
| 74 | + {suffix && <span className={inputStyles.slot}>{suffix}</span>} |
| 75 | + </div> |
| 76 | + <Autocomplete.Portal container={appElement ?? undefined}> |
| 77 | + <Autocomplete.Positioner anchor={anchorRef} className={styles.positioner} sideOffset={4}> |
| 78 | + <Autocomplete.Popup |
| 79 | + className={cx(styles.popup, classNames?.popup)} |
| 80 | + style={customStyles?.popup} |
| 81 | + > |
| 82 | + {emptyText && ( |
| 83 | + <Autocomplete.Empty className={styles.empty}>{emptyText}</Autocomplete.Empty> |
| 84 | + )} |
| 85 | + <Autocomplete.List className={styles.list}> |
| 86 | + {(item: AutoCompleteOption) => ( |
| 87 | + <Autocomplete.Item |
| 88 | + className={cx(styles.item, classNames?.item)} |
| 89 | + disabled={item.disabled} |
| 90 | + key={item.value} |
| 91 | + style={customStyles?.item} |
| 92 | + value={item} |
| 93 | + > |
| 94 | + {item.label ?? item.value} |
| 95 | + </Autocomplete.Item> |
| 96 | + )} |
| 97 | + </Autocomplete.List> |
| 98 | + </Autocomplete.Popup> |
| 99 | + </Autocomplete.Positioner> |
| 100 | + </Autocomplete.Portal> |
| 101 | + </Autocomplete.Root> |
| 102 | + ); |
| 103 | + }, |
| 104 | +); |
| 105 | + |
| 106 | +AutoComplete.displayName = 'AutoComplete'; |
| 107 | + |
| 108 | +export default AutoComplete; |
0 commit comments