最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change selected react-select input color? - Stack Overflow

programmeradmin2浏览0评论

Using react-select (React.js) I notice that when the select field is clicked on it shows a blue-ish color.

I say blue-ish because it seems to let through some of the yellow border I gave to it too, so it may look green.

How do change this color?

I am assuming that I need the right css selector, and that I need the 'control' style key. Is that correct?

I have already managed to style the general border color, and the border color on hover:

const SelectStyle = {
  control: styles => ({
    ...styles,
    border: `1px solid ${Colors.sec6}`,
    "&:hover": {
      borderColor: "red"
    }
  }),
  ...
};

And I thought I could use :focus, or maybe :active to change the color when the color, but that doesn't seem to work. I have tried the following, to no avail:

"&:focus": {
  borderColor: "pink"
},
"&:active": {
  borderColor: "orange"
}

I have checked the list of css selectors at W3schools, but I don't see which of those could be the one that I need.

Using react-select (React.js) I notice that when the select field is clicked on it shows a blue-ish color.

I say blue-ish because it seems to let through some of the yellow border I gave to it too, so it may look green.

How do change this color?

I am assuming that I need the right css selector, and that I need the 'control' style key. Is that correct?

I have already managed to style the general border color, and the border color on hover:

const SelectStyle = {
  control: styles => ({
    ...styles,
    border: `1px solid ${Colors.sec6}`,
    "&:hover": {
      borderColor: "red"
    }
  }),
  ...
};

And I thought I could use :focus, or maybe :active to change the color when the color, but that doesn't seem to work. I have tried the following, to no avail:

"&:focus": {
  borderColor: "pink"
},
"&:active": {
  borderColor: "orange"
}

I have checked the list of css selectors at W3schools, but I don't see which of those could be the one that I need.

Share Improve this question asked Apr 16, 2019 at 15:40 Rik SchoonbeekRik Schoonbeek 4,5204 gold badges29 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Found the answer on the react-select GitHub page.

const customStyles = {
  control: (base, state) => ({
    ...base,
    boxShadow: "none"
    // You can also use state.isFocused to conditionally style based on the focus state
  })
};

so, this did it for me: boxShadow: "none"

source: https://github./JedWatson/react-select/issues/2728

The answer by Rik Schoonbeek is correct on how to remove the blue border.

To further change the color, we have to set boxShadow: "none" first for the control divisor.

Then, add border color to focus-within subclass:

    boxShadow: "none",
    "&:focus-within": {
      borderColor: "#f7c6b9",
      boxShadow: "0 0 0.2rem rgba(233, 105, 71, 0.25)",
    }

This way, the behavior will match that of react-bootstrap text input.

We can add either using the customStyle in js or add a wrapper divisor with a specific className and add it in scss using class_end_with_selector:

.myReactSelectClass {
   [class$='-control'] {
      ...
   }
}

Update: There is a react-select open bug when building prod DoM such that the style is not applied in prod: https://github./JedWatson/react-select/issues/3309

So to avoid it, add a classNamePrefix="react-select" (could be any preferred string) to enforce the classNames in prod DoM And the using the following classNames (use react-select Prefix as an example)

  .react-select__value-container {...}
  .react-select__indicators {...}
  .react-select__input {...}
  .react-select__control {...}
发布评论

评论列表(0)

  1. 暂无评论