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

a11y group legends #384

Merged
merged 2 commits into from
Oct 10, 2019
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
13 changes: 12 additions & 1 deletion src/forms/inputs/checkbox-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../helpers'
import { LabeledField } from '../labels'
import { addToArray, removeFromArray, serializeOptions, compose } from '../../utils'
import { convertNameToLabel } from '../helpers'

/**
*
Expand Down Expand Up @@ -59,6 +60,12 @@ const defaultProps = {
options: []
}

function CheckboxGroupLegend ({ name, label }) {
return (
<legend>{ label || convertNameToLabel(name) }</legend>
)
}

function CheckboxGroup (props) {
const {
input: { value, onChange, name },
Expand All @@ -76,7 +83,11 @@ function CheckboxGroup (props) {
}
}
return (
<LabeledField className="CheckboxGroup" { ...props }>
<LabeledField
className="CheckboxGroup"
labelComponent={ CheckboxGroupLegend }
{ ...props }
>
{
optionObjects.map((option, i) => {
return (
Expand Down
13 changes: 12 additions & 1 deletion src/forms/inputs/radio-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
// import PropTypes from 'prop-types'
import Input from './input'
import {
convertNameToLabel,
fieldPropTypes,
fieldOptionsType,
omitLabelProps,
Expand Down Expand Up @@ -58,6 +59,12 @@ const defaultProps = {
options: []
}

function RadioGroupLegend ({ label, name }) {
return (
<legend>{ label || convertNameToLabel(name) }</legend>
)
}

function RadioGroup (props) {
const {
input: { value, onChange, name },
Expand All @@ -67,7 +74,11 @@ function RadioGroup (props) {
} = omitLabelProps(props)
const optionObjects = serializeOptions(options)
return (
<LabeledField className="RadioGroup" { ...props }>
<LabeledField
className="RadioGroup"
labelComponent={ RadioGroupLegend }
{ ...props }
>
{
optionObjects.map((option, i) => {
return (
Expand Down
33 changes: 32 additions & 1 deletion test/forms/inputs/checkbox-group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,35 @@ test('CheckboxGroup removes value to array when selected option clicked', () =>
wrapper.find('input').simulate('change')
const newValue = onChange.mock.calls[0][0]
expect(newValue).toEqual([])
})
})

test('CheckboxGroup has a legend with the group\'s name by default', () => {
const props = {
input: {
name: 'testGroup',
value: '',
},
meta: {},
options: ['TOGGLED_OPTION']
}
const wrapper = mount(<CheckboxGroup { ...props }/>)
const legend = wrapper.find('fieldset').first().find('legend')
expect(legend).toBeTruthy()
expect(legend.text()).toEqual('Test Group')
})

test('CheckboxGroup has a legend with the group\'s label (when provided)', () => {
const props = {
input: {
name: 'testGroup',
value: '',
},
label: 'Checkbox Group',
meta: {},
options: ['TOGGLED_OPTION']
}
const wrapper = mount(<CheckboxGroup { ...props }/>)
const legend = wrapper.find('fieldset').first().find('legend')
expect(legend).toBeTruthy()
expect(legend.text()).toEqual('Checkbox Group')
})
33 changes: 33 additions & 0 deletions test/forms/inputs/radio-group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,36 @@ test('A RadioGroup\'s inputs all have the same name', () => {
expect(wrapper.find('input').first().prop('name')).toEqual(name)
expect(wrapper.find('input').last().prop('name')).toEqual(name)
})

test('A RadioGroup has a legend with the group\'s name by default', () => {
const name = "sameName"
const props = {
input: {
name,
value: '',
},
meta: {},
options: ['Option 1', 'Option 2']
}
const wrapper = mount(<RadioGroup { ...props }/>)
const legend = wrapper.find('fieldset').first().find('legend')
expect(legend).toBeTruthy()
expect(legend.text()).toEqual('Same Name')
})

test('A RadioGroup has a legend with the group\'s label (when provided)', () => {
const name = "sameName"
const props = {
input: {
name,
value: '',
},
meta: {},
label: "Different Name",
options: ['Option 1', 'Option 2']
}
const wrapper = mount(<RadioGroup { ...props }/>)
const legend = wrapper.find('fieldset').first().find('legend')
expect(legend).toBeTruthy()
expect(legend.text()).toEqual('Different Name')
})