Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Support Checkbox selection by label without passed id (#4067)
Browse files Browse the repository at this point in the history
  • Loading branch information
cm9361 authored Mar 20, 2024
1 parent d5ea19f commit c45bef2
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/terra-core-docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Added
* Added an example for `terra-button` to demonstrate isLeftAligned property.
* Added a test for `terra-form-checkbox` to cover a checkbox without an id property passed.

## 1.67.0 - (March 15, 2024)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import Checkbox from 'terra-form-checkbox';

import classNames from 'classnames/bind';
import styles from './CheckboxTestCommon.module.scss';

const cx = classNames.bind(styles);

export default () => (
<div className={cx('spacing')}>
<Checkbox labelText="Default Checkbox" />
</div>
);
3 changes: 3 additions & 0 deletions packages/terra-form-checkbox/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Changed
* Updated the Checkbox component to generate an id when not passed so that click on the label controls the selection state.

## 4.25.0 - (March 15, 2024)

* Added
Expand Down
3 changes: 2 additions & 1 deletion packages/terra-form-checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"prop-types": "^15.5.8",
"terra-mixins": "^1.41.0",
"terra-theme-context": "^1.0.0",
"terra-visually-hidden-text": "^2.38.0"
"terra-visually-hidden-text": "^2.38.0",
"uuid": "3.4.0"
},
"peerDependencies": {
"react": "^16.8.5",
Expand Down
7 changes: 5 additions & 2 deletions packages/terra-form-checkbox/src/Checkbox.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { v4 as uuidv4 } from 'uuid';
import classNamesBind from 'classnames/bind';
import ThemeContext from 'terra-theme-context';
import styles from './Checkbox.module.scss';
Expand Down Expand Up @@ -104,6 +105,8 @@ const Checkbox = ({
}) => {
const theme = React.useContext(ThemeContext);

const inputId = id || uuidv4();

const controlInputAttrs = { ...inputAttrs };

if (checked !== undefined) {
Expand Down Expand Up @@ -149,11 +152,11 @@ const Checkbox = ({

return (
<div {...customProps} className={checkboxClasses}>
<label htmlFor={id} className={labelClasses}>
<label htmlFor={inputId} className={labelClasses}>
<input
{...controlInputAttrs}
type="checkbox"
id={id}
id={inputId}
disabled={disabled}
name={name}
value={value}
Expand Down
33 changes: 32 additions & 1 deletion packages/terra-form-checkbox/tests/jest/Checkbox.test.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React from 'react';
import ThemeContextProvider from 'terra-theme-context/lib/ThemeContextProvider';

import { v4 as uuidv4 } from 'uuid';
import Checkbox from '../../src';

let mockSpyUuid;
beforeAll(() => {
mockSpyUuid = jest.spyOn(uuidv4, 'v4').mockReturnValue('00000000-0000-0000-0000-000000000000');
});

afterAll(() => {
mockSpyUuid.mockRestore();
});

window.matchMedia = () => ({ matches: true });

it('should render a checkbox', () => {
Expand All @@ -11,6 +20,28 @@ it('should render a checkbox', () => {
expect(wrapper).toMatchSnapshot();
});

it('should render a checkbox using id prop', () => {
const checkBox = (<Checkbox id="testId" labelText="Checkbox" />);
const wrapper = enzyme.shallow(checkBox);

const label = wrapper.find('.label');
const input = wrapper.find('.native-input');

expect(label.props().htmlFor).toBe('testId');
expect(input.props().id).toBe('testId');
});

it('should render a checkbox without id prop', () => {
const checkBox = (<Checkbox labelText="Checkbox" />);
const wrapper = enzyme.shallow(checkBox);

const label = wrapper.find('.label');
const input = wrapper.find('.native-input');

expect(label.props().htmlFor).toBe('00000000-0000-0000-0000-000000000000');
expect(input.props().id).toBe('00000000-0000-0000-0000-000000000000');
});

it('should render an uncontrolled checkbox', () => {
const checkBox = (<Checkbox defaultChecked labelText="Checkbox" />);
const wrapper = enzyme.shallow(checkBox);
Expand Down
10 changes: 10 additions & 0 deletions packages/terra-form-checkbox/tests/jest/CheckboxField.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import React from 'react';
import { v4 as uuidv4 } from 'uuid';
import ThemeContextProvider from 'terra-theme-context/lib/ThemeContextProvider';

import Checkbox, { CheckboxField } from '../../src';

window.matchMedia = () => ({ matches: true });

let mockSpyUuid;
beforeAll(() => {
mockSpyUuid = jest.spyOn(uuidv4, 'v4').mockReturnValue('00000000-0000-0000-0000-000000000000');
});

afterAll(() => {
mockSpyUuid.mockRestore();
});

let userAgentGetter;
beforeEach(() => {
userAgentGetter = jest.spyOn(window.navigator, 'userAgent', 'get');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ exports[`correctly applies the theme context className 1`] = `
>
<label
className="label healtheintent-label-text"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
className="native-input healtheintent-application"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name="children_present"
type="checkbox"
value="children_present"
Expand All @@ -56,10 +58,12 @@ exports[`should render a ChoiceField with a checkbox 1`] = `
>
<label
className="label healtheintent-label-text"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
className="native-input healtheintent-application"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name="children_present"
type="checkbox"
value="children_present"
Expand All @@ -79,10 +83,12 @@ exports[`should render a checkbox 1`] = `
>
<label
className="label"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
className="native-input"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name={null}
type="checkbox"
/>
Expand All @@ -101,12 +107,14 @@ exports[`should render a checkbox with a hidden label 1`] = `
>
<label
className="label"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
aria-label="Checkbox"
checked={true}
className="native-input"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name={null}
onChange={[Function]}
type="checkbox"
Expand All @@ -124,11 +132,13 @@ exports[`should render a controlled checkbox 1`] = `
>
<label
className="label"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
checked={true}
className="native-input"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name={null}
onChange={[Function]}
type="checkbox"
Expand All @@ -148,11 +158,13 @@ exports[`should render a disabled checkbox 1`] = `
>
<label
className="label is-disabled"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
checked={true}
className="native-input"
disabled={true}
id="00000000-0000-0000-0000-000000000000"
name={null}
onChange={[Function]}
type="checkbox"
Expand All @@ -172,11 +184,13 @@ exports[`should render an uncontrolled checkbox 1`] = `
>
<label
className="label"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
className="native-input"
defaultChecked={true}
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name={null}
type="checkbox"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ exports[`correctly applies "inputAttrs" property to the Checkbox component 1`] =
>
<label
className="label"
htmlFor="00000000-0000-0000-0000-000000000000"
>
<input
aria-describedby="terra-checkbox-field-description-4 "
className="native-input"
data-custom-attr="attr data"
disabled={false}
id="00000000-0000-0000-0000-000000000000"
name={null}
type="checkbox"
/>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/terra-form-checkbox/tests/wdio/form-checkbox-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ Terra.describeViewports('Checkbox', ['medium'], () => {

Terra.validates.element('three Checkboxes, first defaulted to checked');
});

describe('No Identifier', () => {
it('should display checked Checkbox', () => {
browser.url('/raw/tests/cerner-terra-core-docs/form-checkbox/checkbox/checkbox-without-id');
$('label[class*="label"').click();

Terra.validates.element('no-id-checked');
});
});
});

0 comments on commit c45bef2

Please sign in to comment.