Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
refactor(checkbox): align 'on' function handlers
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `onChange` is called with two parameters:
First parameter an object with properties for `checked`, `name`, and
`value`.
The second is the React `event` object.
  • Loading branch information
varl committed Nov 25, 2019
1 parent 611a394 commit 53c700d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
54 changes: 47 additions & 7 deletions src/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ class Checkbox extends Component {
}
}

handleChange = e => {
const { onChange, disabled } = this.props

if (disabled) {
return
}

if (onChange) {
onChange(
{
value: this.props.value,
name: this.props.name,
checked: !this.props.checked,
},
e
)
}
}

handleBlur = e => {
const { onBlur, disabled } = this.props

if (disabled) {
return
}

if (onBlur) {
onBlur(e)
}
}

handleFocus = e => {
const { onFocus, disabled } = this.props

if (disabled) {
return
}

if (onFocus) {
onFocus(e)
}
}

render() {
const {
checked = false,
Expand All @@ -35,13 +78,10 @@ class Checkbox extends Component {
error,
label,
name,
onChange,
tabIndex,
valid,
value,
warning,
onFocus,
onBlur,
dense,
} = this.props

Expand Down Expand Up @@ -69,9 +109,9 @@ class Checkbox extends Component {
checked={checked}
disabled={disabled}
tabIndex={tabIndex}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
onChange={this.handleChange}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
/>

<div className={cx('icon', { dense })}>
Expand Down Expand Up @@ -150,7 +190,7 @@ class Checkbox extends Component {
* @static
* @prop {string} value
* @prop {Node} label
* @prop {function} [onChange]
* @prop {function} [onChange] - called with the signature `object, event`
* @prop {string} [name]
* @prop {string} [className]
* @prop {string} [tabIndex]
Expand Down
3 changes: 2 additions & 1 deletion stories/Checkbox.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react'

import { Checkbox } from '../src'

const logger = ({ target }) => console.info(`${target.name}: ${target.value}`)
const logger = ({ name, value, checked }) =>
console.info(`name: ${name}, value: ${value}, checked: ${checked}`)

storiesOf('Checkbox', module)
// Regular
Expand Down
3 changes: 2 additions & 1 deletion stories/CheckboxField.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react'

import { CheckboxField } from '../src'

const logger = ({ target }) => console.info(`${target.name}: ${target.value}`)
const logger = ({ name, value, checked }) =>
console.info(`name: ${name}, value: ${value}, checked: ${checked}`)

storiesOf('CheckboxField', module)
// Regular
Expand Down
3 changes: 2 additions & 1 deletion stories/CheckboxGroup.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react'

import { Checkbox, CheckboxGroup } from '../src'

const logger = ({ target }) => console.info(`${target.name}: ${target.value}`)
const logger = ({ name, value, checked }) =>
console.info(`name: ${name}, value: ${value}, checked: ${checked}`)

storiesOf('CheckboxGroup', module)
.add('Default', () => (
Expand Down
3 changes: 2 additions & 1 deletion stories/CheckboxGroupField.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React from 'react'

import { Checkbox, CheckboxGroupField } from '../src'

const logger = ({ target }) => console.info(`${target.name}: ${target.value}`)
const logger = ({ name, value, checked }) =>
console.info(`name: ${name}, value: ${value}, checked: ${checked}`)

storiesOf('CheckboxGroupField', module)
.add('Default', () => (
Expand Down

0 comments on commit 53c700d

Please sign in to comment.