1.5.0 • Published 2 years ago

react-multi-search-select v1.5.0

Weekly downloads
48
License
MIT
Repository
github
Last release
2 years ago

React Multi Search Select

A fully tested Typescript React component that shows a multi select searchable dropdown.

Installation

yarn add react-multi-search-select
npm install react-multi-search-select

Usage

import React, { FC, ReactElement } from 'react';
import { ReactMultiSearchSelect } from 'react-multi-search-select';
import "~react-multi-search-select/dist/index.css";

export const Component: FC = (): ReactElement => {
  const onChange = (selectedOptions: SelectedOption[]): void => {
    console.log(selectedOptions);
  };
  
  return (
    <>
      <ReactMultiSearchSelect options={["test", "test2", "test3"]} onChange={onChange} />

      <ReactMultiSearchSelect
        options={[{id: 1, name: "test"}, {id: 2, name: "test2"}, {id: 3, name: "test3"}]}
        optionsObject={{key: "id", value: "name"}}
        onChange={onChange}
      />
    </>
  );
}

Ref

React Multi Search Select has its own internal state. If you wish to update this internal state to match your own state, you can pass through a ref and call the setOptions() function.

import React, { FC, ReactElement } from 'react';
import { ReactMultiSearchSelect } from 'react-multi-search-select';
import "~react-multi-search-select/dist/index.css";

export const Component: FC = (): ReactElement => {
  const ref = useRef<ReactMultiSearchSelectRef>();

  const clear = (): void => ref.current.setOptions([]);

  return (
    <>
      <button onClick={clear}>Clear All Options</button>
      <ReactMultiSearchSelect
        options={["test", "test2", "test3"]}
        defaultValues={["test", "test2", "test3"]}
        ref={ref}
      />
    </>
  );
}

Props

PropTypeDefaultDescription
optionsarray (string or key value pair)RequiredCan either be an array of strings: ["test", "test2"] or an array of objects: [{ id: 1, name: "test}, { id: 2, name: "test2}]
defaultValuesnumber[] or string[]nullDefault options when first loaded. This is either an array of strings or numbers that matches the key if using optionsObject
optionsObjectobjectnullThe key and value to show when using an array of objects for options. For example: {key: "id", value: "name"}
onChangefunctionnullFunction to call when the selected options are changed. Returns an array of either string or number defined by key if using optionsObject
disabledbooleanfalseDisables search input
loadingbooleanfalseDisables search input and shows loading spinner
selectionLimitnumbernullThe amount of selected options before the search input is disabled and no more can be selected
placeholderTextstringSelectPlace holder text
noOptionsTextstringThere are no optionsNo options text
caseSensitiveSearchbooleanfalseSets if search is case sensitive
refReactMultiSearchSelectRefnullAllows calling some functions to update the internal state

Styling

You can override each class as needed. The names are "name spaced" so that they do not affect any of your styling. These are all the classes and what they do:

ClassDescription
react-multi-search-select-containerContainer for the whole component
react-multi-search-select-search-wrapperWrapper for the search input
react-multi-search-select-loadingContainer for loading elements
react-multi-search-select-loading > spanLoading text
react-multi-search-select-container inputSearch input
react-multi-search-select-container input:focusSearch input focus
react-multi-search-select-selected-optionSelected options next to the search input
react-multi-search-select-selected-option:hoverSelected options hover
react-multi-search-select-options-containerList for options to select
react-multi-search-select-options-container.hideList when its inactive
react-multi-search-select-options-container liOption to select
react-multi-search-select-options-container li:not(.no-hover):hoverNo options text (for no hover effect)

Licence

MIT

1.5.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago