Skip to content

Commit

Permalink
fix space missing in button (#16551)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed May 17, 2019
1 parent 947e8e5 commit 1517fcb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
11 changes: 11 additions & 0 deletions components/button/__tests__/__snapshots__/index.test.js.snap
Expand Up @@ -211,6 +211,17 @@ exports[`Button should has click wave effect 1`] = `
</button>
`;

exports[`Button should merge text if children using variable 1`] = `
<button
class="ant-btn"
type="button"
>
<span>
This is a test 1
</span>
</button>
`;

exports[`Button should not render as link button when href is undefined 1`] = `
<button
class="ant-btn ant-btn-primary"
Expand Down
11 changes: 11 additions & 0 deletions components/button/__tests__/index.test.js
Expand Up @@ -157,4 +157,15 @@ describe('Button', () => {
);
expect(wrapper.render()).toMatchSnapshot();
});

// https://github.com/ant-design/ant-design/issues/15342
it('should merge text if children using variable', () => {
const wrapper = mount(
<Button>
This {'is'} a test {1}
</Button>,
);

expect(wrapper.render()).toMatchSnapshot();
});
});
27 changes: 24 additions & 3 deletions components/button/button.tsx
Expand Up @@ -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.
Expand Down Expand Up @@ -243,9 +266,7 @@ class Button extends React.Component<ButtonProps, ButtonState> {
const iconNode = iconType ? <Icon type={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']);
Expand Down
5 changes: 1 addition & 4 deletions components/input/__tests__/__snapshots__/demo.test.js.snap
Expand Up @@ -1198,10 +1198,7 @@ exports[`renders ./components/input/demo/textarea-resize.md correctly 1`] = `
type="button"
>
<span>
Auto Resize:
</span>
<span>
false
Auto Resize: false
</span>
</button>
<textarea
Expand Down

0 comments on commit 1517fcb

Please sign in to comment.