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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Checkbox): prevent click propagation from the input element #3435

Merged
merged 10 commits into from
Feb 21, 2019
1 change: 1 addition & 0 deletions src/modules/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export default class Checkbox extends Component {
tabIndex={this.computeTabIndex()}
type={type}
value={value}
onClick={e => e.stopPropagation()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create a handleInputClick method and make a comment there with a link to the matching issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And sort props in alphabetic order ❤️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

/>
{/*
Heads Up!
Expand Down
44 changes: 44 additions & 0 deletions test/specs/modules/Checkbox/Checkbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,50 @@ describe('Checkbox', () => {
})
})

describe('click propagation', () => {
const assertions = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const assertions = [
const assertMatrix= [

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll change that

{
description: 'propagates once on "label" without "id"',
id: undefined,
target: 'label',
propagates: true,
},
{
description: 'does not propagate on "input" without "id"',
id: undefined,
target: 'input',
propagates: false,
},
{
description: 'propagates once on "label" with "id"',
id: 'foo',
target: 'label',
propagates: true,
},
{
description: 'does not propagate on "input" with "id"',
id: 'foo',
target: 'input',
propagates: false,
},
]

assertions.forEach((assertion) => {
it(assertion.description, () => {
const onClick = sandbox.spy()
wrapperMount(
<div role='presentation' onClick={onClick}>
<Checkbox id={assertion.id} />
</div>,
)
const target = document.querySelector(`.ui.checkbox ${assertion.target}`)
domEvent.click(target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-const target = document.querySelector(`.ui.checkbox ${assertion.target}`)
-domEvent.click(target)
+domEvent.click(`.ui.checkbox ${assertion.target}`)

This should be a valid change 😺 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know!

if (assertion.propagates) onClick.should.have.been.calledOnce()
else onClick.should.not.have.been.called()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-if (assertion.propagates) onClick.should.have.been.calledOnce()
-else onClick.should.not.have.been.called()
+onClick.should.have.callCount(Number(assertion.propagates))

We can test the call count directly, https://github.com/domenic/sinon-chai

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea!

})
})
})

describe('label', () => {
it('adds the "fitted" class when not present', () => {
shallow(<Checkbox name='firstName' />).should.have.className('fitted')
Expand Down