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

@amirkzm/react-auto-complete

v1.0.2

Published

## Description

Downloads

1

Readme

react-auto-complete

Description

A search auto-completer with dynamic backend support.

Table of Contents

  1. Main Features 
  2. Installation
  3. Basic Usage 
  4. Props
  5. License

Features

  • Provide list of all suggestions
  • Accepts fully customized component as suggestion item
  • Accepts input component as input element from MUI,Bootstrap or any other library even your own customized component
  • Suggestions are highlighted and can be navigated with arrow keys or by mouse
  • Bolding already entered text that match any part of suggestion list
  • Accept your component to handle error and loading states
  • Ability to controll refs and also forward passed ref
  • Full controll on each part of component for styling purpose
  • Pass through arbitrary props to the input (e.g. placeholder, type, onChange, onBlur, or any other)
  • Support Free-solo mode
  • And more yet to discover!

Installation

Install by npm:

npm install amirkzm/react-auto-complete"

Or:

yarn add amirkzm/react-auto-complete"

Usage

import React, { useState } from "react";
import AutoCompleteInput, {
  RenderFunctionParams,
} from "@amirkzm/react-auto-complete"";
import TextField from "@mui/material/TextField";

//Transform the returned reponse from server to an array (array of suggestions)
const transformResult = (response: any) => {
  return response.data;
};

// Extract a displayable value for representation purpose from a suggestion
const getLabel = (item: any) => {
  if (typeof item === "string") {
    return item;
  } else {
    return item.fact;
  }
};

// Generating address for entered query
const addressGenerator = (query: string) => {
  return "some url";
};

// Display each suggestion item. It receive data of selected suggestion, getLabel function, props (including onClick,onMouseEnter,key, className and style)
//and finally boldedMatchText that contains suggestion display text with bolded matched entered value.
//
const renderFunction = ({
  data,
  getLabel,
  props,
  boldedMatchText,
}: RenderFunctionParams) => {
  return <div {...props}>{boldedMatchText}</div>;
};

function App() {
  const [selectedItem, setSelectedItem] = useState<any>();
  const [value, setValue] = useState<string>("");

  // This function will be called everytime user select a suggestion
  const onChange = (selectedItem: any) => {
    setSelectedItem(newValue);
    setValue(getLabel(newValue));
  };

  // This will be called everytime user enter a character
  const inputChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
    setValue(e.target.value);
  };

  return (
    <div>
      <AutoCompleteInput
        renderFunction={renderFunction}
        transformResult={transformResult}
        addressGenerator={addressGenerator}
        onChange={onChange}
        onInputChange={inputChangeHandler}
        userInputValue={value}
        value={selectedItem}
        freeSolo
        getLabel={getLabel}
        keyGenerator={keyGenerator}
        itemHeight={50}
        InputComponent={TextField}
      />
    </div>
  );
}

Props

| Props | Types | Required | Description | |-------------------------|:---------:|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | renderFunction | Function | ✓ | React component responsible for rendering the each individual item of the suggestion's list. | | addressGenerator | Function | ✓ | A function that receives query entered by user and generate the address for that input to fetch data from. | | getLabel | Function | ✓ | This function will be used when we want to show string representation of an suggestion item received from server. | | transformResult | Function | ✓ | This function will be used to extract or (transform server's response) an array of suggestions from the server response. | | value | Any | ✓ | Use this if you want to pass your own item (object, string or anything else) to the component. If you use this the input field will be getLabel(item) no matter what user has typed already. | | userInputValue | String | ✓ | The input value from the user that should be displayed in the input field. | | itemHeight | Number | ✓ | Height of each suggestion item. | | loadingElement | ReactNode | | An Optional ReactNode element if we want to show user as loading | | InputComponent | Element | | An input component to be used as the main input element. default is HTML input tag. | | errorElement | ReactNode | | An Optional ReactNode element if we want to show user as error | | keyGenerator | Function | | A function to generate key. It accepts a suggested item and the index and return a key. | | onChange | Function | | This function will be called when an item has been selected from suggestion's list or when user types a character(only if freeSolo is true otherwise it won't be called when user types) | | onInputChange | Function | | This function will be called when user types a character. | | wrapperProps | Object | | Pass through arbitrary props to the root wrapper of the component. | | placeholder | String | | Placeholder for the component. | | outerWrapperRef | ref | | This ref prop target the root div. | | innerWrapperRef | ref | | This ref prop target the inner(suggestions div) div. | | freeSolo | Boolean | | Use this if you want user can have the ability of not choosing any suggestion and use it's own entered input. Default is true. | | inputComponenProps | Object | | Pass through arbitrary props to the input. It is useful when you define your own component as input component(e.g. MUI textField or your own customized components with specified props set). | | maxSuggestionListHeight | number | | Max height of suggestion list. | |

renderFunction(Required)

This function is reposible to display each suggestion item. It receives:

  • data : the entire data received from server for this item. It's actually the corresponding item from suggestion list.
  • getLabel : this function receives an item as input and display string representation of that item.
  • props : including:
    • onClick
    • key
    • style
    • classsName
    • onMouseEnter
  • boldedMatchText : resresents each suggestions with entered text as bold
const renderFunction = ({
  data,
  getLabel,
  props,
  boldedMatchText,
}: RenderFunctionParams) => {
  const { onClick, key, style, className, onMouseEnter } = props; // if you want to use each one seperatedly
  return <div {...props}>{boldedMatchText}</div>;
  //return <div {...props}>{getLabel(data)}</div> if you don't want to use boldMatchedText
};

license

MIT