简体   繁体   English

如何隐藏 MUI React ListItem?

[英]How to hide MUI React ListItem?

I have the following:我有以下几点:

<ListItem key={name} hidden={true} aria-hidden={true}>
  name
</ListItem>

but the ListItem is still showing up.ListItem仍然出现。 How can it be hidden?怎么可能隐藏?

As far as I know, there is no hidden props on the ListItem component in Material-UI, so you will have to implement you own behavior to hide the ListItem :据我所知,Material-UI 中的ListItem组件上没有hidden道具,因此您必须实现自己的行为来隐藏ListItem

I was looking to programmatically hide a Material-UI FormControl component, and found the same issue (ie the lack of a hidden prop).我希望以编程方式隐藏 Material-UI FormControl 组件,但发现了同样的问题(即缺少hidden道具)。

What worked for me was to add a method that returned the appropriate class string, based on whether I wanted to show the component in question or not.对我有用的是添加一个返回适当类字符串的方法,这取决于我是否想显示有问题的组件。

For example, with styles like:例如,样式如下:

const styles = createStyles({
    ...
    formcontrol: {
        minWidth: 120,
        margin: 10
    },
    invisible: {
        visibility: "hidden"
    },
});

I added this to my component class:我将此添加到我的组件类中:

getStyle() {
    let cls: string;
    if (this.props.whatever) {
        cls = this.props.classes.formcontrol;
    } else {
        cls = this.props.classes.invisible + " " + this.props.classes.formcontrol;
    }
    return cls;
}

And then reference that from render() when creating the component I want to sometimes hide:然后在创建我有时想隐藏的组件时从render()引用它:

<FormControl className={this.getStyle()}>
...
</FormControl>

This should work for any styled MUI component.这应该适用于任何样式的 MUI 组件。

(Side-note: the display prop appears from the docs to do this, but didn't work for me. Perhaps it works only for Box components, which happen to be what's used in all of the examples in the docs. This is worth further investigation which I have not yet spent the time to do.) (旁注:显示道具从文档中出现来执行此操作,但对我不起作用。也许它仅适用于 Box 组件,这恰好是文档中所有示例中使用的组件。这是值得的我还没有花时间做进一步的调查。)

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

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