Skip to content

Commit 2e149f3

Browse files
author
Sasha Kondrashov
committed
checkbox
1 parent 0c8662c commit 2e149f3

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/modules/Checkbox/Checkbox.d.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,61 @@ export interface StrictCheckboxProps {
4343
* Called when the user attempts to change the checked state.
4444
*
4545
* @param {SyntheticEvent} event - React's original SyntheticEvent.
46-
* @param {object} data - All props and proposed checked/indeterminate state.
46+
* @param {object} props - All props.
47+
* @param {boolean} checked - Current checked state.
48+
* @param {boolean} indeterminate - Current indeterminate state.
4749
*/
48-
onChange?: (event: React.FormEvent<HTMLInputElement>, data: CheckboxProps) => void
50+
onChange?: (
51+
event: React.FormEvent<HTMLInputElement>,
52+
props: CheckboxProps,
53+
checked: boolean,
54+
indeterminate: boolean,
55+
) => void
4956

5057
/**
5158
* Called when the checkbox or label is clicked.
5259
*
5360
* @param {SyntheticEvent} event - React's original SyntheticEvent.
54-
* @param {object} data - All props and current checked/indeterminate state.
61+
* @param {object} props - All props.
62+
* @param {boolean} checked - Current checked state.
63+
* @param {boolean} indeterminate - Current indeterminate state.
5564
*/
56-
onClick?: (event: React.MouseEvent<HTMLInputElement>, data: CheckboxProps) => void
65+
onClick?: (
66+
event: React.MouseEvent<HTMLInputElement>,
67+
props: CheckboxProps,
68+
checked: boolean,
69+
indeterminate: boolean,
70+
) => void
5771

5872
/**
5973
* Called when the user presses down on the mouse.
6074
*
6175
* @param {SyntheticEvent} event - React's original SyntheticEvent.
62-
* @param {object} data - All props and current checked/indeterminate state.
76+
* @param {object} props - All props.
77+
* @param {boolean} checked - Current checked state.
78+
* @param {boolean} indeterminate - Current indeterminate state.
6379
*/
64-
onMouseDown?: (event: React.MouseEvent<HTMLInputElement>, data: CheckboxProps) => void
80+
onMouseDown?: (
81+
event: React.MouseEvent<HTMLInputElement>,
82+
props: CheckboxProps,
83+
checked: boolean,
84+
indeterminate: boolean,
85+
) => void
6586

6687
/**
6788
* Called when the user releases the mouse.
6889
*
6990
* @param {SyntheticEvent} event - React's original SyntheticEvent.
70-
* @param {object} data - All props and current checked/indeterminate state.
91+
* @param {object} props - All props.
92+
* @param {boolean} checked - Current checked state.
93+
* @param {boolean} indeterminate - Current indeterminate state.
7194
*/
72-
onMouseUp?: (event: React.MouseEvent<HTMLInputElement>, data: CheckboxProps) => void
95+
onMouseUp?: (
96+
event: React.MouseEvent<HTMLInputElement>,
97+
props: CheckboxProps,
98+
checked: boolean,
99+
indeterminate: boolean,
100+
) => void
73101

74102
/** Format as a radio element. This means it is an exclusive option. */
75103
radio?: boolean

src/modules/Checkbox/Checkbox.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ const Checkbox = React.forwardRef(function (props, ref) {
9696

9797
debug('handleChange()', _.get(e, 'target.tagName'))
9898

99-
_.invoke(props, 'onChange', e, {
100-
...props,
101-
checked: !checked,
102-
indeterminate: false,
103-
})
99+
_.invoke(props, 'onChange', e, props, !checked, false)
104100
setChecked(!checked)
105101
setIndeterminate(false)
106102
}
@@ -117,11 +113,7 @@ const Checkbox = React.forwardRef(function (props, ref) {
117113

118114
// https://github.com/Semantic-Org/Semantic-UI-React/pull/3351
119115
if (!isLabelClickAndForwardedToInput) {
120-
_.invoke(props, 'onClick', e, {
121-
...props,
122-
checked: !checked,
123-
indeterminate: !!indeterminate,
124-
})
116+
_.invoke(props, 'onClick', e, props, !checked, !!indeterminate)
125117
}
126118

127119
if (isClickFromMouse.current) {
@@ -147,11 +139,7 @@ const Checkbox = React.forwardRef(function (props, ref) {
147139
const handleMouseDown = (e) => {
148140
debug('handleMouseDown()')
149141

150-
_.invoke(props, 'onMouseDown', e, {
151-
...props,
152-
checked: !!checked,
153-
indeterminate: !!indeterminate,
154-
})
142+
_.invoke(props, 'onMouseDown', e, props, !!checked, !!indeterminate)
155143

156144
if (!e.defaultPrevented) {
157145
_.invoke(inputRef.current, 'focus')
@@ -166,11 +154,7 @@ const Checkbox = React.forwardRef(function (props, ref) {
166154
debug('handleMouseUp()')
167155

168156
isClickFromMouse.current = true
169-
_.invoke(props, 'onMouseUp', e, {
170-
...props,
171-
checked: !!checked,
172-
indeterminate: !!indeterminate,
173-
})
157+
_.invoke(props, 'onMouseUp', e, props, !!checked, !!indeterminate)
174158
}
175159

176160
// ----------------------------------------

test/specs/modules/Checkbox/Checkbox-test.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe('Checkbox', () => {
223223
})
224224

225225
describe('onChange', () => {
226-
it('is called with (e, data) on mouse up', () => {
226+
it('is called on mouse up', () => {
227227
const onChange = sandbox.spy()
228228
const props = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
229229

@@ -233,17 +233,10 @@ describe('Checkbox', () => {
233233
wrapper.find('label').simulate('click')
234234

235235
onChange.should.have.been.calledOnce()
236-
onChange.should.have.been.calledWithMatch(
237-
{},
238-
{
239-
...props,
240-
checked: true,
241-
indeterminate: false,
242-
},
243-
)
236+
onChange.should.have.been.calledWithMatch({}, props, true, false)
244237
})
245238

246-
it('is not called when on change when "id" is passed', () => {
239+
it('is not called on change when "id" is passed', () => {
247240
const onChange = sandbox.spy()
248241
wrapperMount(<Checkbox id='foo' onChange={onChange} />)
249242

@@ -264,22 +257,16 @@ describe('Checkbox', () => {
264257
})
265258

266259
describe('onClick', () => {
267-
it('is called with (event, data) on click', () => {
260+
it('is called on click', () => {
268261
const onClick = sandbox.spy()
269262
const props = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
270263
mount(<Checkbox onClick={onClick} {...props} />).simulate('click')
271264

272265
onClick.should.have.been.calledOnce()
273-
onClick.should.have.been.calledWithMatch(
274-
{},
275-
{
276-
...props,
277-
checked: true,
278-
},
279-
)
266+
onClick.should.have.been.calledWithMatch({}, props, true, true)
280267
})
281268

282-
it('is not called when "id" is passed', () => {
269+
it('is not called on click if "id" is passed', () => {
283270
const onClick = sandbox.spy()
284271
wrapperMount(<Checkbox id='foo' onClick={onClick} />)
285272

@@ -290,13 +277,13 @@ describe('Checkbox', () => {
290277
})
291278

292279
describe('onMouseDown', () => {
293-
it('is called with (event, data) on mouse down', () => {
280+
it('is called on mouse down without changing the checked status', () => {
294281
const onMousedDown = sandbox.spy()
295282
const props = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
296283
mount(<Checkbox onMouseDown={onMousedDown} {...props} />).simulate('mousedown')
297284

298285
onMousedDown.should.have.been.calledOnce()
299-
onMousedDown.should.have.been.calledWithMatch({}, props)
286+
onMousedDown.should.have.been.calledWithMatch({}, props, false, true)
300287
})
301288

302289
it('sets focus to container', () => {
@@ -307,7 +294,7 @@ describe('Checkbox', () => {
307294
document.activeElement.should.equal(input)
308295
})
309296

310-
it('will not set focus to container, if default is prevented', () => {
297+
it('does not set focus to container, if default is prevented', () => {
311298
wrapperMount(<Checkbox onMouseDown={(e) => e.preventDefault()} />)
312299

313300
domEvent.fire('.ui.checkbox input', 'mousedown')
@@ -316,20 +303,24 @@ describe('Checkbox', () => {
316303
})
317304

318305
describe('onMouseUp', () => {
319-
it('is called with (event, data) on mouse up', () => {
306+
it('is called on mouse up without changing the checked status', () => {
320307
const onMouseUp = sandbox.spy()
321308
const props = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
322309
mount(<Checkbox onMouseUp={onMouseUp} {...props} />).simulate('mouseup')
323310

324311
onMouseUp.should.have.been.calledOnce()
325-
onMouseUp.should.have.been.calledWithMatch({}, props)
312+
onMouseUp.should.have.been.calledWithMatch({}, props, false, true)
326313
})
327314

328-
it('is called with (event, data) on mouse up with right button', () => {
315+
it('is called on mouse up with right button without changing the checked status', () => {
329316
const onMouseUp = sandbox.spy()
330-
mount(<Checkbox id='foo' onMouseUp={onMouseUp} />).simulate('mouseup', { button: 2 })
317+
const props = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
318+
mount(<Checkbox id='foo' onMouseUp={onMouseUp} {...props} />).simulate('mouseup', {
319+
button: 2,
320+
})
331321

332322
onMouseUp.should.have.been.calledOnce()
323+
onMouseUp.should.have.been.calledWithMatch({}, props, false, true)
333324
})
334325
})
335326

0 commit comments

Comments
 (0)