From f8b2b0a76f0f2a241f00b4bcb555553e556d1b6c Mon Sep 17 00:00:00 2001 From: Alexander Fedyashov Date: Tue, 21 Mar 2017 14:04:32 +0200 Subject: [PATCH 1/2] fix(Button): update handling of floated prop --- src/elements/Button/Button.js | 42 ++++++++++++++--------- test/specs/elements/Button/Button-test.js | 5 +++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/elements/Button/Button.js b/src/elements/Button/Button.js index 62ab5ede6b..7c6aad7d4c 100644 --- a/src/elements/Button/Button.js +++ b/src/elements/Button/Button.js @@ -160,15 +160,10 @@ class Button extends Component { static Group = ButtonGroup static Or = ButtonOr - handleClick = (e) => { - const { disabled, onClick } = this.props + computeElementType = () => { + const { attached, label } = this.props - if (disabled) { - e.preventDefault() - return - } - - if (onClick) onClick(e, this.props) + if (!_.isNil(attached) || !_.isNil(label)) return 'div' } computeTabIndex = ElementType => { @@ -179,6 +174,17 @@ class Button extends Component { if (ElementType === 'div') return 0 } + handleClick = (e) => { + const { disabled, onClick } = this.props + + if (disabled) { + e.preventDefault() + return + } + + if (onClick) onClick(e, this.props) + } + render() { const { active, @@ -207,10 +213,6 @@ class Button extends Component { toggle, } = this.props - const labeledClasses = cx( - useKeyOrValueAndKey(labelPosition || !!label, 'labeled'), - ) - const baseClasses = cx( color, size, @@ -229,18 +231,23 @@ class Button extends Component { useKeyOnly(toggle, 'toggle'), useKeyOrValueAndKey(animated, 'animated'), useKeyOrValueAndKey(attached, 'attached'), + ) + const labeledClasses = cx( + useKeyOrValueAndKey(labelPosition || !!label, 'labeled'), + ) + const wrapperClasses = cx( + useKeyOnly(disabled, 'disabled'), useValueAndKey(floated, 'floated'), ) - const wrapperClasses = cx(useKeyOnly(disabled, 'disabled')) + const rest = getUnhandledProps(Button, this.props) - const ElementType = getElementType(Button, this.props, () => { - if (!_.isNil(label) || !_.isNil(attached)) return 'div' - }) + const ElementType = getElementType(Button, this.props, this.computeElementType) const tabIndex = this.computeTabIndex(ElementType) if (!_.isNil(children)) { const classes = cx('ui', baseClasses, wrapperClasses, labeledClasses, 'button', className) debug('render children:', { classes }) + return ( {children} @@ -252,9 +259,11 @@ class Button extends Component { basic: true, pointing: labelPosition === 'left' ? 'right' : 'left', }) + if (labelElement) { const classes = cx('ui', baseClasses, 'button', className) const containerClasses = cx('ui', labeledClasses, 'button', className, wrapperClasses) + debug('render label:', { classes, containerClasses }, this.props) return ( @@ -271,6 +280,7 @@ class Button extends Component { if (!_.isNil(icon) && _.isNil(label)) { const classes = cx('ui', labeledClasses, baseClasses, 'button', className, wrapperClasses) debug('render icon && !label:', { classes }) + return ( {Icon.create(icon)} {content} diff --git a/test/specs/elements/Button/Button-test.js b/test/specs/elements/Button/Button-test.js index b9f2d3c18e..dcab2e1e24 100644 --- a/test/specs/elements/Button/Button-test.js +++ b/test/specs/elements/Button/Button-test.js @@ -109,6 +109,11 @@ describe('Button', () => { wrapper.find('Label').should.not.have.className('disabled') wrapper.find('button').should.not.have.className('disabled') }) + it('contains children without floated class when floated attribute is set', () => { + const wrapper = shallow(