Git Product home page Git Product logo

autocomplete's Introduction

Autocomplete Component

A simple autocomplete component built with React and TypeScript.

Props

The Autocomplete component accepts the following props:

Name Type(s) Description
description string Description or additional information to display below the component.
disabled boolean Disables the component from any interaction if true.
filterOptions (options: T[], inputValue: string) => T[] Function to filter options based on the input value.

Note: By default, options are filtered using a simple string equality comparison on the label.
label string Label to display above the component.
loading boolean Shows a loading indicator if true.
multiple boolean Allows multiple selections if true.
value T | T[] (Controlled) The selected value or an array of selected values.

Note: It must be used with onChange for controlled behavior.
onChange (value: T | T[]) => void (Controlled) Callback function invoked when the value changes.

Note: It must be used with value for controlled behavior.
onInputChange (inputValue: string) => void Callback function invoked when the input value changes.
options T[] An array of options to be displayed and selected.

Note: T either can be either a string or an object (with a compulsory label field).
placeholder string Placeholder text displayed when no option is selected.
renderOption (option: T, isActive: boolean, isSelected: boolean) => JSX.Element Custom renderer for each option in the dropdown.

Note: isActive indicates if the options is currently hovered/highlighted in the dropdown. isSelected indicates if the option is currently selected.

Note: The options prop is mandatory.

Considerations

These are some considerations made when making this component, so minimal changes are needed to be made with the given props.

Controlled vs Uncontrolled

The value and onChange props are enforced to be used together to make the component controlled (and to prevent any unforeseen client-side bugs as well). This ensures that the parent component has full control over the value(s) of the component.

Similarly, if the value and onChange props are not provided, the component will be uncontrolled and will manage its own state internally.

Mandatory label field for the Object

Currently, the object must include a label field. This is used by the Autocomplete component for displaying options and filtering. Future versions may include a renderLabel prop for more flexibility so the label field will be no longer mandatory.

String comparison check

By default, options are filtered using a simple string equality comparison on the option’s label.

Getting Started

To get started with this project, follow these steps:

Installation

Clone the repository and install dependencies using Yarn:

git clone https://github.com/hwennnn/autocomplete

cd autocomplete

yarn

Development

To run the development server:

yarn dev

This command starts Vite's development server and opens your project in the default browser.

Building

To build the project for production:

yarn build

This will compile TypeScript files and bundle your application using Vite.

Preview

To preview the production build locally:

yarn preview

This command serves the production build using Vite.

Dependencies

  • React
  • TypeScript
  • Vite
  • Floating UI (@floating-ui/react)

autocomplete's People

Contributors

hwennnn avatar

Watchers

 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.