reactjs - 将 aria-role 传递给 Material UI零件
问题描述
我目前正在使用 Material UI 的<Icon />
组件。role="img"
Material UI 的文档在这篇文章中提到了通过的能力: https ://material-ui.com/components/icons/#semantic-svg-icons但是我认为它只适用于<SvgIcon />
. 有没有人幸运地将这些道具传递给<Icon />
组件?
这是我正在使用的示例代码。
import React from 'react';
import { Icon } from '@material-ui/core';
export interface IconProps {
icon: string;
}
export const SampleIcon = ({icon = 'home'}: IconProps) => {
return (
<Icon
aria-hidden="false"
aria-label="Sample Icon"
component="span"
role="img"
title="Sample Icon">
{icon}
</Icon>
);
});
解决方案
您拥有的代码没有任何问题。 Icon
接受所有可分配给任何 DOM 元素的标准 JSX 属性,包括role
.
您的代码输出以下 HTML:
<span class="material-icons MuiIcon-root" aria-hidden="false" aria-label="Sample Icon" role="img" title="Sample Icon">star</span>
推荐阅读
- reactjs - 无法使用带有功能组件的 refs 从父级调用子函数
- drupal - 列出多种内容类型的数据
- boot - 从目录创建纯 uefi 可启动 iso
- c++ - 如何
使用 Base 和 Derived 对象默认初始化 std::vector? - javascript - React Native 添加多个表单 onPress 按钮和 onchangeText 问题
- php - 在 SQL 查询中获取日期
- macos - 在 macOS 的 SceneKit 中无法显示 stl 文件
- python - 如何隐藏按钮以及如何在其上进行新的选项卡重定向?
- apache-kafka - 如何通过kafka生产者通知用户到消费者进程
- r - mlr:在 CV 循环中检索 generateFilterValuesData 的输出