Coder Social home page Coder Social logo

ceamkrier / radix-multi-select-filter Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 246 KB

A showcase of React component built with Radix-UI for multi-selection and real-time search with Fuse.js.

Home Page: https://ceamkrier.github.io/radix-multi-select-filter/

JavaScript 6.50% HTML 3.70% TypeScript 89.20% CSS 0.60%
fusejs multiselect radix-ui react react-query

radix-multi-select-filter's Introduction

MultiSelect Component

The MultiSelect Component is designed to provide a user-friendly interface for selecting multiple options from a list. It fetches data from a specified remote address, and allows for real-time searching and selection of options. This documentation outlines the key features of the component, the libraries and hooks used, and the rationale behind the design choices made.

Try it

You can check the working demo on here: https://ceamkrier.github.io/radix-multi-select-filter/

Table of Contents

  1. Key Features
  2. Libraries and Hooks Used
  3. Usage
  4. Component Structure
  5. Error Handling and Retry Mechanism
  6. Example Data for Testing

Key Features

  • Remote Data Fetching:
    • Fetches options from a remote server to ensure data is always up-to-date.
  • Error Handling:
    • Graceful error handling with a retry mechanism for failed data fetches.
  • Real-Time Search:
    • Utilizes Fuse.js for real-time search functionality, allowing users to easily filter through the available options.
  • Refetching on Demand:
    • Provides a "Retry" button to trigger a refetch in case of an error.

Libraries and Hooks Used

  • React:
    • Utilizes state and effect hooks for managing component state and side effects.
  • React Query:
    • Used for data fetching, caching, and state management.
    • Handles data fetching and provides hooks for managing query state.
    • Provides a useQuery hook to manage fetching, caching, and the state of the fetched data.
  • React Router Dom:
    • The useSearchParams hook is used to manage query parameters in the URL.
  • Radix UI:
    • Provides accessible UI primitives for building the user interface.
  • Fuse.js:
    • Lightweight fuzzy-search library, which is utilized for real-time search functionality.

Usage

import MultiSelect from './MultiSelect';

function App() {
    return (
        <MultiSelect optionsRemoteAddress="https://example.com/api/options" />
    );
}

export default App;

Component Structure

The MultiSelect component is structured into several sub-components and utility functions to keep the code organized and maintainable:

  • MultiSelectContainer: A container component for managing loading and error states.
  • MultiSelectOverlay: An overlay component displayed when an error occurs.
  • SelectOption: Represents an individual option in the list.
  • fetchOptions: A utility function for fetching options from the remote server.

Error Handling and Retry Mechanism

The component is designed to handle network errors gracefully. If a network error occurs while fetching data, an overlay is displayed with a "Retry" button. Users can click the "Retry" button to trigger a refetch of the data.

The retry mechanism is built using the refetch function provided by React Query, which is triggered by clicking the "Retry" button. The retry option in the useQuery hook is set to 2, meaning that React Query will automatically retry the fetch two times before moving to the error state.

// Inside MultiSelect Component
{isError ? (
    <MultiSelectOverlay>
        <div className='flex flex-col opacity-100 text-center text-sm'>
            Failed to load data
            <br />
            <br />
            <Button
                variant='solid'
                onClick={() =>
                    refetch({
                        type: "active"
                    })
                }>
                Retry
            </Button>
        </div>
    </MultiSelectOverlay>
) : null}

Example Data for Testing

For testing purposes, an example dataset is provided in the public folder, named data.json. This file contains a list of searchable keys under the "data" property as shown below:

{
    "data": ["searchable keys",...]
}

This dataset is used to test the fetching, searching, and selection functionalities of the MultiSelect component in a controlled environment, ensuring that the component behaves as expected with the given data structure.

radix-multi-select-filter's People

Contributors

ceamkrier avatar

Stargazers

 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.