From 1517fcbbd6155147d91682416253cd6aa9cc2f38 Mon Sep 17 00:00:00 2001 From: zombieJ Date: Fri, 17 May 2019 12:04:47 +0800 Subject: [PATCH] fix space missing in button (#16551) --- .../__snapshots__/index.test.js.snap | 11 ++++++++ components/button/__tests__/index.test.js | 11 ++++++++ components/button/button.tsx | 27 ++++++++++++++++--- .../__tests__/__snapshots__/demo.test.js.snap | 5 +--- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/components/button/__tests__/__snapshots__/index.test.js.snap b/components/button/__tests__/__snapshots__/index.test.js.snap index 463fbb629c8d..9206286b7da0 100644 --- a/components/button/__tests__/__snapshots__/index.test.js.snap +++ b/components/button/__tests__/__snapshots__/index.test.js.snap @@ -211,6 +211,17 @@ exports[`Button should has click wave effect 1`] = ` `; +exports[`Button should merge text if children using variable 1`] = ` + +`; + exports[`Button should not render as link button when href is undefined 1`] = ` , + ); + + expect(wrapper.render()).toMatchSnapshot(); + }); }); diff --git a/components/button/button.tsx b/components/button/button.tsx index b6a94e606861..abbf9ebf0b41 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -15,6 +15,29 @@ function isString(str: any) { return typeof str === 'string'; } +function spaceChildren(children: React.ReactNode, needInserted: boolean) { + let isPrevChildPure: boolean = false; + const childList: React.ReactNode[] = []; + React.Children.forEach(children, child => { + const type = typeof child; + const isCurrentChildPure = type === 'string' || type === 'number'; + if (isPrevChildPure && isCurrentChildPure) { + const lastIndex = childList.length - 1; + const lastChild = childList[lastIndex]; + childList[lastIndex] = `${lastChild}${child}`; + } else { + childList.push(child); + } + + isPrevChildPure = isCurrentChildPure; + }); + + // Pass to React.Children.map to auto fill key + return React.Children.map(childList, child => + insertSpace(child as React.ReactChild, needInserted), + ); +} + // Insert one space between two chinese characters automatically. function insertSpace(child: React.ReactChild, needInserted: boolean) { // Check the child if is undefined or null. @@ -243,9 +266,7 @@ class Button extends React.Component { const iconNode = iconType ? : null; const kids = children || children === 0 - ? React.Children.map(children, child => - insertSpace(child as React.ReactChild, this.isNeedInserted() && autoInsertSpace), - ) + ? spaceChildren(children, this.isNeedInserted() && autoInsertSpace) : null; const linkButtonRestProps = omit(rest as AnchorButtonProps, ['htmlType']); diff --git a/components/input/__tests__/__snapshots__/demo.test.js.snap b/components/input/__tests__/__snapshots__/demo.test.js.snap index 1e796ac855e2..508568e1c43d 100644 --- a/components/input/__tests__/__snapshots__/demo.test.js.snap +++ b/components/input/__tests__/__snapshots__/demo.test.js.snap @@ -1198,10 +1198,7 @@ exports[`renders ./components/input/demo/textarea-resize.md correctly 1`] = ` type="button" > - Auto Resize: - - - false + Auto Resize: false