How to add backgrond color and change color on hover for Select component from Chakra UI using emotion/react

huangapple go评论58阅读模式
英文:

How to add backgrond color and change color on hover for Select component from Chakra UI using emotion/react

问题

I'm using chakra ui for components and emotion/react for styling.
我在使用 Chakra UI 来创建组件,并使用 emotion/react 进行样式设计。

I'm struggling with changing background-color for selected option, and also change backrgoung-color on hover. I've done it as in documentation: https://chakra-ui.com/docs/components/select/usage#usage
我在尝试更改选定选项的背景颜色以及悬停时更改背景颜色。我已按照文档中的说明进行操作:https://chakra-ui.com/docs/components/select/usage#usage

I don't see there how can I customize options from Select component.
但我在文档中没有看到如何自定义 Select 组件的选项。

Here You can find how my Select looks like after some sort of customization of Select field:
以下是我在对 Select 字段进行一些自定义操作后的 Select 外观:

<Select
  icon={<DropdownIcon />}
  value={yearsRange[date.getFullYear()]}
  onChange={handleYearChange}
  fontWeight="600"
  border="none"
  background="transparent"
  fontSize="20px"
  color={colors.primary}
  sx={{
    appearance: 'none',
    padding: '0 5px',
    paddingInlineStart: 'none',
    WebkitPaddingStart: 'none',
    '& + .chakra-select__icon-wrapper': {
      order: -1,
      position: 'static',
      transform: 'none',
    },
  }}
  rootProps={{
    display: 'flex',
    alignItems: 'center',
  }}
>
  {yearsRange.map((year) => (
    <StyledOption key={year} value={year}>
      {year}
    </StyledOption>
  ))}
</Select>

In external file for styled I have styles like this:
在用于样式的外部文件中,我的样式如下:

import styled from '@emotion/styled';

export const StyledOption = styled.option`
  :hover {
    background-color: yellow;
  }

  background-color: red;
  :active {
    background-color: pink;
  }
`;

And nothing happens, I still have the default color of the selected option.
但什么也没有发生,我仍然保持了选定选项的默认颜色。

英文:

I'm using chakra ui for components and emotion/react for styling.
I'm struggling with changing background-color for selected option, and also change backrgoung-color on hover. I've done it as in documentation: https://chakra-ui.com/docs/components/select/usage#usage
I don't see there how can I customize options from Select component.

Here You can find how my Select looks like after some sorto of customization of Select field:

&lt;Select
          icon={&lt;DropdownIncon /&gt;}
          value={yearsRange[date.getFullYear()]}
          onChange={handleYearChange}
          fontWeight=&quot;600&quot;
          border=&quot;none&quot;
          background=&quot;transparent&quot;
          fontSize=&quot;20px&quot;
          color={colors.primary}
          sx={{
            appearance: &#39;none&#39;,
            padding: &#39;0 5px&#39;,
            paddingInlineStart: &#39;none&#39;,
            WebkitPaddingStart: &#39;none&#39;,
            &#39;&amp; + .chakra-select__icon-wrapper&#39;: {
              order: -1,
              position: &#39;static&#39;,
              transform: &#39;none&#39;,
            },
          }}
          rootProps={{
            display: &#39;flex&#39;,
            alignItems: &#39;center&#39;,
          }}
        &gt;
          {yearsRange.map((year) =&gt; (
            &lt;StyledOption key={year} value={year}&gt;
              {year}
            &lt;/StyledOption&gt;
          ))}
        &lt;/Select&gt;

In external file for styled I have styles like this:

import styled from &#39;@emotion/styled&#39;;

export const StyledOption = styled.option`
  :hover {
    background-color: yellow;
  }

  background-color: red;
  :active {
    background_color: pink;
  }
`;

And nothing happens, I still have default color of selected option

答案1

得分: 1

代替自定义 option 来自定义 select:

export const StyledSelect = styled.select`
  &amp; &gt; option {
    background-color: red;
  }
  &amp; &gt; option:hover {
    background-color: yellow;
  }
  &amp; &gt; option[selected] {
    background-color: pink;
  }
`;
英文:

Instead of customizing option customize the select:

export const StyledSelect = styled.select`
  &amp; &gt; option {
    background-color: red;
  }
  &amp; &gt; option:hover {
    background-color: yellow;
  }
  &amp; &gt; option[selected] {
    background-color: pink;
  }
`;

huangapple
  • 本文由 发表于 2023年4月17日 04:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定