简体   繁体   English

在另一个元素周围扩展 MUI Select 的边框

[英]Extend the border of MUI Select around another element

I have a dropdown menu using MUI Select to sort (AZ, newest-oldest), and I have a button next to it to reverse direction (ZA, oldest-newest).我有一个使用 MUI Select 排序的下拉菜单(AZ,最新-最旧),旁边有一个按钮可以反转方向(ZA,最旧-最新)。 Is it possible to extend the border of the Select (which darkens on hover) around the IconButton ?是否可以在IconButton周围扩展 Select 的边框(悬停时变暗)?

Here's a gif of what currently happens: border highlights on hover but goes back to normal when cursor moves to button .这是当前发生的情况的 gif:悬停边框突出显示,但当光标移动到 button 时恢复正常

If I just change the both elements are in to variant="outlined" , the outline cuts right through the label of the Select, whereas the Select's outline leave a gap for the label:如果我只是将两个元素都更改为variant="outlined" ,则轮廓会直接穿过 Select 的标签,而 Select 的轮廓会为标签留下间隙:

轮廓直接穿过 Select 的标签 如果 Paper 和 Select 都带有轮廓,则 Select 周围的边框会加倍,并且会切穿其标签 Select 的轮廓为标签留出空隙

If it's not possible to extend the Select's outline around the button, then can I make a new border around the whole element that has the same behavior on hover -- but without doubling the width of the border around the Select?如果无法在按钮周围扩展 Select 的轮廓,那么我可以在整个元素周围创建一个新的边框,该边框在悬停时具有相同的行为 - 但不会将 Select 周围的边框宽度加倍吗? Or can I make a border just around three sides of the little nubbin that's sticking out with the IconButton in it?或者我可以在里面有 IconButton 的小块的三个边周围做一个边框吗? How might I do that?我该怎么做?

Here's the relevant part of my code for that component:这是该组件的代码的相关部分:

return (
    <FormControl sx={{ minWidth: "152px", mt: 1, }} >
        <Paper sx={{ bgcolor: "#fefcf9", }}>
        <InputLabel id="arrange-by-label">Arrange By</InputLabel>
        <Select
            labelId="arrange-by-label"
            id="arrange-by"
            value={litTextsOrder}
            label="Arrange By"
            onChange={handleLitTextsOrder}
            sx={{ bgcolor: "#fefcf9", minWidth: "132px" }}
        >
            {valuesArr.map((valueArr, index) => {
                return (
                    <MenuItem 
                        key={index} 
                        value={valueArr[0]}
                    >
                        {valueArr[1]} 
                    </MenuItem>
                )
            })}
        </Select>
        <Box component="span">
        <IconButton>
            <CompareArrowsIcon sx={{ transform: "rotate(90deg)"}}/>
        </IconButton>
        </Box>
        </Paper>
    </FormControl>
)

I tried just moving the inside the , but of course that didn't work.我试着只移动里面的 ,但这当然没有用。 Thanks for your help, y'all!谢谢大家的帮助!

You can do that by target the next element using & + span selector, remove the border right border of the Select and add a border except the left side in the button next to the Select .您可以使用做到这一点的目标下一个元素& + span选择,删除的边框右边框Select和添加边框除了左侧旁的按钮Select The hover color is not in a theme, it's hardcoded at this line, so you also have to copy it.悬停颜色不在主题中,它在这一行硬编码,因此您还必须复制它。

const theme = useTheme();
const hoverColor =
  theme.palette.mode === "light"
    ? "rgba(0, 0, 0, 0.23)"
    : "rgba(255, 255, 255, 0.23)";
const activeColor = theme.palette.primary.main;
<Select
  label="Arrange By"
  sx={{
    bgcolor: "#fefcf9",
    minWidth: "132px",
    "& fieldset": {
      borderTopRightRadius: 0,
      borderBottomRightRadius: 0
    },
    "&&:hover fieldset": {
      borderRight: "none",
      borderLeft: `solid 1px ${hoverColor}`,
      borderTop: `solid 1px ${hoverColor}`,
      borderBottom: `solid 1px ${hoverColor}`
    },
    "&&.Mui-focused fieldset": {
      borderRight: "none",
      borderLeft: `solid 1px ${activeColor}`,
      borderTop: `solid 1px ${activeColor}`,
      borderBottom: `solid 1px ${activeColor}`
    },
    "& + span": {
      display: "flex",
      borderRadius: theme.shape.borderRadius + "px",
      borderTopLeftRadius: 0,
      borderBottomLeftRadius: 0,

      "& button": {
        height: "100%"
      }
    },
    "&:hover + span": {
      borderColor: hoverColor,
      border: `solid 1px ${hoverColor}`,
      borderLeft: "none"
    },
    "&.Mui-focused + span": {
      borderColor: activeColor,
      border: `solid 1px ${activeColor}`,
      borderLeft: "none"
    }
  }}
>

代码沙盒演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM