Back to Ant Design

ConfigProvider

components/config-provider/index.zh-CN.md

6.4.413.1 KB
Original Source

使用 {#usage}

ConfigProvider 使用 React 的 context 特性,只需在应用外围包裹一次即可全局生效。

tsx
import React from 'react';
import { ConfigProvider } from 'antd';

// ...
const Demo: React.FC = () => (
  <ConfigProvider direction="rtl">
    <App />
  </ConfigProvider>
);

export default Demo;

内容安全策略(CSP){#csp}

部分组件为了支持波纹效果,使用了动态样式。如果开启了 Content Security Policy (CSP),你可以通过 csp 属性来进行配置:

tsx
<ConfigProvider csp={{ nonce: 'YourNonceCode' }}>
  <Button>My Button</Button>
</ConfigProvider>

代码演示 {#examples}

<!-- prettier-ignore -->

<code src="./demo/locale.tsx">国际化</code> <code src="./demo/direction.tsx">方向</code> <code src="./demo/size.tsx">组件尺寸</code> <code src="./demo/theme.tsx">主题</code> <code src="./demo/wave.tsx">自定义波纹</code> <code src="./demo/holderRender.tsx">静态方法</code> <code src="./demo/prefixCls.tsx" debug>前缀</code> <code src="./demo/useConfig.tsx" debug>获取配置</code> <code src="./demo/warning.tsx" debug>警告</code>

API

参数说明类型默认值版本
componentDisabled设置 antd 组件禁用状态boolean-4.21.0
componentSize设置 antd 组件大小small | medium | large-
csp设置 Content Security Policy 配置{ nonce: string }-
direction设置文本展示方向。 示例ltr | rtlltr
getPopupContainer弹出框(Select, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。(trigger?: HTMLElement) => HTMLElement | ShadowRoot() => document.body
getTargetContainer配置 Affix、Anchor 滚动监听容器。() => HTMLElement | Window | ShadowRoot() => window4.2.0
iconPrefixCls设置图标统一样式前缀stringanticon4.11.0
locale语言包配置,语言包可到 antd/locale 目录下寻找object-
popupMatchSelectWidth下拉菜单和选择器同宽。默认将设置 min-width,当值小于选择框宽度时会被忽略。false 时会关闭虚拟滚动boolean | number-5.5.0
popupOverflowSelect 类组件弹层展示逻辑,默认为可视区域滚动,可配置成滚动区域滚动'viewport' | 'scroll' <InlinePopover previewURL="https://user-images.githubusercontent.com/5378891/230344474-5b9f7e09-0a5d-49e8-bae8-7d2abed6c837.png"></InlinePopover>'viewport'5.5.0
prefixCls设置统一样式前缀stringant
renderEmpty自定义组件空状态。参考 空状态function(componentName: string): ReactNode-
theme设置主题,参考 定制主题Theme-5.0.0
variant设置全局输入组件形态变体outlined | filled | borderless-5.19.0
virtual设置 false 时关闭虚拟滚动boolean-4.3.0
warning设置警告等级,strictfalse 时会将废弃相关信息聚合为单条信息{ strict: boolean }-5.10.0
autoInsertSpaceInButtonButton 自动空格配置,请使用 button={{ autoInsertSpace: boolean }} 替代boolean--
dropdownMatchSelectWidth下拉菜单和选择器是否同宽,请使用 popupMatchSelectWidth 替代boolean--

ConfigProvider.config() {#config}

设置 ModalMessageNotification 静态方法配置,只会对非 hooks 的静态方法调用生效。

tsx
ConfigProvider.config({
  // 5.13.0+
  holderRender: (children) => (
    <ConfigProvider
      prefixCls="ant"
      iconPrefixCls="anticon"
      theme={{ token: { colorPrimary: 'red' } }}
    >
      {children}
    </ConfigProvider>
  ),
});

ConfigProvider.useConfig() <Badge>5.3.0+</Badge> {#useconfig}

获取父级 Provider 的值,如 DisabledContextProviderSizeContextProvider

jsx
const {
  componentDisabled, // 5.3.0+
  componentSize, // 5.3.0+
} = ConfigProvider.useConfig();
<!-- prettier-ignore -->
返回值说明类型默认值版本
componentDisabledantd 组件禁用状态boolean-5.3.0
componentSizeantd 组件大小状态small | medium | large-5.3.0

组件配置 {#component-config}

以下配置项用于设置对应组件的通用属性或全局效果配置,具体 API 见链接:

  • affixAffix(自 6.0.0 起支持)
  • alertAlert(自 5.7.0 起支持)
  • anchorAnchor(自 6.0.0 起支持)
  • appApp(自 6.3.0 起支持)
  • avatarAvatar(自 5.7.0 起支持)
  • badgeBadge(自 5.7.0 起支持)
  • borderBeamBorderBeam(自 6.4.0 起支持)
  • breadcrumbBreadcrumb(自 5.7.0 起支持)
  • buttonButton(自 5.6.0 起支持)
  • calendarCalendar(自 6.0.0 起支持)
  • cardCard(自 5.14.0 起支持)
  • cardMetaCard.Meta(自 6.0.0 起支持)
  • carouselCarousel(自 5.7.0 起支持)
  • cascaderCascader(自 5.13.0 起支持)
  • checkboxCheckbox(自 6.0.0 起支持)
  • collapseCollapse(自 5.15.0 起支持)
  • colorPickerColorPicker(自 6.3.0 起支持)
  • datePickerDatePicker(自 5.7.0 起支持)
  • rangePickerRangePicker(自 5.11.0 起支持)
  • descriptionsDescriptions(自 5.23.0 起支持)
  • dividerDivider(自 5.10.0 起支持)
  • drawerDrawer(自 5.10.0 起支持)
  • dropdownDropdown(自 5.11.0 起支持)
  • emptyEmpty(自 5.23.0 起支持)
  • flexFlex(自 5.10.0 起支持)
  • floatButtonFloatButton(自 6.0.0 起支持)
  • floatButtonGroupFloatButton.Group(自 5.16.0 起支持)
  • formForm(自 4.8.0 起支持)
  • imageImage(自 5.14.0 起支持)
  • inputInput(自 4.2.0 起支持)
  • inputNumberInputNumber(自 5.19.0 起支持)
  • otpInput.OTP(自 6.0.0 起支持)
  • inputPasswordInput.Password(自 6.4.0 起支持)
  • inputSearchInput.Search(自 6.4.0 起支持)
  • textAreaInput.TextArea(自 5.15.0 起支持)
  • layoutLayout(自 5.7.0 起支持)
  • listList(自 5.7.0 起支持)
  • masonryMasonry(自 6.0.0 起支持)
  • menuMenu(自 5.15.0 起支持)
  • mentionsMentions(自 5.13.0 起支持)
  • messageMessage(自 5.7.0 起支持)
  • modalModal(自 5.10.0 起支持)
  • notificationNotification(自 5.14.0 起支持)
  • paginationPagination(自 6.0.0 起支持)
  • progressProgress(自 5.7.0 起支持)
  • radioRadio(自 6.0.0 起支持)
  • rateRate(自 5.7.0 起支持)
  • resultResult(自 6.0.0 起支持)
  • ribbonBadge.Ribbon(自 6.0.0 起支持)
  • skeletonSkeleton(自 6.0.0 起支持)
  • segmentedSegmented(自 6.0.0 起支持)
  • selectSelect(自 5.13.0 起支持)
  • sliderSlider(自 5.23.0 起支持)
  • switchSwitch(自 6.0.0 起支持)
  • spaceSpace(自 5.6.0 起支持)
  • splitterSplitter(自 5.21.0 起支持)
  • spinSpin(自 5.20.0 起支持)
  • statisticStatistic(自 6.0.0 起支持)
  • stepsSteps(自 5.10.0 起支持)
  • tableTable(自 6.2.0 起支持)
  • tabsTabs(自 5.14.0 起支持)
  • tagTag(自 5.14.0 起支持)
  • timelineTimeline(自 6.0.0 起支持)
  • timePickerTimePicker(自 5.13.0 起支持)
  • tourTour(自 5.14.0 起支持)
  • tooltipTooltip(自 6.1.0 起支持)
  • popoverPopover(自 5.23.0 起支持)
  • popconfirmPopconfirm(自 5.23.0 起支持)
  • qrcodeQRCode(自 6.0.0 起支持)
  • transferTransfer(自 5.7.0 起支持)
  • treeTree(自 6.0.0 起支持)
  • treeSelectTreeSelect(自 5.19.0 起支持)
  • typographyTypography(自 6.4.0 起支持)
  • uploadUpload(自 5.27.0 起支持)
  • watermarkWatermark(自 6.0.0 起支持)
  • waveWaveConfig(自 5.8.0 起支持)

WaveConfig

参数说明类型默认值版本
disabled是否禁用水波纹效果booleanfalse
showEffect自定义水波纹效果(node: HTMLElement, info: { className, token, component }) => void-
triggerType触发水波纹效果的事件click | pointerdown | pointerup | mousedown | mouseupclick6.4.0

FAQ

如何增加一个新的语言包? {#faq-add-locale}

参考《增加语言包》

为什么时间类组件的国际化 locale 设置不生效? {#faq-locale-not-work}

参考 FAQ 为什么时间类组件的国际化 locale 设置不生效?

配置 getPopupContainer 导致 Modal 报错? {#faq-get-popup-container}

相关 issue:https://github.com/ant-design/ant-design/issues/19974

当如下全局设置 getPopupContainer 为触发节点的 parentNode 时,由于 Modal 的用法不存在 triggerNode,这样会导致 triggerNode is undefined 的报错,需要增加一个判断条件

diff
 <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 的属性?比如 prefixClstheme。 {#faq-message-inherit}

静态方法是使用 ReactDOM.render 重新渲染一个 React 根节点上,和主应用的 React 节点是脱离的。我们建议使用 useMessage、useNotification 和 useModal 来使用相关方法。原先的静态方法在 5.0 中已被废弃。

Vite 生产模式打包后国际化 locale 设置不生效? {#faq-vite-locale-not-work}

相关 issue:#39045

由于 Vite 生产模式下打包与开发模式不同,cjs 格式的文件会多一层,需要 zhCN.default 来获取。推荐 Vite 用户直接从 antd/es/locale 目录下引入 esm 格式的 locale 文件。

prefixCls 优先级(前者被后者覆盖) {#faq-prefixcls-priority}

  1. ConfigProvider.config({ prefixCls: 'prefix-1' })
  2. ConfigProvider.config({ holderRender: (children) => <ConfigProvider prefixCls="prefix-2">{children}</ConfigProvider> })
  3. message.config({ prefixCls: 'prefix-3' })