英文:
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:
<Select
icon={<DropdownIncon />}
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 default color of selected option
答案1
得分: 1
代替自定义 option
来自定义 select
:
export const StyledSelect = styled.select`
& > option {
background-color: red;
}
& > option:hover {
background-color: yellow;
}
& > option[selected] {
background-color: pink;
}
`;
英文:
Instead of customizing option
customize the select
:
export const StyledSelect = styled.select`
& > option {
background-color: red;
}
& > option:hover {
background-color: yellow;
}
& > option[selected] {
background-color: pink;
}
`;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论