ConfigProvider全局化配置

为组件提供统一的全局化配置。

使用

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

  1. import { ConfigProvider } from 'antd';
  2. // ...
  3. return (
  4. <ConfigProvider {...yourConfig}>
  5. <App />
  6. </ConfigProvider>
  7. );

Content Security Policy

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

  1. <ConfigProvider csp={{ nonce: 'YourNonceCode' }}>
  2. <Button>My Button</Button>
  3. </ConfigProvider>

代码演示

ConfigProvider全局化配置 - 图1

国际化

此处列出 Ant Design 中需要国际化支持的组件,你可以在演示里切换语言。

  1. import {
  2. ConfigProvider,
  3. Pagination,
  4. DatePicker,
  5. TimePicker,
  6. Calendar,
  7. Popconfirm,
  8. Table,
  9. Modal,
  10. Button,
  11. Select,
  12. Transfer,
  13. Radio,
  14. } from 'antd';
  15. import enUS from 'antd/es/locale/en_US';
  16. import zhCN from 'antd/es/locale/zh_CN';
  17. import moment from 'moment';
  18. import 'moment/locale/zh-cn';
  19. moment.locale('en');
  20. const { Option } = Select;
  21. const { RangePicker } = DatePicker;
  22. const columns = [
  23. {
  24. title: 'Name',
  25. dataIndex: 'name',
  26. filters: [
  27. {
  28. text: 'filter1',
  29. value: 'filter1',
  30. },
  31. ],
  32. },
  33. {
  34. title: 'Age',
  35. dataIndex: 'age',
  36. },
  37. ];
  38. class Page extends React.Component {
  39. state = {
  40. visible: false,
  41. };
  42. showModal = () => {
  43. this.setState({ visible: true });
  44. };
  45. hideModal = () => {
  46. this.setState({ visible: false });
  47. };
  48. render() {
  49. const info = () => {
  50. Modal.info({
  51. title: 'some info',
  52. content: 'some info',
  53. });
  54. };
  55. const confirm = () => {
  56. Modal.confirm({
  57. title: 'some info',
  58. content: 'some info',
  59. });
  60. };
  61. return (
  62. <div className="locale-components">
  63. <div className="example">
  64. <Pagination defaultCurrent={1} total={50} showSizeChanger />
  65. </div>
  66. <div className="example">
  67. <Select showSearch style={{ width: 200 }}>
  68. <Option value="jack">jack</Option>
  69. <Option value="lucy">lucy</Option>
  70. </Select>
  71. <DatePicker />
  72. <TimePicker />
  73. <RangePicker style={{ width: 200 }} />
  74. </div>
  75. <div className="example">
  76. <Button type="primary" onClick={this.showModal}>
  77. Show Modal
  78. </Button>
  79. <Button onClick={info}>Show info</Button>
  80. <Button onClick={confirm}>Show confirm</Button>
  81. <Popconfirm title="Question?">
  82. <a href="#">Click to confirm</a>
  83. </Popconfirm>
  84. </div>
  85. <div className="example">
  86. <Transfer dataSource={[]} showSearch targetKeys={[]} render={item => item.title} />
  87. </div>
  88. <div className="site-config-provider-calendar-wrapper">
  89. <Calendar fullscreen={false} value={moment()} />
  90. </div>
  91. <div className="example">
  92. <Table dataSource={[]} columns={columns} />
  93. </div>
  94. <Modal title="Locale Modal" visible={this.state.visible} onCancel={this.hideModal}>
  95. <p>Locale Modal</p>
  96. </Modal>
  97. </div>
  98. );
  99. }
  100. }
  101. class App extends React.Component {
  102. constructor() {
  103. super();
  104. this.state = {
  105. locale: enUS,
  106. };
  107. }
  108. changeLocale = e => {
  109. const localeValue = e.target.value;
  110. this.setState({ locale: localeValue });
  111. if (!localeValue) {
  112. moment.locale('en');
  113. } else {
  114. moment.locale('zh-cn');
  115. }
  116. };
  117. render() {
  118. const { locale } = this.state;
  119. return (
  120. <div>
  121. <div className="change-locale">
  122. <span style={{ marginRight: 16 }}>Change locale of components: </span>
  123. <Radio.Group value={locale} onChange={this.changeLocale}>
  124. <Radio.Button key="en" value={enUS}>
  125. English
  126. </Radio.Button>
  127. <Radio.Button key="cn" value={zhCN}>
  128. 中文
  129. </Radio.Button>
  130. </Radio.Group>
  131. </div>
  132. <ConfigProvider locale={locale}>
  133. <Page
  134. key={locale ? locale.locale : 'en' /* Have to refresh for production environment */}
  135. />
  136. </ConfigProvider>
  137. </div>
  138. );
  139. }
  140. }
  141. ReactDOM.render(<App />, mountNode);
  1. .site-config-provider-calendar-wrapper {
  2. width: 319px;
  3. border: 1px solid #d9d9d9;
  4. border-radius: 2px;
  5. }
  6. .locale-components {
  7. border-top: 1px solid #d9d9d9;
  8. padding-top: 16px;
  9. }
  10. .code-box-demo .example {
  11. margin: 16px 0;
  12. }
  13. .code-box-demo .example > * {
  14. margin-right: 8px;
  15. }
  16. .change-locale {
  17. margin-bottom: 16px;
  18. }

ConfigProvider全局化配置 - 图2

方向

这里列出了支持 rtl 方向的组件,您可以在演示中切换方向。

  1. import {
  2. Input,
  3. Col,
  4. Row,
  5. Select,
  6. InputNumber,
  7. ConfigProvider,
  8. Cascader,
  9. Radio,
  10. Switch,
  11. Tree,
  12. TreeSelect,
  13. Modal,
  14. Button,
  15. Pagination,
  16. Steps,
  17. Rate,
  18. Badge,
  19. Divider,
  20. } from 'antd';
  21. import {
  22. SearchOutlined as SearchIcon,
  23. SmileOutlined,
  24. DownloadOutlined,
  25. LeftOutlined,
  26. RightOutlined,
  27. MinusOutlined,
  28. PlusOutlined,
  29. } from '@ant-design/icons';
  30. const InputGroup = Input.Group;
  31. const ButtonGroup = Button.Group;
  32. const { Option } = Select;
  33. const { TreeNode } = Tree;
  34. const { Search } = Input;
  35. const { Step } = Steps;
  36. const cascaderOptions = [
  37. {
  38. value: 'tehran',
  39. label: 'تهران',
  40. children: [
  41. {
  42. value: 'tehran-c',
  43. label: 'تهران',
  44. children: [
  45. {
  46. value: 'saadat-abad',
  47. label: 'سعادت آیاد',
  48. },
  49. ],
  50. },
  51. ],
  52. },
  53. {
  54. value: 'ardabil',
  55. label: 'اردبیل',
  56. children: [
  57. {
  58. value: 'ardabil-c',
  59. label: 'اردبیل',
  60. children: [
  61. {
  62. value: 'primadar',
  63. label: 'پیرمادر',
  64. },
  65. ],
  66. },
  67. ],
  68. },
  69. {
  70. value: 'gilan',
  71. label: 'گیلان',
  72. children: [
  73. {
  74. value: 'rasht',
  75. label: 'رشت',
  76. children: [
  77. {
  78. value: 'district-3',
  79. label: 'منطقه ۳',
  80. },
  81. ],
  82. },
  83. ],
  84. },
  85. ];
  86. class Page extends React.Component {
  87. state = {
  88. currentStep: 0,
  89. modalVisible: false,
  90. badgeCount: 5,
  91. showBadge: true,
  92. };
  93. selectBefore = (
  94. <Select defaultValue="Http://" style={{ width: 90 }}>
  95. <Option value="Http://">Http://</Option>
  96. <Option value="Https://">Https://</Option>
  97. </Select>
  98. );
  99. selectAfter = (
  100. <Select defaultValue=".com" style={{ width: 80 }}>
  101. <Option value=".com">.com</Option>
  102. <Option value=".jp">.jp</Option>
  103. <Option value=".cn">.cn</Option>
  104. <Option value=".org">.org</Option>
  105. </Select>
  106. );
  107. // ==== Cascader ====
  108. cascaderFilter = (inputValue, path) => {
  109. return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  110. };
  111. onCascaderChange = value => {
  112. console.log(value);
  113. };
  114. // ==== End Cascader ====
  115. // ==== Modal ====
  116. showModal = () => {
  117. this.setState({
  118. modalVisible: true,
  119. });
  120. };
  121. handleOk = e => {
  122. console.log(e);
  123. this.setState({
  124. modalVisible: false,
  125. });
  126. };
  127. handleCancel = e => {
  128. console.log(e);
  129. this.setState({
  130. modalVisible: false,
  131. });
  132. };
  133. // ==== End Modal ====
  134. onStepsChange = currentStep => {
  135. console.log('onChange:', currentStep);
  136. this.setState({ currentStep });
  137. };
  138. // ==== Badge ====
  139. increaseBadge = () => {
  140. const badgeCount = this.state.badgeCount + 1;
  141. this.setState({ badgeCount });
  142. };
  143. declineBadge = () => {
  144. let badgeCount = this.state.badgeCount - 1;
  145. if (badgeCount < 0) {
  146. badgeCount = 0;
  147. }
  148. this.setState({ badgeCount });
  149. };
  150. onChangeBadge = showBadge => {
  151. this.setState({ showBadge });
  152. };
  153. // ==== End Badge ====
  154. render() {
  155. const { currentStep } = this.state;
  156. return (
  157. <div className="direction-components">
  158. <Row>
  159. <Col span={24}>
  160. <Divider orientation="left">Cascader example</Divider>
  161. <Cascader
  162. suffixIcon={<SearchIcon />}
  163. options={cascaderOptions}
  164. onChange={this.onCascaderChange}
  165. placeholder="یک مورد انتخاب کنید"
  166. popupPlacement={this.props.popupPlacement}
  167. />
  168. &nbsp;&nbsp;&nbsp;&nbsp; With search:
  169. <Cascader
  170. suffixIcon={<SmileOutlined />}
  171. options={cascaderOptions}
  172. onChange={this.onCascaderChange}
  173. placeholder="Select an item"
  174. popupPlacement={this.props.popupPlacement}
  175. showSearch={this.cascaderFilter}
  176. />
  177. </Col>
  178. </Row>
  179. <br />
  180. <Row>
  181. <Col span={12}>
  182. <Divider orientation="left">Switch example</Divider>
  183. &nbsp;&nbsp;
  184. <Switch defaultChecked />
  185. &nbsp;&nbsp;
  186. <Switch loading defaultChecked />
  187. &nbsp;&nbsp;
  188. <Switch size="small" loading />
  189. </Col>
  190. <Col span={12}>
  191. <Divider orientation="left">Radio Group example</Divider>
  192. <Radio.Group defaultValue="c" buttonStyle="solid">
  193. <Radio.Button value="a">تهران</Radio.Button>
  194. <Radio.Button value="b" disabled>
  195. اصفهان
  196. </Radio.Button>
  197. <Radio.Button value="c">فارس</Radio.Button>
  198. <Radio.Button value="d">خوزستان</Radio.Button>
  199. </Radio.Group>
  200. </Col>
  201. </Row>
  202. <br />
  203. <Row>
  204. <Col span={12}>
  205. <Divider orientation="left">Button example</Divider>
  206. <div className="button-demo">
  207. <Button type="primary" icon={<DownloadOutlined />} />
  208. <Button type="primary" shape="circle" icon={<DownloadOutlined />} />
  209. <Button type="primary" shape="round" icon={<DownloadOutlined />} />
  210. <Button type="primary" shape="round" icon={<DownloadOutlined />}>
  211. Download
  212. </Button>
  213. <Button type="primary" icon={<DownloadOutlined />}>
  214. Download
  215. </Button>
  216. <br />
  217. <Button.Group>
  218. <Button type="primary">
  219. <LeftOutlined />
  220. Backward
  221. </Button>
  222. <Button type="primary">
  223. Forward
  224. <RightOutlined />
  225. </Button>
  226. </Button.Group>
  227. <Button type="primary" loading>
  228. Loading
  229. </Button>
  230. <Button type="primary" size="small" loading>
  231. Loading
  232. </Button>
  233. </div>
  234. </Col>
  235. <Col span={12}>
  236. <Divider orientation="left">Tree example</Divider>
  237. <Tree
  238. showLine
  239. checkable
  240. defaultExpandedKeys={['0-0-0', '0-0-1']}
  241. defaultSelectedKeys={['0-0-0', '0-0-1']}
  242. defaultCheckedKeys={['0-0-0', '0-0-1']}
  243. >
  244. <TreeNode title="parent 1" key="0-0">
  245. <TreeNode title="parent 1-0" key="0-0-0" disabled>
  246. <TreeNode title="leaf" key="0-0-0-0" disableCheckbox />
  247. <TreeNode title="leaf" key="0-0-0-1" />
  248. </TreeNode>
  249. <TreeNode title="parent 1-1" key="0-0-1">
  250. <TreeNode title={<span style={{ color: '#1890ff' }}>sss</span>} key="0-0-1-0" />
  251. </TreeNode>
  252. </TreeNode>
  253. </Tree>
  254. </Col>
  255. </Row>
  256. <br />
  257. <Row>
  258. <Col span={24}>
  259. <Divider orientation="left">Input (Input Group) example</Divider>
  260. <InputGroup size="large">
  261. <Row gutter={8}>
  262. <Col span={5}>
  263. <Input defaultValue="0571" />
  264. </Col>
  265. <Col span={8}>
  266. <Input defaultValue="26888888" />
  267. </Col>
  268. </Row>
  269. </InputGroup>
  270. <br />
  271. <InputGroup compact>
  272. <Input style={{ width: '20%' }} defaultValue="0571" />
  273. <Input style={{ width: '30%' }} defaultValue="26888888" />
  274. </InputGroup>
  275. <br />
  276. <InputGroup compact>
  277. <Select defaultValue="Option1">
  278. <Option value="Option1">Option1</Option>
  279. <Option value="Option2">Option2</Option>
  280. </Select>
  281. <Input style={{ width: '50%' }} defaultValue="input content" />
  282. <InputNumber />
  283. </InputGroup>
  284. <br />
  285. <Search placeholder="input search text" enterButton="Search" size="large" />
  286. <br />
  287. <br />
  288. <div style={{ marginBottom: 16 }}>
  289. <Input
  290. addonBefore={this.selectBefore}
  291. addonAfter={this.selectAfter}
  292. defaultValue="mysite"
  293. />
  294. </div>
  295. <br />
  296. <Row>
  297. <Col span={12}>
  298. <Divider orientation="left">Select example</Divider>
  299. <Select mode="multiple" defaultValue="مورچه" style={{ width: 120 }}>
  300. <Option value="jack">Jack</Option>
  301. <Option value="مورچه">مورچه</Option>
  302. <Option value="disabled" disabled>
  303. Disabled
  304. </Option>
  305. <Option value="Yiminghe">yiminghe</Option>
  306. </Select>
  307. <Select defaultValue="مورچه" style={{ width: 120 }} disabled>
  308. <Option value="مورچه">مورچه</Option>
  309. </Select>
  310. <Select defaultValue="مورچه" style={{ width: 120 }} loading>
  311. <Option value="مورچه">مورچه</Option>
  312. </Select>
  313. <Select
  314. showSearch
  315. style={{ width: 200 }}
  316. placeholder="Select a person"
  317. optionFilterProp="children"
  318. filterOption={(input, option) =>
  319. option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
  320. }
  321. >
  322. <Option value="jack">Jack</Option>
  323. <Option value="سعید">سعید</Option>
  324. <Option value="tom">Tom</Option>
  325. </Select>
  326. </Col>
  327. <Col span={12}>
  328. <Divider orientation="left">TreeSelect example</Divider>
  329. <div>
  330. <TreeSelect
  331. showSearch
  332. style={{ width: '100%' }}
  333. dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
  334. placeholder="Please select"
  335. allowClear
  336. treeDefaultExpandAll
  337. >
  338. <TreeNode value="parent 1" title="parent 1" key="0-1">
  339. <TreeNode value="parent 1-0" title="parent 1-0" key="0-1-1">
  340. <TreeNode value="leaf1" title="my leaf" key="random" />
  341. <TreeNode value="leaf2" title="your leaf" key="random1" />
  342. </TreeNode>
  343. <TreeNode value="parent 1-1" title="parent 1-1" key="random2">
  344. <TreeNode
  345. value="sss"
  346. title={<b style={{ color: '#08c' }}>sss</b>}
  347. key="random3"
  348. />
  349. </TreeNode>
  350. </TreeNode>
  351. </TreeSelect>
  352. </div>
  353. </Col>
  354. </Row>
  355. <br />
  356. <Row>
  357. <Col span={24}>
  358. <Divider orientation="left">Modal example</Divider>
  359. <div>
  360. <Button type="primary" onClick={this.showModal}>
  361. Open Modal
  362. </Button>
  363. <Modal
  364. title="پنچره ساده"
  365. visible={this.state.modalVisible}
  366. onOk={this.handleOk}
  367. onCancel={this.handleCancel}
  368. >
  369. <p>نگاشته‌های خود را اینجا قراردهید</p>
  370. <p>نگاشته‌های خود را اینجا قراردهید</p>
  371. <p>نگاشته‌های خود را اینجا قراردهید</p>
  372. </Modal>
  373. </div>
  374. </Col>
  375. </Row>
  376. <br />
  377. <Row>
  378. <Col span={24}>
  379. <Divider orientation="left">Steps example</Divider>
  380. <div>
  381. <Steps progressDot current={currentStep}>
  382. <Step title="Finished" description="This is a description." />
  383. <Step title="In Progress" description="This is a description." />
  384. <Step title="Waiting" description="This is a description." />
  385. </Steps>
  386. <br />
  387. <Steps current={currentStep} onChange={this.onStepsChange}>
  388. <Step title="Step 1" description="This is a description." />
  389. <Step title="Step 2" description="This is a description." />
  390. <Step title="Step 3" description="This is a description." />
  391. </Steps>
  392. </div>
  393. </Col>
  394. </Row>
  395. <br />
  396. <Row>
  397. <Col span={12}>
  398. <Divider orientation="left">Rate example</Divider>
  399. <div>
  400. <Rate defaultValue={2.5} />
  401. <br />
  402. <strong>* Note:</strong> Half star not implemented in RTL direction, it will be
  403. supported after <a href="https://github.com/react-component/rate">rc-rate</a>{' '}
  404. implement rtl support.
  405. </div>
  406. </Col>
  407. <Col span={12}>
  408. <Divider orientation="left">Badge example</Divider>
  409. <div>
  410. <div>
  411. <Badge count={this.state.badgeCount}>
  412. <a href="#" className="head-example" />
  413. </Badge>
  414. <ButtonGroup>
  415. <Button onClick={this.declineBadge}>
  416. <MinusOutlined />
  417. </Button>
  418. <Button onClick={this.increaseBadge}>
  419. <PlusOutlined />
  420. </Button>
  421. </ButtonGroup>
  422. </div>
  423. <div style={{ marginTop: 10 }}>
  424. <Badge dot={this.state.showBadge}>
  425. <a href="#" className="head-example" />
  426. </Badge>
  427. <Switch onChange={this.onChangeBadge} checked={this.state.showBadge} />
  428. </div>
  429. </div>
  430. </Col>
  431. </Row>
  432. </Col>
  433. </Row>
  434. <br />
  435. <br />
  436. <Row>
  437. <Col span={24}>
  438. <Divider orientation="left">Pagination example</Divider>
  439. <Pagination showSizeChanger defaultCurrent={3} total={500} />
  440. </Col>
  441. </Row>
  442. <br />
  443. <Row>
  444. <Col span={24}>
  445. <Divider orientation="left">Grid System example</Divider>
  446. <div className="grid-demo">
  447. <div className="code-box-demo">
  448. <p>
  449. <strong>* Note:</strong> Every calculation in RTL grid system is from right side
  450. (offset, push, etc.)
  451. </p>
  452. <Row>
  453. <Col span={8}>col-8</Col>
  454. <Col span={8} offset={8}>
  455. col-8
  456. </Col>
  457. </Row>
  458. <Row>
  459. <Col span={6} offset={6}>
  460. col-6 col-offset-6
  461. </Col>
  462. <Col span={6} offset={6}>
  463. col-6 col-offset-6
  464. </Col>
  465. </Row>
  466. <Row>
  467. <Col span={12} offset={6}>
  468. col-12 col-offset-6
  469. </Col>
  470. </Row>
  471. <Row>
  472. <Col span={18} push={6}>
  473. col-18 col-push-6
  474. </Col>
  475. <Col span={6} pull={18}>
  476. col-6 col-pull-18
  477. </Col>
  478. </Row>
  479. </div>
  480. </div>
  481. </Col>
  482. </Row>
  483. </div>
  484. );
  485. }
  486. }
  487. class App extends React.Component {
  488. state = {
  489. direction: 'ltr',
  490. popupPlacement: 'bottomLeft',
  491. };
  492. changeDirection = e => {
  493. const directionValue = e.target.value;
  494. this.setState({ direction: directionValue });
  495. if (directionValue === 'rtl') {
  496. this.setState({ popupPlacement: 'bottomRight' });
  497. } else {
  498. this.setState({ popupPlacement: 'bottomLeft' });
  499. }
  500. };
  501. render() {
  502. const { direction, popupPlacement } = this.state;
  503. return (
  504. <>
  505. <div style={{ marginBottom: 16 }}>
  506. <span style={{ marginRight: 16 }}>Change direction of components: </span>
  507. <Radio.Group defaultValue="ltr" onChange={this.changeDirection}>
  508. <Radio.Button key="ltr" value="ltr">
  509. LTR
  510. </Radio.Button>
  511. <Radio.Button key="rtl" value="rtl">
  512. RTL
  513. </Radio.Button>
  514. </Radio.Group>
  515. </div>
  516. <ConfigProvider direction={direction}>
  517. <Page className={direction} popupPlacement={popupPlacement} />
  518. </ConfigProvider>
  519. </>
  520. );
  521. }
  522. }
  523. ReactDOM.render(<App />, mountNode);
  1. .button-demo .ant-btn,
  2. .button-demo .ant-btn-group {
  3. margin-right: 8px;
  4. margin-bottom: 12px;
  5. }
  6. .button-demo .ant-btn-group > .ant-btn,
  7. .button-demo .ant-btn-group > span > .ant-btn {
  8. margin-right: 0;
  9. margin-left: 0;
  10. }
  11. .head-example {
  12. display: inline-block;
  13. width: 42px;
  14. height: 42px;
  15. vertical-align: middle;
  16. background: #eee;
  17. border-radius: 4px;
  18. }
  19. .ant-badge:not(.ant-badge-not-a-wrapper) {
  20. margin-right: 20px;
  21. }
  22. .ant-badge-rtl:not(.ant-badge-not-a-wrapper) {
  23. margin-right: 0;
  24. margin-left: 20px;
  25. }

ConfigProvider全局化配置 - 图3

组件尺寸

修改默认组件尺寸。

  1. import React, { useState } from 'react';
  2. import {
  3. ConfigProvider,
  4. Radio,
  5. Input,
  6. Button,
  7. Select,
  8. DatePicker,
  9. Divider,
  10. Table,
  11. Card,
  12. } from 'antd';
  13. const FormSizeDemo = () => {
  14. const [componentSize, setComponentSize] = useState('small');
  15. return (
  16. <div>
  17. <Radio.Group
  18. value={componentSize}
  19. onChange={e => {
  20. setComponentSize(e.target.value);
  21. }}
  22. >
  23. <Radio.Button value="small">Small</Radio.Button>
  24. <Radio.Button value="middle">Middle</Radio.Button>
  25. <Radio.Button value="large">Large</Radio.Button>
  26. </Radio.Group>
  27. <Divider />
  28. <ConfigProvider componentSize={componentSize}>
  29. <div className="example">
  30. <Input />
  31. </div>
  32. <div className="example">
  33. <Input.Search allowClear />
  34. </div>
  35. <div className="example">
  36. <Select defaultValue="demo" options={[{ value: 'demo' }]} />
  37. </div>
  38. <div className="example">
  39. <DatePicker />
  40. </div>
  41. <div className="example">
  42. <DatePicker.RangePicker />
  43. </div>
  44. <div className="example">
  45. <Button>Button</Button>
  46. </div>
  47. <div className="example">
  48. <Card title="Card">
  49. <Table
  50. columns={[
  51. { title: 'Name', dataIndex: 'name' },
  52. { title: 'Age', dataIndex: 'age' },
  53. ]}
  54. dataSource={[
  55. {
  56. key: '1',
  57. name: 'John Brown',
  58. age: 32,
  59. },
  60. {
  61. key: '2',
  62. name: 'Jim Green',
  63. age: 42,
  64. },
  65. {
  66. key: '3',
  67. name: 'Joe Black',
  68. age: 32,
  69. },
  70. ]}
  71. />
  72. </Card>
  73. </div>
  74. </ConfigProvider>
  75. </div>
  76. );
  77. };
  78. ReactDOM.render(<FormSizeDemo />, mountNode);

API

参数说明类型默认值版本
autoInsertSpaceInButton设置为 false 时,移除按钮中 2 个汉字之间的空格booleantrue
componentSize设置 antd 组件大小small | middle | large-
csp设置 Content Security Policy 配置{ nonce: string }-
form设置 Form 组件的通用属性{ validateMessages?: ValidateMessages }-
input设置 Input 组件的通用属性{ autoComplete?: string }-4.2.0
renderEmpty自定义组件空状态。参考 空状态Function(componentName: string): ReactNode-
getPopupContainer弹出框(Select, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。Function(triggerNode)() => document.body
getTargetContainer配置 Affix、Anchor 滚动监听容器。() => HTMLElement() => window4.2.0
locale语言包配置,语言包可到 antd/es/locale 目录下寻找object-
prefixCls设置统一样式前缀。注意:这将不会应用由 antd 提供的默认样式stringant
pageHeader统一设置 PageHeader 的 ghost,参考 PageHeader{ ghost: boolean }true
direction设置文本展示方向。 示例ltr | rtlltr
space设置 Space 的 size,参考 Space{ size: small | middle | large | number }-4.1.0
virtual设置 false 时关闭虚拟滚动boolean-4.3.0
dropdownMatchSelectWidth下拉菜单和选择器同宽。默认将设置 min-widthfalse 时会关闭虚拟滚动boolean | number-4.3.0

FAQ

如何增加一个新的语言包?

参考《增加语言包》

为什么我使用了 ConfigProvider locale,时间类组件的国际化还有问题?

请检查是否正确设置了 moment 语言包,或者是否有两个版本的 moment 共存。

  1. import 'moment/locale/zh-cn';
  2. moment.locale('zh-cn');

配置 getPopupContainer 导致 Modal 报错?

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

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

  1. <ConfigProvider
  2. - getPopupContainer={triggerNode => triggerNode.parentNode}
  3. + getPopupContainer={node => {
  4. + if (node) {
  5. + return node.parentNode;
  6. + }
  7. + return document.body;
  8. + }}
  9. >
  10. <App />
  11. </ConfigProvider>