AlertAPI
React Alert 组件的 API 参考文档。了解此导出模块的 props、CSS 和其他 API。
演示
导入
import Alert from '@mui/material/Alert';
// or
import { Alert } from '@mui/material';
通过阅读有关最小化 bundle 大小的指南,了解它们之间的区别。
Paper 组件的 props 也可用。
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
action | node | - | 要显示的操作。它在消息之后、警报末尾呈现。 |
children | node | - | 组件的内容。 |
classes | object | - | 覆盖或扩展应用于组件的样式。 有关更多详细信息,请参见下方的CSS 类 API。 |
closeText | string | '关闭' | 覆盖关闭弹出窗口图标按钮的默认标签。 |
color | 'error' | 'info' | 'success' | 'warning' | string | - | 组件的颜色。除非提供,否则该值取自 |
components | { CloseButton?: elementType, CloseIcon?: elementType } | {} | 用于内部每个插槽的组件。 |
componentsProps | { closeButton?: object, closeIcon?: object } | {} | 插槽组件的额外 props。您可以覆盖现有 props 或添加新的 props。 |
icon | node | - | 覆盖在 children 之前显示的图标。除非提供,否则图标将映射到 |
iconMapping | { error?: node, info?: node, success?: node, warning?: node } | - | 组件将 |
onClose | func | - | 当组件请求关闭时触发的回调。当提供且未设置 签名: function(event: React.SyntheticEvent) => void
|
role | string | 'alert' | 元素的 ARIA role 属性。 |
severity | 'error' | 'info' | 'success' | 'warning' | string | 'success' | 警报的严重程度。这定义了使用的颜色和图标。 |
slotProps | { action?: func | object, closeButton?: func | object, closeIcon?: func | object, icon?: func | object, message?: func | object, root?: func | object } | {} | 用于内部每个插槽的 props。 |
slots | { action?: elementType, closeButton?: elementType, closeIcon?: elementType, icon?: elementType, message?: elementType, root?: elementType } | {} | 用于内部每个插槽的组件。 |
sx | Array<func | object | bool> | func | object | - | 系统 prop,允许定义系统覆盖以及额外的 CSS 样式。 有关更多详细信息,请参见 `sx` 页面。 |
variant | 'filled' | 'outlined' | 'standard' | string | 'standard' | 要使用的 variant。 |
ref
被转发到根元素。继承
虽然上面没有明确文档说明,但 Paper 组件的 props 也可在 Alert 中使用。您可以利用这一点来定位嵌套组件。
主题默认 props
您可以使用 MuiAlert
通过主题更改此组件的默认 props。
插槽名称 | 类名 | 默认组件 | 描述 |
---|---|---|---|
root | .MuiAlert-root | Paper | 渲染 root 插槽的组件。 |
icon | .MuiAlert-icon | div | 渲染 icon 插槽的组件。 |
message | .MuiAlert-message | div | 渲染 message 插槽的组件。 |
action | .MuiAlert-action | div | 渲染 action 插槽的组件。 |
closeButton | IconButton | 渲染 close button 的组件。 | |
closeIcon | svg | 渲染 close icon 的组件。 |
这些类名对于使用 CSS 进行样式设置很有用。当触发特定状态时,它们会应用于组件的插槽。
类名 | 规则名称 | 描述 |
---|---|---|
.MuiAlert-colorError | colorError | 如果 color="error" ,则应用于根元素的样式。 |
.MuiAlert-colorInfo | colorInfo | 如果 color="info" ,则应用于根元素的样式。 |
.MuiAlert-colorSuccess | colorSuccess | 如果 color="success" ,则应用于根元素的样式。 |
.MuiAlert-colorWarning | colorWarning | 如果 color="warning" ,则应用于根元素的样式。 |
.MuiAlert-filled | filled | 如果 variant="filled" ,则应用于根元素的样式。 |
.MuiAlert-filledError | filledError | 如果 variant="filled" 且 color="error" ,则应用于根元素的样式。 |
.MuiAlert-filledInfo | filledInfo | 如果 variant="filled" 且 color="info" ,则应用于根元素的样式。 |
.MuiAlert-filledSuccess | filledSuccess | 如果 variant="filled" 且 color="success" ,则应用于根元素的样式。 |
.MuiAlert-filledWarning | filledWarning | 如果 variant="filled" 且 color="warning" ,则应用于根元素的样式。 |
.MuiAlert-outlined | outlined | 如果 variant="outlined" ,则应用于根元素的样式。 |
.MuiAlert-outlinedError | outlinedError | 如果 variant="outlined" 且 color="error" ,则应用于根元素的样式。 |
.MuiAlert-outlinedInfo | outlinedInfo | 如果 variant="outlined" 且 color="info" ,则应用于根元素的样式。 |
.MuiAlert-outlinedSuccess | outlinedSuccess | 如果 variant="outlined" 且 color="success" ,则应用于根元素的样式。 |
.MuiAlert-outlinedWarning | outlinedWarning | 如果 variant="outlined" 且 color="warning" ,则应用于根元素的样式。 |
.MuiAlert-standard | standard | 如果 variant="standard" ,则应用于根元素的样式。 |
.MuiAlert-standardError | standardError | 如果 variant="standard" 且 color="error" ,则应用于根元素的样式。 |
.MuiAlert-standardInfo | standardInfo | 如果 variant="standard" 且 color="info" ,则应用于根元素的样式。 |
.MuiAlert-standardSuccess | standardSuccess | 如果 variant="standard" 且 color="success" ,则应用于根元素的样式。 |
.MuiAlert-standardWarning | standardWarning | 如果 variant="standard" 且 color="warning" ,则应用于根元素的样式。 |
您可以使用以下自定义选项之一来覆盖组件的样式
- 使用全局类名。
- 在自定义主题中使用规则名称作为组件的
styleOverrides
属性的一部分。
源代码
如果您在此页面中找不到所需的信息,请考虑查看组件的实现以获取更多详细信息。