Git Product home page Git Product logo

chakra-ui-simple-autocomplete's Introduction

Chakra UI Simple Autocomplete

A simple autocomplete input built with Chakra UI

NPM version Build Status Downloads

Demo

demo

Storybook

Typescript CodeSandbox

Install

npm install chakra-ui-simple-autocomplete

Usage

  • Usage Example with TSX/Typescript
import { Autocomplete, Option } from 'chakra-ui-simple-autocomplete';

const options = [
  { value: 'javascript', label: 'Javascript' },
  { value: 'chakra', label: 'Chakra' },
  { value: 'react', label: 'React' },
  { value: 'css', label: 'CSS' },
];

const AutocompleteWrapper = () => {
  const [result, setResult] = React.useState<Option[]>([]);

  return (
      <Box maxW="md">
        <Autocomplete
          options={options}
          result={result}
          setResult={(options: Option[]) => setResult(options)}
          placeholder="Autocomplete"
        />
      </Box>
  );
};
  • Usage Example with TSX and Formik + Yup
import { Autocomplete, Option } from 'chakra-ui-simple-autocomplete';
import { Badge, Box } from '@chakra-ui/react';

const options = [
  { value: 'javascript', label: 'Javascript' },
  { value: 'chakra', label: 'Chakra' },
  { value: 'react', label: 'React' },
  { value: 'css', label: 'CSS' },
];

const autocompleteSchema = Yup.object().shape({
  tags: Yup.array()
    .of(
      Yup.object().shape({
        value: Yup.string(),
        label: Yup.string(),
      }),
    )
    .min(1),
});

const AutocompleteWrapper = () => {
  const [result, setResult] = React.useState<Option[]>([]);

  return (
    <Formik
      validationSchema={autocompleteSchema}
      initialValues={{ tags: [] }}
      onSubmit={(values) => {
        console.log(values)
      }}>
      {(props) => (
        <Box maxW="md">
          <Field name="tags">
            {({ field, form }: FieldProps) => (
              <Autocomplete
                options={options}
                result={result}
                setResult={(options: Option[]) => {
                  form.setFieldValue('tags', options);
                  setResult(options);
                }}
                placeholder="Autocomplete"
              />
            )}
          </Field>
        </Box>
      )}
    </Formik>
  );
};

Props

Property Type Required Description
options Option[] Yes An array of Option to render on the autocomplete
result Option[] Yes State where the selected options are going to be stored
setResult (options: Option[]) => void Yes Callback to be triggered everytime the we add/remove an option from the result
renderBadge (option: Option) => React.ReactNode No Renders each selected option
renderCheckIcon (option: Option) => React.ReactNode No Custom check icon
renderCreateIcon () => React.ReactNode No Custom create icon
placeholder String No Placeholder for the input
colorScheme String No Color scheme to be applied on the input
bgHoverColor String No Background color when hovering the options
allowCreation Boolean No Show the create new tag option. Default true
notFoundText String No "Not found" text to be displayed if allowCreation is false.
disableRenderBadge Boolean No Optional prop for turning off the renderBadge function.

๐Ÿ› ๏ธ Support

Please open an issue for support.

๐Ÿ“ Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

๐Ÿ“œ License

License: MIT

chakra-ui-simple-autocomplete's People

Contributors

fedeorlandau avatar anubra266 avatar csirianni avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.