【问题标题】:How to implement conditional styles in MUI v5 sx prop如何在 MUI v5 sx prop 中实现条件样式
【发布时间】:2021-10-08 18:35:46
【问题描述】:

在 MUI5 的 sx 属性中寻找巧妙的条件样式。我玩过的一个例子是:

const statusStyle = (status) => {
  switch (status) {
    case "aborted":
      return "#D66460";
      break;
    case "queue":
      return "#6685F0";
      break;
    case "processing":
      return "#F0E666";
      break;
    default:
      return "#60D660";
  }
};

<TableRow
  key={job.job}
  sx={{ color: statusStyle(status) }}
>

但我想知道是否有人想出了更优雅的东西?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    如果只是简单的原始键映射,可以使用对象:

    const statusColors = {
      aborted: '#D66460',
      queue: '#6685F0',
      processing: '#F0E666',
    }
    
    sx={{ color: statusColors[job.status] ?? defaultColor }}
    

    如果你想有条件地传递一个样式对象,你可以使用扩展语法...,这就是MUI团队apply the styles对具有不同变体和状态的组件的方式:

    sx={{
      color: defaultColor,
      backgroundColor: defaultBgcolor,
      ...(job.status === 'aborted' && {
        color: color1,
        backgroundColor: bgcolor1,
      }),
      ...(job.status === 'queue' && {
        color: color2,
        backgroundColor: bgcolor2,
      }),
      ...(job.status === 'processing' && {
        color: color3,
        backgroundColor: bgcolor3,
      }),
    }}
    

    【讨论】:

    • 当我尝试类似 statusColors[job.status] 我得到No index signature with a parameter of type 'string' was found on type '{ aborted: string; queue: string; processing: string; }
    • @isea 我不确定发生了什么。你能重现thiscodesandbox 中的错误吗?它在那里工作正常。
    • 嗯,是的,这就是我所期望的。我认为错误来自对我正在从事的项目的严格检查。
    • @isea 你可以把你项目的 tsconfig 复制到here 看看有什么问题。
    • @isea 可能是打字稿错误,可以改成statusColors[job.status as keyof statusColors]
    【解决方案2】:

    使用他们的触发器

    例如,在卷轴上:

    import useScrollTrigger from '@mui/material/useScrollTrigger';
    
    //...
    
    const trigger = useScrollTrigger({
    disableHysteresis: true,
    threshold: 38,
    });
    
    //...
    
    <Topbar
    colorInvert={trigger ? false : colorInvert}/>
    

    【讨论】:

      【解决方案3】:

      这似乎对我有用

      const [emailStatus, setEmailStatus] = useState('');
        const [emailStatusText, setEmailStatusText] = useState('');
        const [isEmailSubmitted, setIsEmailSubmitted] = useState(false);
      

      ...

      return response.json().then((data) => {
                  const { success, error } = data;
                  setEmailStatus(success ? 'success' : 'error');
                  setEmailStatusText(success || error);
                  return data;
                });
      

      ...

      {isEmailSubmitted && (
                    <Typography
                      sx={{
                        textAlign: 'left',
                        color:
                          emailStatus === 'success'
                            ? theme.palette.success.main
                            : theme.palette.error.main,
                      }}
                    >
                      {emailStatusText}
                    </Typography>
                  )}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        • 2022-10-05
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多