Giter Site home page Giter Site logo

fedeorlandau / chakra-ui-simple-autocomplete Goto Github PK

View Code? Open in Web Editor NEW
34.0 34.0 9.0 2.57 MB

A simple autocomplete for Chakra UI

License: MIT License

TypeScript 48.73% JavaScript 5.44% Shell 1.09% HTML 44.74%
autocomplete chakra chakra-ui input react

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

anubra266 avatar csirianni avatar dependabot[bot] avatar fedeorlandau avatar in43sh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

chakra-ui-simple-autocomplete's Issues

Navigate with the keyboard

I'd love to make this component more accesible. For this reason, we need to provide keyboard navigation on the option list.

SyntaxError: Named export 'chakra' not found.

Hey there, I'm running into the following error:

SyntaxError: Named export 'chakra' not found. The requested module '@chakra-ui/system' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@chakra-ui/system';

Screenshot 2023-01-14 at 3 14 46 PM

It looks to be the same thing as chakra-ui/chakra-ui#7170. It looks like updating @chakra-ui/react and @chakra-ui/icons should fix it.

styles not being applied

I installed the latest package via npm install and been using it within component
instead of seeing chakra ui styled input, I am seeing vanilla (no style) component

import { Autocomplete, Option } from 'chakra-ui-simple-autocomplete';
....
            <FormControl>
              <FormLabel>Team 1</FormLabel>
              <Autocomplete
                options={availablePlayers}
                result={team1Options}
                setResult={(availablePlayers: Option[]) => setTeam1Options(availablePlayers)}
                placeholder={'Team 1'}
              />
            </FormControl>

Add configuration for storybook

It would be nice if we can put some controls on storybook to show different autocomplete configurations (like a placeholder for instance)

Add invalid state

Simply add a new property isInvalid to be passed to the Chakra Input component.

Enhancement Request: Prop to Prevent Rendering of Badges

Love the component but for our use case we don't want to render badges as we are only allowing a select limit of 1. The rederBadge function could return null but the with the margin would still render. I think a simple prop to turn off badge rendering would be a great addition. I can open a PR if you are open to allowing this.

Cheers

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.