【问题标题】:NO WRAP Challenge in Material UI List, ListItemText材质 UI 列表、ListItemText 中的 NO WRAP 挑战
【发布时间】:2019-12-29 17:39:44
【问题描述】:

有没有办法在ListItemText 中不换行?

我需要 ListItemText 只是为了剪切单词而不需要任何扩展或滚动。

谢谢

【问题讨论】:

  • 你能给我们看看样品吗?

标签: css material-ui


【解决方案1】:

从 Material UI 版本 5.0.6 开始,您可以使用 primaryTypography 属性覆盖 ListItemText

有关ListItemText Api Documentation的更多信息

primaryTypographyProps:这些道具将被转发到主要排版组件(只要 disableTypography 不为真)。

这意味着传递的道具将被传递给Typography Component

要隐藏或换行文本,我们可以限制 ListItem 的宽度(如果需要,取决于父项)在这种情况下将 maxWidth 设置为 300px,然后使用 primaryTypographyProps 并传递一个 style 对象将 whiteSpaceoverflowtextOverflow 设置为隐藏。

import { ListItem, ListItemText } from '@mui/material';

<ListItem
    sx={{
        maxWidth: 300
    }}
>
    <ListItemText
        primary={"Super long string that needs to be wrapped"}
        secondary={"other text not important"}
        primaryTypographyProps={{ 
            variant: 'subtitle2', 
            style: {
                whiteSpace: 'nowrap',
                overflow: 'hidden',
                textOverflow: 'ellipsis'
            }
        }
        secondaryTypographyProps={{ variant: 'caption' }}
    />
</ListItem>

如果您愿意,您还可以禁用 Typography 并传递您自己的组件来获得更多控制权,该组件完全有自己的样式。

disableTypography:如果为 true,则子代不会被 Typography 组件包裹。这对于通过使用 Typography 组件包装子(或主要)文本和可选的辅助文本来呈现替代 Typography 变体很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2022-01-24
    • 2021-10-05
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    相关资源
    最近更新 更多