1.1.0 • Published 9 months ago

react-select-search-plus v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

React Select Search Plus

A flexible and customizable select component with search functionality for React applications.

Features

  • 🔍 Search functionality with customizable filtering
  • ✹ Single and multiple selection modes
  • 🎚 Fully customizable styling
  • 🔄 Async data loading support
  • 🎯 TypeScript support
  • ⌚ Keyboard navigation
  • 🎭 Custom option rendering
  • 📱 Mobile-friendly

Installation

npm install react-select-search-plus
# or
yarn add react-select-search-plus

Basic Usage

import { SelectSearch } from 'react-select-search-plus';

const options = [
  { id: 1, label: 'Option 1', value: 'value1' },
  { id: 2, label: 'Option 2', value: 'value2' },
  { id: 3, label: 'Option 3', value: 'value3' },
];

function MyComponent() {
  const handleChange = (selected) => {
    console.log('Selected options:', selected);
  };

  return (
    <SelectSearch
      options={options}
      onChange={handleChange}
      multiple
      searchPlaceholder="Search options..."
    />
  );
}

Advanced Usage

Async Data Loading

if you are using typescript do not use any clear your response interface

function AsyncExample() {
  const fetchOptions = async () => {
    const response = await fetch('https://api.example.com/options');
    const data = await response.json();
    return data.map(item => ({
      id: item.id,
      label: item.name,
      value: item
    }));
  };

  return (
    <SelectSearch
      fetchOptions={fetchOptions}
      loading={true}
      loadingMessage="Fetching options..."
    />
  );
}

Custom Filtering

function CustomFilterExample() {
  const customFilter = (option, searchTerm) => {
    // Custom filtering logic
    return option.label.toLowerCase().startsWith(searchTerm.toLowerCase());
  };

  return (
    <SelectSearch
      options={options}
      customFilter={customFilter}
    />
  );
}

Props

PropTypeDefaultDescription
optionsOption[][]Array of options to display
valueOption[][]Currently selected options
onChange(selected: Option[]) => void-Callback when selection changes
onSearch(searchTerm: string) => void-Callback when search input changes
multiplebooleanfalseEnable multiple selection
disabledbooleanfalseDisable the component
loadingbooleanfalseShow loading state
searchPlaceholderstring"Type to search..."Placeholder text for search input
noOptionsMessagestring"No options found"Message when no options match search
loadingMessagestring"Loading..."Message during loading state
classNamestring""Additional CSS class for container
optionClassNamestring""Additional CSS class for options
selectedClassNamestring""Additional CSS class for selected options
fetchOptions() => Promise<Option[]>-Async function to fetch options
customFilter(option: Option, searchTerm: string) => boolean-Custom filter function
maxSelectednumber-Maximum number of selections allowed

Styling

The component comes with basic styling that you can extend or override using CSS classes. Each element has a specific class name that you can target:

  .this .component is styled with .taildwindcss you can style with any css class 

TypeScript Usage

The component is fully typed and supports generic types for option values:

interface User {
  id: number;
  name: string;
  email: string;
}

function UserSelect() {
  return (
    <SelectSearch<User>
      options={[
        {
          id: 1,
          label: 'John Doe',
          value: { id: 1, name: 'John Doe', email: 'john@example.com' }
        }
      ]}
      onChange={(selected) => {
        // selected[0].value will be typed as User
        console.log(selected[0].value.email);
      }}
    />
  );
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change. i remain Anthony JR👌🀣❀

License

ISC License - see the LICENSE file for details.

1.1.0

9 months ago

1.0.0

9 months ago