From a7665ca0f2eef2c574c4f210495ee8d208d4dd5f Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Thu, 6 Jun 2019 11:30:56 +0200 Subject: [PATCH] Make Checkbox component accessible --- .../main/js/components/controls/Checkbox.tsx | 19 +++++++++++++------ .../controls/__tests__/Checkbox-test.tsx | 7 +++++++ .../__snapshots__/Checkbox-test.tsx.snap | 13 +++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/server/sonar-web/src/main/js/components/controls/Checkbox.tsx b/server/sonar-web/src/main/js/components/controls/Checkbox.tsx index a1df816ed01..b73e3003d3b 100644 --- a/server/sonar-web/src/main/js/components/controls/Checkbox.tsx +++ b/server/sonar-web/src/main/js/components/controls/Checkbox.tsx @@ -31,6 +31,7 @@ interface Props { onCheck: (checked: boolean, id?: string) => void; right?: boolean; thirdState?: boolean; + title?: string; } export default class Checkbox extends React.PureComponent { @@ -47,23 +48,26 @@ export default class Checkbox extends React.PureComponent { }; render() { - const { children, disabled, loading, right } = this.props; + const { checked, children, disabled, id, loading, right, thirdState, title } = this.props; const className = classNames('icon-checkbox', { - 'icon-checkbox-checked': this.props.checked, - 'icon-checkbox-single': this.props.thirdState, + 'icon-checkbox-checked': checked, + 'icon-checkbox-single': thirdState, 'icon-checkbox-disabled': disabled }); if (children) { return ( + id={id} + onClick={this.handleClick} + role="checkbox" + title={title}> {right && children} @@ -79,10 +83,13 @@ export default class Checkbox extends React.PureComponent { return ( ); } diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx index f089b43dfa5..8353052c830 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/Checkbox-test.tsx @@ -22,14 +22,21 @@ import * as React from 'react'; import Checkbox from '../Checkbox'; import { click } from '../../../helpers/testUtils'; +it('should render', () => { + const checkbox = shallow( {}} title="Title value" />); + expect(checkbox).toMatchSnapshot(); +}); + it('should render unchecked', () => { const checkbox = shallow( true} />); expect(checkbox.is('.icon-checkbox-checked')).toBeFalsy(); + expect(checkbox.prop('aria-checked')).toBe(false); }); it('should render checked', () => { const checkbox = shallow( true} />); expect(checkbox.is('.icon-checkbox-checked')).toBeTruthy(); + expect(checkbox.prop('aria-checked')).toBe(true); }); it('should render disabled', () => { diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap index f733f88a916..69c0c60af8c 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/Checkbox-test.tsx.snap @@ -1,9 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should render 1`] = ` + +`; + exports[`should render the checkbox on the right 1`] = ` `; -- 2.39.5