Our Code Editor is editable, and the preview is live.
npm i @choc-ui/chakra-autocomplete
yarn add @choc-ui/chakra-autocomplete
import {AutoComplete,AutoCompleteInput,AutoCompleteItem,AutoCompleteList,AutoCompleteGroup,AutoCompleteFixedItem,} from '@choc-ui/chakra-autocomplete';
() => {const options = ["apple", "appoint", "zap", "cap", "japan"];return (<FlexboxSize="full"h="100vh"pos="absolute"bg={"gray.400"}_dark={{bg: "gray.600",}}p={30}justifyContent="center"><AutoComplete rollNavigation><AutoCompleteInput variant="filled" placeholder="Search..." autoFocus /><AutoCompleteList>{options.map((option, oid) => (<AutoCompleteItemkey={`option-${oid}`}value={option}textTransform="capitalize">{option}</AutoCompleteItem>))}</AutoCompleteList></AutoComplete></Flex>);};
You can create groups with the
AutoCompleteGroup
Component() => {const fruits = ["Apple", "Grape", "Pawpaw"];const countries = ["Korea", "Nigeria", "India"];return (<FlexboxSize="full"h="100vh"pos="absolute"bg={"gray.400"}_dark={{bg: "gray.600",}}p={30}justifyContent="center"><AutoComplete rollNavigation><AutoCompleteInput variant="filled" placeholder="Search..." autoFocus /><AutoCompleteList><AutoCompleteGroup title="Fruits" showDivider>{fruits.map((option, oid) => (<AutoCompleteItemkey={`fruits-${oid}`}value={option}textTransform="capitalize">{option}</AutoCompleteItem>))}</AutoCompleteGroup><AutoCompleteGroup title="countries" showDivider>{countries.map((option, oid) => (<AutoCompleteItemkey={`countries-${oid}`}value={option}textTransform="capitalize">{option}</AutoCompleteItem>))}</AutoCompleteGroup></AutoCompleteList></AutoComplete></Flex>);};
To access the internal state of the
AutoComplete
, use a function as children (commonly known as a render prop). You&aposll get access to the internal state isOpen
and method onClose
.Watch the Icon on the right.
() => {const options = ["apple", "appoint", "zap", "cap", "japan"];return (<FlexboxSize="full"h="100vh"pos="absolute"bg={"gray.400"}_dark={{bg: "gray.600",}}p={30}justifyContent="center"><AutoComplete rollNavigation>{({ isOpen }) => (<><InputGroup><AutoCompleteInput variant="filled" placeholder="Search..." /><InputRightElement><Icon as={isOpen ? ChevronRightIcon : ChevronDownIcon} /></InputRightElement></InputGroup><AutoCompleteList>{options.map((option, oid) => (<AutoCompleteItemkey={`optio-${oid}`}value={option}textTransform="capitalize"align="center">{option}</AutoCompleteItem>))}</AutoCompleteList></>)}</AutoComplete></Flex>);};
You can Render whatever you want. The
AutoCompleteItem
is a regular Chakra Boxes.() => {const people = [{name: "Dan Abramov",image: "https://bit.ly/dan-abramov",},{name: "Kent Dodds",image: "https://bit.ly/kent-c-dodds",},{name: "Segun Adebayo",image: "https://bit.ly/sage-adebayo",},{name: "Prosper Otemuyiwa",image: "https://bit.ly/prosper-baba",},{name: "Ryan Florence",image: "https://bit.ly/ryan-florence",},];return (<FlexboxSize="full"h="100vh"pos="absolute"bg={"gray.400"}_dark={{bg: "gray.600",}}p={30}justifyContent="center"><AutoComplete rollNavigation><AutoCompleteInput variant="filled" placeholder="Search..." autoFocus /><AutoCompleteList>{people.map((person, oid) => (<AutoCompleteItemkey={`option-${oid}`}value={person.name}textTransform="capitalize"align="center"><Avatar size="sm" name={person.name} src={person.image} /><Text ml="4">{person.name}</Text></AutoCompleteItem>))}</AutoCompleteList></AutoComplete></Flex>);};
Proudly developed in by Anuoluwapo Abraham