Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@choc-ui/chakra-autocomplete

Package Overview
Dependencies
Maintainers
1
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@choc-ui/chakra-autocomplete

```bash npm i --save @choc-ui/chakra-autocomplete #or yarn add @choc-ui/chakra-autocomplete ```

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
decreased by-5.7%
Maintainers
1
Weekly downloads
 
Created
Source


🥵
@choc-ui/chakra-autocomplete



npm package npm  downloads NPM

GitHub Repo stars


AutoComplete Component for the Chakra UI Library.




npm i @choc-ui/chakra-autocomplete





Install

npm i --save @choc-ui/chakra-autocomplete
#or
yarn add @choc-ui/chakra-autocomplete

Preview

With Mouse

With Keyboard

Usage

Basic Usage

import {
  AutoComplete,
  AutoCompleteInput,
  AutoCompleteItem,
  AutoCompleteList,
} from '@choc-ui/chakra-autocomplete';

export default () => {
  const options = ['apple', 'appoint', 'zap', 'cap', 'japan'];

  return (
    <AutoComplete>
      <AutoCompleteInput
        variant="filled"
        placeholder="Search..."
        defaultValue="ap"
        autoFocus
      />
      <AutoCompleteList rollNavigation>
        {options.map((option, oid) => (
          <AutoCompleteItem
            key={`option-${oid}`}
            value={option}
            textTransform="capitalize"
          >
            {option}
          </AutoCompleteItem>
        ))}
      </AutoCompleteList>
    </AutoComplete>
  );
};

Creating Groups

You can create groups with the AutoCompleteGroup Component

import {
  AutoComplete,
  AutoCompleteGroup,
  AutoCompleteInput,
  AutoCompleteItem,
  AutoCompleteList,
} from '@choc-ui/chakra-autocomplete';

export default () => {
  const fruits = ['Apple', 'Grape', 'Pawpaw'];
  const countries = ['Korea', 'Nigeria', 'India'];

  return (
    <AutoComplete>
      <AutoCompleteInput
        variant="filled"
        placeholder="Search..."
        pl="10"
        defaultValue="ap"
        autoFocus
      />
      <AutoCompleteList rollNavigation>
        <AutoCompleteGroup title="Fruits" showDivider>
          {fruits.map((option, oid) => (
            <AutoCompleteItem
              key={`fruits-${oid}`}
              value={option}
              textTransform="capitalize"
            >
              {option}
            </AutoCompleteItem>
          ))}
        </AutoCompleteGroup>
        <AutoCompleteGroup title="countries" showDivider>
          {countries.map((option, oid) => (
            <AutoCompleteItem
              key={`countries-${oid}`}
              value={option}
              textTransform="capitalize"
            >
              {option}
            </AutoCompleteItem>
          ))}
        </AutoCompleteGroup>
      </AutoCompleteList>
    </AutoComplete>
  );
};

Custom Rendering

You can Render whatever you want. The AutoComplete Items are regular Chakra Boxes.

import {
  AutoComplete,
  AutoCompleteInput,
  AutoCompleteItem,
  AutoCompleteList,
} from '@choc-ui/chakra-autocomplete';
import { Avatar, Box, Text } from '@chakra-ui/react';

export default () => {
  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 (
    <AutoComplete>
      <AutoCompleteInput
        variant="filled"
        placeholder="Search..."
        pl="10"
        defaultValue="ap"
        autoFocus
      />
      <AutoCompleteList rollNavigation>
        {people.map((person, oid) => (
          <AutoCompleteItem
            key={`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>
  );
};

API Reference

NB: Feel free to request any additional Prop in Issues.

AutoComplete

Wrapper and Provider for AutoCompleteInput and AutoCompleteList

AutoComplete composes Box so you can pass all Box props to change its style.

NB: If you have more than one AutoComplete Component on a page, they must be assigned unique ids


onChange


Description

Function that provides current Input value and is called anytime, suggestion is selected- useful for uncontrolled Input, but wants to use Value

Type

(value: string) => void

Default

null

Required

No


onSelectOption


Description

Will be called every time suggestion is selected via mouse or keyboard.

Type

(optionValue: string, selectMethod: 'click'|'keyboard') => void;

Default

null

Required

No


onOptionHighlight


Description

Will be called every time the highlighted option changes.

Type

(optionValue: string) => void

Default

null

Required

No


emphasize


Description

The parts of the option string that match the AutoCompleteInput value are emphasized. Pass boolean to bolden it, or a Chakra CSSObject for custom styling. e.g.

<AutoComplete emphasize>
  ...
</AutoComplete>

<!--Or-->

<AutoComplete emphasize={{ color: 'blue.400', fontWeight: 'bold' }}>
  ...
</AutoComplete>

Type

boolean | CSSObject;

Default

const emphasizeStyles = {
  fontWeight: 'extrabold',
};

Required

No


shouldRenderSuggestions


Description

By default, suggestions are rendered when the input isn't blank. Feel free to override this behaviour. This function gets the current value of the input

e.g. The following function is to show the suggestions only if the input has more than two characters.

function shouldRenderSuggestions(value) {
  return value.trim().length > 2;
}

Type

(value: string) => void

Default

null

Required

No


focusInputOnSelect


Description

Determines if Input should be focused after Select

Type

boolean;

Default

true

Required

No


closeOnSelect


Description

If true, the menu will close when an item is selected, by mouse or keyboard.

Type

boolean;

Default

true

Required

No


closeOnBlur


Description

If true, the menu will close when the AutoComplete Component loses focus.

Type

boolean;

Default

true

Required

No


suggestWhenEmpty


Description

If the suggestions shoud show when the input is Empty. - It is used when the input is focused.

Type

boolean;

Default

false

Required

No


suggestWhenEmpty


Description

Component to render when no match is found. Pass null, to just close the menu.

Type

ReactNode;

Default

null

Required

No

AutoCompleteInput

Input for AutoComplete value.

AutoComplete composes Input so you can pass all Input props to change its style.


onChange


Description

When you want the AutoComplete in controlled mode, it's a function that returns, the change event anytime the input value changes.

Type

(e: ChangeEVent) => void

Default

undefined

Required

No


value


Description

The value of the AutoComplete input in controlled mode. You should store this in state. And it must bes used with the onChange prop.

Type

string;

Default

undefined

Required

No

AutoCompleteList

Wrapper for AutoCompleteGroup and AutoCompleteItem

AutoCompleteList composes Box so you can pass all Box props to change its style.


rollNavigation


Description

Determines if keyboard navigation should roll over after getting to either ends.

Type

boolean;

Default

false

Required

No

AutoCompleteGroup

Wrapper for collections of AutoCompleteItems

AutoComplete composes Box so you can pass all Box props to change its style.


showDivider


Description

Determines if a divider should be rendered above the group. The divider will automatically be unrendered when that group is first in that suggestions.

Type

boolean;

Default

null

Required

No


dividerColor


Description

Color for the group divider, when showDivider is true

Type

string;

Default

inherit

Required

No


titleStyles


Description

Styles to be applied to group's title. It Composes Text so you can pass all Text props to change its style.

Type

TextProps;

Default

const textStyles: TextProps = {
  fontSize: 'xs',
  textTransform: 'uppercase',
  fontWeight: 'extrabold',
  letterSpacing: 'wider',
  ml: '5',
};

Required

No

AutoCompleteItem

This Composes your suggestions

AutoComplete composes Flex so you can pass all Flex props to change its style.


_focus


Description

Like the default pseudo _focus, but this determines the style applied when that Item highlighted by mouse or keyboard,

Type

CSSObject;

Default

const _focus = {
  bg: useColorModeValue('gray.200', 'whiteAlpha.100'),
};

Required

No

FAQs

Package last updated on 16 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc