From 6a7945d2fd2d35cd9c9aa17f5e6c0d36f7342c71 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Thu, 11 Apr 2024 15:29:10 +0800 Subject: [PATCH 1/4] feat: Button support autoInsertSpace prop (#48348) * feat: Button support autoInsertSpaceInButton prop * demo: update demo * fix: fix * test: add test case * docs: update docs * chore: deprecated autoInsertSpaceInButton prop * Update components/button/button.tsx Co-authored-by: MadCcc Signed-off-by: lijianan <574980606@qq.com> * fix: fix * fix: fix * Update components/button/button.tsx Co-authored-by: MadCcc Signed-off-by: lijianan <574980606@qq.com> * fix: fix * Update components/button/button.tsx Signed-off-by: lijianan <574980606@qq.com> * fix: fix * fix: fix --------- Signed-off-by: lijianan <574980606@qq.com> Co-authored-by: MadCcc --- .../__snapshots__/demo-extend.test.ts.snap | 25 +++++++++++++++++++ .../__tests__/__snapshots__/demo.test.ts.snap | 23 +++++++++++++++++ components/button/__tests__/index.test.tsx | 6 +++++ components/button/button.tsx | 15 ++++++----- components/button/demo/noSpace.md | 7 ++++++ components/button/demo/noSpace.tsx | 15 +++++++++++ components/button/index.en-US.md | 15 ++--------- components/button/index.zh-CN.md | 16 +++--------- .../config-provider/__tests__/index.test.tsx | 12 ++++++++- .../__tests__/useConfig.test.tsx | 15 +++++++++++ components/config-provider/context.ts | 4 ++- components/config-provider/index.en-US.md | 3 +-- components/config-provider/index.tsx | 20 ++++++++++++++- components/config-provider/index.zh-CN.md | 3 +-- 14 files changed, 140 insertions(+), 39 deletions(-) create mode 100644 components/button/demo/noSpace.md create mode 100644 components/button/demo/noSpace.tsx diff --git a/components/button/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/button/__tests__/__snapshots__/demo-extend.test.ts.snap index d94a8373dcf0..49edf12208e9 100644 --- a/components/button/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/button/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -2776,6 +2776,31 @@ exports[`renders components/button/demo/multiple.tsx extend context correctly 1` exports[`renders components/button/demo/multiple.tsx extend context correctly 2`] = `[]`; +exports[`renders components/button/demo/noSpace.tsx extend context correctly 1`] = ` +
+ + +
+`; + +exports[`renders components/button/demo/noSpace.tsx extend context correctly 2`] = `[]`; + exports[`renders components/button/demo/size.tsx extend context correctly 1`] = ` Array [
`; +exports[`renders components/button/demo/noSpace.tsx correctly 1`] = ` +
+ + +
+`; + exports[`renders components/button/demo/size.tsx correctly 1`] = ` Array [
{ const { container } = render(); + expect(container.querySelector('button')?.textContent).toBe(text); + }); }); diff --git a/components/button/button.tsx b/components/button/button.tsx index 4a62cc6ef89e..cdd709050088 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -51,6 +51,7 @@ type MergedHTMLAttributes = Omit< export interface ButtonProps extends BaseButtonProps, MergedHTMLAttributes { href?: string; htmlType?: ButtonHTMLType; + autoInsertSpace?: boolean; } type LoadingConfigType = { @@ -98,6 +99,7 @@ const InternalCompoundedButton = React.forwardRef< htmlType = 'button', classNames: customClassNames, style: customStyle = {}, + autoInsertSpace, ...rest } = props; @@ -105,7 +107,10 @@ const InternalCompoundedButton = React.forwardRef< // Compatible with original `type` behavior const mergedType = type || 'default'; - const { getPrefixCls, autoInsertSpaceInButton, direction, button } = useContext(ConfigContext); + const { getPrefixCls, direction, button } = useContext(ConfigContext); + + const mergedInsertSpace = autoInsertSpace ?? button?.autoInsertSpace ?? true; + const prefixCls = getPrefixCls('btn', customizePrefixCls); const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls); @@ -151,7 +156,7 @@ const InternalCompoundedButton = React.forwardRef< useEffect(() => { // FIXME: for HOC usage like - if (!buttonRef || !(buttonRef as any).current || autoInsertSpaceInButton === false) { + if (!buttonRef || !(buttonRef as any).current || !mergedInsertSpace) { return; } const buttonText = (buttonRef as any).current.textContent; @@ -190,7 +195,6 @@ const InternalCompoundedButton = React.forwardRef< ); } - const autoInsertSpace = autoInsertSpaceInButton !== false; const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction); const sizeClassNameMap = { large: 'lg', small: 'sm', middle: undefined }; @@ -214,7 +218,7 @@ const InternalCompoundedButton = React.forwardRef< [`${prefixCls}-icon-only`]: !children && children !== 0 && !!iconType, [`${prefixCls}-background-ghost`]: ghost && !isUnBorderedButtonType(mergedType), [`${prefixCls}-loading`]: innerLoading, - [`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace && !innerLoading, + [`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && mergedInsertSpace && !innerLoading, [`${prefixCls}-block`]: block, [`${prefixCls}-dangerous`]: !!danger, [`${prefixCls}-rtl`]: direction === 'rtl', @@ -252,12 +256,11 @@ const InternalCompoundedButton = React.forwardRef< ); const kids = - children || children === 0 ? spaceChildren(children, needInserted && autoInsertSpace) : null; + children || children === 0 ? spaceChildren(children, needInserted && mergedInsertSpace) : null; const genButtonContent = (iconComponent: React.ReactNode, kidsComponent: React.ReactNode) => { const isRTL = direction === 'rtl'; const iconFirst = (iconPosition === 'start' && !isRTL) || (iconPosition === 'end' && isRTL); - return ( <> {iconFirst ? iconComponent : kidsComponent} diff --git a/components/button/demo/noSpace.md b/components/button/demo/noSpace.md new file mode 100644 index 000000000000..8a5d3a85706d --- /dev/null +++ b/components/button/demo/noSpace.md @@ -0,0 +1,7 @@ +## zh-CN + +我们默认在两个汉字之间添加空格,可以通过设置 `autoInsertSpace` 为 `false` 关闭。 + +## en-US + +We add a space between two Chinese characters by default, which can be removed by setting `autoInsertSpace` to `false`. diff --git a/components/button/demo/noSpace.tsx b/components/button/demo/noSpace.tsx new file mode 100644 index 000000000000..9eeab746b235 --- /dev/null +++ b/components/button/demo/noSpace.tsx @@ -0,0 +1,15 @@ +import React from 'react'; +import { Button, Flex } from 'antd'; + +const App: React.FC = () => ( + + + + +); + +export default App; diff --git a/components/button/index.en-US.md b/components/button/index.en-US.md index dcf9f4a60c18..f250d3b14cbe 100644 --- a/components/button/index.en-US.md +++ b/components/button/index.en-US.md @@ -58,6 +58,7 @@ Different button styles can be generated by setting Button properties. The recom | Property | Description | Type | Default | Version | | --- | --- | --- | --- | --- | +| autoInsertSpace | We add a space between two Chinese characters by default, which can be removed by setting `autoInsertSpace` to `false`. | boolean | `true` | 5.17.0 | | block | Option to fit button width to its parent width | boolean | false | | | classNames | Semantic DOM class | [Record](#semantic-dom) | - | 5.4.0 | | danger | Set the danger status of button | boolean | false | | @@ -73,7 +74,7 @@ Different button styles can be generated by setting Button properties. The recom | styles | Semantic DOM style | [Record](#semantic-dom) | - | 5.4.0 | | target | Same as target attribute of a, works when href is specified | string | - | | | type | Set button type | `primary` \| `dashed` \| `link` \| `text` \| `default` | `default` | | -| onClick | Set the handler to handle `click` event | (event: MouseEvent) => void | - | | +| onClick | Set the handler to handle `click` event | (event: React.MouseEvent) => void | - | | It accepts all props which native buttons support. @@ -97,18 +98,6 @@ If you don't need this feature, you can set `disabled` of `wave` in [ConfigProvi ``` -### How to remove space between 2 chinese characters? - -Following the Ant Design specification, we will add one space between if Button (exclude Text button and Link button) contains two Chinese characters only. If you don't need that, you can use [ConfigProvider](/components/config-provider/#api) to set `autoInsertSpaceInButton` as `false`. - -```jsx - - - -``` - -Button with two Chinese characters -