Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-select-material-ui

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-select-material-ui

A react SELECT component based on react-select and looking like a material-ui component

  • 9.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.8K
increased by0.59%
Maintainers
1
Weekly downloads
 
Created
Source

react-select-material-ui Weekly downloads

A react SELECT component based on react-select and looking like a material-ui component


Demo

You can access the storybook for this component here.

Props

The component accepts the props defined bellow in the table plus all props defined for BaseTextFieldProps except InputProps, inputProps and variant (as there is no input).

Props accepted by ReactSelectMaterialUi

NameTypeRequiredDefaultDescription
defaultValuestringnoundefinedThe default value for a single select*
defaultValuesstring[]noundefinedThe default value for a multiple select*
idstringnoundefinedThe id assigned to the input field and referenced by label
onChange(value: string | string[], SelectOption | SelectOption[] | undefined) => voidyes-The callback function called when the option is changed
optionsstring[] | SelectOption[]yes-The selectable options
SelectPropsSelectPropsnoundefinedThe props for react-select component
valuestringnoundefinedThe value for a single select*
valuesstring[]noundefinedThe values for a multiple select*

* The order of the evaluated fields for deciding which is the selected value (take attention to the presence or not of the "s" after "value"):

  • single: value, defaultValue (values, defaultValues)
  • multiple: values, defaultValues (value, defaultValue)

Fields defined by SelectProps

NameTypeRequiredDefaultDescription
isClearablebooleannofalseSet to true to allow remove of selection with backspace or clicking on the x of the value(s)
isCreatablebooleannofalseSet to true to allow creation of new values based on the input string
msgNoOptionsAvailablestringnoNo more options are availableThe message displayed when all options are already selected
msgNoOptionsMatchFilterstringnoNo options match the filterThe message displayed when no options match case-insensitive the input value
msgNoValidValuestringnoThe new value is not valid (contains space)The message displayed when the input value is not accepted by a Creatable for creating a new value

Props ignored in ReactSelectMaterialUiProps

  • placeholder (if there is set prop 'label', as they can overlap)
  • variant (as it is implemented only 'standard')

Props ignored in SelectProps if defined

  • noOptionsMessage
  • placeholder

Fields defined by SelectOption

NameTypeRequiredDescription
labelstringyesThe text displayed as option or value
valueanyyesThe value associated to this option and returned by onChange

Notes about a Creatable select

It does not accept by default new options containing space.

set SelectProps.isValidNewOption to something like the following code to define your own validation:

(inputValue: string) => inputValue !== '';

The format for a new options is: 'INPUTED_TEXT (new)'.

set SelectProps.formatCreateLabel to something like the following code for creating your own formated option:

(value: string) => `${value} (New Label)`;

The new option will be at start of options list.

Set SelectProps.createOptionPosition to last to display the new option to the end of options list.

Styling the component

Label: inputLabelProps.style

ReactSelect: SelectProps.styles

Please check the code in storybook/stories.tsx to see how to customize ReactSelectMaterialUi.


Versions

Please check section Versions in CHANGELOG.md.


Subcomponents

SingleSelect - a component for selecting a single value. It can be imported with:

import { SingleSelect } from 'react-select-material-ui';
import * as React from "react";
import MaterialUiCreatable, { MaterialUiCreatableProps } from "./MaterialUiCreatable";

const SingleSelect = (props: MaterialUiCreatableProps) => (
  <MaterialUiCreatable
    {...props}
    SelectProps={{Without helper text
      ...props.SelectProps,
      isMulti: false
    }}
    fullWidth={true}
  />
);

export default SingleSelect;

MultipleSelect - a component for selecting multiple values. It can be imported with:

import { MultipleSelect } from 'react-select-material-ui';

Setting SelectProps.isClearable to true will display the clearable button only if there are more then one selected value.

import * as React from 'react';
import MaterialUiCreatable, { MaterialUiCreatableProps } from './MaterialUiCreatable';

const MultipleSelect = (props: MaterialUiCreatableProps) => (
  <MaterialUiCreatable
    {...props}
    SelectProps={{
      ...props.SelectProps,
      isMulti: true,
      isClearable: true,
    }}
    fullWidth={true}
  />
);

export default MultipleSelect;

TagsSelect - a component for selecting multiple tag based on MultipleSelect. It can be imported with:

import { TagsSelect } from 'react-select-material-ui';

ColorsSelect - a component for selecting multiple HTML colors (with preview) based on MultipleSelect. It can be imported with:

import { ColorsSelect } from 'react-select-material-ui';

Examples

The base component which allowes to create read-only or creatable select components for selecting only one or more values:

import * as React from 'react';
import ReactSelectMaterialUi from 'react-select-material-ui';

class App extends React.Component {
  render() {
    const options: string[] = ['Africa', 'America', 'Asia', 'Europe'];

    return (
      <div className="App">
        <ReactSelectMaterialUi style={{ width: 100 }} value="Europe" options={options} onChange={this.handleChange} />
      </div>
    );
  }

  handleChange = (value: string) => {
    console.log(value);
  };
}

export default App;

The single select which creates a full width component for selecting a single value:

import * as React from 'react';
import { SingleSelect } from 'react-select-material-ui';

class App extends React.Component {
  render() {
    const options: string[] = ['Africa', 'America', 'Asia', 'Europe'];

    return (
      <div className="App">
        <SingleSelect value="America" placeholder="Select a continent" options={options} onChange={this.handleChange} />
      </div>
    );
  }

  handleChange = (value: string) => {
    console.log(value);
  };
}

export default App;

The multiple select which creates a full width component for selecting multiple values:

import * as React from 'react';
import { MultipleSelect } from 'react-select-material-ui';

class App extends React.Component {
  render() {
    const options: string[] = ['New York', 'London', 'Vienna', 'Budapest'];

    return (
      <div className="App">
        <MultipleSelect
          label="Choose some cities"
          values={['London', 'Vienna']}
          options={options}
          helperText="You can add a new city by writing its name and pressing enter"
          onChange={this.handleChange}
          SelectProps={{
            isCreatable: true,
            msgNoOptionsAvailable: 'All cities are selected',
            msgNoOptionsMatchFilter: 'No city name matches the filter',
          }}
        />
      </div>
    );
  }

  handleChange = (values: string[]) => {
    console.log(values);
  };
}

export default App;

The select multiple tags component:

import * as React from 'react';
import { TagsSelect } from 'react-select-material-ui';

class App extends React.Component {
  render() {
    const options: string[] = ['Personal', 'Work', 'Important', 'Optional', 'Required'];

    return (
      <div className="App">
        <TagsSelect
          label="Tags"
          options={options}
          onChange={this.handleChange}
          SelectProps={{
            isCreatable: true,
            msgNoOptionsAvailable: 'All tags are selected',
            msgNoOptionsMatchFilter: 'No tag matches the filter',
          }}
        />
      </div>
    );
  }

  handleChange = (values: string[]) => {
    console.log(values);
  };
}

export default App;

The select multiple colors component:

import * as React from 'react';
import { ColorsSelect } from 'react-select-material-ui';

class App extends React.Component {
  render() {
    const options: string[] = ['red', '#123456', 'yellow', '#fedcba'];

    return (
      <div className="App">
        <ColorsSelect
          label="Colors"
          options={options}
          helperText="You can add a new color as long as it is a valid HTML color"
          onChange={this.handleChange}
          SelectProps={{
            isCreatable: true,
          }}
        />
      </div>
    );
  }

  handleChange = (values: string[]) => {
    console.log(values);
  };
}

export default App;

Changelog

Please check section Changelog in CHANGELOG.md.

Keywords

FAQs

Package last updated on 10 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc