Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Space missing in button #16551

Merged
merged 2 commits into from May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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