• Stars
    star
    303
  • Rank 137,655 (Top 3 %)
  • Language
    TypeScript
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An utility autocomplete UI library to use with Chakra UI

Chakra-UI AutoComplete

All Contributors

Financial Contributors on Open Collective All Contributors

An Accessible Autocomplete Utility for Chakra UI that composes Downshift ComboBox

NPM JavaScript Style Guide

demo-image

Install

*Warning This Package is still WIP at the Moment and there might be some missing features

npm install --save chakra-ui-autocomplete

Usage

  • Usage Example with TSX/Typescript
import React from 'react'
import { CUIAutoComplete } from 'chakra-ui-autocomplete'


export interface Item {
  label: string;
  value: string;
}
const countries = [
  { value: "ghana", label: "Ghana" },
  { value: "nigeria", label: "Nigeria" },
  { value: "kenya", label: "Kenya" },
  { value: "southAfrica", label: "South Africa" },
  { value: "unitedStates", label: "United States" },
  { value: "canada", label: "Canada" },
  { value: "germany", label: "Germany" }
];

export default function App() {
  const [pickerItems, setPickerItems] = React.useState(countries);
  const [selectedItems, setSelectedItems] = React.useState<Item[]>([]);

  const handleCreateItem = (item: Item) => {
    setPickerItems((curr) => [...curr, item]);
    setSelectedItems((curr) => [...curr, item]);
  };

  const handleSelectedItemsChange = (selectedItems?: Item[]) => {
    if (selectedItems) {
      setSelectedItems(selectedItems);
    }
  };

  return (
        <CUIAutoComplete
          label="Choose preferred work locations"
          placeholder="Type a Country"
          onCreateItem={handleCreateItem}
          items={pickerItems}
          selectedItems={selectedItems}
          onSelectedItemsChange={(changes) =>
            handleSelectedItemsChange(changes.selectedItems)
          }
        />
  );
}

  • Usage Example with JSX/Javascript
import React from 'react'
import { CUIAutoComplete } from 'chakra-ui-autocomplete'

const countries = [
  { value: "ghana", label: "Ghana" },
  { value: "nigeria", label: "Nigeria" },
  { value: "kenya", label: "Kenya" },
  { value: "southAfrica", label: "South Africa" },
  { value: "unitedStates", label: "United States" },
  { value: "canada", label: "Canada" },
  { value: "germany", label: "Germany" }
];

export default function App() {
  const [pickerItems, setPickerItems] = React.useState(countries);
  const [selectedItems, setSelectedItems] = React.useState([]);

  const handleCreateItem = (item) => {
    setPickerItems((curr) => [...curr, item]);
    setSelectedItems((curr) => [...curr, item]);
  };

  const handleSelectedItemsChange = (selectedItems) => {
    if (selectedItems) {
      setSelectedItems(selectedItems);
    }
  };

  return (
        <CUIAutoComplete
          label="Choose preferred work locations"
          placeholder="Type a Country"
          onCreateItem={handleCreateItem}
          items={pickerItems}
          selectedItems={selectedItems}
          onSelectedItemsChange={(changes) =>
            handleSelectedItemsChange(changes.selectedItems)
          }
        />
  );
}

View on CodeSandbox 109296467-3cdbe100-7828-11eb-9491-1bd069bf90a4

Usage Example with Custom Item Renderer

custom-render-image

import React from 'react'
import { Text, Flex, Avatar } from '@chakra-ui/react'
import { CUIAutoComplete } from 'chakra-ui-autocomplete'


const countries = [
  { value: "ghana", label: "Ghana" },
  { value: "nigeria", label: "Nigeria" },
  { value: "kenya", label: "Kenya" },
  { value: "southAfrica", label: "South Africa" },
  { value: "unitedStates", label: "United States" },
  { value: "canada", label: "Canada" },
  { value: "germany", label: "Germany" }
];

export default function App() {
  const [pickerItems, setPickerItems] = React.useState(countries);
  const [selectedItems, setSelectedItems] = React.useState([]);

  const handleCreateItem = (item) => {
    setPickerItems((curr) => [...curr, item]);
    setSelectedItems((curr) => [...curr, item]);
  };

  const handleSelectedItemsChange = (selectedItems) => {
    if (selectedItems) {
      setSelectedItems(selectedItems);
    }
  };

  const customRender = (selected) => {
    return (
      <Flex flexDir="row" alignItems="center">
        <Avatar mr={2} size="sm" name={selected.label} />
        <Text>{selected.label}</Text>
      </Flex>
    )
  }

  const customCreateItemRender = (value) => {
    return (
      <Text>
        <Box as='span'>Create</Box>{' '}
        <Box as='span' bg='red.300' fontWeight='bold'>
          "{value}"
        </Box>
      </Text>
    )
  }



  return (
          <CUIAutoComplete
            tagStyleProps={{
              rounded: 'full'
            }}
            label="Choose preferred work locations"
            placeholder="Type a Country"
            onCreateItem={handleCreateItem}
            items={pickerItems}
            itemRenderer={customRender}
            createItemRenderer={customCreateItemRender}
            selectedItems={selectedItems}
            onSelectedItemsChange={(changes) =>
              handleSelectedItemsChange(changes.selectedItems)
            }
          />
  );
}

Props

Property Type Required Decscription
items Array Yes An array of the items to be selected within the input field
placeholder string The placeholder for the input field
label string Yes Input Form Label to describe the activity or process
highlightItemBg string For accessibility, you can define a custom color for the highlight color when user is typing also accept props like yellow.300 based on chakra theme provider
onCreateItem Function Yes Function to handle creating new Item
optionFilterFunc Function You can define a custom Function to handle filter logic
itemRenderer Function Custom Function that can either return a JSX Element or String, in order to control how the list items within the Dropdown is rendered
labelStyleProps Object Custom style props based on chakra-ui for labels, Example `{{ bg: 'gray.100', pt: '4'}}
inputStyleProps Object Custom style props based on chakra-ui for input field, Example`{{ bg: 'gray.100', pt: '4'}}
toggleButtonStyleProps Object Custom style props based on chakra-ui for toggle button, Example `{{ bg: 'gray.100', pt: '4'}}
tagStyleProps Object Custom style props based on chakra-ui for multi option tags, Example`{{ bg: 'gray.100', pt: '4'}}
listStyleProps Object Custom style props based on chakra-ui for dropdown list, Example `{{ bg: 'gray.100', pt: '4'}}
listItemStyleProps Object Custom style props based on chakra-ui for single list item in dropdown, Example`{{ bg: 'gray.100', pt: '4'}}
selectedIconProps Object Custom style props based on chakra-ui for the green tick icon in dropdown list, Example `{{ bg: 'gray.100', pt: '4'}}
icon Object CheckCircleIcon @chakra-ui/icons Icon to be displayed instead of CheckCircleIcon
hideToggleButton boolean Hide the toggle button
disableCreateItem boolean Disable the "create new" list Item. Default is false
createItemRenderer Function Custom Function that can either return a JSX Element or String, in order to control how the create new item within the Dropdown is rendered. The input value is passed as the first function parameter, Example: (value) => `Create ${value}`
renderCustomInput Function Custom function to render input from outside chakra-ui-autocomplete. Receives input props for the input element and toggleButtonProps for the toggle button. Can use this to render chakra-ui's <InputGroup>. Example: (inputProps) => (<InputGroup><InputLeftElement pointerEvents="none" children={<PhoneIcon color="gray.300" />} /><Input {...inputProps} /></InputGroup>)

Todo

  • Add Combobox Support for Single Select Downshift Combobox
  • Multi Select Support
  • Feature to Create when not in list
  • Add prop for Items Renderer to enable rendering of React Element
  • Ability to define chakra-ui components that will render in place of Tags, MenuList, TextInput, Form Label will check render props or headless UI patterns.

Contributors

Code Contributors


Arafat Zahan

πŸ’»

JoΓ£o Viana

πŸ’»

Hamed Sedighi

πŸ’»

Akash Singh

πŸ’»

Anthony Master

πŸ“– ⚠️ πŸ’»

Vidur Murali

πŸ’»

Marco Nalon

⚠️ πŸ’» πŸ“–

U.M Andrew

πŸ“– ⚠️ πŸ’»

sr.cristofher

πŸ“–

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

License

MIT Β© koolamusic

More Repositories

1

awesome-imba

⭐ A curated list of awesome Imba frameworks, libraries, software and resources
131
star
2

defi-asset-tracker

A dApp to assist you in tracking your assets on the blockchain networks in BSC, ETH, MATIC, see your ROI on Tokens, NFTs etc.
TypeScript
16
star
3

scrapegpt

⚠️ ⚠️ Warning: Very Experimental, use GPT-4 to scrape a website intelligently
JavaScript
6
star
4

nextjs-typescript-graphql-chakra-eslint

Custom Boilerplate to scaffold a nextjs app with Typescript - Apollo Graphql - Chakra UI - Eslint
TypeScript
5
star
5

wpshell

A wordpress shell script to automatically setup Mysql and Wordpress on a server with extra security config included
Shell
4
star
6

nest-auth-session-sandbox

TypeScript
3
star
7

naijahacks-certificate

Certificate Generator for Naijahacks
CSS
3
star
8

vesta-bedrock-starter

⭐ Vesta CP Bedrock Setup to use with Vesta CP enabled Servers
PHP
3
star
9

phpmlm

PHP
2
star
10

pidgin.ipsum

Lorem ipsum but in pidgin english
HTML
2
star
11

analytic-demo

A super app for hands on experience with tracking user engagement using a CDP like Segment and integrating with Hotjar, Fullstory and Google Analytics
TypeScript
2
star
12

sars-angel

And I heard a voice
TypeScript
2
star
13

ts_labs

TypeScript
2
star
14

openzeppelin-typechain-sample

Tooling sample with openzeppelin and typechain. You get generated types and interfaces from contract ABIs written with solidity/truffle/web3 toolkit
TypeScript
2
star
15

falsisign

Mirror of the Falsisign repo on Gitlab
Shell
2
star
16

koolamusic.github.io

Based on Static Site Generator Jekyll, Archived for headshots
HTML
2
star
17

gptsam

A CLI Wrapper ontop of OpenAI ChatGPT
TypeScript
2
star
18

ovpn

A Free VPN box for Linux Distros based on Open VPN that allows you to connect to vpn services that support Open VPN profiles
Shell
2
star
19

bourbon

❀️ New web blog for my personal website built with Jekyll named after whisky flavor β€” 2019
HTML
2
star
20

karis

Karis Digital Publishing Website
CSS
1
star
21

wpsh

Wordpress Shell Script to Auto Configure Apache Varnish NG and MySQL and wordpress with some modapache hacks
Shell
1
star
22

100DaysOfCode

⭐ 100 days of code ⭐
1
star
23

daccred-api

on chain credibility
TypeScript
1
star
24

dryrun

:bowtie: A Simple React project for coding coach mentees
1
star
25

recxMrHN449TwhLoK

Find a Hospital near you, using ant design and Google maps to build Geo-recommendation features
TypeScript
1
star
26

eitx

Eit Manager App
JavaScript
1
star
27

passive

passive coding journey
HTML
1
star
28

electron-lab

Playground for Cross Platform Desktop Applications with Electron
JavaScript
1
star
29

next-antd-airtable

Boilerplate -- Heavy I might warn
TypeScript
1
star
30

zettelkasten

Building a second brain with Foam
HTML
1
star
31

lovesanta

The Secret Santa App: Anonymously pair yourself with a team-mate you can show a random act of kindness to
TypeScript
1
star
32

functionswp

Wordpress security functions for ;paranoid folks
PHP
1
star
33

nginx-reverseproxy

⭐ A reverse proxy implementation to run multiple NodeJs Apps with NGINX
1
star
34

0xalzzy-721

ERC-721 with Metadata, Opensea support, headshots
Solidity
1
star
35

smyride

Schedule My Ride - A Value Proposition User Test Study for Bolt (Estonia)
JavaScript
1
star
36

gnosis-safe-react-legacy

TypeScript
1
star
37

koolamusic

Welcome
1
star
38

decks

A Repo from Slide Decks and Presentations I have given at Workshops and Talks
1
star