# Ant Design 组件文档 本文件包含所有组件文档的聚合内容。 > 总计 73 个组件 ## _util-cn Source: https://ant.design/components/_util-cn.md --- category: Components title: Util subtitle: 工具类 description: 辅助开发,提供一些常用的工具方法。 showImport: false cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*rRDlT7ST8DUAAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*rRDlT7ST8DUAAAAAAAAAAAAADrJ8AQ/original demo: cols: 2 group: title: 其他 order: 99 --- 自 `5.13.0` 版本开始提供这些方法。 ## GetRef 获取组件的 `ref` 属性定义,这对于未直接暴露或者子组件的 `ref` 属性定义非常有用。 ```tsx import { Select } from 'antd'; import type { GetRef } from 'antd'; type SelectRefType = GetRef; // BaseSelectRef ``` ## GetProps 获取组件的 `props` 属性定义: ```tsx import { Checkbox } from 'antd'; import type { GetProps } from 'antd'; type CheckboxGroupType = GetProps; ``` 同时也支持获取 Context 的属性定义: ```tsx import type { GetProps } from 'antd'; interface InternalContextProps { name: string; } const Context = React.createContext({ name: 'Ant Design' }); type ContextType = GetProps; // InternalContextProps ``` ## GetProp 获取组件的单个 `props` 或者 `context` 属性定义。它已经将 `NonNullable` 进行了封装,所以不用再考虑为空的情况: ```tsx import { Select } from 'antd'; import type { GetProp, SelectProps } from 'antd'; // 以下两种都可以生效 type SelectOptionType1 = GetProp[number]; type SelectOptionType2 = GetProp[number]; type ContextOptionType = GetProp; ``` --- ## affix-cn Source: https://ant.design/components/affix-cn.md --- category: Components title: Affix subtitle: 固钉 description: 将页面元素钉在可视范围。 cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*YSm4RI3iOJ8AAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*03dxS64LxeQAAAAAAAAAAAAADrJ8AQ/original demo: cols: 2 group: title: 其他 order: 7 --- ## 何时使用 {#when-to-use} 当内容区域比较长,需要滚动页面时,这部分内容对应的操作或者导航需要在滚动范围内始终展现。常用于侧边菜单和按钮组合。 页面可视范围过小时,慎用此功能以免出现遮挡页面内容的情况。 > 开发者注意事项: > > 自 `5.10.0` 起,由于 Affix 组件由 class 重构为 FC,之前获取 `ref` 并调用内部实例方法的写法都会失效。 ## 代码演示 {#examples} ### 基本 最简单的用法。 ```tsx import React from 'react'; import { Affix, Button } from 'antd'; const App: React.FC = () => { const [top, setTop] = React.useState(100); const [bottom, setBottom] = React.useState(100); return ( <>
); }; export default App; ``` ### 固定状态改变的回调 可以获得是否固定的状态。 ```tsx import React from 'react'; import { Affix, Button } from 'antd'; const App: React.FC = () => ( console.log(affixed)}> ); export default App; ``` ### 滚动容器 用 `target` 设置 `Affix` 需要监听其滚动事件的元素,默认为 `window`。 ```tsx import React from 'react'; import { Affix, Button } from 'antd'; const containerStyle: React.CSSProperties = { width: '100%', height: 100, overflow: 'auto', boxShadow: '0 0 0 1px #1677ff', scrollbarWidth: 'thin', scrollbarGutter: 'stable', }; const style: React.CSSProperties = { width: '100%', height: 1000, }; const App: React.FC = () => { const [container, setContainer] = React.useState(null); return (
container}>
); }; export default App; ``` ## API 通用属性参考:[通用属性](/docs/react/common-props) | 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | offsetBottom | 距离窗口底部达到指定偏移量后触发 | number | - | | offsetTop | 距离窗口顶部达到指定偏移量后触发 | number | 0 | | target | 设置 `Affix` 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 | () => HTMLElement | () => window | | onChange | 固定状态改变时触发的回调函数 | (affixed?: boolean) => void | - | **注意:**`Affix` 内的元素不要使用绝对定位,如需要绝对定位的效果,可以直接设置 `Affix` 为绝对定位: ```jsx ... ``` ## FAQ ### Affix 使用 `target` 绑定容器时,元素会跑到容器外。 {#faq-target-container} 从性能角度考虑,我们只监听容器滚动事件。如果希望任意滚动,你可以在窗体添加滚动监听: 相关 issue:[#3938](https://github.com/ant-design/ant-design/issues/3938) [#5642](https://github.com/ant-design/ant-design/issues/5642) [#16120](https://github.com/ant-design/ant-design/issues/16120) ### Affix 在水平滚动容器中使用时, 元素 `left` 位置不正确。 {#faq-horizontal-scroll} Affix 一般只适用于单向滚动的区域,只支持在垂直滚动容器中使用。如果希望在水平容器中使用,你可以考虑使用 原生 `position: sticky` 实现。 相关 issue: [#29108](https://github.com/ant-design/ant-design/issues/29108) --- ## alert-cn Source: https://ant.design/components/alert-cn.md --- category: Components title: Alert subtitle: 警告提示 description: 警告提示,展现需要关注的信息。 cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*QsvtS41m45UAAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*gOTISoMFZV0AAAAAAAAAAAAADrJ8AQ/original demo: cols: 2 group: title: 反馈 order: 6 --- ## 何时使用 {#when-to-use} - 当某个页面需要向用户显示警告的信息时。 - 非浮层的静态展现形式,始终展现,不会自动消失,用户可以点击关闭。 ## 代码演示 {#examples} ### 基本 最简单的用法,适用于简短的警告提示。 ```tsx import React from 'react'; import { Alert } from 'antd'; const App: React.FC = () => ; export default App; ``` ### 四种样式 共有四种样式 `success`、`info`、`warning`、`error`。 ```tsx import React from 'react'; import { Alert } from 'antd'; const App: React.FC = () => ( <>


); export default App; ``` ### 可关闭的警告提示 显示关闭按钮,点击可关闭警告提示。 ```tsx import React from 'react'; import { Alert } from 'antd'; const onClose: React.MouseEventHandler = (e) => { console.log(e, 'I was closed.'); }; const App: React.FC = () => ( <>


); export default App; ``` ### 含有辅助性文字介绍 含有辅助性文字介绍的警告提示。 ```tsx import React from 'react'; import { Alert } from 'antd'; const App: React.FC = () => ( <>


); export default App; ``` ### 图标 可口的图标让信息类型更加醒目。 ```tsx import React from 'react'; import { Alert } from 'antd'; const App: React.FC = () => ( <>






); export default App; ``` ### 顶部公告 页面顶部通告形式,默认有图标且 `type` 为 'warning'。 ```tsx import React from 'react'; import { Alert } from 'antd'; const App: React.FC = () => ( <>


); export default App; ``` ### 轮播的公告 配合 [react-text-loop-next](https://npmjs.com/package/react-text-loop-next) 或 [react-fast-marquee](https://npmjs.com/package/react-fast-marquee) 实现消息轮播通知栏。 ```tsx import React from 'react'; import { Alert } from 'antd'; import Marquee from 'react-fast-marquee'; const App: React.FC = () => ( I can be a React component, multiple React components, or just some text. } /> ); export default App; ``` ### 平滑地卸载 平滑、自然的卸载提示。 ```tsx import React, { useState } from 'react'; import { Alert, Switch } from 'antd'; const App: React.FC = () => { const [visible, setVisible] = useState(true); const handleClose = () => { setVisible(false); }; return ( <> {visible && ( )}

click the close button to see the effect

); }; export default App; ``` ### React 错误处理 友好的 [React 错误处理](https://zh-hans.react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) 包裹组件。 ```tsx import React, { useState } from 'react'; import { Alert, Button } from 'antd'; const { ErrorBoundary } = Alert; const ThrowError: React.FC = () => { const [error, setError] = useState(); const onClick = () => { setError(new Error('An Uncaught Error')); }; if (error) { throw error; } return ( ); }; const App: React.FC = () => ( ); export default App; ``` ### 操作 可以在右上角自定义操作项。 ```tsx import React from 'react'; import { Alert, Button, Space } from 'antd'; const App: React.FC = () => ( <> UNDO } closable />
Detail } />
} closable />
} closable /> ); export default App; ``` ### 自定义语义结构的样式和类 通过 `classNames` 和 `styles` 传入对象/函数可以自定义 Alert 的[语义化结构](#semantic-dom)样式。 ```tsx import React from 'react'; import { Alert, Button, Flex } from 'antd'; import type { AlertProps, AlertSemanticType } from 'antd'; import { createStaticStyles } from 'antd-style'; const classNames = createStaticStyles(({ css }) => ({ root: css` border: 2px dashed #ccc; border-radius: 8px; padding: 12px; `, })); const styleFn: AlertProps['styles'] = ({ props: { type } }): AlertSemanticType['styles'] => { if (type === 'success') { return { root: { backgroundColor: 'rgba(82, 196, 26, 0.1)', borderColor: '#b7eb8f', }, icon: { color: '#52c41a', }, }; } if (type === 'warning') { return { root: { backgroundColor: 'rgba(250, 173, 20, 0.1)', borderColor: '#ffe58f', }, icon: { color: '#faad14', }, }; } return {}; }; const App: React.FC = () => { const alertSharedProps: AlertProps = { showIcon: true, classNames: { root: classNames.root, }, }; return ( Action} /> ); }; export default App; ``` ## API 通用属性参考:[通用属性](/docs/react/common-props) | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | | action | 自定义操作项 | ReactNode | - | 4.9.0 | | ~~afterClose~~ | 关闭动画结束后触发的回调函数,请使用 `closable.afterClose` 替换 | () => void | - | | | banner | 是否用作顶部公告 | boolean | false | | | classNames | 自定义组件内部各语义化结构的类名。支持对象或函数 | Record<[SemanticDOM](#semantic-dom), string> \| (info: { props }) => Record<[SemanticDOM](#semantic-dom), string> | - | | | closable | 可关闭配置,>=5.15.0: 支持 `aria-*` | boolean \| [ClosableType](#closabletype) & React.AriaAttributes | `false` | | | description | 警告提示的辅助性文字介绍 | ReactNode | - | | | icon | 自定义图标,`showIcon` 为 true 时有效 | ReactNode | - | | | ~~message~~ | 警告提示内容,请使用 `title` 替换 | ReactNode | - | | | title | 警告提示内容 | ReactNode | - | | | showIcon | 是否显示辅助图标 | boolean | false,`banner` 模式下默认值为 true | | | styles | 自定义组件内部各语义化结构的内联样式。支持对象或函数 | Record<[SemanticDOM](#semantic-dom), CSSProperties> \| (info: { props }) => Record<[SemanticDOM](#semantic-dom), CSSProperties> | - | | | type | 指定警告提示的样式,有四种选择 `success`、`info`、`warning`、`error` | string | `info`,`banner` 模式下默认值为 `warning` | | | ~~onClose~~ | 关闭时触发的回调函数,请使用 `closable.onClose` 替换 | (e: MouseEvent) => void | - | | ### ClosableType | 参数 | 说明 | 类型 | 默认值 | 版本 | | ---------- | ---------------------------- | ----------------------- | ------ | ---- | | afterClose | 关闭动画结束后触发的回调函数 | function | - | - | | closeIcon | 自定义关闭图标 | ReactNode | - | - | | onClose | 关闭时触发的回调函数 | (e: MouseEvent) => void | - | - | ### Alert.ErrorBoundary | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | | description | 自定义错误内容,如果未指定会展示报错堆栈 | ReactNode | {{ error stack }} | | | ~~message~~ | 自定义错误标题,如果未指定会展示原生报错信息,请使用 `title` 替换 | ReactNode | {{ error }} | | | title | 自定义错误标题,如果未指定会展示原生报错信息 | ReactNode | {{ error }} | | ## Semantic DOM https://ant.design/components/alert-cn/semantic.md ## Design Token ## 组件 Token (Alert) | Token 名称 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | | defaultPadding | 默认内间距 | Padding \| undefined | 8px 12px | | withDescriptionIconSize | 带有描述时的图标尺寸 | number | 24 | | withDescriptionPadding | 带有描述的内间距 | Padding \| undefined | 20px 24px | ## 全局 Token | Token 名称 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | | borderRadiusLG | LG号圆角,用于组件中的一些大圆角,如 Card、Modal 等一些组件样式。 | number | | | colorError | 用于表示操作失败的 Token 序列,如失败按钮、错误状态提示(Result)组件等。 | string | | | colorErrorBg | 错误色的浅色背景颜色 | string | | | colorErrorBorder | 错误色的描边色 | string | | | colorIcon | 控制弱操作图标的颜色,例如 allowClear 或 Alert 关闭按钮。 * | string | | | colorIconHover | 控制弱操作图标在悬浮状态下的颜色,例如 allowClear 或 Alert 关闭按钮。 | string | | | colorInfo | 用于表示操作信息的 Token 序列,如 Alert 、Tag、 Progress 等组件都有用到该组梯度变量。 | string | | | colorInfoBg | 信息色的浅色背景颜色。 | string | | | colorInfoBorder | 信息色的描边色。 | string | | | colorSuccess | 用于表示操作成功的 Token 序列,如 Result、Progress 等组件会使用该组梯度变量。 | string | | | colorSuccessBg | 成功色的浅色背景颜色,用于 Tag 和 Alert 的成功态背景色 | string | | | colorSuccessBorder | 成功色的描边色,用于 Tag 和 Alert 的成功态描边色 | string | | | colorText | 最深的文本色。为了符合W3C标准,默认的文本颜色使用了该色,同时这个颜色也是最深的中性色。 | string | | | colorTextHeading | 控制标题字体颜色。 | string | | | colorWarning | 用于表示操作警告的 Token 序列,如 Notification、 Alert等警告类组件或 Input 输入类等组件会使用该组梯度变量。 | string | | | colorWarningBg | 警戒色的浅色背景颜色 | string | | | colorWarningBorder | 警戒色的描边色 | string | | | fontFamily | Ant Design 的字体家族中优先使用系统默认的界面字体,同时提供了一套利于屏显的备用字体库,来维护在不同平台以及浏览器的显示下,字体始终保持良好的易读性和可读性,体现了友好、稳定和专业的特性。 | string | | | fontSize | 设计系统中使用最广泛的字体大小,文本梯度也将基于该字号进行派生。 | number | | | fontSizeIcon | 控制选择器、级联选择器等中的操作图标字体大小。正常情况下与 fontSizeSM 相同。 | number | | | fontSizeLG | 大号字体大小 | number | | | lineHeight | 文本行高 | number | | | lineType | 用于控制组件边框、分割线等的样式,默认是实线 | string | | | lineWidth | 用于控制组件边框、分割线等的宽度 | number | | | marginSM | 控制元素外边距,中小尺寸。 | number | | | marginXS | 控制元素外边距,小尺寸。 | number | | | motionDurationMid | 动效播放速度,中速。用于中型元素动画交互 | string | | | motionDurationSlow | 动效播放速度,慢速。用于大型元素如面板动画交互 | string | | | motionEaseInOutCirc | 预设动效曲率 | string | | --- ## anchor-cn Source: https://ant.design/components/anchor-cn.md --- category: Components title: Anchor subtitle: 锚点 description: 用于跳转到页面指定位置。 cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*ufP1TLS5VvIAAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*_9_eTrgvHNQAAAAAAAAAAAAADrJ8AQ/original demo: group: title: 导航 order: 3 --- ## 何时使用 {#when-to-use} 需要展现当前页面上可供跳转的锚点链接,以及快速在锚点之间跳转。 > 开发者注意事项: > > 自 `4.24.0` 起,由于组件从 class 重构成 FC,之前一些获取 `ref` 并调用内部实例方法的写法都会失效 ## 代码演示 {#examples} ### 基本 最简单的用法。 ```tsx import React from 'react'; import { Anchor, Col, Row } from 'antd'; const App: React.FC = () => (
); export default App; ``` ### 横向 Anchor 横向 Anchor。 ```tsx import React from 'react'; import { Anchor } from 'antd'; const App: React.FC = () => ( <>
); export default App; ``` ### 静态位置 不浮动,状态不随页面滚动变化。 ```tsx import React from 'react'; import { Anchor } from 'antd'; const App: React.FC = () => ( ); export default App; ``` ### 自定义 onClick 事件 点击锚点不记录历史。 ```tsx import React from 'react'; import { Anchor } from 'antd'; const handleClick = ( e: React.MouseEvent, link: { title: React.ReactNode; href: string; }, ) => { e.preventDefault(); console.log(link); }; const App: React.FC = () => ( ); export default App; ``` ### 自定义锚点高亮 自定义锚点高亮。 ```tsx import React from 'react'; import { Anchor } from 'antd'; const getCurrentAnchor = () => '#anchor-demo-static'; const App: React.FC = () => ( ); export default App; ``` ### 设置锚点滚动偏移量 锚点目标滚动到屏幕正中间。 ```tsx import React, { useEffect, useState } from 'react'; import { Anchor, Col, Row } from 'antd'; const style: React.CSSProperties = { height: '30vh', backgroundColor: 'rgba(0, 0, 0, 0.85)', position: 'fixed', top: 0, insetInlineStart: 0, width: '75%', color: '#fff', }; const App: React.FC = () => { const topRef = React.useRef(null); const [targetOffset, setTargetOffset] = useState(); useEffect(() => { setTargetOffset(topRef.current?.clientHeight); }, []); return (
Part 1
Part 2
Part 3
Fixed Top Block
); }; export default App; ``` ### 监听锚点链接改变 监听锚点链接改变 ```tsx import React from 'react'; import { Anchor } from 'antd'; const onChange = (link: string) => { console.log('Anchor:OnChange', link); }; const App: React.FC = () => ( ); export default App; ``` ### 替换历史中的 href 替换浏览器历史记录中的路径,后退按钮将返回到上一页而不是上一个锚点。 ```tsx import React from 'react'; import { Anchor, Col, Row } from 'antd'; const App: React.FC = () => (
); export default App; ``` ### 自定义语义结构的样式和类 通过 `classNames` 和 `styles` 传入对象/函数可以自定义 Anchor 的[语义化结构](#semantic-dom)样式。 ```tsx import React from 'react'; import { Anchor, Col, Row } from 'antd'; import type { AnchorProps } from 'antd'; const classNamesObject: AnchorProps['classNames'] = { root: 'demo-anchor-root', item: 'demo-anchor-item', itemTitle: 'demo-anchor-title', indicator: 'demo-anchor-indicator', }; const stylesFn: AnchorProps['styles'] = (info) => { if (info.props.direction === 'vertical') { return { root: { backgroundColor: 'rgba(255,251,230,0.5)', height: '100vh', }, } satisfies AnchorProps['styles']; } return {}; }; const items: NonNullable = [ { key: 'part-1', href: '#part-1', title: 'Part 1', }, { key: 'part-2', href: '#part-2', title: 'Part 2', }, { key: 'part-3', href: '#part-3', title: 'Part 3', }, ]; const App: React.FC = () => { return (
); }; export default App; ``` ## API 通用属性参考:[通用属性](/docs/react/common-props) ### Anchor Props | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | | affix | 固定模式 | boolean \| Omit | true | object: 5.19.0 | | bounds | 锚点区域边界 | number | 5 | | | classNames | 用于自定义组件内部各语义化结构的 class,支持对象或函数 | Record<[SemanticDOM](#semantic-dom), string> \| (info: { props })=> Record<[SemanticDOM](#semantic-dom), string> | - | | | getContainer | 指定滚动的容器 | () => HTMLElement | () => window | | | getCurrentAnchor | 自定义高亮的锚点 | (activeLink: string) => string | - | | | offsetTop | 距离窗口顶部达到指定偏移量后触发 | number | | | | showInkInFixed | `affix={false}` 时是否显示小方块 | boolean | false | | | styles | 用于自定义组件内部各语义化结构的行内 style,支持对象或函数 | Record<[SemanticDOM](#semantic-dom), CSSProperties> \| (info: { props })=> Record<[SemanticDOM](#semantic-dom), CSSProperties> | - | | | targetOffset | 锚点滚动偏移量,默认与 offsetTop 相同,[例子](#anchor-demo-targetoffset) | number | - | | | onChange | 监听锚点链接改变 | (currentActiveLink: string) => void | - | | | onClick | `click` 事件的 handler | (e: MouseEvent, link: object) => void | - | | | items | 数据化配置选项内容,支持通过 children 嵌套 | { key, href, title, target, children }\[] [具体见](#anchoritem) | - | 5.1.0 | | direction | 设置导航方向 | `vertical` \| `horizontal` | `vertical` | 5.2.0 | | replace | 替换浏览器历史记录中项目的 href 而不是推送它 | boolean | false | 5.7.0 | ### AnchorItem | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | | key | 唯一标志 | string \| number | - | | | href | 锚点链接 | string | - | | | target | 该属性指定在何处显示链接的资源 | string | - | | | title | 文字内容 | ReactNode | - | | | children | 嵌套的 Anchor Link,`注意:水平方向该属性不支持` | [AnchorItem](#anchoritem)\[] | - | | | replace | 替换浏览器历史记录中的项目 href 而不是推送它 | boolean | false | 5.7.0 | ### Link Props 建议使用 items 形式。 | 参数 | 说明 | 类型 | 默认值 | 版本 | | ------ | ------------------------------ | --------- | ------ | ---- | | href | 锚点链接 | string | - | | | target | 该属性指定在何处显示链接的资源 | string | - | | | title | 文字内容 | ReactNode | - | | ## Semantic DOM https://ant.design/components/anchor-cn/semantic.md ## 主题变量(Design Token){#design-token} ## 组件 Token (Anchor) | Token 名称 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | | linkPaddingBlock | 链接纵向内间距 | number | 4 | | linkPaddingInlineStart | 链接横向内间距 | number | 16 | ## 全局 Token | Token 名称 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | | colorPrimary | 品牌色是体现产品特性和传播理念最直观的视觉元素之一。在你完成品牌主色的选取之后,我们会自动帮你生成一套完整的色板,并赋予它们有效的设计语义 | string | | | colorSplit | 用于作为分割线的颜色,此颜色和 colorBorderSecondary 的颜色一致,但是用的是透明色。 | string | | | colorText | 最深的文本色。为了符合W3C标准,默认的文本颜色使用了该色,同时这个颜色也是最深的中性色。 | string | | | fontFamily | Ant Design 的字体家族中优先使用系统默认的界面字体,同时提供了一套利于屏显的备用字体库,来维护在不同平台以及浏览器的显示下,字体始终保持良好的易读性和可读性,体现了友好、稳定和专业的特性。 | string | | | fontSize | 设计系统中使用最广泛的字体大小,文本梯度也将基于该字号进行派生。 | number | | | fontSizeLG | 大号字体大小 | number | | | lineHeight | 文本行高 | number | | | lineType | 用于控制组件边框、分割线等的样式,默认是实线 | string | | | lineWidth | 用于控制组件边框、分割线等的宽度 | number | | | lineWidthBold | 描边类组件的默认线宽,如 Button、Input、Select 等输入类控件。 | number | | | motionDurationSlow | 动效播放速度,慢速。用于大型元素如面板动画交互 | string | | | paddingXXS | 控制元素的极小内间距。 | number | | ## FAQ ### 在 `5.25.0+` 版本中,锚点跳转后,目标元素的 `:target` 伪类未按预期生效 {#faq-target-pseudo-class} 出于页面性能优化考虑,锚点跳转的实现方式从 `window.location.href` 调整为 `window.history.pushState/replaceState`。由于 `pushState/replaceState` 不会触发页面重载,因此浏览器不会自动更新 `:target` 伪类的匹配状态。可以手动构造完整URL:`href = window.location.origin + window.location.pathname + '#xxx'` 来解决这问题。 相关issues:[#53143](https://github.com/ant-design/ant-design/issues/53143) [#54255](https://github.com/ant-design/ant-design/issues/54255) --- ## app-cn Source: https://ant.design/components/app-cn.md --- category: Components group: 其他 title: App subtitle: 包裹组件 description: 提供重置样式和提供消费上下文的默认环境。 cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*HJz8SZos2wgAAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*oC92TK44Ex8AAAAAAAAAAAAADrJ8AQ/original demo: cols: 2 --- ## 何时使用 {#when-to-use} - 提供可消费 React context 的 `message.xxx`、`Modal.xxx`、`notification.xxx` 的静态方法,可以简化 useMessage 等方法需要手动植入 `contextHolder` 的问题。 - 提供基于 `.ant-app` 的默认重置样式,解决原生元素没有 antd 规范样式的问题。 ## 代码演示 {#examples} ### 基本用法 获取 `message`、`notification`、`modal` 实例。 ```tsx import React from 'react'; import { App, Button, Space } from 'antd'; // Sub page const MyPage = () => { const { message, modal, notification } = App.useApp(); const showMessage = () => { message.success('Success!'); }; const showModal = () => { modal.warning({ title: 'This is a warning message', content: 'some messages...some messages...', }); }; const showNotification = () => { notification.info({ title: 'Notification topLeft', description: 'Hello, Ant Design!!', placement: 'topLeft', }); }; return ( ); }; // Entry component export default () => ( ); ``` ### Hooks 配置 对 `message`、`notification` 进行配置。 ```tsx import React from 'react'; import { App, Button, Space } from 'antd'; // Sub page const MyPage = () => { const { message, notification } = App.useApp(); const showMessage = () => { message.success('Success!'); }; const showNotification = () => { notification.info({ title: 'Notification', description: 'Hello, Ant Design!!', }); }; return ( ); }; // Entry component export default () => ( ); ``` ## 如何使用 {#how-to-use} ### 基础用法 {#basic-usage} App 组件通过 `Context` 提供上下文方法调用,因而 useApp 需要作为子组件才能使用,我们推荐在应用中顶层包裹 App。 ```tsx import React from 'react'; import { App } from 'antd'; const MyPage: React.FC = () => { const { message, notification, modal } = App.useApp(); message.success('Good!'); notification.info({ title: 'Good' }); modal.warning({ title: 'Good' }); // .... // other message, notification, modal static function return
Hello word
; }; const MyApp: React.FC = () => ( ); export default MyApp; ``` 注意:App.useApp 必须在 App 之下方可使用。 ### 与 ConfigProvider 先后顺序 {#sequence-with-configprovider} App 组件只能在 `ConfigProvider` 之下才能使用 Design Token, 如果需要使用其样式重置能力,则 ConfigProvider 与 App 组件必须成对出现。 ```tsx ... ``` ### 内嵌使用场景(如无必要,尽量不做嵌套) {#embedded-usage-scenarios} ```tsx ... ... ``` ### 全局场景(redux 场景) {#global-scene-redux} ```tsx // Entry component import { App } from 'antd'; import type { MessageInstance } from 'antd/es/message/interface'; import type { ModalStaticFunctions } from 'antd/es/modal/confirm'; import type { NotificationInstance } from 'antd/es/notification/interface'; let message: MessageInstance; let notification: NotificationInstance; let modal: Omit; export default () => { const staticFunction = App.useApp(); message = staticFunction.message; modal = staticFunction.modal; notification = staticFunction.notification; return null; }; export { message, notification, modal }; ``` ```tsx // sub page import React from 'react'; import { Button, Space } from 'antd'; import { message } from './store'; export default () => { const showMessage = () => { message.success('Success!'); }; return ( ); }; ``` ## API 通用属性参考:[通用属性](/docs/react/common-props) > 自 `antd@5.1.0` 版本开始提供该组件。 ### App | 参数 | 说明 | 类型 | 默认值 | 版本 | | --- | --- | --- | --- | --- | | component | 设置渲染元素,为 `false` 则不创建 DOM 节点 | ComponentType \| false | div | 5.11.0 | | message | App 内 Message 的全局配置 | [MessageConfig](/components/message-cn/#messageconfig) | - | 5.3.0 | | notification | App 内 Notification 的全局配置 | [NotificationConfig](/components/notification-cn/#notificationconfig) | - | 5.3.0 | ## 主题变量(Design Token){#design-token} ## 全局 Token | Token 名称 | 描述 | 类型 | 默认值 | | --- | --- | --- | --- | | colorText | 最深的文本色。为了符合W3C标准,默认的文本颜色使用了该色,同时这个颜色也是最深的中性色。 | string | | | fontFamily | Ant Design 的字体家族中优先使用系统默认的界面字体,同时提供了一套利于屏显的备用字体库,来维护在不同平台以及浏览器的显示下,字体始终保持良好的易读性和可读性,体现了友好、稳定和专业的特性。 | string | | | fontSize | 设计系统中使用最广泛的字体大小,文本梯度也将基于该字号进行派生。 | number | | | lineHeight | 文本行高 | number | | ## FAQ ### CSS Var 在 `` 内不起作用 {#faq-css-var-component-false} 请确保 App 的 `component` 是一个有效的 html 标签名,以便在启用 CSS 变量时有一个容器来承载 CSS 类名。如果不设置,则默认为 `div` 标签,如果设置为 `false`,则不会创建额外的 DOM 节点,也不会提供默认样式。 --- ## auto-complete-cn Source: https://ant.design/components/auto-complete-cn.md --- category: Components title: AutoComplete subtitle: 自动完成 description: 输入框自动完成功能。 cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*g8THS4NpV6sAAAAAAAAAAAAADrJ8AQ/original coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*WERTQ6qvgEYAAAAAAAAAAAAADrJ8AQ/original group: title: 数据录入 order: 4 demo: cols: 2 --- ## 何时使用 {#when-to-use} - 需要一个输入框而不是选择器。 - 需要输入建议/辅助提示。 和 Select 的区别是: - AutoComplete 是一个带提示的文本输入框,用户可以自由输入,关键词是辅助**输入**。 - Select 是在限定的可选项中进行选择,关键词是**选择**。 ## 代码演示 {#examples} ### 基本使用 基本使用,通过 `options` 设置自动完成的数据源。 ```tsx import React, { useState } from 'react'; import { AutoComplete } from 'antd'; import type { AutoCompleteProps } from 'antd'; const mockVal = (str: string, repeat = 1) => ({ value: str.repeat(repeat), }); const App: React.FC = () => { const [value, setValue] = useState(''); const [options, setOptions] = useState([]); const [anotherOptions, setAnotherOptions] = useState([]); const getPanelValue = (searchText: string) => !searchText ? [] : [mockVal(searchText), mockVal(searchText, 2), mockVal(searchText, 3)]; const onSelect = (data: string) => { console.log('onSelect', data); }; const onChange = (data: string) => { setValue(data); }; return ( <> setOptions(getPanelValue(text)), }} placeholder="input here" />

setAnotherOptions(getPanelValue(text)) }} options={anotherOptions} style={{ width: 200 }} onSelect={onSelect} onChange={onChange} placeholder="control mode" /> ); }; export default App; ``` ### 自定义选项 可以返回自定义的 `Option` label ```tsx import React from 'react'; import { AutoComplete } from 'antd'; import type { AutoCompleteProps } from 'antd'; const App: React.FC = () => { const [options, setOptions] = React.useState([]); const handleSearch = (value: string) => { setOptions(() => { if (!value || value.includes('@')) { return []; } return ['gmail.com', '163.com', 'qq.com'].map((domain) => ({ label: `${value}@${domain}`, value: `${value}@${domain}`, })); }); }; return ( ); }; export default App; ``` ### 自定义输入组件 自定义输入组件。 ```tsx import React, { useState } from 'react'; import { AutoComplete, Input } from 'antd'; import type { AutoCompleteProps } from 'antd'; const { TextArea } = Input; const App: React.FC = () => { const [options, setOptions] = useState([]); const handleSearch = (value: string) => { setOptions( !value ? [] : [{ value }, { value: value + value }, { value: value + value + value }], ); }; const handleKeyPress = (ev: React.KeyboardEvent) => { console.log('handleKeyPress', ev); }; const onSelect = (value: string) => { console.log('onSelect', value); }; return (