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

feat(Checkbox): add support of htmlFor #2293

Merged
merged 1 commit into from
Nov 25, 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
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