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): focus should be obtained on mouseDown #1762

Merged
merged 1 commit into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/modules/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cx from 'classnames'
import _ from 'lodash/fp'
import _ from 'lodash'
Copy link
Member Author

@layershifter layershifter Jun 12, 2017

Choose a reason for hiding this comment

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

@levithomason I removed usage of lodash/fp because I don't understand how I can make this in FP style:

_.invoke(this.props, 'onMouseDown', e, { ...this.props, checked: !!checked, indeterminate: !!indeterminate })

Copy link
Member

Choose a reason for hiding this comment

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

No worries, this works 👍

import PropTypes from 'prop-types'
import React from 'react'

Expand Down Expand Up @@ -155,26 +155,24 @@ export default class Checkbox extends Component {

handleClick = e => {
debug('handleClick()')

const { onChange, onClick } = this.props
const { checked, indeterminate } = this.state

if (this.canToggle()) {
if (onClick) onClick(e, { ...this.props, checked: !!checked, indeterminate: !!indeterminate })
if (onChange) onChange(e, { ...this.props, checked: !checked, indeterminate: false })
if (!this.canToggle()) return

_.invoke(this.props, 'onClick', e, { ...this.props, checked: !!checked, indeterminate: !!indeterminate })
_.invoke(this.props, 'onChange', e, { ...this.props, checked: !checked, indeterminate: false })

this.trySetState({ checked: !checked, indeterminate: false })
}
this.trySetState({ checked: !checked, indeterminate: false })
}

handleMouseDown = e => {
debug('handleMouseDown()')

const { onMouseDown } = this.props
const { checked, indeterminate } = this.state

_.invoke('focus', this.inputRef)
if (onMouseDown) onMouseDown(e, { ...this.props, checked: !!checked, indeterminate: !!indeterminate })
_.invoke(this.props, 'onMouseDown', e, { ...this.props, checked: !!checked, indeterminate: !!indeterminate })
_.invoke(this.inputRef, 'focus')

e.preventDefault()
}

// Note: You can't directly set the indeterminate prop on the input, so we
Expand Down
20 changes: 20 additions & 0 deletions test/specs/modules/Checkbox/Checkbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ describe('Checkbox', () => {
})

describe('onMouseDown', () => {
it('is called with (event { name, value, checked }) on label mouse down', () => {
const onMousedDown = sandbox.spy()
const expectProps = { name: 'foo', value: 'bar', checked: false, indeterminate: true }
mount(<Checkbox onMouseDown={onMousedDown} {...expectProps} />)
.simulate('mousedown')

onMousedDown.should.have.been.calledOnce()
onMousedDown.should.have.been.calledWithMatch({}, {
...expectProps,
checked: expectProps.checked,
indeterminate: expectProps.indeterminate,
})
})
it('prevents default event', () => {
const preventDefault = sandbox.spy()
const wrapper = shallow(<Checkbox />)

wrapper.simulate('mousedown', { preventDefault })
preventDefault.should.have.been.calledOnce()
})
it('sets focus to container', () => {
const mountNode = document.createElement('div')
document.body.appendChild(mountNode)
Expand Down