From 47e53ab0fe2d1231c1d07aa90b2c92f6f44e076e Mon Sep 17 00:00:00 2001 From: zombieJ Date: Tue, 12 Feb 2019 21:39:36 +0800 Subject: [PATCH] Fix Input.Search not support addonAfter (#14799) * fix addonAfter * update snapshot --- components/input/Search.tsx | 38 +++++--- components/input/__tests__/Search.test.js | 9 ++ .../__snapshots__/Search.test.js.snap | 96 +++++++++++++++++++ .../__snapshots__/index.test.js.snap | 1 - 4 files changed, 128 insertions(+), 16 deletions(-) diff --git a/components/input/Search.tsx b/components/input/Search.tsx index 743c055615..c75b940109 100644 --- a/components/input/Search.tsx +++ b/components/input/Search.tsx @@ -68,14 +68,16 @@ export default class Search extends React.Component { }; renderAddonAfter = (prefixCls: string) => { - const { enterButton, size, disabled } = this.props; - if (!enterButton) return null; + const { enterButton, size, disabled, addonAfter } = this.props; + if (!enterButton) return addonAfter; const btnClassName = `${prefixCls}-button`; + let button: React.ReactNode; const enterButtonAsElement = enterButton as React.ReactElement; if (enterButtonAsElement.type === Button || enterButtonAsElement.type === 'button') { - return React.cloneElement(enterButtonAsElement, { + button = React.cloneElement(enterButtonAsElement, { onClick: this.onSearch, + key: 'enterButton', ...(enterButtonAsElement.type === Button ? { className: btnClassName, @@ -83,20 +85,26 @@ export default class Search extends React.Component { } : {}), }); + } else { + button = ( + + ); } - return ( - - ); + if (addonAfter) { + return [button, addonAfter]; + } + + return button; }; renderSearch = ({ getPrefixCls }: ConfigConsumerProps) => { diff --git a/components/input/__tests__/Search.test.js b/components/input/__tests__/Search.test.js index 09779bf9a8..e469f5714c 100644 --- a/components/input/__tests__/Search.test.js +++ b/components/input/__tests__/Search.test.js @@ -126,4 +126,13 @@ describe('Input.Search', () => { }), ); }); + + // https://github.com/ant-design/ant-design/issues/14785 + it('should support addonAfter', () => { + const addonAfter = Addon After; + const wrapper = mount(); + const wrapperWithEnterButton = mount(); + expect(wrapper.render()).toMatchSnapshot(); + expect(wrapperWithEnterButton.render()).toMatchSnapshot(); + }); }); diff --git a/components/input/__tests__/__snapshots__/Search.test.js.snap b/components/input/__tests__/__snapshots__/Search.test.js.snap index fa59d332b9..66dc25669a 100644 --- a/components/input/__tests__/__snapshots__/Search.test.js.snap +++ b/components/input/__tests__/__snapshots__/Search.test.js.snap @@ -1,5 +1,101 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Input.Search should support addonAfter 1`] = ` + + + + + + + + + + + + + Addon After + + + + +`; + +exports[`Input.Search should support addonAfter 2`] = ` + + + + + + + Addon After + + + + +`; + exports[`Input.Search should support custom Button 1`] = `