Is there a way to style the labels in my react-select option?

Viewed 1205

I'm using react-select and and I'm sending an object as options. The first element contains a title and second contains a subtitle. Using the getOptionLabel API I'm able to populate the option in my select field. This is the code along with its associated style:

export default (props) => {
  const selectStyles = {
    control: (_provided, state) => {
      return {
        ..._provided,
        width: "100%",
        fontSize: "1rem",
        verticalAlign: "middle",
        color: "#1a1a1a",
        appearance: "none",
        minHeight: "auto",
      };
    },
    valueContainer: (provided) => {
      return {
        ...provided,
        minHeight: "auto",
        flexWrap: "nowrap",
        overflowX: "auto",
        padding: "0 .5rem",
      };
    },
 
    container: (provided) => {
      return {
        ...provided,
        padding: 0,
        flexGrow: "1",
      };
    },
  
    option: (provided, { data, isSelected, isFocused }) => {
      return {
        ...provided,
        backgroundColor: isSelected ? "#98c1ff" : provided.backgroundColor,
        color: data.color ? data.color : provided.color,
        fontWeight: data.color ? "800" : provided.fontWeight,
        cursor: "pointer",
        paddingTop: ".25rem",
        paddingBottom: ".25rem",
        fontSize: "0.75em",
      };
    },


  
    multiValue: (provided) => {
      return {
        ...provided,
        padding: ".25rem .5rem",
        background: "#EBEBEB",
        minHeight: "auto",
        borderRadius: "2px",
        marginRight: ".25rem",
        flexShrink: "0",
      };
    },
    multiValueLabel: (provided) => {
      return {
        ...provided,
        fontSize: ".75rem",
        fontWeight: "400",
        color: "#292929",
        padding: 0,
      };
    },
    multiValueRemove: (provided) => {
      return {
        ...provided,
        color: "#8F8F8F",
        background: "transparent",
        paddingRight: 0,
        ":hover": {
          color: "#1B4EA3",
          background: "transparent",
          cursor: "pointer",
        },
      };
    },
    clearIndicator: (provided) => {
      return {
        ...provided,
        paddingTop: 0,
        paddingBottom: 0,
        minHeight: "auto",
      };
    },
    dropdownIndicator: (provided) => {
      return {
        ...provided,
        paddingTop: 0,
        paddingBottom: 0,
        minHeight: "auto",
      };
    },

    menu: (provided) => ({
      ...provided,
      marginTop: ".5rem",
      marginBottom: 0,
      borderRadius: 0,
    }),
    menuList: (provided) => ({
      ...provided,
      paddingTop: 0,
      paddingBottom: 0,
    }),
  };

  let {
    name,
    id,
    options,
    value,
  } = props;

  return (
    <Select
      {...props}
      placeholder=""
      id={id}
      name={name}
      value={value}
      options={options}
      getOptionLabel={(options) => `${options.title} ${options.subTitle}`}
      styles={selectStyles}
    />
  );
};

With this I'm able to get the following result:

enter image description here

However, I want the subtitle to have a different color, ideally like this:

enter image description here

1 Answers

You can override SingleValue and Option components and add custom style in your the wrapper components:

Options

const options = [
  {
    value: "chocolate",
    title: "Chocolate",
    subTitle: "Icescream"
  },
  {
    value: "strawberry",
    title: "Strawberry",
    subTitle: "Icescream"
  },
  {
    value: "vanilla",
    title: "Vanilla",
    subTitle: "Icescream"
  }
];

Component

  • Option component: displays each option in the menu.
  • SingleValue component: displays the selected value in the input for a single select.
const SingleValue = (props) => {
  const { title, subTitle } = props.getValue()[0];

  return (
    <components.SingleValue {...props}>
      <span>{title}</span> <span style={{ color: "darkgray" }}>{subTitle}</span>
    </components.SingleValue>
  );
};

const Option = (props) => {
  const { title, subTitle } = props.data;

  return (
    <components.Option {...props}>
      <span>{title}</span> <span style={{ color: "darkgray" }}>{subTitle}</span>
    </components.Option>
  );
};

Usage

<Select
  options={options}
  getOptionLabel={(options) => `${options.title} ${options.subTitle}`}
  components={{ SingleValue, Option }}
/>

Live Demo

Edit 66741201/is-there-a-way-i-can-style-the-two-labels-i-send-in-my-react-select-option

Related