Skip to content

Commit

Permalink
feat: readonly toggle (#12402)
Browse files Browse the repository at this point in the history
* feat: readonly toggle

* chore: remove unnecessary scss vars

* fix: readonly cursor and text selection

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
lee-chase and kodiakhq[bot] committed Nov 14, 2022
1 parent 88243d4 commit 4fd8c24
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8457,6 +8457,9 @@ Map {
"onToggle": Object {
"type": "func",
},
"readOnly": Object {
"type": "bool",
},
"size": Object {
"args": Array [
Array [
Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/components/Toggle/Toggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ describe('Toggle', () => {
'true'
);
});

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

expect(onClick).not.toHaveBeenCalled();
userEvent.click(wrapper.getByRole('switch'));
expect(onClick).toHaveBeenCalledTimes(1);
expect(onToggle).not.toHaveBeenCalled();
});
});

describe('emits events as expected', () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/react/src/components/Toggle/Toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function Toggle({
labelText,
onClick,
onToggle,
readOnly,
size = 'md',
toggled,
...other
Expand All @@ -34,8 +35,9 @@ export function Toggle({
});

function handleClick(e) {
setChecked(!checked);

if (!readOnly) {
setChecked(!checked);
}
if (onClick) {
onClick(e);
}
Expand All @@ -48,6 +50,7 @@ export function Toggle({
`${prefix}--toggle`,
{
[`${prefix}--toggle--disabled`]: disabled,
[`${prefix}--toggle--readonly`]: readOnly,
},
className
);
Expand Down Expand Up @@ -151,6 +154,11 @@ Toggle.propTypes = {
*/
onToggle: PropTypes.func,

/**
* Whether the toggle should be read-only
*/
readOnly: PropTypes.bool,

/**
* Specify the size of the Toggle. Currently only supports 'sm' or 'md' (default)
*/
Expand Down
33 changes: 33 additions & 0 deletions packages/styles/scss/components/toggle/_toggle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
visibility: visible;
}

// ----------------------------------------
// disabled
// ----------------------------------------

.#{$prefix}--toggle--disabled .#{$prefix}--toggle__appearance {
cursor: not-allowed;
}
Expand All @@ -147,6 +151,35 @@
fill: button.$button-disabled;
}

// ----------------------------------------
// readonly
// ----------------------------------------
.#{$prefix}--toggle--readonly .#{$prefix}--toggle__appearance {
cursor: default;
}

.#{$prefix}--toggle--readonly .#{$prefix}--toggle__switch {
border: 1px solid $icon-disabled;
background-color: transparent;

&::before {
top: rem(2px);
left: rem(2px);
background-color: $text-primary;
}
}

.#{$prefix}--toggle--readonly .#{$prefix}--toggle__check {
top: rem(5px);
right: rem(4px);
fill: $background;
}

.#{$prefix}--toggle--readonly .#{$prefix}--toggle__text {
cursor: text;
user-select: text;
}

// HCM

.#{$prefix}--toggle__switch,
Expand Down

0 comments on commit 4fd8c24

Please sign in to comment.