npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jtk-chakraui-autocomplete

v1.0.1

Published

The `Autocomplete` component is a reusable React component built with Chakra UI that provides an input field with an autocomplete feature. It allows users to filter and select from a list of countries. The component dynamically displays a dropdown list of

Downloads

9

Readme

Countries Autocomplete Component

The Autocomplete component is a reusable React component built with Chakra UI that provides an input field with an autocomplete feature. It allows users to filter and select from a list of countries. The component dynamically displays a dropdown list of countries based on user input and provides options to clear the input.

Features

  • Autocomplete Functionality: Dynamically filters and displays a list of countries as the user types.
  • Clear Input: Allows users to clear the input with a single click.
  • Customizable Placeholder: The input placeholder can be customized via props.
  • User Selection Handling: Calls a callback function when a country is selected.

Props

| Prop | Type | Default Value | Description | |--------------|------------|---------------|-----------------------------------------------------------| | onSelected | function | undefined | Callback function that is called when a country is selected. | | placeholder | string | "" | Placeholder text for the input field. | | rest | object | {} | Additional props that can be passed to the Box component. |

Installation

Install the library via NPM:

  • npm install react react-dom @chakra-ui/react @emotion/react @emotion/styled framer-motion react-icons
  • npm i jtk-chakraui-autocomplete

Version Compatibility

  • React version 17.x.x or higher
  • Chakra UI version 2.8.2

Demo

Autocomplete Demo

Usage

import React from 'react';
import Autocomplete from 'jtk-chakraui-autocomplete';

const handleCountrySelect = (country) => {
  console.log('Selected country:', country);
};

const App = () => (
  <div>
    <Autocomplete 
      onSelected={handleCountrySelect} 
      placeholder="Type a country..." 
      width="300px"
    />
  </div>
);

export default App;

Full Code

import React, { useState } from "react";
import {
  Box,
  Input,
  List,
  ListItem,
  Icon,
  Text,
  InputGroup,
  InputRightElement,
  InputLeftAddon
} from "@chakra-ui/react";
import { AiOutlineCloseCircle } from "react-icons/ai";
import { countries } from "./config";

const Autocomplete = ({ onSelected, placeholder = "", ...rest }) => {
  const [inputValue, setInputValue] = useState("");
  const [filteredCountries, setFilteredCountries] = useState([]);
  const [showOptions, setShowOptions] = useState(false);

  const handleChange = (event) => {
    const value = event.target.value;
    setInputValue(value);
    if (value.length > 0) {
      const filtered = countries.filter((country) =>
        country.toLowerCase().includes(value.toLowerCase())
      );
      setFilteredCountries(filtered);
      setShowOptions(true);
    } else {
      setShowOptions(false);
      setFilteredCountries([]);
    }
  };

  const handleSelect = (value) => {
    setInputValue(value);
    setShowOptions(false);
    onSelected(value);
  };

  const handleClear = () => {
    setInputValue("");
    setShowOptions(false);
  };

  return (
    <Box position="relative" w="100%" {...rest}>
      <InputGroup>
        <InputLeftAddon>Country</InputLeftAddon>
        <Input
          type="text"
          value={inputValue}
          onChange={handleChange}
          placeholder={placeholder}
        />
        {inputValue && (
          <InputRightElement>
            <Icon
              as={AiOutlineCloseCircle}
              cursor="pointer"
              onClick={handleClear}
            />
          </InputRightElement>
        )}
      </InputGroup>

      {showOptions && (
        <Box
          position="absolute"
          width="100%"
          mt={1}
          border="1px"
          borderColor="gray.200"
          shadow="md"
          bg="white"
          zIndex="1"
          maxHeight={'200px'}
          overflowY={'scroll'}
        >
          <List>
            {filteredCountries.map((item, index) => (
              <ListItem
                key={index}
                px={3}
                py={2}
                _hover={{ bg: "gray.100", cursor: "pointer" }}
                onClick={() => handleSelect(item)}
              >
                <Text>{item}</Text>
              </ListItem>
            ))}
          </List>
        </Box>
      )}
    </Box>
  );
};

export default Autocomplete;

More Info

Chakra UI

https://v2.chakra-ui.com/docs/components

Github

https://github.com/jovanes-work/jtk-chakraui-autocomplete