Back to Ant Design

ConfigProvider

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

6.3.728.7 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}

参数说明类型默认值版本
affix设置 Affix 组件的通用属性{ className?: string, style?: React.CSSProperties }-6.0.0
alert设置 Alert 组件的通用属性{ className?: string, style?: React.CSSProperties, closeIcon?: React.ReactNode, successIcon?: React.ReactNode, infoIcon?: React.ReactNode, warningIcon?: React.ReactNode, errorIcon?: React.ReactNode }-5.7.0, closeIcon: 5.14.0, successIcon, infoIcon, warningIconerrorIcon: 6.2.0
anchor设置 Anchor 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: AnchorStyleConfig["classNames"], styles?: AnchorStyleConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
avatar设置 Avatar 组件的通用属性{ className?: string, style?: React.CSSProperties }-5.7.0
badge设置 Badge 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: BadgeProps["classNames"], styles?: BadgeProps["styles"] }-5.7.0
breadcrumb设置 Breadcrumb 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: BreadcrumbConfig["classNames"], styles?: BreadcrumbConfig["styles"], separator?: ReactNode, dropdownIcon?: ReactNode }-5.7.0, classNames, separatorstyles: 6.0.0, dropdownIcon: 6.2.0
button设置 Button 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: ButtonProps["classNames"], styles?: ButtonProps["styles"], autoInsertSpace?: boolean, variant?: ButtonVariantType, color?: ButtonColorType, shape?: ButtonProps["shape"], loadingIcon?: ReactNode }-5.6.0, autoInsertSpace: 5.17.0, variantcolor: 5.25.0, shape: 5.27.0, loadingIcon: 6.3.0
calendar设置 Calendar 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: CalendarConfig["classNames"], styles?: CalendarConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
card设置 Card 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: CardProps["classNames"], styles?: CardProps["styles"] }-5.7.0, classNamesstyles: 5.14.0
cardMeta设置 Card.Meta 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: CardMetaProps["classNames"], styles?: CardMetaProps["styles"] }-6.0.0
carousel设置 Carousel 组件的通用属性{ className?: string, style?: React.CSSProperties }-5.7.0
cascader设置 Cascader 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: CascaderConfig["classNames"], styles?: CascaderConfig["styles"], expandIcon?: React.ReactNode, loadingIcon?: React.ReactNode }-5.7.0, classNamesstyles: 6.0.0, expandIconloadingIcon: 6.3.0
checkbox设置 Checkbox 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: CheckboxConfig["classNames"], styles?: CheckboxConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
collapse设置 Collapse 组件的通用属性{ className?: string, style?: React.CSSProperties, expandIcon?: (props) => ReactNode, classNames?: CollapseProps["classNames"], styles?: CollapseProps["styles"] }-5.7.0, expandIcon: 5.15.0, classNamesstyles: 6.0.0
colorPicker设置 ColorPicker 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: ColorPickerConfig["classNames"], styles?: ColorPickerConfig["styles"] }-5.7.0
datePicker设置 DatePicker 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: DatePickerConfig["classNames"], styles?: DatePickerConfig["styles"], suffixIcon?: React.ReactNode }-5.7.0, suffixIcon: 6.3.0
rangePicker设置 RangePicker 组件的通用属性{ className?: string, style?: React.CSSProperties, separator?: React.ReactNode }-5.11.0, separator: 6.3.0
descriptions设置 Descriptions 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: DescriptionsProps["classNames"], styles?: DescriptionsProps["styles"] }-5.7.0, classNamesstyles: 5.23.0
divider设置 Divider 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: DividerProps["classNames"], styles?: DividerProps["styles"] }-
drawer设置 Drawer 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: DrawerProps["classNames"], styles?: DrawerProps["styles"], closeIcon?: ReactNode, closable?: DrawerProps["closable"]}-5.7.0, classNamesstyles: 5.10.0, closeIcon: 5.14.0
dropdown设置 Dropdown 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: DropdownConfig["classNames"], styles?: DropdownConfig["styles"] }-
empty设置 Empty 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:EmptyProps["classNames"], styles?: EmptyProps["styles"], image?: ReactNode }-5.7.0, classNamesstyles: 5.23.0, image: 5.27.0
flex设置 Flex 组件的通用属性{ className?: string, style?: React.CSSProperties, vertical?: boolean }-5.10.0
floatButton设置 FloatButton 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: FloatButtonProps["classNames"], styles?: FloatButtonProps["styles"], backTopIcon?: React.ReactNode }-
floatButtonGroup设置 FloatButton.Group 组件的通用属性{ closeIcon?: React.ReactNode, className?: string, style?: React.CSSProperties, classNames?: FloatButtonProps["classNames"], styles?: FloatButtonProps["styles"] }-
form设置 Form 组件的通用属性{ className?: string, style?: React.CSSProperties, validateMessages?: ValidateMessages, requiredMark?: boolean | optional, colon?: boolean, scrollToFirstError?: boolean | Options, classNames?:FormConfig["classNames"], styles?: FormConfig["styles"], tooltip?: TooltipProps & { icon?: ReactNode } }-requiredMark: 4.8.0; colon: 4.18.0; scrollToFirstError: 5.2.0; classNamestyle: 5.7.0; tooltip: 6.3.0
image设置 Image 组件的通用属性{ className?: string, style?: React.CSSProperties, preview?: { closeIcon?: React.ReactNode, classNames?:ImageConfig["classNames"], styles?: ImageConfig["styles"] }, fallback?: string }-5.7.0, closeIcon: 5.14.0, classNamesstyles: 6.0.0
input设置 Input 组件的通用属性{ autoComplete?: string, className?: string, style?: React.CSSProperties,classNames?:InputConfig["classNames"], styles?: InputConfig["styles"], allowClear?: boolean | { clearIcon?: ReactNode } }-5.7.0, allowClear: 5.15.0
inputNumber设置 Input 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: InputNumberConfig["classNames"], styles?: InputNumberConfig["styles"] }-
otp设置 OTP 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: OTPConfig["classNames"], styles?: OTPConfig["styles"] }-
inputSearch设置 Search 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: InputSearchConfig["classNames"], styles?: InputSearchConfig["styles"] }-
textArea设置 TextArea 组件的通用属性{ autoComplete?: string, className?: string, style?: React.CSSProperties,classNames?:TextAreaConfig["classNames"], styles?: TextAreaConfig["styles"], allowClear?: boolean | { clearIcon?: ReactNode } }-5.15.0
layout设置 Layout 组件的通用属性{ className?: string, style?: React.CSSProperties }-5.7.0
list设置 List 组件的通用属性{ className?: string, style?: React.CSSProperties, item?:{ classNames: ListItemProps["classNames"], styles: ListItemProps["styles"] } }-5.7.0
masonry设置 Masonry 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: MasonryProps["classNames"], styles?: MasonryProps["styles"] }-
menu设置 Menu 组件的通用属性{ className?: string, style?: React.CSSProperties, expandIcon?: ReactNode | props => ReactNode }-5.7.0, expandIcon: 5.15.0
mentions设置 Mentions 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:MentionsConfig["classNames"], styles?: MentionsConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
message设置 Message 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: MessageConfig["classNames"], styles?: MessageConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
modal设置 Modal 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: ModalProps["classNames"], styles?: ModalProps["styles"], closeIcon?: React.ReactNode }-5.7.0, classNamesstyles: 5.10.0, closeIcon: 5.14.0
notification设置 Notification 组件的通用属性{ className?: string, style?: React.CSSProperties, closeIcon?: React.ReactNode, classNames?: NotificationConfig["classNames"], styles?: NotificationConfig["styles"] }-5.7.0, closeIcon: 5.14.0, classNamesstyles: 6.0.0
pagination设置 Pagination 组件的通用属性{ showSizeChanger?: boolean, totalBoundaryShowSizeChanger?: number, className?: string, style?: React.CSSProperties,classNames?:PaginationConfig["classNames"], styles?: PaginationConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
progress设置 Progress 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: ProgressConfig["classNames"], styles?: ProgressConfig["styles"] }-
radio设置 Radio 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: RadioConfig["classNames"], styles?: RadioConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
rate设置 Rate 组件的通用属性{ className?: string, style?: React.CSSProperties }-5.7.0
result设置 Result 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: ResultProps["classNames"], styles?: ResultProps["styles"] }-5.7.0, classNamesstyles: 6.0.0
ribbon设置 Ribbon 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: RibbonProps["classNames"], styles?: RibbonProps["styles"] }-6.0.0
skeleton设置 Skeleton 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: SkeletonProps["classNames"], styles?: SkeletonProps["styles"] }-5.7.0, classNamesstyles: 6.0.0
segmented设置 Segmented 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: SegmentedProps["classNames"], styles?: SegmentedProps["styles"] }-5.7.0, classNamesstyles: 6.0.0
select设置 Select 组件的通用属性{ className?: string, showSearch?: boolean, style?: React.CSSProperties, classNames?: SelectConfig["classNames"], styles?: SelectConfig["styles"] }-5.7.0,classNamesstyles: 6.0.0
slider设置 Slider 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: SliderProps["classNames"], styles?: SliderProps["styles"] }-5.7.0, classNamesstyles: 5.23.0
switch设置 Switch 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: SwitchStyleConfig["classNames"], styles?: SwitchStyleConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
space设置 Space 的通用属性,参考 Space{ size: small | middle | large | number, className?: string, style?: React.CSSProperties, classNames?: SpaceProps["classNames"], styles?: SpaceProps["styles"] }-5.6.0
splitter设置 Splitter 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:Splitter["classNames"], styles?: Splitter["styles"] }-5.21.0
spin设置 Spin 组件的通用属性{ className?: string, style?: React.CSSProperties, indicator?: React.ReactElement, classNames?:SpinConfig["classNames"], styles?: SpinConfig["styles"] }-5.7.0, indicator: 5.20.0, classNamesstyles: 6.0.0
statistic设置 Statistic 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: StatisticProps["classNames"], styles?: StatisticProps["styles"] }-5.7.0, classNamesstyles: 6.0.0
steps设置 Steps 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:StepsConfig["classNames"], styles?: StepsConfig["styles"] }-
table设置 Table 组件的通用属性{ className?: string, style?: React.CSSProperties, expandable?: { expandIcon?: props => React.ReactNode }, rowKey?: TableProps["rowKey"], scroll?: TableProps["scroll"], classNames?: TableProps["classNames"], styles?: TableProps["styles"] }-scroll: 6.2.0
tabs设置 Tabs 组件的通用属性{ className?: string, style?: React.CSSProperties, indicator?: { size?: GetIndicatorSize, align?: start | center | end }, moreIcon?: ReactNode, addIcon?: ReactNode, removeIcon?: ReactNode, classNames?: TabsConfig["classNames"], styles?: TabsConfig["styles"] }-5.7.0, moreIcon and addIcon: 5.14.0, removeIcon: 5.15.0, classNamesstyles: 6.0.0
tag设置 Tag 组件的通用属性{ className?: string, style?: React.CSSProperties, closeIcon?: React.ReactNode, classNames?: TagProps["classNames"], styles?: TagProps["styles"] }-5.7.0, closeIcon: 5.14.0, classNamesstyles: 6.0.0
timeline设置 Timeline 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: TimelineConfig["classNames"], styles?: TimelineConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
timePicker设置 TimePicker 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: TimePickerConfig["classNames"], styles?: TimePickerConfig["styles"], suffixIcon?: React.ReactNode }-5.7.0, suffixIcon: 6.3.0
tour设置 Tour 组件的通用属性{ closeIcon?: React.ReactNode, className?: string, style?: React.CSSProperties, classNames?: TourProps["classNames"], styles?: TourProps["styles"] }-5.14.0, classNamesstylesclassNamestyle: 6.0.0
tooltip设置 Tooltip 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:Tooltip["classNames"], styles?: Tooltip["styles"], arrow: boolean | { pointAtCenter: boolean }, unique?: boolean, trigger?: Tooltip["trigger"]}-trigger: 6.1.0
popover设置 Popover 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:Popover["classNames"], styles?: Popover["styles"], arrow: boolean | { pointAtCenter: boolean }, trigger?: Popover["trigger"]}-5.23.0, arrow: 6.0.0, trigger: 6.1.0
popconfirm设置 Popconfirm 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:Popconfirm["classNames"], styles?: Popconfirm["styles"], arrow: boolean | { pointAtCenter: boolean }, trigger?: Popconfirm["trigger"]}-5.23.0, arrow: 6.0.0, trigger: 6.1.0
qrcode设置 QRCode 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:QRCode["classNames"], styles?: QRCode["styles"] }-
transfer设置 Transfer 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: TransferConfig["classNames"], styles?: TransferConfig["styles"], selectionsIcon?: React.ReactNode }-
tree设置 Tree 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: TreeConfig["classNames"], styles?: TreeConfig["styles"] }-5.7.0, classNamesstyles: 6.0.0
treeSelect设置 TreeSelect 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?: TreeSelectConfig["classNames"], styles?: TreeSelectConfig["styles"], switcherIcon?: TreeSelect["switcherIcon"]}-
typography设置 Typography 组件的通用属性{ className?: string, style?: React.CSSProperties }-5.7.0
upload设置 Upload 组件的通用属性{ className?: string, style?: React.CSSProperties, classNames?:UploadConfig["classNames"], styles?: UploadConfig["styles"], customRequest?: Upload["customRequest"] }-5.7.0, customRequest: 5.27.0, classNamesstyles: 6.0.0
wave设置水波纹特效{ disabled?: boolean, showEffect?: (node: HTMLElement, info: { className, token, component }) => void }-5.8.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' })