2018-11-26 12:06:42 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
|
|
|
|
subtitle: 全局化配置
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 其他
|
2018-11-26 12:06:42 +08:00
|
|
|
|
title: ConfigProvider
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*NVKORa7BCVwAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*YC4ERpGAddoAAAAAAAAAAAAADrJ8AQ/original
|
2018-11-26 12:06:42 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
为组件提供统一的全局化配置。
|
|
|
|
|
|
|
|
|
|
## 使用
|
|
|
|
|
|
|
|
|
|
ConfigProvider 使用 React 的 [context](https://facebook.github.io/react/docs/context.html) 特性,只需在应用外围包裹一次即可全局生效。
|
|
|
|
|
|
2023-01-19 15:37:54 +08:00
|
|
|
|
```tsx
|
|
|
|
|
import React from 'react';
|
2023-09-13 11:49:30 +08:00
|
|
|
|
import { ConfigProvider } from 'antd';
|
2018-11-26 12:06:42 +08:00
|
|
|
|
|
|
|
|
|
// ...
|
2023-01-19 15:37:54 +08:00
|
|
|
|
const Demo: React.FC = () => (
|
2020-08-03 14:35:11 +08:00
|
|
|
|
<ConfigProvider direction="rtl">
|
2018-11-26 12:06:42 +08:00
|
|
|
|
<App />
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
);
|
2023-01-19 15:37:54 +08:00
|
|
|
|
|
|
|
|
|
export default Demo;
|
2018-11-26 12:06:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-01-09 20:15:37 +08:00
|
|
|
|
### Content Security Policy
|
|
|
|
|
|
|
|
|
|
部分组件为了支持波纹效果,使用了动态样式。如果开启了 Content Security Policy (CSP),你可以通过 `csp` 属性来进行配置:
|
|
|
|
|
|
2023-01-19 15:37:54 +08:00
|
|
|
|
```tsx
|
2019-01-09 20:15:37 +08:00
|
|
|
|
<ConfigProvider csp={{ nonce: 'YourNonceCode' }}>
|
|
|
|
|
<Button>My Button</Button>
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/locale.tsx">国际化</code>
|
|
|
|
|
<code src="./demo/direction.tsx">方向</code>
|
|
|
|
|
<code src="./demo/size.tsx">组件尺寸</code>
|
|
|
|
|
<code src="./demo/theme.tsx">主题</code>
|
2023-07-25 16:29:47 +08:00
|
|
|
|
<code src="./demo/wave.tsx">自定义波纹</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/prefixCls.tsx" debug>前缀</code>
|
2023-09-13 11:49:30 +08:00
|
|
|
|
<code src="./demo/useConfig.tsx" debug>获取配置</code>
|
|
|
|
|
<code src="./demo/warning.tsx" debug>警告</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
2018-11-26 12:06:42 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| autoInsertSpaceInButton | 设置为 `false` 时,移除按钮中 2 个汉字之间的空格 | boolean | true | |
|
|
|
|
|
| componentDisabled | 设置 antd 组件禁用状态 | boolean | - | 4.21.0 |
|
|
|
|
|
| componentSize | 设置 antd 组件大小 | `small` \| `middle` \| `large` | - | |
|
|
|
|
|
| csp | 设置 [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) 配置 | { nonce: string } | - | |
|
|
|
|
|
| direction | 设置文本展示方向。 [示例](#components-config-provider-demo-direction) | `ltr` \| `rtl` | `ltr` | |
|
|
|
|
|
| getPopupContainer | 弹出框(Select, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。 | function(triggerNode) | () => document.body | |
|
|
|
|
|
| getTargetContainer | 配置 Affix、Anchor 滚动监听容器。 | () => HTMLElement | () => window | 4.2.0 |
|
|
|
|
|
| iconPrefixCls | 设置图标统一样式前缀 | string | `anticon` | 4.11.0 |
|
|
|
|
|
| locale | 语言包配置,语言包可到 [antd/locale](http://unpkg.com/antd/locale/) 目录下寻找 | object | - | |
|
2023-06-19 19:30:16 +08:00
|
|
|
|
| popupMatchSelectWidth | 下拉菜单和选择器同宽。默认将设置 `min-width`,当值小于选择框宽度时会被忽略。`false` 时会关闭虚拟滚动 | boolean \| number | - | 5.5.0 |
|
|
|
|
|
| popupOverflow | Select 类组件弹层展示逻辑,默认为可视区域滚动,可配置成滚动区域滚动 | 'viewport' \| 'scroll' <InlinePopover previewURL="https://user-images.githubusercontent.com/5378891/230344474-5b9f7e09-0a5d-49e8-bae8-7d2abed6c837.png"></InlinePopover> | 'viewport' | 5.5.0 |
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| prefixCls | 设置统一样式前缀 | string | `ant` | |
|
2022-12-13 15:49:05 +08:00
|
|
|
|
| renderEmpty | 自定义组件空状态。参考 [空状态](/components/empty-cn) | function(componentName: string): ReactNode | - | |
|
2022-11-24 11:26:38 +08:00
|
|
|
|
| theme | 设置主题,参考 [定制主题](/docs/react/customize-theme-cn) | - | - | 5.0.0 |
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| virtual | 设置 `false` 时关闭虚拟滚动 | boolean | - | 4.3.0 |
|
2023-09-13 11:49:30 +08:00
|
|
|
|
| warning | 设置警告等级,`strict` 为 `false` 时会将废弃相关信息聚合为单条信息 | { strict: boolean } | - | 5.10.0 |
|
2019-11-09 16:24:07 +08:00
|
|
|
|
|
2023-05-18 23:53:34 +08:00
|
|
|
|
### ConfigProvider.config()
|
2021-02-09 21:49:15 +08:00
|
|
|
|
|
2023-05-18 23:53:34 +08:00
|
|
|
|
设置 `Modal`、`Message`、`Notification` 静态方法配置,只会对非 hooks 的静态方法调用生效。
|
2021-02-09 21:49:15 +08:00
|
|
|
|
|
2023-01-19 15:37:54 +08:00
|
|
|
|
```ts
|
2021-02-09 21:49:15 +08:00
|
|
|
|
ConfigProvider.config({
|
2023-05-18 23:53:34 +08:00
|
|
|
|
prefixCls: 'ant',
|
|
|
|
|
iconPrefixCls: 'anticon',
|
|
|
|
|
|
|
|
|
|
// 5.6.0+
|
|
|
|
|
// 请优先考虑使用 hooks 版本
|
|
|
|
|
theme: { token: { colorPrimary: 'red' } },
|
2021-02-09 21:49:15 +08:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-14 17:39:35 +08:00
|
|
|
|
### ConfigProvider.useConfig() `5.3.0+`
|
|
|
|
|
|
|
|
|
|
`5.2.0` 版本后可用。获取父级 `Provider` 的值。如 `DisabledContextProvider`、`SizeContextProvider`。
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
const {
|
|
|
|
|
componentDisabled, // 5.3.0+
|
|
|
|
|
componentSize, // 5.3.0+
|
|
|
|
|
} = ConfigProvider.useConfig();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<!-- prettier-ignore -->
|
|
|
|
|
| 返回值 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| componentDisabled | antd 组件禁用状态 | boolean | - | 5.3.0 |
|
|
|
|
|
| componentSize | antd 组件大小状态 | `small` \| `middle` \| `large` | - | 5.3.0 |
|
|
|
|
|
|
2023-06-19 19:30:16 +08:00
|
|
|
|
### 组件配置
|
|
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
2023-07-06 09:33:54 +08:00
|
|
|
|
| alert | 设置 Alert 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 17:09:55 +08:00
|
|
|
|
| anchor | 设置 Anchor 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 10:27:31 +08:00
|
|
|
|
| avatar | 设置 Avatar 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-29 16:43:11 +08:00
|
|
|
|
| badge | 设置 Badge 组件的通用属性 | { className?: string, style?: React.CSSProperties, classNames?: { count?: string, indicator?: string }, styles?: { count?: React.CSSProperties, indicator?: React.CSSProperties } } | - | 5.7.0 |
|
2023-06-25 18:28:57 +08:00
|
|
|
|
| breadcrumb | 设置 Breadcrumb 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-19 19:30:16 +08:00
|
|
|
|
| button | 设置 Button 组件的通用属性 | { className?: string, style?: React.CSSProperties, classNames?: { icon: string }, styles?: { icon: React.CSSProperties } } | - | 5.6.0 |
|
2023-07-06 09:38:21 +08:00
|
|
|
|
| calendar | 设置 Calendar 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 10:25:55 +08:00
|
|
|
|
| card | 设置 Card 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-07 09:30:37 +08:00
|
|
|
|
| carousel | 设置 Carousel 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-27 17:13:27 +08:00
|
|
|
|
| cascader | 设置 Cascader 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 11:42:46 +08:00
|
|
|
|
| checkbox | 设置 Checkbox 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 09:45:25 +08:00
|
|
|
|
| collapse | 设置 Collapse 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-04 11:23:58 +08:00
|
|
|
|
| colorPicker | 设置 ColorPicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-07 19:02:55 +08:00
|
|
|
|
| datePicker | 设置 DatePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 14:38:02 +08:00
|
|
|
|
| descriptions | 设置 Descriptions 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-19 19:30:16 +08:00
|
|
|
|
| divider | 设置 Divider 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 10:42:13 +08:00
|
|
|
|
| drawer | 设置 Drawer 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 17:10:26 +08:00
|
|
|
|
| empty | 设置 Empty 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-28 14:13:14 +08:00
|
|
|
|
| form | 设置 Form 组件的通用属性 | { className?: string, style?: React.CSSProperties, validateMessages?: [ValidateMessages](/components/form-cn#validatemessages), requiredMark?: boolean \| `optional`, colon?: boolean, scrollToFirstError?: boolean \| [Options](https://github.com/stipsan/scroll-into-view-if-needed/tree/ece40bd9143f48caf4b99503425ecb16b0ad8249#options)} | - | requiredMark: 4.8.0; colon: 4.18.0; scrollToFirstError: 5.2.0; className: 5.7.0; style: 5.7.0 |
|
2023-06-25 14:46:13 +08:00
|
|
|
|
| image | 设置 Image 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-28 11:53:26 +08:00
|
|
|
|
| input | 设置 Input 组件的通用属性 | { autoComplete?: string, className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-28 13:51:24 +08:00
|
|
|
|
| layout | 设置 Layout 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 09:43:03 +08:00
|
|
|
|
| list | 设置 List 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-05 19:48:33 +08:00
|
|
|
|
| menu | 设置 Menu 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-27 10:51:23 +08:00
|
|
|
|
| mentions | 设置 Mentions 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 20:38:14 +08:00
|
|
|
|
| message | 设置 Message 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-27 17:14:08 +08:00
|
|
|
|
| modal | 设置 Modal 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 20:37:11 +08:00
|
|
|
|
| notification | 设置 Notification 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 15:32:18 +08:00
|
|
|
|
| pagination | 设置 Pagination 组件的通用属性 | { showSizeChanger?: boolean, className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-05 19:21:56 +08:00
|
|
|
|
| progress | 设置 Progress 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-26 19:07:57 +08:00
|
|
|
|
| radio | 设置 Radio 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-29 19:30:34 +08:00
|
|
|
|
| rate | 设置 Rate 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-25 16:20:39 +08:00
|
|
|
|
| result | 设置 Result 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-05 18:00:46 +08:00
|
|
|
|
| skeleton | 设置 Skeleton 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-25 09:32:53 +08:00
|
|
|
|
| segmented | 设置 Segmented 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-28 11:53:26 +08:00
|
|
|
|
| select | 设置 Select 组件的通用属性 | { className?: string, showSearch?: boolean, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-25 17:28:00 +08:00
|
|
|
|
| slider | 设置 Slider 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-30 11:29:39 +08:00
|
|
|
|
| switch | 设置 Switch 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-19 19:30:16 +08:00
|
|
|
|
| space | 设置 Space 的通用属性,参考 [Space](/components/space-cn) | { size: `small` \| `middle` \| `large` \| `number`, className?: string, style?: React.CSSProperties, classNames?: { item: string }, styles?: { item: React.CSSProperties } } | - | 5.6.0 |
|
2023-06-20 19:34:45 +08:00
|
|
|
|
| spin | 设置 Spin 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 21:54:54 +08:00
|
|
|
|
| statistic | 设置 Statistic 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-25 11:42:31 +08:00
|
|
|
|
| steps | 设置 Steps 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-30 20:42:43 +08:00
|
|
|
|
| table | 设置 Table 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 13:38:38 +08:00
|
|
|
|
| tabs | 设置 Tabs 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-30 17:19:27 +08:00
|
|
|
|
| tag | 设置 Tag 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 10:27:39 +08:00
|
|
|
|
| timeline | 设置 Timeline 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-07 19:02:55 +08:00
|
|
|
|
| timePicker | 设置 TimePicker 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 09:36:45 +08:00
|
|
|
|
| transfer | 设置 Transfer 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-06 10:27:39 +08:00
|
|
|
|
| tree | 设置 Tree 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-06-20 14:35:05 +08:00
|
|
|
|
| typography | 设置 Typography 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-03 16:38:56 +08:00
|
|
|
|
| upload | 设置 Upload 组件的通用属性 | { className?: string, style?: React.CSSProperties } | - | 5.7.0 |
|
2023-07-25 16:29:47 +08:00
|
|
|
|
| wave | 设置水波纹特效 | { disabled?: boolean, showEffect?: (node: HTMLElement, info: { className, token, component }) => void } | - | 5.8.0 |
|
2023-06-19 19:30:16 +08:00
|
|
|
|
|
2019-11-09 16:24:07 +08:00
|
|
|
|
## FAQ
|
|
|
|
|
|
2020-05-27 20:05:13 +08:00
|
|
|
|
#### 如何增加一个新的语言包?
|
|
|
|
|
|
|
|
|
|
参考[《增加语言包》](/docs/react/i18n#%E5%A2%9E%E5%8A%A0%E8%AF%AD%E8%A8%80%E5%8C%85)。
|
|
|
|
|
|
2022-07-12 13:10:20 +08:00
|
|
|
|
#### 为什么时间类组件的国际化 locale 设置不生效?
|
2019-11-09 16:24:07 +08:00
|
|
|
|
|
2022-12-22 14:12:26 +08:00
|
|
|
|
参考 FAQ [为什么时间类组件的国际化 locale 设置不生效?](/docs/react/faq#为什么时间类组件的国际化-locale-设置不生效)。
|
2019-12-09 19:40:43 +08:00
|
|
|
|
|
|
|
|
|
#### 配置 `getPopupContainer` 导致 Modal 报错?
|
|
|
|
|
|
2020-10-21 10:33:43 +08:00
|
|
|
|
相关 issue:<https://github.com/ant-design/ant-design/issues/19974>
|
2019-12-09 19:40:43 +08:00
|
|
|
|
|
|
|
|
|
当如下全局设置 `getPopupContainer` 为触发节点的 parentNode 时,由于 Modal 的用法不存在 `triggerNode`,这样会导致 `triggerNode is undefined` 的报错,需要增加一个[判断条件](https://github.com/afc163/feedback-antd/commit/3e4d1ad1bc1a38460dc3bf3c56517f737fe7d44a)。
|
|
|
|
|
|
|
|
|
|
```diff
|
|
|
|
|
<ConfigProvider
|
|
|
|
|
- getPopupContainer={triggerNode => triggerNode.parentNode}
|
|
|
|
|
+ getPopupContainer={node => {
|
|
|
|
|
+ if (node) {
|
|
|
|
|
+ return node.parentNode;
|
|
|
|
|
+ }
|
|
|
|
|
+ return document.body;
|
|
|
|
|
+ }}
|
|
|
|
|
>
|
|
|
|
|
<App />
|
|
|
|
|
</ConfigProvider>
|
|
|
|
|
```
|
2022-11-29 10:39:57 +08:00
|
|
|
|
|
|
|
|
|
#### 为什么 message.info、notification.open 或 Modal.confirm 等方法内的 ReactNode 无法继承 ConfigProvider 的属性?比如 `prefixCls` 和 `theme`。
|
|
|
|
|
|
|
|
|
|
静态方法是使用 ReactDOM.render 重新渲染一个 React 根节点上,和主应用的 React 节点是脱离的。我们建议使用 useMessage、useNotification 和 useModal 来使用相关方法。原先的静态方法在 5.0 中已被废弃。
|
2022-12-13 18:07:28 +08:00
|
|
|
|
|
|
|
|
|
#### Vite 生产模式打包后国际化 locale 设置不生效?
|
|
|
|
|
|
|
|
|
|
相关 issue:[#39045](https://github.com/ant-design/ant-design/issues/39045)
|
|
|
|
|
|
|
|
|
|
由于 Vite 生产模式下打包与开发模式不同,cjs 格式的文件会多一层,需要 `zhCN.default` 来获取。推荐 Vite 用户直接从 `antd/es/locale` 目录下引入 esm 格式的 locale 文件。
|