Archive
AutoComplete

AutoComplete

Example

Default

Steps

Prerequisite

Reset CSS

datalist 고유의 picker icon을 삭제해 줍니다.

styles/global.css
input::-webkit-calendar-picker-indicator {
  opacity: 0;
}

Copy Code

components/AutoComplete/index.tsx
import { useId } from 'react'
import type { FC, OptionHTMLAttributes, ComponentProps } from 'react'
import { Input } from 'components'
 
interface Props extends ComponentProps<typeof Input> {
  options: Array<OptionHTMLAttributes<HTMLOptionElement>['value']>
  id?: string
}
 
const Autocomplete: FC<Props> = ({ options, ...props }) => {
  const id = useId()
  return (
    <>
      <Input {...props} list={props.id || id} />
      <datalist id={props.id || id}>
        {options.map((value, key) => (
          <option value={value} key={key} />
        ))}
      </datalist>
    </>
  )
}
 
export default Autocomplete

Usage

<AutoComplete
  options={['Apple', 'Amazon', 'Facebook', 'Tesla', 'Microsoft', 'Google']}
/>

Props

NameTypeDefault
optionsArray<string>
idstring

References


© 2023 kidow. All rights reserved.
안녕하세요?