Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Select - Replacing Components for custom option content

Using React-Select (version 2) I would like to have custom designed (select) options.

The documentation suggests that Replacing Components would be a method that I could use to achieve this.

Unfortunately I'm unable to find examples that show implementations of this feature.

Is there anyone that could present to me usage of this feature whereby you would have a simple custom option (perhaps a label and value that also includes an SVG graphic to the left of each option label).

Many thanks in advance

like image 565
user2190690 Avatar asked Oct 15 '18 15:10

user2190690


People also ask

Is option selected React select?

To select a default option in React, the selected attribute is used in the option element. In React, though, instead of using the selected attribute, the value prop is used on the root select element. So, you can set a default value by passing the value of the option in the value prop of the select input element.

How do I use onChange With React select?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

How do you clear the value of a React select when another React select changes?

You can clear the value of react select using the ref. Just store the value in the state, and change the state programmatically using componentDidUpdate etc... Note: 'value' should be an object. A simple option would be to pass null to the value prop.


4 Answers

For a majority of use cases, you probably don't need to replace the full Option component. If you're looking to stay with the same overall structure and look and feel of the Option, but you want to display several blocks of text, or an image, or some other special treatment to the body of each option, there is an easier way.

That's to use the formatOptionLabel render prop.

import React from "react";
import ReactDOM from "react-dom";
import Select from "react-select";

const options = [
  { value: "Abe", label: "Abe", customAbbreviation: "A" },
  { value: "John", label: "John", customAbbreviation: "J" },
  { value: "Dustin", label: "Dustin", customAbbreviation: "D" }
];

const formatOptionLabel = ({ value, label, customAbbreviation }) => (
  <div style={{ display: "flex" }}>
    <div>{label}</div>
    <div style={{ marginLeft: "10px", color: "#ccc" }}>
      {customAbbreviation}
    </div>
  </div>
);

const CustomControl = () => (
  <Select
    defaultValue={options[0]}
    formatOptionLabel={formatOptionLabel}
    options={options}
  />
);

ReactDOM.render(<CustomControl />, document.getElementById("root"));

https://codesandbox.io/embed/reactselect-formatoptionlabel-bde1q

https://react-select.com/props - search for formatOptionLabel

like image 82
duhseekoh Avatar answered Oct 14 '22 19:10

duhseekoh


You can replace any component by including your override in the components property.

<Select components={{Option: MyOption}} />

Something like:

const MyOption = props => {
  const { innerProps, innerRef } = props;
  return (
    <article ref={innerRef} {...innerProps} className="custom-option">
      <h4>{props.data.artist}</h4>
      <div className="sub">{props.data.title} </div>
    </article>
  );
};

<Select components={{Option: MyOption}} />

The innerRef and innerProps properties are very important, as they carry forward things like the hover and onClick needed by the Option. The data in props is where your option data is.

like image 43
Steve -Cutter- Blades Avatar answered Oct 14 '22 18:10

Steve -Cutter- Blades


Generally, you indeed want to use formatOptionLabel prop (no extra component). But in case you don't, you may override the Option component this way:

import Select, { components } from 'react-select';

const CustomOption = ({ children, ...props }) => {
  return (
    <components.Option {...props}>
      <img src={...} />
      {children}
    </components.Option>
  );
};

function App() {
  return (
    <Select components={{Option: CustomOption}} ... />
  );
}

Here I reuse the stock component (components.Option). This way I don't need to care about innerRef or something.

https://codesandbox.io/s/react-select-item-icon-2z3tb

like image 25
x-yuri Avatar answered Oct 14 '22 19:10

x-yuri


If you want to style your option when it has been selected you can use a custom option like so:

const customOption = (props) => (
{props.isSelected ? (
  <img className="custom-option__img" src={IconCheck} alt="" />
) : (
  ''
)}
);
like image 32
salym akhmedov Avatar answered Oct 14 '22 19:10

salym akhmedov