2

I am using https://react-select.com/home#getting-started and I am trying to create something as close as possible to this: enter image description here

The original style of select that I coded (without customStyles function from the select library) gave me something like this:

enter image description here

But as soon as I start adding the styles (customStyles function from the select library) as showed here below:

import { useEffect, useState } from 'react';
// styled components
import styled from 'styled-components';
// icons
import { IoIosArrowDown } from 'react-icons/io';
// select input
import Select from 'react-select';

const ContextItem = ({ text, options }) => {
  const [treeNodeNames, setTreeNodeNames] = useState();

// THIS IS FROM THE LIBRARY
const customStyles = {
  option: (provided, state) => ({
    ...provided,
    // borderBottom: '1px dotted pink',
    color: state.isSelected ? 'white' : 'black',

  }),
  control: () => ({
    // none of react-select's styles are passed to <Control />
    width: 200,
  }),
  singleValue: (provided, state) => {
    const opacity = state.isDisabled ? 0.5 : 1;
    const transition = 'opacity 300ms';

    return { ...provided, opacity, transition };
  },
};
  useEffect(() => {
    if (options) {
      const treeNodes = options.data.findTreeNodesByTree.map((element) => {
        return {value: element.name, label: element.name  };
      });

      setTreeNodeNames(treeNodes);
    }
  }, [options]);

  return (
    <ContextContainer>
      <ContextTitle>
        <p> {text}</p>
      </ContextTitle>
      {treeNodeNames && (
        <SearchInput>
          <Select options={treeNodeNames} styles={customStyles} />
        </SearchInput>
      )}
    </ContextContainer>
  );
};

// STYLES
const ContextContainer = styled.div`
  display: flex;
  align-items: center;
  justify-content: space-between;
`;

const ContextTitle = styled.div`
  display: flex;
  width: 30%;
  align-items: center;
  justify-content: space-between;
`;
const SearchInput = styled.div`
  width: 60%;
  padding: 5px;
  outline: black;
`;

export default ContextItem;

I get something like this:

enter image description here

So the question is: how do I achieve the desired results showed in the first photo using the customStyles from select without destroying everything? is there a way to have the original react-select's style passed to and modify them accordingly. At the end I just need the:

  • No border around the select option
  • The select option moved to the right
  • same color
  • But different background on when selected or hovered
  • and no changed border color when clicked on the selected input field

I hope makes sense, let me know if the question is not clear:) I appreciate any sort of help and clues.

David Leuliette
  • 1,595
  • 18
  • 26
user14749773
  • 143
  • 2
  • 18

2 Answers2

1

This react-select component is composed of multiple smaller parts.

You're on the right track to apply custom styles. Here is a list of various parts of react-select that you can customize. The syntax:

const customStyles = {
  option: (provided, state) => ({
    ...provided,
    color: state.isSelected ? 'red' : 'blue',
    // rest of styling
  })
}

For example, if you want to change the text for no options available, you'll style the noOptionsMessage.

Other things to note:

  1. To access the pseudo-classes like :hover for the option component, CSS-in-JS uses this syntax: "&:hover": {...}.
  2. More details on how to inspect and style a component's pseudostates here.

Here is a codesandbox which has the react-select styled close to your first picture.

DariusP
  • 448
  • 4
  • 9
  • What I need is to find a way to have the original react-select's style passed to and modify them accordingly. Because I only need: No border around the select option, The select option moved to the right same color, different background on options, when selected or hovered, and no border colors, also clicked on the selected input field. So I still want to scroll the option, I still want the arrow and the pipe with hover but just different color – user14749773 Sep 15 '21 at 10:02
  • You need to pass the `provided` argument to your returned object in the custom styles: `return {...provided, /* rest of styling */}` – DariusP Sep 15 '21 at 10:06
  • Thanks a lot, how about the alignment of the input field (when the user type) being next to where the "select..." option is, and when you type goes away. What is the provided style for that? – user14749773 Sep 15 '21 at 10:54
0

You can use styled-components then use the base component as the Select you want then change specific things about it as you wish

Here's another SO thread on the matter

Styled-components docs on the matter https://styled-components.com/docs/advanced#issues-with-specificity

Noam Yizraeli
  • 4,446
  • 18
  • 35
  • I tried already but does not work, also I am not sure which type of element is select as it comes from the framework, so I thought the only way to style it is by using their made customStyles function. I also tried inline styles but does not work – user14749773 Sep 14 '21 at 09:30
  • Try fiddling with the devtools style editor and when you the class and style force it in the styled component – Noam Yizraeli Sep 14 '21 at 09:33
  • can you add your trials to your question under "UPDATE:" section? – Noam Yizraeli Sep 14 '21 at 10:53