Skip to content

Commit 0ca9e59

Browse files
authored
fix: Workaround for Firefox outside click issue (#177)
Closes #148
1 parent 21a02ec commit 0ca9e59

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

__snapshots__/src/checkbox/index.test.js.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Generated by [AVA](https://ava.li).
1010
1111
<input
1212
className="sample"
13+
onChange={Function {}}
1314
type="checkbox"
1415
/>
1516

@@ -19,5 +20,6 @@ Generated by [AVA](https://ava.li).
1920
2021
<input
2122
disabled={true}
23+
onChange={Function {}}
2224
type="checkbox"
2325
/>
53 Bytes
Binary file not shown.

docs/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/checkbox/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,19 @@ class Checkbox extends PureComponent {
1515
onChange: PropTypes.func
1616
}
1717

18+
// this (stopPropagation) is needed since FireFox wrongly detects inside clicks
19+
// See https://github.com/dowjones/react-dropdown-tree-select/pull/154
20+
// and https://github.com/dowjones/react-dropdown-tree-select/issues/148
21+
handleChange = e => {
22+
e.stopPropagation()
23+
e.nativeEvent.stopImmediatePropagation()
24+
this.props.onChange(e)
25+
}
26+
1827
render() {
1928
const { checked, indeterminate = false, onChange, ...rest } = this.props
2029

21-
return <input type="checkbox" ref={refUpdater({ checked, indeterminate })} onChange={onChange} {...rest} />
30+
return <input type="checkbox" ref={refUpdater({ checked, indeterminate })} onChange={this.handleChange} {...rest} />
2231
}
2332
}
2433

src/checkbox/index.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { shallow } from 'enzyme'
22
import React from 'react'
33
import test from 'ava'
44
import toJson from 'enzyme-to-json'
5+
import { spy } from 'sinon'
56

67
import Checkbox, { refUpdater } from './index'
78

@@ -26,3 +27,16 @@ test('renders disabled state', t => {
2627
const tree = toJson(shallow(<Checkbox disabled />))
2728
t.snapshot(tree)
2829
})
30+
31+
test('call stopPropagation and stopImmediatePropagation when clicked', t => {
32+
const onChange = spy()
33+
const wrapper = shallow(<Checkbox className="sample" onChange={onChange} />)
34+
const event = {
35+
stopPropagation: spy(),
36+
nativeEvent: { stopImmediatePropagation: spy() }
37+
}
38+
wrapper.simulate('change', event)
39+
t.true(onChange.called)
40+
t.true(event.stopPropagation.called)
41+
t.true(event.nativeEvent.stopImmediatePropagation.called)
42+
})

src/tag/index.test.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import toJson from 'enzyme-to-json'
66

77
import Tag from './index'
88

9-
const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => {} } }
9+
const mockEvent = { nativeEvent: { stopImmediatePropagation: spy() } }
1010

1111
test('renders label when passed in', t => {
1212
const wrapper = toJson(shallow(<Tag label="hello" id="abc" />))
@@ -19,20 +19,17 @@ test('call stopPropagation and stopImmediatePropagation when pill is closed', t
1919
const event = {
2020
type: 'click',
2121
stopPropagation: spy(),
22-
nativeEvent: {
23-
stopImmediatePropagation: spy()
24-
}
22+
nativeEvent: { stopImmediatePropagation: spy() }
2523
}
2624
wrapper.find('.tag-remove').prop('onClick')(event)
2725
t.true(event.stopPropagation.called)
2826
t.true(event.nativeEvent.stopImmediatePropagation.called)
2927
})
3028

31-
3229
test('call onDelete handler when pill is closed', t => {
3330
const onDelete = spy()
3431
const wrapper = mount(<Tag label="hello" id="abc" onDelete={onDelete} />)
35-
wrapper.find('.tag-remove').simulate('click', nativeEvent)
32+
wrapper.find('.tag-remove').simulate('click', mockEvent)
3633
t.true(onDelete.calledWith('abc'))
3734
})
3835

@@ -42,6 +39,6 @@ test('should not cause form submit', t => {
4239
const wrapper = mount(<form onSubmit={onSubmit}>
4340
<Tag label="hello" id="abc" onDelete={onDelete} />
4441
</form>)
45-
wrapper.find('.tag-remove').simulate('click', nativeEvent)
42+
wrapper.find('.tag-remove').simulate('click', mockEvent)
4643
t.false(onSubmit.called)
4744
})

0 commit comments

Comments
 (0)