Skip to content

Commit 18b8ea6

Browse files
feat(checkbox): make focus behavior configurable (#3214)
* feat(checkbox): make focus behavior configurable * test(checkbox): update test which checks className of Checkbox * test(checkbox): test for onFocus * test(checkbox): tests for hideFocusStyle prop * fix(checkbox): flow type error * chore(checkbox): remove hideFocusStyles prop * chore(checkbox): remove --isFocused classname from test; fix flow file * chore(checkbox): remove unnecessary classnames function call Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 5d05a2b commit 18b8ea6

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/components/checkbox/Checkbox.js.flow

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Props = {
1717
hideLabel?: boolean,
1818
/** Unique `id` for the input */
1919
id?: string,
20+
inputClassName?: string,
2021
/** Checkbox checked state */
2122
isChecked?: boolean, // @TODO: eventually call this `checked`
2223
/** Checkbox disabled state */
@@ -29,6 +30,8 @@ type Props = {
2930
onBlur?: (e: SyntheticInputEvent<HTMLInputElement>) => any,
3031
/** change callback function called with event as the argument */
3132
onChange?: (e: SyntheticInputEvent<HTMLInputElement>) => any,
33+
/** onFocus - focus callback function that takes the event as the argument */
34+
onFocus?: (e: SyntheticFocusEvent<HTMLInputElement>) => void,
3235
/** Subsection below the checkbox */
3336
subsection?: React.Node,
3437
/** Info tooltip text next to the checkbox label */
@@ -43,10 +46,12 @@ const Checkbox = ({
4346
fieldLabel,
4447
hideLabel,
4548
id,
49+
inputClassName,
4650
isChecked,
4751
isDisabled,
4852
label,
4953
name,
54+
onFocus,
5055
onChange,
5156
subsection,
5257
tooltip,
@@ -60,9 +65,11 @@ const Checkbox = ({
6065
<span className="checkbox-label">
6166
<input
6267
checked={isChecked}
68+
className={classNames(inputClassName)}
6369
disabled={isDisabled}
6470
id={inputID}
6571
name={name}
72+
onFocus={onFocus}
6673
onChange={onChange}
6774
type="checkbox"
6875
{...rest}

src/components/checkbox/Checkbox.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface CheckboxProps {
3232
| {
3333
(e: React.FocusEvent<HTMLInputElement>): void;
3434
};
35+
/** onFocus - focus callback function that takes the event as the argument */
36+
onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void;
3537
/** onChange - change callback function that takes the event as the argument */
3638
onChange?: (e: React.SyntheticEvent<HTMLInputElement, Event>) => string | number | boolean | void;
3739
/** Subsection below the checkbox */
@@ -53,6 +55,7 @@ const Checkbox = ({
5355
isDisabled,
5456
label,
5557
name,
58+
onFocus,
5659
onChange,
5760
subsection,
5861
tooltip,
@@ -71,6 +74,7 @@ const Checkbox = ({
7174
disabled={isDisabled}
7275
id={inputID}
7376
name={name}
77+
onFocus={onFocus}
7478
onChange={onChange}
7579
type="checkbox"
7680
{...rest}

src/components/checkbox/__tests__/Checkbox.test.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import React, { SyntheticEvent } from 'react';
22
import { shallow, ShallowWrapper } from 'enzyme';
33

4-
import Checkbox from '..';
4+
import Checkbox from '../Checkbox';
55

66
describe('components/checkbox/Checkbox', () => {
77
let wrapper: ShallowWrapper;
88
let onChange: (e: SyntheticEvent<HTMLInputElement, Event>) => string | number | boolean | void;
9+
let onFocus: (e: SyntheticEvent<HTMLInputElement, Event>) => string | number | boolean | void;
910

1011
beforeEach(() => {
1112
onChange = jest.fn();
13+
onFocus = jest.fn();
1214
wrapper = shallow(
13-
<Checkbox id="1" inputClassName="inputClassName" label="Check things" name="name" onChange={onChange} />,
15+
<Checkbox
16+
id="1"
17+
inputClassName="inputClassName"
18+
label="Check things"
19+
name="name"
20+
onChange={onChange}
21+
onFocus={onFocus}
22+
/>,
1423
);
1524
});
1625

@@ -73,6 +82,11 @@ describe('components/checkbox/Checkbox', () => {
7382
expect(onChange).toBeCalledWith(event);
7483
});
7584

85+
test('should call onFocus callback when onFocus triggers', () => {
86+
wrapper.find('input').simulate('focus');
87+
expect(onFocus).toBeCalledTimes(1);
88+
});
89+
7690
test('should render a hidden label when hideLabel is specified', () => {
7791
wrapper.setProps({ hideLabel: true });
7892
const label = wrapper.find('.accessibility-hidden');

0 commit comments

Comments
 (0)