Skip to content

Commit

Permalink
feat(Checkbox): add support of htmlFor (#2293)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and levithomason committed Nov 25, 2017
1 parent 93c09a1 commit 84a8352
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/modules/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
customPropTypes,
getElementType,
getUnhandledProps,
htmlInputAttrs,
makeDebugger,
META,
partitionHTMLInputProps,
Expand Down Expand Up @@ -216,7 +217,8 @@ export default class Checkbox extends Component {
)
const unhandled = getUnhandledProps(Checkbox, this.props)
const ElementType = getElementType(Checkbox, this.props)
const [htmlInputProps, rest] = partitionHTMLInputProps(unhandled, { htmlProps: [] })
const [htmlInputProps, rest] = partitionHTMLInputProps(unhandled, { htmlProps: htmlInputAttrs })
const id = _.get(htmlInputProps, 'id')

return (
<ElementType
Expand All @@ -241,7 +243,7 @@ export default class Checkbox extends Component {
Heads Up!
Do not remove empty labels, they are required by SUI CSS
*/}
{createHTMLLabel(label) || <label />}
{createHTMLLabel(label, { defaultProps: { htmlFor: id } }) || <label htmlFor={id} />}
</ElementType>
)
}
Expand Down
35 changes: 35 additions & 0 deletions test/specs/modules/Checkbox/Checkbox-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash'
import React from 'react'

import { htmlInputAttrs } from 'src/lib'
import Checkbox from 'src/modules/Checkbox/Checkbox'
import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'
Expand Down Expand Up @@ -141,6 +143,39 @@ describe('Checkbox', () => {
})
})

describe('id', () => {
it('passes value to the input', () => {
shallow(<Checkbox id='foo' />)
.find('input')
.should.have.prop('id', 'foo')
})

it('adds htmlFor prop to the label', () => {
shallow(<Checkbox id='foo' />)
.find('label')
.should.have.prop('htmlFor', 'foo')
})

it('adds htmlFor prop to the label when it is empty', () => {
shallow(<Checkbox id='foo' label={null} />)
.find('label')
.should.have.prop('htmlFor', 'foo')
})
})

describe('input', () => {
// Heads up! Input handles some of html props
const props = _.without(htmlInputAttrs, 'defaultChecked', 'disabled')

_.forEach(props, (propName) => {
it(`passes "${propName}" to the input`, () => {
shallow(<Checkbox {...{ [propName]: 'radio' }} />)
.find('input')
.should.have.prop(propName)
})
})
})

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

0 comments on commit 84a8352

Please sign in to comment.