G
Tokens
TypographySpacingColorRadiusShadowMotion
Foundational
FieldMenuTextbox

Autocomplete

Autocomplete is deprecated, use Autocomplete2 instead

Options


    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: 'Neopan 100 Acros' },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          hint="Choose your favorite film"
          options={options}
          value={option.value}
          onChange={(option) => setOption(option)}
        />
      )
    }

Grouped menu


    () => {
      const options = Object.values(demoFilmsObject).map((film, index) => {
        return { value: film.name, content: film.name, groupTitle: film.company, }
      })
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)

      return (
        <Autocomplete
          label="Film"
          hint="Choose your favorite film"
          options={options}
          value={option.value}
          onChange={(option) => setOption(option)}
        />
      )
    }



    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: 'Neopan 100 Acros' },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      const sharedProps = {        
        options: options,
        value: option.value,
        onChange: (option) => setOption(option),
        menuWidth: '240px',
      }
      return (
        <Arrange gap="s">
          <Autocomplete menuPlacement="bottomStart" {...sharedProps} />
          <Autocomplete menuPlacement="bottomEnd" {...sharedProps} />
          <Autocomplete menuPlacement="topStart" {...sharedProps} />
          <Autocomplete menuPlacement="topEnd" {...sharedProps} />
        </Arrange>
      )
    }

Use the menuItemsHaveEllipsis to disable ellipsis on menu items


    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: demoText.l },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          options={options}
          value={option.value}
          onChange={(option) => setOption(option)}
          menuItemsHaveEllipsis={false}
        />
      )
    }

With error


    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: 'Neopan 100 Acros' },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = { value: '', content: ''}
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          value={option.value}
          hint="Choose your favorite film"
          options={options}
          onChange={(option) => setOption(option)}
          hasError
          errorMessage="This field is required"
        />
      )
    }

Pre content


    () => {
      const Pre = ({backgroundColor}) => (
        <Box 
          marginLeft="6px" 
          backgroundColor={backgroundColor} 
          paddingY="xs"
          paddingX="s"
          radius="xs"
        >
          <Text color="white" variant="caps">PRE</Text>
        </Box>
      )
      const options = [
        { 
          value: 'Neopan 100 Acros', 
          content: 'Neopan 100 Acros',
          preContent: <Pre backgroundColor="red"/>
        },
        { 
          value: 'Fujicolor Pro 160NS', 
          content: 'Fujicolor Pro 160NS',
          preContent: <Pre backgroundColor="blue"/>
        },
        { 
          value: 'Fujicolor Superior 200', 
          content: 'Fujicolor Superior 200',
          preContent: <Pre backgroundColor="green"/>
        },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          value={option.value}
          hint="Choose your favorite film"
          options={options}
          onChange={(option) => setOption(option)}
        />
      )
    }

Create new option


    () => {
      const [options, setOptions] = useState([
        {
          value: 'Neopan 100 Acros',
          content: 'Neopan 100 Acros',
        },
        {
          value: 'Fujicolor Pro 160NS',
          content: 'Fujicolor Pro 160NS',
        },
        {
          value: 'Fujicolor Superior 200',
          content: 'Fujicolor Superior 200',
        },
      ])

      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          value={option.value}
          hint="Choose your favorite film"
          options={options}
          onChange={(option) => setOption(option)}
          isCreatable
          onCreate={(option) => {
            setOption(option)
            setOptions([...options, option])
          }}
        />
      )
    }


Background color


    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: 'Neopan 100 Acros' },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Box padding="l2" backgroundColor="background2" borderSide="all" radius="m">
          <Autocomplete
            backgroundColor="background"
            label="Film"
            hint="Choose your favorite film"
            options={options}
            value={option.value}
            onChange={(option) => setOption(option)}
          />
        </Box>
      )
    }

Clearable


    () => {
      const options = [
        { value: 'Neopan 100 Acros', content: 'Neopan 100 Acros' },
        { value: 'Fujicolor Pro 160NS', content: 'Fujicolor Pro 160NS' },
        { value: 'Fujicolor Superior 200', content: 'Fujicolor Superior 200' },
      ];
      const defaultOption = options[0];
      const [option, setOption] = useState(defaultOption)
      return (
        <Autocomplete
          label="Film"
          hint="Choose your favorite film"
          options={options}
          value={option.value}
          onChange={(option) => setOption(option)}
          onClear={() => setOption({})}
        />
      )
    }

Props

Name
Type
Default
menuPlacement
oneOf
'top' 'topStart' 'topEnd' 'bottom' 'bottomStart' 'bottomEnd'
'bottomStart'
menuMaxHeight
oneOfType
number
string
'468px'
menuItemsHaveEllipsis
bool
true
hasPortal
bool
true
searchField
'content'
options
array
value
string
onChangeREQUIRED
func
isCreatable
bool
onCreate
func
menuWidth
oneOfType
number
string
menuZIndex
oneOfType
number
string
label
oneOfType
string
node
hint
oneOfType
string
node
hasError
bool
errorMessage
string
backgroundColor
oneOfType
Object.keys(vars.colors)
string
onClear
func