Skip to content
Open
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
34 changes: 22 additions & 12 deletions packages/yoga/src/Checkbox/web/Checkbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import styled, { withTheme } from 'styled-components';
import { hexToRgb } from '@gympass/yoga-common';
import { Check, Rectangle } from '@gympass/yoga-icons';

import { HiddenInput } from '../../shared';
import { VisuallyHidden } from '../../shared';

const CheckboxWrapper = styled.div`
display: inline-block;
Expand Down Expand Up @@ -266,6 +266,13 @@ const Checkbox = ({
}
});

const handleKeyDown = event => {
if (event.key === ' ' || event.key === 'Enter') {
event.preventDefault();
onClick(event);
}
};

return (
<CheckboxWrapper
style={style}
Expand All @@ -282,6 +289,8 @@ const Checkbox = ({
<Label id={checkboxLabelId}>
<Shadow />
<CheckMark
tabIndex={0}
onKeyDown={handleKeyDown}
aria-labelledby={ariaLabel ? undefined : checkboxLabelId}
aria-label={ariaLabel}
{...{
Expand All @@ -299,17 +308,18 @@ const Checkbox = ({
<Rectangle width={checkbox.size} height={checkbox.size} />
)}
</CheckMark>
<HiddenInput
type="checkbox"
ref={inputRef}
checked={checked}
disabled={disabled}
{...(value ? { value } : {})}
{...restWithoutEvents}
onChange={onChange}
onClick={onClick}
aria-hidden
/>
<VisuallyHidden>
<input
type="checkbox"
ref={inputRef}
checked={checked}
disabled={disabled}
{...(value ? { value } : {})}
{...restWithoutEvents}
onChange={onChange}
onClick={onClick}
/>
</VisuallyHidden>
{label}
</Label>
</CheckboxStyled>
Expand Down
14 changes: 10 additions & 4 deletions packages/yoga/src/Checkbox/web/Checkbox.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,22 @@ describe('<Checkbox />', () => {
});

describe('Events', () => {
it('should call onChange function when press on Checkbox', () => {
it('should call onChange and onClick functions when interacting with Checkbox', () => {
const onChangeMock = jest.fn();
const { getByTestId } = render(
const onClickMock = jest.fn();

const { container } = render(
<ThemeProvider>
<Checkbox {...data} onChange={onChangeMock} />
<Checkbox {...data} onChange={onChangeMock} onClick={onClickMock} />
</ThemeProvider>,
);

fireEvent.click(getByTestId('checkbox-checkMark'));
const hiddenInput = container.querySelector('input[type="checkbox"]');

fireEvent.click(hiddenInput);

expect(onChangeMock).toHaveBeenCalled();
expect(onClickMock).toHaveBeenCalled();
});
});
});
Loading
Loading