From 6a8f5b576513bdcfedb65a5c7d7e6b8f1d578b7a Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Mon, 21 Nov 2022 09:52:33 +0800 Subject: [PATCH 1/6] refactor(Menu): rewrite CC => FC (#38751) --- components/menu/MenuDivider.tsx | 8 ++--- components/menu/MenuItem.tsx | 48 ++++++++++++----------------- components/menu/OverrideContext.tsx | 15 ++++----- components/menu/SubMenu.tsx | 13 +++----- components/menu/index.tsx | 9 +++--- components/menu/menu.tsx | 4 +-- 6 files changed, 39 insertions(+), 58 deletions(-) diff --git a/components/menu/MenuDivider.tsx b/components/menu/MenuDivider.tsx index ae93440a87..1441481bec 100644 --- a/components/menu/MenuDivider.tsx +++ b/components/menu/MenuDivider.tsx @@ -10,12 +10,8 @@ export interface MenuDividerProps extends React.HTMLAttributes { dashed?: boolean; } -const MenuDivider: React.FC = ({ - prefixCls: customizePrefixCls, - className, - dashed, - ...restProps -}) => { +const MenuDivider: React.FC = (props) => { + const { prefixCls: customizePrefixCls, className, dashed, ...restProps } = props; const { getPrefixCls } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('menu', customizePrefixCls); diff --git a/components/menu/MenuItem.tsx b/components/menu/MenuItem.tsx index 14d781c254..47e6ac1ee6 100644 --- a/components/menu/MenuItem.tsx +++ b/components/menu/MenuItem.tsx @@ -2,6 +2,7 @@ import classNames from 'classnames'; import type { MenuItemProps as RcMenuItemProps } from 'rc-menu'; import { Item } from 'rc-menu'; import toArray from 'rc-util/lib/Children/toArray'; +import omit from 'rc-util/lib/omit'; import * as React from 'react'; import type { SiderContextProps } from '../layout/Sider'; import { SiderContext } from '../layout/Sider'; @@ -17,15 +18,16 @@ export interface MenuItemProps extends Omit { title?: React.ReactNode; } -export default class MenuItem extends React.Component { - static contextType = MenuContext; - - context: MenuContextProps; - - renderItemChildren(inlineCollapsed: boolean) { - const { prefixCls, firstLevel } = this.context; - const { icon, children } = this.props; - +const MenuItem: React.FC = (props) => { + const { className, children, icon, title, danger } = props; + const { + prefixCls, + firstLevel, + direction, + disableMenuItemTitleTooltip, + inlineCollapsed: isInlineCollapsed, + } = React.useContext(MenuContext); + const renderItemChildren = (inlineCollapsed: boolean) => { const wrapNode = {children}; // inline-collapsed.md demo 依赖 span 来隐藏文字,有 icon 属性,则内部包裹一个 span // ref: https://github.com/ant-design/ant-design/pull/23456 @@ -35,25 +37,16 @@ export default class MenuItem extends React.Component { } } return wrapNode; - } - - renderItem = ({ siderCollapsed }: SiderContextProps) => { - const { prefixCls, firstLevel, inlineCollapsed, direction, disableMenuItemTitleTooltip } = - this.context; - const { className, children } = this.props; - const { title, icon, danger, ...rest } = this.props; - + }; + const renderItem = ({ siderCollapsed }: SiderContextProps) => { let tooltipTitle = title; if (typeof title === 'undefined') { tooltipTitle = firstLevel ? children : ''; } else if (title === false) { tooltipTitle = ''; } - const tooltipProps: TooltipProps = { - title: tooltipTitle, - }; - - if (!siderCollapsed && !inlineCollapsed) { + const tooltipProps: TooltipProps = { title: tooltipTitle }; + if (!siderCollapsed && !isInlineCollapsed) { tooltipProps.title = null; // Reset `open` to fix control mode tooltip display not correct // ref: https://github.com/ant-design/ant-design/issues/16742 @@ -63,7 +56,7 @@ export default class MenuItem extends React.Component { let returnNode = ( { `${prefixCls}-item-icon`, ), })} - {this.renderItemChildren(inlineCollapsed)} + {renderItemChildren(isInlineCollapsed)} ); @@ -97,8 +90,7 @@ export default class MenuItem extends React.Component { return returnNode; }; + return {renderItem}; +}; - render() { - return {this.renderItem}; - } -} +export default MenuItem; diff --git a/components/menu/OverrideContext.tsx b/components/menu/OverrideContext.tsx index 70dd828efe..900feebca5 100644 --- a/components/menu/OverrideContext.tsx +++ b/components/menu/OverrideContext.tsx @@ -15,17 +15,14 @@ export interface OverrideContextProps { const OverrideContext = React.createContext(null); /** @internal Only used for Dropdown component. Do not use this in your production. */ -export const OverrideProvider = ({ - children, - ...restProps -}: OverrideContextProps & { children: React.ReactNode }) => { +export const OverrideProvider: React.FC = ( + props, +) => { + const { children, ...restProps } = props; const override = React.useContext(OverrideContext); - const context = React.useMemo( - () => ({ - ...override, - ...restProps, - }), + const context = React.useMemo( + () => ({ ...override, ...restProps }), [ override, restProps.prefixCls, diff --git a/components/menu/SubMenu.tsx b/components/menu/SubMenu.tsx index 972f19e74b..1ae6b50840 100644 --- a/components/menu/SubMenu.tsx +++ b/components/menu/SubMenu.tsx @@ -3,7 +3,7 @@ import { SubMenu as RcSubMenu, useFullPath } from 'rc-menu'; import omit from 'rc-util/lib/omit'; import * as React from 'react'; import { cloneElement, isValidElement } from '../_util/reactNode'; -import type { MenuTheme } from './MenuContext'; +import type { MenuContextProps, MenuTheme } from './MenuContext'; import MenuContext from './MenuContext'; interface TitleEventEntity { @@ -27,7 +27,7 @@ export interface SubMenuProps { theme?: MenuTheme; } -function SubMenu(props: SubMenuProps) { +const SubMenu: React.FC = (props) => { const { popupClassName, icon, title, theme: customTheme } = props; const context = React.useContext(MenuContext); const { prefixCls, inlineCollapsed, theme: contextTheme, mode } = context; @@ -60,11 +60,8 @@ function SubMenu(props: SubMenuProps) { ); } - const contextValue = React.useMemo( - () => ({ - ...context, - firstLevel: false, - }), + const contextValue = React.useMemo( + () => ({ ...context, firstLevel: false }), [context], ); @@ -84,6 +81,6 @@ function SubMenu(props: SubMenuProps) { /> ); -} +}; export default SubMenu; diff --git a/components/menu/index.tsx b/components/menu/index.tsx index 5c75a65915..14d0a1b53b 100644 --- a/components/menu/index.tsx +++ b/components/menu/index.tsx @@ -21,9 +21,9 @@ export type MenuRef = { interface CompoundedComponent extends React.ForwardRefExoticComponent> { - Divider: typeof MenuDivider; Item: typeof Item; SubMenu: typeof SubMenu; + Divider: typeof MenuDivider; ItemGroup: typeof ItemGroup; } @@ -32,18 +32,17 @@ const Menu = forwardRef((props, ref) => { const context = React.useContext(SiderContext); useImperativeHandle(ref, () => ({ - focus: options => { + menu: menuRef.current, + focus: (options) => { menuRef.current?.focus(options); }, - menu: menuRef.current, })); - return ; }) as CompoundedComponent; -Menu.Divider = MenuDivider; Menu.Item = Item; Menu.SubMenu = SubMenu; +Menu.Divider = MenuDivider; Menu.ItemGroup = ItemGroup; export default Menu; diff --git a/components/menu/menu.tsx b/components/menu/menu.tsx index c98a532687..1600476deb 100644 --- a/components/menu/menu.tsx +++ b/components/menu/menu.tsx @@ -16,7 +16,7 @@ import OverrideContext from './OverrideContext'; import useItems from './hooks/useItems'; import type { ItemType } from './hooks/useItems'; import MenuContext from './MenuContext'; -import type { MenuTheme } from './MenuContext'; +import type { MenuTheme, MenuContextProps } from './MenuContext'; export interface MenuProps extends Omit { theme?: MenuTheme; @@ -131,7 +131,7 @@ const InternalMenu = forwardRef((props, ref) => { } // ======================== Context ========================== - const contextValue = React.useMemo( + const contextValue = React.useMemo( () => ({ prefixCls, inlineCollapsed: mergedInlineCollapsed || false, From 67ee0194789f7365f75ffdc44e5b68bed4212b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E9=9B=A8?= Date: Mon, 21 Nov 2022 21:34:23 +0800 Subject: [PATCH 2/6] Mentions data driven (#38630) * feat: reset and force update * feat: update package * feat: reset * feat: update for viewer * feat: update for viewer * feat: update for viewer * feat: update for viewer * feat: update for viewer * update doc * feat: add waring * feat: update doc * feat: add test case --- components/mentions/__tests__/index.test.tsx | 19 +++++++- components/mentions/demo/async.tsx | 31 ++++++++----- components/mentions/demo/autoSize.tsx | 25 ++++++++--- components/mentions/demo/basic.tsx | 26 +++++++---- components/mentions/demo/form.tsx | 47 +++++++++++++++----- components/mentions/demo/placement.tsx | 25 ++++++++--- components/mentions/demo/prefix.tsx | 15 +++---- components/mentions/demo/readonly.tsx | 31 +++++++------ components/mentions/demo/render-panel.tsx | 18 +++++--- components/mentions/demo/status.tsx | 47 +++++++++++++------- components/mentions/index.en-US.md | 24 ++++++++-- components/mentions/index.tsx | 42 ++++++++++------- components/mentions/index.zh-CN.md | 25 +++++++++-- package.json | 2 +- 14 files changed, 260 insertions(+), 117 deletions(-) diff --git a/components/mentions/__tests__/index.test.tsx b/components/mentions/__tests__/index.test.tsx index 320336eafd..725a4e5bae 100644 --- a/components/mentions/__tests__/index.test.tsx +++ b/components/mentions/__tests__/index.test.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import Mentions from '..'; +import Mentions,{Option} from '..'; import focusTest from '../../../tests/shared/focusTest'; import mountTest from '../../../tests/shared/mountTest'; import rtlTest from '../../../tests/shared/rtlTest'; @@ -84,4 +84,21 @@ describe('Mentions', () => { expect(wrapper.container.querySelectorAll('li.ant-mentions-dropdown-menu-item').length).toBe(1); expect(wrapper.container.querySelectorAll('.bamboo-light').length).toBeTruthy(); }); + + it('warning if use Mentions.Option', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + render( + + + + + , + ); + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: [antd: Mentions] `Mentions.Option` is deprecated. Please use `options` instead.', + ); + }); }); diff --git a/components/mentions/demo/async.tsx b/components/mentions/demo/async.tsx index 4403424148..447c918997 100644 --- a/components/mentions/demo/async.tsx +++ b/components/mentions/demo/async.tsx @@ -2,7 +2,6 @@ import React, { useCallback, useRef, useState } from 'react'; import { Mentions } from 'antd'; import debounce from 'lodash/debounce'; -const { Option } = Mentions; const App: React.FC = () => { const [loading, setLoading] = useState(false); const [users, setUsers] = useState<{ login: string; avatar_url: string }[]>([]); @@ -15,12 +14,12 @@ const App: React.FC = () => { } fetch(`https://api.github.com/search/users?q=${key}`) - .then(res => res.json()) - .then(({ items = [] }) => { + .then((res) => res.json()) + .then(({ options = [] }) => { if (ref.current !== key) return; setLoading(false); - setUsers(items.slice(0, 10)); + setUsers(options.slice(0, 10)); }); }; @@ -36,14 +35,22 @@ const App: React.FC = () => { }; return ( - - {users.map(({ login, avatar_url: avatar }) => ( - - ))} - + ({ + key: login, + value: login, + className: 'antd-demo-dynamic-option', + label: ( + <> + {login} + {login} + + ), + }))} + /> ); }; diff --git a/components/mentions/demo/autoSize.tsx b/components/mentions/demo/autoSize.tsx index 9454cd4864..3f6bc78476 100644 --- a/components/mentions/demo/autoSize.tsx +++ b/components/mentions/demo/autoSize.tsx @@ -1,14 +1,25 @@ import React from 'react'; import { Mentions } from 'antd'; -const { Option } = Mentions; - const App: React.FC = () => ( - - - - - + ); export default App; diff --git a/components/mentions/demo/basic.tsx b/components/mentions/demo/basic.tsx index e307d99ae9..10f6f91505 100644 --- a/components/mentions/demo/basic.tsx +++ b/components/mentions/demo/basic.tsx @@ -1,14 +1,12 @@ import React from 'react'; import { Mentions } from 'antd'; -import type { OptionProps } from 'antd/es/mentions'; - -const { Option } = Mentions; +import type { MentionsOptionProps } from 'antd/es/mentions'; const onChange = (value: string) => { console.log('Change:', value); }; -const onSelect = (option: OptionProps) => { +const onSelect = (option: MentionsOptionProps) => { console.log('select', option); }; @@ -18,11 +16,21 @@ const App: React.FC = () => ( onChange={onChange} onSelect={onSelect} defaultValue="@afc163" - > - - - - + options={[ + { + value: 'afc163', + label: 'afc163', + }, + { + value: 'zombieJ', + label: 'zombieJ', + }, + { + value: 'yesmeck', + label: 'yesmeck', + }, + ]} + /> ); export default App; diff --git a/components/mentions/demo/form.tsx b/components/mentions/demo/form.tsx index 98d5703d35..53879bbd0f 100644 --- a/components/mentions/demo/form.tsx +++ b/components/mentions/demo/form.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Button, Form, Mentions } from 'antd'; -const { Option, getMentions } = Mentions; +const { getMentions } = Mentions; const App: React.FC = () => { const [form] = Form.useForm(); @@ -36,11 +36,23 @@ const App: React.FC = () => { wrapperCol={{ span: 16 }} rules={[{ validator: checkMention }]} > - - - - - + { wrapperCol={{ span: 16 }} rules={[{ required: true }]} > - - - - - + + + + + + + +`; + exports[`Tour rtl render component should be rendered correctly in RTL direction 1`] = `null`; exports[`Tour single 1`] = ` diff --git a/components/tour/__tests__/index.test.tsx b/components/tour/__tests__/index.test.tsx index 5dcc54b5d6..f55b0fad05 100644 --- a/components/tour/__tests__/index.test.tsx +++ b/components/tour/__tests__/index.test.tsx @@ -259,4 +259,44 @@ describe('Tour', () => { panelRender({ total: undefined, title:
test
}, 0, 'default'); }).not.toThrow(); }); + + it('custom step pre btn & next btn className & style', () => { + const App: React.FC = () => ( + + ), + }, + ]} + /> + ); + + const { container } = render(); + // className + expect( + screen.getByRole('button', { name: 'Next' }).className.includes('customClassName'), + ).toEqual(true); + // style + expect(screen.getByRole('button', { name: 'Next' }).style.backgroundColor).toEqual( + 'rgb(69, 69, 255)', + ); + expect(container.firstChild).toMatchSnapshot(); + }); }); diff --git a/components/tour/interface.ts b/components/tour/interface.ts index 0831e51e8b..fd682d759a 100644 --- a/components/tour/interface.ts +++ b/components/tour/interface.ts @@ -15,8 +15,18 @@ export type TourProps = Omit & { export interface TourStepProps extends RCTourStepProps { cover?: ReactNode; // 展示的图片或者视频 - nextButtonProps?: { children?: ReactNode; onClick?: () => void }; - prevButtonProps?: { children?: ReactNode; onClick?: () => void }; + nextButtonProps?: { + children?: ReactNode; + onClick?: () => void; + className?: string; + style?: React.CSSProperties; + }; + prevButtonProps?: { + children?: ReactNode; + onClick?: () => void; + className?: string; + style?: React.CSSProperties; + }; stepRender?: (current: number, total: number) => ReactNode; type?: 'default' | 'primary'; // default 类型,影响底色与文字颜色 } diff --git a/components/tour/panelRender.tsx b/components/tour/panelRender.tsx index 867780f997..cb9d777fdf 100644 --- a/components/tour/panelRender.tsx +++ b/components/tour/panelRender.tsx @@ -103,7 +103,7 @@ const panelRender = ( {...prevButtonProps} onClick={prevBtnClick} size="small" - className={`${prefixCls}-prev-btn`} + className={classNames(`${prefixCls}-prev-btn`, prevButtonProps?.className)} > {prevButtonProps?.children ?? contextLocale.Previous} @@ -113,7 +113,7 @@ const panelRender = ( {...nextButtonProps} onClick={nextBtnClick} size="small" - className={`${prefixCls}-next-btn`} + className={classNames(`${prefixCls}-next-btn`, nextButtonProps?.className)} > {nextButtonProps?.children ?? (isLastStep ? contextLocale.Finish : contextLocale.Next)} From 51da7e3dc1648070f37338e943c7ae84ce14f4bf Mon Sep 17 00:00:00 2001 From: Yuki Zhang Date: Wed, 30 Nov 2022 15:55:43 +0800 Subject: [PATCH 5/6] feat: Anchor component changed to data-driven (#39034) * feat: Anchor component changed to data-driven * test: add test cases for data driven items * fix: type * chore: mark deprecated for anchor children prop * docs: add items description * test: update snapshot * docs: demos changed to data-driven * docs: Keep the old jsx syntax demo for debugging --- components/anchor/Anchor.tsx | 29 ++++++- components/anchor/AnchorLink.tsx | 7 +- components/anchor/__tests__/Anchor.test.tsx | 60 ++++++++++++++ .../__snapshots__/Anchor.test.tsx.snap | 81 +++++++++++++++++++ .../__snapshots__/demo-extend.test.ts.snap | 80 ++++++++++++++++++ .../__tests__/__snapshots__/demo.test.ts.snap | 80 ++++++++++++++++++ components/anchor/demo/basic.tsx | 41 +++++++--- components/anchor/demo/customizeHighlight.tsx | 43 +++++++--- components/anchor/demo/legacy-anchor.md | 7 ++ components/anchor/demo/legacy-anchor.tsx | 17 ++++ components/anchor/demo/onChange.tsx | 43 +++++++--- components/anchor/demo/onClick.tsx | 43 +++++++--- components/anchor/demo/static.tsx | 42 +++++++--- components/anchor/demo/targetOffset.tsx | 42 +++++++--- components/anchor/index.en-US.md | 2 + components/anchor/index.zh-CN.md | 2 + 16 files changed, 556 insertions(+), 63 deletions(-) create mode 100644 components/anchor/__tests__/__snapshots__/Anchor.test.tsx.snap create mode 100644 components/anchor/demo/legacy-anchor.md create mode 100644 components/anchor/demo/legacy-anchor.tsx diff --git a/components/anchor/Anchor.tsx b/components/anchor/Anchor.tsx index 9c377a3324..e555e5445f 100644 --- a/components/anchor/Anchor.tsx +++ b/components/anchor/Anchor.tsx @@ -6,10 +6,18 @@ import type { ConfigConsumerProps } from '../config-provider'; import { ConfigContext } from '../config-provider'; import getScroll from '../_util/getScroll'; import scrollTo from '../_util/scrollTo'; +import warning from '../_util/warning'; import AnchorContext from './context'; +import type { AnchorLinkBaseProps } from './AnchorLink'; +import AnchorLink from './AnchorLink'; import useStyle from './style'; +export interface AnchorLinkItemProps extends AnchorLinkBaseProps { + key: React.Key; + children?: AnchorLinkItemProps[]; +} + export type AnchorContainer = HTMLElement | Window; function getDefaultContainer() { @@ -45,6 +53,9 @@ export interface AnchorProps { prefixCls?: string; className?: string; style?: React.CSSProperties; + /** + * @deprecated Please use `items` instead. + */ children?: React.ReactNode; offsetTop?: number; bounds?: number; @@ -61,6 +72,7 @@ export interface AnchorProps { targetOffset?: number; /** Listening event when scrolling change active link */ onChange?: (currentActiveLink: string) => void; + items?: AnchorLinkItemProps[]; } interface InternalAnchorProps extends AnchorProps { @@ -100,6 +112,7 @@ const AnchorContent: React.FC = (props) => { affix = true, showInkInFixed = false, children, + items, bounds, targetOffset, onClick, @@ -108,6 +121,11 @@ const AnchorContent: React.FC = (props) => { getCurrentAnchor, } = props; + // =================== Warning ===================== + if (process.env.NODE_ENV !== 'production') { + warning(!children, 'Anchor', '`Anchor children` is deprecated. Please use `items` instead.'); + } + const [links, setLinks] = React.useState([]); const [activeLink, setActiveLink] = React.useState(null); const activeLinkRef = React.useRef(activeLink); @@ -257,13 +275,22 @@ const AnchorContent: React.FC = (props) => { ...style, }; + const createNestedLink = (options?: AnchorLinkItemProps[]) => + Array.isArray(options) + ? options.map((item) => ( + + {createNestedLink(item.children)} + + )) + : null; + const anchorContent = (
- {children} + {'items' in props ? createNestedLink(items) : children}
); diff --git a/components/anchor/AnchorLink.tsx b/components/anchor/AnchorLink.tsx index 6d2c87fa2c..5e7c4aa611 100644 --- a/components/anchor/AnchorLink.tsx +++ b/components/anchor/AnchorLink.tsx @@ -5,15 +5,18 @@ import { ConfigConsumer } from '../config-provider'; import type { AntAnchor } from './Anchor'; import AnchorContext from './context'; -export interface AnchorLinkProps { +export interface AnchorLinkBaseProps { prefixCls?: string; href: string; target?: string; title: React.ReactNode; - children?: React.ReactNode; className?: string; } +export interface AnchorLinkProps extends AnchorLinkBaseProps { + children?: React.ReactNode; +} + const AnchorLink: React.FC = (props) => { const { href = '#', title, prefixCls: customizePrefixCls, children, className, target } = props; diff --git a/components/anchor/__tests__/Anchor.test.tsx b/components/anchor/__tests__/Anchor.test.tsx index aa47e56c3c..c4468742d5 100644 --- a/components/anchor/__tests__/Anchor.test.tsx +++ b/components/anchor/__tests__/Anchor.test.tsx @@ -432,4 +432,64 @@ describe('Anchor Render', () => { }).not.toThrow(); }); }); + + it('renders items correctly', () => { + const { container, asFragment } = render( + , + ); + expect(container.querySelectorAll('.ant-anchor .ant-anchor-link').length).toBe(5); + const linkTitles = Array.from(container.querySelector('.ant-anchor')?.childNodes!) + .slice(1) + .map((n) => (n as HTMLElement).querySelector('.ant-anchor-link-title')); + expect((linkTitles[0] as HTMLAnchorElement).href).toContain('#components-anchor-demo-basic'); + expect((linkTitles[1] as HTMLAnchorElement).href).toContain('#components-anchor-demo-static'); + expect((linkTitles[2] as HTMLAnchorElement).href).toContain('#api'); + expect(asFragment().firstChild).toMatchSnapshot(); + expect( + ( + container.querySelector( + '.ant-anchor .ant-anchor-link .ant-anchor-link .ant-anchor-link-title', + ) as HTMLAnchorElement + )?.href, + ).toContain('#anchor-props'); + expect( + ( + container.querySelector( + '.ant-anchor .ant-anchor-link .ant-anchor-link .ant-anchor-link .ant-anchor-link-title', + ) as HTMLAnchorElement + )?.href, + ).toContain('#link-props'); + }); }); diff --git a/components/anchor/__tests__/__snapshots__/Anchor.test.tsx.snap b/components/anchor/__tests__/__snapshots__/Anchor.test.tsx.snap new file mode 100644 index 0000000000..9fddec2866 --- /dev/null +++ b/components/anchor/__tests__/__snapshots__/Anchor.test.tsx.snap @@ -0,0 +1,81 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Anchor Render renders items correctly 1`] = ` + +`; diff --git a/components/anchor/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/anchor/__tests__/__snapshots__/demo-extend.test.ts.snap index 582b236f77..fe72574f59 100644 --- a/components/anchor/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/anchor/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -154,6 +154,86 @@ exports[`renders ./components/anchor/demo/customizeHighlight.tsx extend context `; +exports[`renders ./components/anchor/demo/legacy-anchor.tsx extend context correctly 1`] = ` + +`; + exports[`renders ./components/anchor/demo/onChange.tsx extend context correctly 1`] = `
`; +exports[`renders ./components/anchor/demo/legacy-anchor.tsx correctly 1`] = ` + +`; + exports[`renders ./components/anchor/demo/onChange.tsx correctly 1`] = `
( - - - - - - - - + ); export default App; diff --git a/components/anchor/demo/customizeHighlight.tsx b/components/anchor/demo/customizeHighlight.tsx index e746c2518d..fed1ebb932 100644 --- a/components/anchor/demo/customizeHighlight.tsx +++ b/components/anchor/demo/customizeHighlight.tsx @@ -1,19 +1,42 @@ import React from 'react'; import { Anchor } from 'antd'; -const { Link } = Anchor; - const getCurrentAnchor = () => '#components-anchor-demo-static'; const App: React.FC = () => ( - - - - - - - - + ); export default App; diff --git a/components/anchor/demo/legacy-anchor.md b/components/anchor/demo/legacy-anchor.md new file mode 100644 index 0000000000..775eb63223 --- /dev/null +++ b/components/anchor/demo/legacy-anchor.md @@ -0,0 +1,7 @@ +## zh-CN + +Debug usage + +## en-US + +Debug usage diff --git a/components/anchor/demo/legacy-anchor.tsx b/components/anchor/demo/legacy-anchor.tsx new file mode 100644 index 0000000000..2280a74ec9 --- /dev/null +++ b/components/anchor/demo/legacy-anchor.tsx @@ -0,0 +1,17 @@ +import React from 'react'; +import { Anchor } from 'antd'; + +const { Link } = Anchor; + +const App: React.FC = () => ( + + + + + + + + +); + +export default App; diff --git a/components/anchor/demo/onChange.tsx b/components/anchor/demo/onChange.tsx index f87d1703bb..da2f7b587e 100644 --- a/components/anchor/demo/onChange.tsx +++ b/components/anchor/demo/onChange.tsx @@ -1,21 +1,44 @@ import React from 'react'; import { Anchor } from 'antd'; -const { Link } = Anchor; - const onChange = (link: string) => { console.log('Anchor:OnChange', link); }; const App: React.FC = () => ( - - - - - - - - + ); export default App; diff --git a/components/anchor/demo/onClick.tsx b/components/anchor/demo/onClick.tsx index ddf88d865f..4f9866572d 100644 --- a/components/anchor/demo/onClick.tsx +++ b/components/anchor/demo/onClick.tsx @@ -1,8 +1,6 @@ import React from 'react'; import { Anchor } from 'antd'; -const { Link } = Anchor; - const handleClick = ( e: React.MouseEvent, link: { @@ -15,14 +13,39 @@ const handleClick = ( }; const App: React.FC = () => ( - - - - - - - - + ); export default App; diff --git a/components/anchor/demo/static.tsx b/components/anchor/demo/static.tsx index a5dabdc510..be2d34c053 100644 --- a/components/anchor/demo/static.tsx +++ b/components/anchor/demo/static.tsx @@ -1,17 +1,39 @@ import React from 'react'; import { Anchor } from 'antd'; -const { Link } = Anchor; - const App: React.FC = () => ( - - - - - - - - + ); export default App; diff --git a/components/anchor/demo/targetOffset.tsx b/components/anchor/demo/targetOffset.tsx index aaf0227267..fd69e252d3 100644 --- a/components/anchor/demo/targetOffset.tsx +++ b/components/anchor/demo/targetOffset.tsx @@ -1,8 +1,6 @@ import React, { useEffect, useState } from 'react'; import { Anchor } from 'antd'; -const { Link } = Anchor; - const App: React.FC = () => { const [targetOffset, setTargetOffset] = useState(undefined); @@ -11,14 +9,38 @@ const App: React.FC = () => { }, []); return ( - - - - - - - - + ); }; diff --git a/components/anchor/index.en-US.md b/components/anchor/index.en-US.md index d018320775..440c183356 100644 --- a/components/anchor/index.en-US.md +++ b/components/anchor/index.en-US.md @@ -28,6 +28,7 @@ For displaying anchor hyperlinks on page and jumping between them. Customize the anchor highlight Set Anchor scroll offset Listening for anchor link change +Deprecated JSX demo ## API @@ -44,6 +45,7 @@ For displaying anchor hyperlinks on page and jumping between them. | targetOffset | Anchor scroll offset, default as `offsetTop`, [example](#components-anchor-demo-targetOffset) | number | - | | | onChange | Listening for anchor link change | (currentActiveLink: string) => void | | | | onClick | Set the handler to handle `click` event | function(e: Event, link: Object) | - | | +| items | Data configuration option content, support nesting through children | { href, title, target, children }\[] | - | | ### Link Props diff --git a/components/anchor/index.zh-CN.md b/components/anchor/index.zh-CN.md index b6e9bbf083..9c1b47a99f 100644 --- a/components/anchor/index.zh-CN.md +++ b/components/anchor/index.zh-CN.md @@ -29,6 +29,7 @@ group: 自定义锚点高亮 设置锚点滚动偏移量 监听锚点链接改变 +废弃的 JSX 示例 ## API @@ -45,6 +46,7 @@ group: | targetOffset | 锚点滚动偏移量,默认与 offsetTop 相同,[例子](#components-anchor-demo-targetOffset) | number | - | | | onChange | 监听锚点链接改变 | (currentActiveLink: string) => void | - | | | onClick | `click` 事件的 handler | function(e: Event, link: Object) | - | | +| items | 数据化配置选项内容,支持通过 children 嵌套 | { href, title, target, children }\[] | - | | ### Link Props From 240210a281e1eba4f3a6f849e88232c6a722ff09 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Mon, 5 Dec 2022 14:15:26 +0800 Subject: [PATCH 6/6] feat: New Component QRCode (#38948) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: qrcode * chore: code clean * feat: New Component Qr-Code (#38891) * feat: QrCode * fix * fix * fix: fix bug * fix: fix bug * fix * fix * fix * delete * delete * test case * fix lint * bundlesize * demo * fix: fix test * remove dep * update snap * en docs * refactor: rename tests dir * Update components/qr-code/demo/base.md Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/base.md Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/download.md Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/download.md Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/download.tsx Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/logo.md Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qr-code/demo/logo.md Co-authored-by: MadCcc <1075746765@qq.com> * rename * fix * adjust text * rename * fix title * rename * rename * fix: snap * fix * bundlesize * update demo * update docs * add demo * add docs * add docs * test: add warning * update demo * bundlesize * update test case * update demo * feat: add onRefresh、add status * fix: fix demo * fix: fix demo * add locale * add locale * add test case * update snap * fix demo * update demo * update demo * update demo * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/interface.ts Co-authored-by: MadCcc <1075746765@qq.com> * Update components/qrcode/style/index.ts Co-authored-by: MadCcc <1075746765@qq.com> * fix * Update components/qrcode/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> * fix * fix * fix * add decs * fix * fix * fix type * fix * fix demo * fix lint * fix lint * add test case for bordered * prettier-ignore Co-authored-by: MadCcc <1075746765@qq.com> Co-authored-by: 栗嘉男 --- .../__snapshots__/index.test.ts.snap | 1 + components/index.tsx | 2 + components/locale-provider/index.tsx | 4 + components/locale/en_US.tsx | 4 + components/locale/zh_CN.tsx | 4 + components/mentions/index.en-US.md | 3 +- .../__snapshots__/demo-extend.test.ts.snap | 403 ++++++++++++++++++ .../__tests__/__snapshots__/demo.test.ts.snap | 363 ++++++++++++++++ .../__snapshots__/index.test.tsx.snap | 20 + .../qrcode/__tests__/demo-extend.test.ts | 3 + components/qrcode/__tests__/demo.test.ts | 3 + components/qrcode/__tests__/image.test.ts | 5 + components/qrcode/__tests__/index.test.tsx | 82 ++++ components/qrcode/demo/Popover.md | 7 + components/qrcode/demo/Popover.tsx | 12 + components/qrcode/demo/base.md | 7 + components/qrcode/demo/base.tsx | 6 + components/qrcode/demo/customColor.md | 7 + components/qrcode/demo/customColor.tsx | 24 ++ components/qrcode/demo/customSize.md | 7 + components/qrcode/demo/customSize.tsx | 48 +++ components/qrcode/demo/download.md | 7 + components/qrcode/demo/download.tsx | 26 ++ components/qrcode/demo/errorlevel.md | 7 + components/qrcode/demo/errorlevel.tsx | 19 + components/qrcode/demo/icon.md | 7 + components/qrcode/demo/icon.tsx | 11 + components/qrcode/demo/status.md | 7 + components/qrcode/demo/status.tsx | 11 + components/qrcode/index.en-US.md | 56 +++ components/qrcode/index.tsx | 92 ++++ components/qrcode/index.zh-CN.md | 57 +++ components/qrcode/interface.ts | 33 ++ components/qrcode/style/index.ts | 59 +++ components/theme/interface/components.ts | 2 + components/theme/themes/seed.ts | 14 +- components/theme/util/alias.ts | 10 +- components/theme/util/getAlphaColor.ts | 3 +- components/tour/style/index.tsx | 12 +- package.json | 3 +- tests/__snapshots__/index.test.ts.snap | 1 + 41 files changed, 1431 insertions(+), 21 deletions(-) create mode 100644 components/qrcode/__tests__/__snapshots__/demo-extend.test.ts.snap create mode 100644 components/qrcode/__tests__/__snapshots__/demo.test.ts.snap create mode 100644 components/qrcode/__tests__/__snapshots__/index.test.tsx.snap create mode 100644 components/qrcode/__tests__/demo-extend.test.ts create mode 100644 components/qrcode/__tests__/demo.test.ts create mode 100644 components/qrcode/__tests__/image.test.ts create mode 100644 components/qrcode/__tests__/index.test.tsx create mode 100644 components/qrcode/demo/Popover.md create mode 100644 components/qrcode/demo/Popover.tsx create mode 100644 components/qrcode/demo/base.md create mode 100644 components/qrcode/demo/base.tsx create mode 100644 components/qrcode/demo/customColor.md create mode 100644 components/qrcode/demo/customColor.tsx create mode 100644 components/qrcode/demo/customSize.md create mode 100644 components/qrcode/demo/customSize.tsx create mode 100644 components/qrcode/demo/download.md create mode 100644 components/qrcode/demo/download.tsx create mode 100644 components/qrcode/demo/errorlevel.md create mode 100644 components/qrcode/demo/errorlevel.tsx create mode 100644 components/qrcode/demo/icon.md create mode 100644 components/qrcode/demo/icon.tsx create mode 100644 components/qrcode/demo/status.md create mode 100644 components/qrcode/demo/status.tsx create mode 100644 components/qrcode/index.en-US.md create mode 100644 components/qrcode/index.tsx create mode 100644 components/qrcode/index.zh-CN.md create mode 100644 components/qrcode/interface.ts create mode 100644 components/qrcode/style/index.ts diff --git a/components/__tests__/__snapshots__/index.test.ts.snap b/components/__tests__/__snapshots__/index.test.ts.snap index 740ef46f1f..9364bf329b 100644 --- a/components/__tests__/__snapshots__/index.test.ts.snap +++ b/components/__tests__/__snapshots__/index.test.ts.snap @@ -40,6 +40,7 @@ exports[`antd exports modules correctly 1`] = ` "Popconfirm", "Popover", "Progress", + "QRCode", "Radio", "Rate", "Result", diff --git a/components/index.tsx b/components/index.tsx index 9043a3add8..a9848329c2 100644 --- a/components/index.tsx +++ b/components/index.tsx @@ -149,4 +149,6 @@ export { default as Typography } from './typography'; export type { TypographyProps } from './typography'; export { default as Upload } from './upload'; export type { UploadFile, UploadProps } from './upload'; +export { default as QRCode } from './qrcode'; +export type { QRCodeProps, QRPropsCanvas } from './qrcode/interface'; export { default as version } from './version'; diff --git a/components/locale-provider/index.tsx b/components/locale-provider/index.tsx index 3691222058..1c93703022 100644 --- a/components/locale-provider/index.tsx +++ b/components/locale-provider/index.tsx @@ -46,6 +46,10 @@ export interface Locale { Image?: { preview: string; }; + QRCode?: { + expired: string; + refresh: string; + }; } export interface LocaleProviderProps { diff --git a/components/locale/en_US.tsx b/components/locale/en_US.tsx index 4b87557341..080e2ebcd9 100644 --- a/components/locale/en_US.tsx +++ b/components/locale/en_US.tsx @@ -136,6 +136,10 @@ const localeValues: Locale = { Image: { preview: 'Preview', }, + QRCode: { + expired: 'QRCode is expired', + refresh: 'click refresh', + }, }; export default localeValues; diff --git a/components/locale/zh_CN.tsx b/components/locale/zh_CN.tsx index 6d155dde0a..ea97e256bf 100644 --- a/components/locale/zh_CN.tsx +++ b/components/locale/zh_CN.tsx @@ -136,6 +136,10 @@ const localeValues: Locale = { Image: { preview: '预览', }, + QRCode: { + expired: '二维码过期', + refresh: '点击刷新', + }, }; export default localeValues; diff --git a/components/mentions/index.en-US.md b/components/mentions/index.en-US.md index 638e0e5bdb..8d607c0b34 100644 --- a/components/mentions/index.en-US.md +++ b/components/mentions/index.en-US.md @@ -68,7 +68,7 @@ return ; | onResize | The callback function that is triggered when textarea resize | function({ width, height }) | - | | | onSearch | Trigger when prefix hit | (text: string, prefix: string) => void | - | | | onSelect | Trigger when user select the option | (option: OptionProps, prefix: string) => void | - | | -| options | Option Configuration | [Options](#Option) | \[] | 5.1.0 | +| options | Option Configuration | [Options](#Option) | \[] | 5.1.0 | ### Mention methods @@ -79,6 +79,7 @@ return ; ### Option + | Property | Description | Type | Default | | --- | --- | --- | --- | | label | Title of the option | React.ReactNode | - | diff --git a/components/qrcode/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/qrcode/__tests__/__snapshots__/demo-extend.test.ts.snap new file mode 100644 index 0000000000..74af4d455c --- /dev/null +++ b/components/qrcode/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -0,0 +1,403 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders ./components/qrcode/demo/Popover.tsx extend context correctly 1`] = ` +Array [ + icon, +
+
+
+
+ +
+ +
+
+
, +] +`; + +exports[`renders ./components/qrcode/demo/base.tsx extend context correctly 1`] = ` +
+ +
+`; + +exports[`renders ./components/qrcode/demo/customColor.tsx extend context correctly 1`] = ` +
+
+
+ +
+
+
+
+ +
+
+
+`; + +exports[`renders ./components/qrcode/demo/customSize.tsx extend context correctly 1`] = ` +Array [ +
+ + +
, +
+ + +
, +] +`; + +exports[`renders ./components/qrcode/demo/download.tsx extend context correctly 1`] = ` +
+
+ +
+ +
+`; + +exports[`renders ./components/qrcode/demo/errorlevel.tsx extend context correctly 1`] = ` +Array [ +
+ +
, +
+
+ + + + +
+
, +] +`; + +exports[`renders ./components/qrcode/demo/icon.tsx extend context correctly 1`] = ` +
+ + +
+`; + +exports[`renders ./components/qrcode/demo/status.tsx extend context correctly 1`] = ` +
+
+
+
+
+ + + + + + +
+
+ +
+
+
+
+
+

+ QRCode is expired +

+ +
+ +
+
+
+`; diff --git a/components/qrcode/__tests__/__snapshots__/demo.test.ts.snap b/components/qrcode/__tests__/__snapshots__/demo.test.ts.snap new file mode 100644 index 0000000000..51e8a82e70 --- /dev/null +++ b/components/qrcode/__tests__/__snapshots__/demo.test.ts.snap @@ -0,0 +1,363 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders ./components/qrcode/demo/Popover.tsx correctly 1`] = ` +icon +`; + +exports[`renders ./components/qrcode/demo/base.tsx correctly 1`] = ` +
+ +
+`; + +exports[`renders ./components/qrcode/demo/customColor.tsx correctly 1`] = ` +
+
+
+ +
+
+
+
+ +
+
+
+`; + +exports[`renders ./components/qrcode/demo/customSize.tsx correctly 1`] = ` +Array [ +
+ + +
, +
+ + +
, +] +`; + +exports[`renders ./components/qrcode/demo/download.tsx correctly 1`] = ` +
+
+ +
+ +
+`; + +exports[`renders ./components/qrcode/demo/errorlevel.tsx correctly 1`] = ` +Array [ +
+ +
, +
+
+ + + + +
+
, +] +`; + +exports[`renders ./components/qrcode/demo/icon.tsx correctly 1`] = ` +
+ + +
+`; + +exports[`renders ./components/qrcode/demo/status.tsx correctly 1`] = ` +
+
+
+
+
+ + + + + + +
+
+ +
+
+
+
+
+

+ QRCode is expired +

+ +
+ +
+
+
+`; diff --git a/components/qrcode/__tests__/__snapshots__/index.test.tsx.snap b/components/qrcode/__tests__/__snapshots__/index.test.tsx.snap new file mode 100644 index 0000000000..6dcb91c4b3 --- /dev/null +++ b/components/qrcode/__tests__/__snapshots__/index.test.tsx.snap @@ -0,0 +1,20 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`QRCode test rtl render component should be rendered correctly in RTL direction 1`] = `null`; + +exports[`QRCode test should correct render 1`] = ` +
+
+ +
+
+`; + +exports[`QRCode test should render \`null\` and console Error when value not exist 1`] = `null`; diff --git a/components/qrcode/__tests__/demo-extend.test.ts b/components/qrcode/__tests__/demo-extend.test.ts new file mode 100644 index 0000000000..79dea1e4fa --- /dev/null +++ b/components/qrcode/__tests__/demo-extend.test.ts @@ -0,0 +1,3 @@ +import { extendTest } from '../../../tests/shared/demoTest'; + +extendTest('qrcode'); diff --git a/components/qrcode/__tests__/demo.test.ts b/components/qrcode/__tests__/demo.test.ts new file mode 100644 index 0000000000..1cb1d77dd4 --- /dev/null +++ b/components/qrcode/__tests__/demo.test.ts @@ -0,0 +1,3 @@ +import demoTest from '../../../tests/shared/demoTest'; + +demoTest('qrcode'); diff --git a/components/qrcode/__tests__/image.test.ts b/components/qrcode/__tests__/image.test.ts new file mode 100644 index 0000000000..df82d405e5 --- /dev/null +++ b/components/qrcode/__tests__/image.test.ts @@ -0,0 +1,5 @@ +import { imageDemoTest } from '../../../tests/shared/imageTest'; + +describe('QRCode image', () => { + imageDemoTest('qrcode'); +}); diff --git a/components/qrcode/__tests__/index.test.tsx b/components/qrcode/__tests__/index.test.tsx new file mode 100644 index 0000000000..6de938dab5 --- /dev/null +++ b/components/qrcode/__tests__/index.test.tsx @@ -0,0 +1,82 @@ +import React, { useState } from 'react'; +import QRCode from '..'; +import mountTest from '../../../tests/shared/mountTest'; +import rtlTest from '../../../tests/shared/rtlTest'; +import { fireEvent, render } from '../../../tests/utils'; +import type { QRCodeProps } from '../interface'; + +describe('QRCode test', () => { + mountTest(QRCode); + rtlTest(QRCode); + + it('should correct render', () => { + const { container } = render(); + expect( + container + ?.querySelector('.ant-qrcode') + ?.querySelector('canvas'), + ).toBeTruthy(); + expect(container).toMatchSnapshot(); + }); + + it('should render `null` and console Error when value not exist', () => { + const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + const { container } = render(); + expect(container.firstChild).toBe(null); + expect(container.firstChild).toMatchSnapshot(); + expect(errSpy).toHaveBeenCalledWith('Warning: [antd: QRCode] need to receive `value` props'); + errSpy.mockRestore(); + }); + + it('support custom icon', () => { + const { container } = render(); + expect( + container + ?.querySelector('.ant-qrcode') + ?.querySelector('img'), + ).toBeTruthy(); + }); + + it('support custom size', () => { + const { container } = render(); + const wapper = container.querySelector('.ant-qrcode'); + expect(wapper?.style?.width).toBe('100px'); + expect(wapper?.style?.height).toBe('100px'); + }); + + it('support refresh', () => { + const refresh = jest.fn(); + const { container } = render(); + fireEvent.click( + container + ?.querySelector('.ant-qrcode') + ?.querySelector('button.ant-btn-link')!, + ); + expect(refresh).toHaveBeenCalled(); + }); + + it('support loading', () => { + const Demo: React.FC = () => { + const [status, setStatus] = useState('active'); + return ( + <> + + + + ); + }; + const { container } = render(); + expect(container.querySelector('.ant-spin-spinning')).toBeFalsy(); + fireEvent.click(container?.querySelector('button')!); + expect(container.querySelector('.ant-spin-spinning')).toBeTruthy(); + }); + + it('support bordered', () => { + const { container } = render(); + expect(container?.querySelector('.ant-qrcode')).toHaveClass( + 'ant-qrcode-borderless', + ); + }); +}); diff --git a/components/qrcode/demo/Popover.md b/components/qrcode/demo/Popover.md new file mode 100644 index 0000000000..48c076f08c --- /dev/null +++ b/components/qrcode/demo/Popover.md @@ -0,0 +1,7 @@ +## zh-CN + +带气泡卡片的例子。 + +## en-US + +With Popover. diff --git a/components/qrcode/demo/Popover.tsx b/components/qrcode/demo/Popover.tsx new file mode 100644 index 0000000000..342e1505d4 --- /dev/null +++ b/components/qrcode/demo/Popover.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import { QRCode, Popover } from 'antd'; + +const src = 'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg'; + +const App: React.FC = () => ( + }> + icon + +); + +export default App; diff --git a/components/qrcode/demo/base.md b/components/qrcode/demo/base.md new file mode 100644 index 0000000000..d7c5bd3ef0 --- /dev/null +++ b/components/qrcode/demo/base.md @@ -0,0 +1,7 @@ +## zh-CN + +基本用法。 + +## en-US + +Basic Usage. diff --git a/components/qrcode/demo/base.tsx b/components/qrcode/demo/base.tsx new file mode 100644 index 0000000000..5092f0f86e --- /dev/null +++ b/components/qrcode/demo/base.tsx @@ -0,0 +1,6 @@ +import React from 'react'; +import { QRCode } from 'antd'; + +const App: React.FC = () => ; + +export default App; diff --git a/components/qrcode/demo/customColor.md b/components/qrcode/demo/customColor.md new file mode 100644 index 0000000000..f682498030 --- /dev/null +++ b/components/qrcode/demo/customColor.md @@ -0,0 +1,7 @@ +## zh-CN + +通过设置 `color` 自定义二维码颜色,通过设置 `style` 自定义背景颜色。 + +## en-US + +Custom Color. diff --git a/components/qrcode/demo/customColor.tsx b/components/qrcode/demo/customColor.tsx new file mode 100644 index 0000000000..8b61184614 --- /dev/null +++ b/components/qrcode/demo/customColor.tsx @@ -0,0 +1,24 @@ +import React from 'react'; +import { QRCode, Space, theme } from 'antd'; + +const { useToken } = theme; + +const App: React.FC = () => { + const { token } = useToken(); + return ( + + + + + ); +}; + +export default App; diff --git a/components/qrcode/demo/customSize.md b/components/qrcode/demo/customSize.md new file mode 100644 index 0000000000..08e60002d8 --- /dev/null +++ b/components/qrcode/demo/customSize.md @@ -0,0 +1,7 @@ +## zh-CN + +自定义尺寸 + +## en-US + +Custom Size. diff --git a/components/qrcode/demo/customSize.tsx b/components/qrcode/demo/customSize.tsx new file mode 100644 index 0000000000..35b06fff45 --- /dev/null +++ b/components/qrcode/demo/customSize.tsx @@ -0,0 +1,48 @@ +import React, { useState } from 'react'; +import { MinusOutlined, PlusOutlined } from '@ant-design/icons'; +import { QRCode, Button } from 'antd'; + +const App: React.FC = () => { + const [size, setSize] = useState(160); + + const increase = () => { + setSize((prevSize) => { + const newSize = prevSize + 10; + if (newSize > 300) { + return 300; + } + return newSize; + }); + }; + + const decline = () => { + setSize((prevSize) => { + const newSize = prevSize - 10; + if (newSize < 48) { + return 48; + } + return newSize; + }); + }; + + return ( + <> + + + + + + + ); +}; + +export default App; diff --git a/components/qrcode/demo/download.md b/components/qrcode/demo/download.md new file mode 100644 index 0000000000..9967cf7fa2 --- /dev/null +++ b/components/qrcode/demo/download.md @@ -0,0 +1,7 @@ +## zh-CN + +下载二维码的简单实现。 + +## en-US + +A way to download QRCode. diff --git a/components/qrcode/demo/download.tsx b/components/qrcode/demo/download.tsx new file mode 100644 index 0000000000..c66ce361cc --- /dev/null +++ b/components/qrcode/demo/download.tsx @@ -0,0 +1,26 @@ +import React from 'react'; +import { QRCode, Button } from 'antd'; + +const downloadQRCode = () => { + const canvas = document.getElementById('myqrcode')?.querySelector('canvas'); + if (canvas) { + const url = canvas.toDataURL(); + const a = document.createElement('a'); + a.download = 'QRCode.png'; + a.href = url; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + } +}; + +const App: React.FC = () => ( +
+ + +
+); + +export default App; diff --git a/components/qrcode/demo/errorlevel.md b/components/qrcode/demo/errorlevel.md new file mode 100644 index 0000000000..1df7f09c46 --- /dev/null +++ b/components/qrcode/demo/errorlevel.md @@ -0,0 +1,7 @@ +## zh-CN + +通过设置 errorLevel 调整不同的容错等级。 + +## en-US + +set Error Level. diff --git a/components/qrcode/demo/errorlevel.tsx b/components/qrcode/demo/errorlevel.tsx new file mode 100644 index 0000000000..0995a120a1 --- /dev/null +++ b/components/qrcode/demo/errorlevel.tsx @@ -0,0 +1,19 @@ +import React, { useState } from 'react'; +import type { QRCodeProps } from 'antd'; +import { Segmented, QRCode } from 'antd'; + +const App: React.FC = () => { + const [level, setLevel] = useState('L'); + return ( + <> + + + + ); +}; + +export default App; diff --git a/components/qrcode/demo/icon.md b/components/qrcode/demo/icon.md new file mode 100644 index 0000000000..05a34f1d8c --- /dev/null +++ b/components/qrcode/demo/icon.md @@ -0,0 +1,7 @@ +## zh-CN + +带 Icon 的二维码。 + +## en-US + +QRCode with Icon. diff --git a/components/qrcode/demo/icon.tsx b/components/qrcode/demo/icon.tsx new file mode 100644 index 0000000000..77a6218592 --- /dev/null +++ b/components/qrcode/demo/icon.tsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { QRCode } from 'antd'; + +const App: React.FC = () => ( + +); + +export default App; diff --git a/components/qrcode/demo/status.md b/components/qrcode/demo/status.md new file mode 100644 index 0000000000..bc25a6993a --- /dev/null +++ b/components/qrcode/demo/status.md @@ -0,0 +1,7 @@ +## zh-CN + +可以通过 `status` 的值控制二维码的状态。 + +## en-US + +The status can be controlled by the value `status`. diff --git a/components/qrcode/demo/status.tsx b/components/qrcode/demo/status.tsx new file mode 100644 index 0000000000..3f0a57bca6 --- /dev/null +++ b/components/qrcode/demo/status.tsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { QRCode, Space } from 'antd'; + +const App: React.FC = () => ( + + + console.log('refresh')} /> + +); + +export default App; diff --git a/components/qrcode/index.en-US.md b/components/qrcode/index.en-US.md new file mode 100644 index 0000000000..63c2df1a6c --- /dev/null +++ b/components/qrcode/index.en-US.md @@ -0,0 +1,56 @@ +--- +category: Components +title: QRCode +cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*cJopQrf0ncwAAAAAAAAAAAAADrJ8AQ/original +demo: + cols: 2 +group: + title: Data Display + order: 5 +--- + +Components that can convert links into QR codes, and support custom color and logo. Available since `antd@5.1.0`. + + + +## When To Use + +Used when the link needs to be converted into a QR Code. + +## Examples + + +base +With Icon +other statu +Custom Size +Custom Color +Download QRCode +Error Level +Advanced Usage + +## API + +> This component is available since `antd@5.1.0` + +| Property | Description | Type | Default | +| :-- | :-- | :-- | :-- | +| value | scanned link | string | - | +| icon | include image url (only image link are supported) | string | - | +| size | QRCode size | number | 128 | +| iconSize | include image size | number | 32 | +| color | QRCode Color | string | `#000` | +| bordered | Whether has border style | boolean | `true` | +| errorLevel | Error Code Level | `'L' \| 'M' \| 'Q' \| 'H' ` | `M` | +| status | QRCode statu | `active \| expired \| loading ` | `active` | +| onRefresh | callback | `() => void` | - | + +## FAQ + +### About QRCode ErrorLevel + +The ErrorLevel means that the QR code can be scanned normally after being blocked, and the maximum area that can be blocked is the error correction rate. + +Generally, the QR code is divided into 4 error correction levels: Level `L` can correct about `7%` errors, Level `M` can correct about `15%` errors, Level `Q` can correct about `25%` errors, and Level `H` can correct about `30%` errors. When the content encoding of the QR code carries less information, in other words, when the value link is short, set different error correction levels, and the generated image will not change. + +> For more information, see the: [https://www.qrcode.com/en/about/error_correction](https://www.qrcode.com/en/about/error_correction.html) diff --git a/components/qrcode/index.tsx b/components/qrcode/index.tsx new file mode 100644 index 0000000000..eec3f9e27f --- /dev/null +++ b/components/qrcode/index.tsx @@ -0,0 +1,92 @@ +import React, { useMemo, useContext } from 'react'; +import { QRCodeCanvas } from 'qrcode.react'; +import classNames from 'classnames'; +import { ReloadOutlined } from '@ant-design/icons'; +import { ConfigContext } from '../config-provider'; +import LocaleReceiver from '../locale-provider/LocaleReceiver'; +import type { ConfigConsumerProps } from '../config-provider'; +import type { QRCodeProps, QRPropsCanvas } from './interface'; +import warning from '../_util/warning'; +import useStyle from './style/index'; +import Spin from '../spin'; +import Button from '../button'; +import theme from '../theme'; + +const { useToken } = theme; + +const QRCode: React.FC = (props) => { + const { + value, + icon = '', + size = 160, + iconSize = 40, + color = '#000', + errorLevel = 'M', + status = 'active', + bordered = true, + onRefresh, + style, + className, + prefixCls: customizePrefixCls, + } = props; + const { getPrefixCls } = useContext(ConfigContext); + const prefixCls = getPrefixCls('qrcode', customizePrefixCls); + const [wrapSSR, hashId] = useStyle(prefixCls); + const { token } = useToken(); + const qrCodeProps = useMemo(() => { + const imageSettings: QRCodeProps['imageSettings'] = { + src: icon, + x: undefined, + y: undefined, + height: iconSize, + width: iconSize, + excavate: true, + }; + return { + value, + size: size - (token.paddingSM + token.lineWidth) * 2, + level: errorLevel, + bgColor: 'transparent', + fgColor: color, + imageSettings: icon ? imageSettings : undefined, + }; + }, [errorLevel, color, icon, iconSize, size, value]); + + if (!value) { + if (process.env.NODE_ENV !== 'production') { + warning(false, 'QRCode', 'need to receive `value` props'); + } + return null; + } + + const cls = classNames(prefixCls, className, hashId, { + [`${prefixCls}-borderless`]: !bordered, + }); + + return wrapSSR( + + {(locale) => ( +
+ {status !== 'active' && ( +
+ {status === 'loading' && } + {status === 'expired' && ( + <> +

{locale.expired}

+ {typeof onRefresh === 'function' && ( + + )} + + )} +
+ )} + +
+ )} +
, + ); +}; + +export default QRCode; diff --git a/components/qrcode/index.zh-CN.md b/components/qrcode/index.zh-CN.md new file mode 100644 index 0000000000..16d2d74025 --- /dev/null +++ b/components/qrcode/index.zh-CN.md @@ -0,0 +1,57 @@ +--- +category: Components +subtitle: 二维码 +title: QRCode +cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*cJopQrf0ncwAAAAAAAAAAAAADrJ8AQ/original +demo: + cols: 2 +group: + title: 数据展示 + order: 5 +--- + +能够将链接转换生成二维码的组件,支持自定义配色和 Logo 配置,自 `antd@5.1.0` 版本开始提供该组件。 + + + +## 何时使用 + +当需要将链接转换成为二维码时使用。 + +## 代码演示 + + +基本使用 +带 Icon 的例子 +不同的状态 +自定义尺寸 +自定义颜色 +下载二维码 +纠错比例 +高级用法 + +## API + +> 自 `antd@5.1.0` 版本开始提供该组件。 + +| 参数 | 说明 | 类型 | 默认值 | +| :-- | :-- | :-- | :-- | +| value | 扫描后的地址 | string | - | +| icon | 二维码中图片的地址(目前只支持图片地址) | string | - | +| size | 二维码大小 | number | 160 | +| iconSize | 二维码中图片的大小 | number | 40 | +| color | 二维码颜色 | string | `#000` | +| bordered | 是否有边框 | boolean | `true` | +| errorLevel | 二维码纠错等级 | `'L' \| 'M' \| 'Q' \| 'H' ` | `M` | +| status | 二维码状态 | `active \| expired \| loading ` | `active` | +| onRefresh | 点击"点击刷新"的回调 | `() => void` | - | + +## FAQ + +### 关于二维码纠错等级 + +纠错等级也叫纠错率,就是指二维码可以被遮挡后还能正常扫描,而这个能被遮挡的最大面积就是纠错率。 + +通常情况下二维码分为 4 个纠错级别:`L级` 可纠正约 `7%` 错误、`M级` 可纠正约 `15%` 错误、`Q级` 可纠正约 `25%` 错误、`H级` 可纠正约`30%` 错误。并不是所有位置都可以缺损,像最明显的三个角上的方框,直接影响初始定位。中间零散的部分是内容编码,可以容忍缺损。当二维码的内容编码携带信息比较少的时候,也就是链接比较短的时候,设置不同的纠错等级,生成的图片不会发生变化。 + +> 有关更多信息,可参阅相关资料:[https://www.qrcode.com/zh/about/error_correction](https://www.qrcode.com/zh/about/error_correction.html) diff --git a/components/qrcode/interface.ts b/components/qrcode/interface.ts new file mode 100644 index 0000000000..3371e02035 --- /dev/null +++ b/components/qrcode/interface.ts @@ -0,0 +1,33 @@ +import type { CSSProperties } from 'react'; + +interface ImageSettings { + src: string; + height: number; + width: number; + excavate: boolean; + x?: number; + y?: number; +} + +interface QRProps { + value: string; + size?: number; + level?: string; + color?: string; + style?: CSSProperties; + includeMargin?: boolean; + imageSettings?: ImageSettings; +} + +export type QRPropsCanvas = QRProps & React.CanvasHTMLAttributes; + +export interface QRCodeProps extends QRProps { + className?: string; + prefixCls?: string; + icon?: string; + iconSize?: number; + bordered?: boolean; + errorLevel?: 'L' | 'M' | 'Q' | 'H'; + status?: 'active' | 'expired' | 'loading'; + onRefresh?: () => void; +} diff --git a/components/qrcode/style/index.ts b/components/qrcode/style/index.ts new file mode 100644 index 0000000000..0cb4e323d7 --- /dev/null +++ b/components/qrcode/style/index.ts @@ -0,0 +1,59 @@ +import type { FullToken, GenerateStyle } from '../../theme/internal'; +import { mergeToken, genComponentStyleHook } from '../../theme/internal'; +import { resetComponent } from '../../style'; + +export interface ComponentToken {} + +interface QRCodeToken extends FullToken<'QRCode'> { + QRCodeMaskBackgroundColor: string; +} + +const genQRCodeStyle: GenerateStyle = (token) => { + const { componentCls } = token; + return { + [componentCls]: { + ...resetComponent(token), + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + padding: token.paddingSM, + borderRadius: token.borderRadiusLG, + border: `${token.lineWidth}px ${token.lineType} ${token.colorSplit}`, + position: 'relative', + width: '100%', + height: '100%', + overflow: 'hidden', + [`& > ${componentCls}-mask`]: { + position: 'absolute', + insetBlockStart: 0, + insetInlineStart: 0, + zIndex: 10, + display: 'flex', + flexDirection: 'column', + justifyContent: 'center', + alignItems: 'center', + width: '100%', + height: '100%', + color: token.colorText, + lineHeight: token.lineHeight, + background: token.QRCodeMaskBackgroundColor, + textAlign: 'center', + }, + '&-icon': { + marginBlockEnd: token.marginXS, + fontSize: token.controlHeight, + }, + }, + [`${componentCls}-borderless`]: { + borderColor: 'transparent', + }, + }; +}; + +export default genComponentStyleHook<'QRCode'>('QRCode', (token) => + genQRCodeStyle( + mergeToken(token, { + QRCodeMaskBackgroundColor: 'rgba(255, 255, 255, 0.96)', + }), + ), +); diff --git a/components/theme/interface/components.ts b/components/theme/interface/components.ts index 2e4a427f56..5ac9468398 100644 --- a/components/theme/interface/components.ts +++ b/components/theme/interface/components.ts @@ -46,6 +46,7 @@ import type { ComponentToken as TransferComponentToken } from '../../transfer/st import type { ComponentToken as TypographyComponentToken } from '../../typography/style'; import type { ComponentToken as UploadComponentToken } from '../../upload/style'; import type { ComponentToken as TourComponentToken } from '../../tour/style'; +import type { ComponentToken as QRCodeComponentToken } from '../../qrcode/style'; export interface ComponentTokenMap { Affix?: {}; @@ -108,4 +109,5 @@ export interface ComponentTokenMap { Space?: SpaceComponentToken; Progress?: ProgressComponentToken; Tour?: TourComponentToken; + QRCode?: QRCodeComponentToken; } diff --git a/components/theme/themes/seed.ts b/components/theme/themes/seed.ts index 032bded2b7..2abdf240e8 100644 --- a/components/theme/themes/seed.ts +++ b/components/theme/themes/seed.ts @@ -43,14 +43,14 @@ const seedToken: SeedToken = { // Motion motionUnit: 0.1, motionBase: 0, - motionEaseOutCirc: `cubic-bezier(0.08, 0.82, 0.17, 1)`, - motionEaseInOutCirc: `cubic-bezier(0.78, 0.14, 0.15, 0.86)`, + motionEaseOutCirc: 'cubic-bezier(0.08, 0.82, 0.17, 1)', + motionEaseInOutCirc: 'cubic-bezier(0.78, 0.14, 0.15, 0.86)', motionEaseOut: 'cubic-bezier(0.215, 0.61, 0.355, 1)', - motionEaseInOut: `cubic-bezier(0.645, 0.045, 0.355, 1)`, - motionEaseOutBack: `cubic-bezier(0.12, 0.4, 0.29, 1.46)`, - motionEaseInBack: `cubic-bezier(0.71, -0.46, 0.88, 0.6)`, - motionEaseInQuint: `cubic-bezier(0.645, 0.045, 0.355, 1)`, - motionEaseOutQuint: `cubic-bezier(0.23, 1, 0.32, 1)`, + motionEaseInOut: 'cubic-bezier(0.645, 0.045, 0.355, 1)', + motionEaseOutBack: 'cubic-bezier(0.12, 0.4, 0.29, 1.46)', + motionEaseInBack: 'cubic-bezier(0.71, -0.46, 0.88, 0.6)', + motionEaseInQuint: 'cubic-bezier(0.645, 0.045, 0.355, 1)', + motionEaseOutQuint: 'cubic-bezier(0.23, 1, 0.32, 1)', // Radius borderRadius: 6, diff --git a/components/theme/util/alias.ts b/components/theme/util/alias.ts index 0a45b6db69..fee69167ce 100644 --- a/components/theme/util/alias.ts +++ b/components/theme/util/alias.ts @@ -175,7 +175,7 @@ export default function formatToken(derivativeToken: RawMergedToken): AliasToken screenXXLMin: screenXXL, // FIXME: component box-shadow, should be removed - boxShadowPopoverArrow: `3px 3px 7px rgba(0, 0, 0, 0.1)`, + boxShadowPopoverArrow: '3px 3px 7px rgba(0, 0, 0, 0.1)', boxShadowCard: ` 0 1px 2px -2px ${new TinyColor('rgba(0, 0, 0, 0.16)').toRgbString()}, 0 3px 6px 0 ${new TinyColor('rgba(0, 0, 0, 0.12)').toRgbString()}, @@ -201,10 +201,10 @@ export default function formatToken(derivativeToken: RawMergedToken): AliasToken 0 -3px 6px -4px rgba(0, 0, 0, 0.12), 0 -9px 28px 8px rgba(0, 0, 0, 0.05) `, - boxShadowTabsOverflowLeft: `inset 10px 0 8px -8px rgba(0, 0, 0, 0.08)`, - boxShadowTabsOverflowRight: `inset -10px 0 8px -8px rgba(0, 0, 0, 0.08)`, - boxShadowTabsOverflowTop: `inset 0 10px 8px -8px rgba(0, 0, 0, 0.08)`, - boxShadowTabsOverflowBottom: `inset 0 -10px 8px -8px rgba(0, 0, 0, 0.08)`, + boxShadowTabsOverflowLeft: 'inset 10px 0 8px -8px rgba(0, 0, 0, 0.08)', + boxShadowTabsOverflowRight: 'inset -10px 0 8px -8px rgba(0, 0, 0, 0.08)', + boxShadowTabsOverflowTop: 'inset 0 10px 8px -8px rgba(0, 0, 0, 0.08)', + boxShadowTabsOverflowBottom: 'inset 0 -10px 8px -8px rgba(0, 0, 0, 0.08)', // Override AliasToken ...overrideTokens, diff --git a/components/theme/util/getAlphaColor.ts b/components/theme/util/getAlphaColor.ts index a0adc212f9..7cd1d3fdbb 100644 --- a/components/theme/util/getAlphaColor.ts +++ b/components/theme/util/getAlphaColor.ts @@ -16,8 +16,9 @@ function getAlphaColor(frontColor: string, backgroundColor: string): string { const r = Math.round((fR - bR * (1 - fA)) / fA); const g = Math.round((fG - bG * (1 - fA)) / fA); const b = Math.round((fB - bB * (1 - fA)) / fA); - if (isStableColor(r) && isStableColor(g) && isStableColor(b)) + if (isStableColor(r) && isStableColor(g) && isStableColor(b)) { return new TinyColor({ r, g, b, a: Math.round(fA * 100) / 100 }).toRgbString(); + } } // fallback diff --git a/components/tour/style/index.tsx b/components/tour/style/index.tsx index 3a892c2261..c32d4d93fc 100644 --- a/components/tour/style/index.tsx +++ b/components/tour/style/index.tsx @@ -209,12 +209,12 @@ const genBaseStyle: GenerateStyle = (token) => { // =========== Limit left and right placement radius ============== [[ - `&-placement-left`, - `&-placement-leftTop`, - `&-placement-leftBottom`, - `&-placement-right`, - `&-placement-rightTop`, - `&-placement-rightBottom`, + '&-placement-left', + '&-placement-leftTop', + '&-placement-leftBottom', + '&-placement-right', + '&-placement-rightTop', + '&-placement-rightBottom', ].join(',')]: { [`${componentCls}-inner`]: { borderRadius: diff --git a/package.json b/package.json index b492e7a83b..dcf8025a96 100644 --- a/package.json +++ b/package.json @@ -120,6 +120,7 @@ "copy-to-clipboard": "^3.2.0", "dayjs": "^1.11.1", "lodash": "^4.17.21", + "qrcode.react": "^3.1.0", "rc-cascader": "~3.7.0", "rc-checkbox": "~2.3.0", "rc-collapse": "~3.4.2", @@ -322,7 +323,7 @@ "bundlesize": [ { "path": "./dist/antd.min.js", - "maxSize": "377.5 kB" + "maxSize": "381 kB" } ], "tnpm": { diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index b867fa54bc..6d5e6564cf 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -40,6 +40,7 @@ exports[`antd dist files exports modules correctly 1`] = ` "Popconfirm", "Popover", "Progress", + "QRCode", "Radio", "Rate", "Result",