Skip to content

Commit

Permalink
feat(toggle): fixes disabled issue and adds test (#13958)
Browse files Browse the repository at this point in the history
* feat(toggle): fixes disabled issue and adds test

* fix: changes location of disabled check

* feat: removes styles that show focus state when disabled

* test(Toggle): fix lint error

* chore(Toggle): add screen to import

---------

Co-authored-by: TJ Egan <tw15egan@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 12, 2023
1 parent c043983 commit 5a76564
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 14 additions & 1 deletion packages/react/src/components/Toggle/Toggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import Toggle from './Toggle';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

const prefix = 'cds';
Expand Down Expand Up @@ -145,6 +145,19 @@ describe('Toggle', () => {
expect(wrapper.getByRole('switch')).toBeChecked();
});

it('does not change value when disabled', () => {
const onClick = jest.fn();
const onToggle = jest.fn();
wrapper.rerender(
<Toggle {...props} onClick={onClick} onToggle={onToggle} disabled />
);

expect(onClick).not.toHaveBeenCalled();
userEvent.click(screen.getByRole('switch'));
expect(onClick).not.toHaveBeenCalled();
expect(onToggle).not.toHaveBeenCalled();
});

it('does not change value when readonly', async () => {
const onClick = jest.fn();
const onToggle = jest.fn();
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Toggle/Toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export function Toggle({
// the keyboard event which already calls handleClick. if we wouldn't catch this, the
// onClick and onToggle functions would be called twice whenever the user activates the
// toggle by keyboard and props['aria-labelledby'] is passed.
if (buttonElement.current && e.target !== buttonElement.current) {
if (
buttonElement.current &&
e.target !== buttonElement.current &&
!disabled
) {
handleClick(e);
buttonElement.current.focus();
}
Expand Down
12 changes: 4 additions & 8 deletions packages/styles/scss/components/toggle/_toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@
.#{$prefix}--toggle__button:focus
+ .#{$prefix}--toggle__label
.#{$prefix}--toggle__switch,
.#{$prefix}--toggle__button:not(:disabled):active
+ .#{$prefix}--toggle__label
.#{$prefix}--toggle__switch,
.#{$prefix}--toggle:active .#{$prefix}--toggle__switch {
.#{$prefix}--toggle:not(.#{$prefix}--toggle--disabled):active
.#{$prefix}--toggle__switch {
box-shadow: 0 0 0 1px $focus-inset, 0 0 0 3px $focus;
}

Expand Down Expand Up @@ -192,10 +190,8 @@
.#{$prefix}--toggle__button:focus
+ .#{$prefix}--toggle__label
.#{$prefix}--toggle__switch,
.#{$prefix}--toggle__button:not(:disabled):active
+ .#{$prefix}--toggle__label
.#{$prefix}--toggle__switch,
.#{$prefix}--toggle:active .#{$prefix}--toggle__switch {
.#{$prefix}--toggle:not(.#{$prefix}--toggle--disabled):active
.#{$prefix}--toggle__switch {
@include high-contrast-mode('focus');
}
}

0 comments on commit 5a76564

Please sign in to comment.