category | subtitle | group | title | cover |
---|---|---|---|---|
Components |
全局化配置 |
其他 |
ConfigProvider |
为组件提供统一的全局化配置。
ConfigProvider 使用 React 的 context 特性,只需在应用外围包裹一次即可全局生效。
import { ConfigProvider } from 'antd';
// ...
export default () => (
<ConfigProvider direction="rtl">
<App />
</ConfigProvider>
);
部分组件为了支持波纹效果,使用了动态样式。如果开启了 Content Security Policy (CSP),你可以通过 csp
属性来进行配置:
<ConfigProvider csp={{ nonce: 'YourNonceCode' }}>
<Button>My Button</Button>
</ConfigProvider>
国际化
方向
组件尺寸
主题
前缀
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
autoInsertSpaceInButton | 设置为 false 时,移除按钮中 2 个汉字之间的空格 |
boolean | true | |
componentDisabled | 设置 antd 组件禁用状态 | boolean | - | 4.21.0 |
componentSize | 设置 antd 组件大小 | small | middle | large |
- | |
csp | 设置 Content Security Policy 配置 | { nonce: string } | - | |
direction | 设置文本展示方向。 示例 | ltr | rtl |
ltr |
|
dropdownMatchSelectWidth | 下拉菜单和选择器同宽。默认将设置 min-width ,当值小于选择框宽度时会被忽略。false 时会关闭虚拟滚动 |
boolean | number | - | 4.3.0 |
form | 设置 Form 组件的通用属性 | { validateMessages?: ValidateMessages, requiredMark?: boolean | optional , colon?: boolean} |
- | requiredMark: 4.8.0; colon: 4.18.0 |
getPopupContainer | 弹出框(Select, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。 | function(triggerNode) | () => document.body | |
getTargetContainer | 配置 Affix、Anchor 滚动监听容器。 | () => HTMLElement | () => window | 4.2.0 |
iconPrefixCls | 设置图标统一样式前缀 | string | anticon |
4.11.0 |
input | 设置 Input 组件的通用属性 | { autoComplete?: string } | - | 4.2.0 |
select | 设置 Select 组件的通用属性 | { showSearch?: boolean } | - | |
locale | 语言包配置,语言包可到 antd/locale 目录下寻找 | object | - | |
prefixCls | 设置统一样式前缀 | string | ant |
|
renderEmpty | 自定义组件空状态。参考 空状态 | function(componentName: string): ReactNode | - | |
space | 设置 Space 的 size ,参考 Space |
{ size: small | middle | large | number } |
- | 4.1.0 |
theme | 设置主题,参考 定制主题 | - | - | 5.0.0 |
virtual | 设置 false 时关闭虚拟滚动 |
boolean | - | 4.3.0 |
设置 Modal
、Message
、Notification
rootPrefixCls。
ConfigProvider.config({
prefixCls: 'ant', // 4.13.0+
iconPrefixCls: 'anticon', // 4.17.0+
});
参考《增加语言包》。
参考 FAQ 为什么时间类组件的国际化 locale 设置不生效?。
相关 issue:#19974
当如下全局设置 getPopupContainer
为触发节点的 parentNode 时,由于 Modal 的用法不存在 triggerNode
,这样会导致 triggerNode is undefined
的报错,需要增加一个判断条件。
<ConfigProvider
- getPopupContainer={triggerNode => triggerNode.parentNode}
+ getPopupContainer={node => {
+ if (node) {
+ return node.parentNode;
+ }
+ return document.body;
+ }}
>
<App />
</ConfigProvider>
为什么 message.info、notification.open 或 Modal.confirm 等方法内的 ReactNode 无法继承 ConfigProvider 的属性?比如 prefixCls
和 theme
。
静态方法是使用 ReactDOM.render 重新渲染一个 React 根节点上,和主应用的 React 节点是脱离的。我们建议使用 useMessage、useNotification 和 useModal 来使用相关方法。原先的静态方法在 5.0 中已被废弃。