Skip to content

Commit

Permalink
feat: add inactive controls (PDS-266) (#765)
Browse files Browse the repository at this point in the history
* feat(checkbox): add inactive variant

* feat(radio): add inactive variant

* feat(switch): add interactive variant

* style: fix css-variables in inactive controls

* test: add tests for inactive controls

* test: update sreenshots for inactive screenshots
  • Loading branch information
koretskyhub committed Aug 3, 2021
1 parent 040bd42 commit ec02c89
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 32 deletions.
24 changes: 24 additions & 0 deletions packages/checkbox/src/Component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,29 @@ describe('Checkbox', () => {

expect(cb).not.toBeCalled();
});

test('should not call `onChange` prop if inactive', () => {
const cb = jest.fn();

const { container } = render(<Checkbox onChange={cb} inactive={true} />);

if (container.firstElementChild) {
fireEvent.click(container.firstElementChild);
}

expect(cb).not.toBeCalled();
});

test('should not call `onChange` prop if inactive and checked', () => {
const cb = jest.fn();

const { container } = render(<Checkbox onChange={cb} checked={true} inactive={true} />);

if (container.firstElementChild) {
fireEvent.click(container.firstElementChild);
}

expect(cb).not.toBeCalled();
});
});
});
14 changes: 13 additions & 1 deletion packages/checkbox/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export type CheckboxProps = Omit<NativeProps, 'size' | 'onChange'> & {
*/
block?: boolean;

/**
* Управление состоянием включен / выключен
*/
disabled?: boolean;

/**
* Управление состоянием активен / неактивен
*/
inactive?: boolean;

/**
* Идентификатор для систем автоматизированного тестирования
*/
Expand All @@ -82,6 +92,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
className,
name,
disabled,
inactive,
dataTestId,
indeterminate = false,
...restProps
Expand All @@ -103,6 +114,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
<label
className={cn(styles.component, styles[size], styles[align], className, {
[styles.disabled]: disabled,
[styles.inactive]: inactive,
[styles.checked]: checked,
[styles.indeterminate]: indeterminate,
[styles.focused]: focused,
Expand All @@ -114,7 +126,7 @@ export const Checkbox = forwardRef<HTMLLabelElement, CheckboxProps>(
<input
type='checkbox'
onChange={handleChange}
disabled={disabled}
disabled={disabled || inactive}
checked={checked}
data-test-id={dataTestId}
{...restProps}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/checkbox/src/component.screenshots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Checkbox | size, disabled, checked', () => {
hint: ['Дополнительная информация'],
size: ['s', 'm'],
disabled: [true, false],
inactive: [true, false],
indeterminate: [true, false],
},
size: { width: 400, height: 100 },
Expand Down
2 changes: 2 additions & 0 deletions packages/checkbox/src/docs/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import styles from '!!raw-loader!../index.module.css';
const handleChange = () => setChecked(!checked);
const size = select('size', ['s', 'm'], 's');
const disabled = boolean('disabled', false);
const inactive = boolean('inactive', false);
const checkedKnob = boolean('checked', false);
const label = text('label', 'Согласен с условиями');
const indeterminate = boolean('indeterminate', false);
Expand All @@ -34,6 +35,7 @@ import styles from '!!raw-loader!../index.module.css';
return (
<Checkbox
disabled={disabled}
inactive={inactive}
size={size}
block={block}
addons={addons}
Expand Down
39 changes: 31 additions & 8 deletions packages/checkbox/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
--checkbox-disabled-bg-color: var(--color-light-graphic-primary-inverted);
--checkbox-disabled-border-color: var(--color-light-graphic-neutral);

/* inactive */
--checkbox-inactive-border-color: var(--color-light-border-secondary-inverted);
--checkbox-checked-inactive-color: var(--color-light-graphic-tertiary);

/* disabled and checked */
--checkbox-checked-disabled-bg-color: var(--color-light-graphic-neutral);
--checkbox-checked-disabled-bg-color: var(--color-light-bg-neutral);
--checkbox-checked-disabled-border-color: transparent;

/* icon */
Expand Down Expand Up @@ -86,12 +90,12 @@
margin: var(--gap-2xs) var(--gap-3xs);
}

.component:hover:not(.disabled) .box {
.component:hover:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-hover-bg-color);
border-color: var(--checkbox-hover-border-color);
}

.component:active:not(.disabled) .box {
.component:active:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-active-bg-color);
border-color: var(--checkbox-active-border-color);
}
Expand All @@ -101,12 +105,12 @@
border-color: var(--checkbox-checked-border-color);
}

.checked:hover:not(.disabled) .box {
.checked:hover:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-checked-hover-bg-color);
border-color: var(--checkbox-checked-hover-border-color);
}

.checked:active:not(.disabled) .box {
.checked:active:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-checked-active-bg-color);
border-color: var(--checkbox-checked-active-border-color);
}
Expand All @@ -116,17 +120,18 @@
border-color: var(--checkbox-checked-border-color);
}

.indeterminate:hover:not(.disabled) .box {
.indeterminate:hover:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-checked-hover-bg-color);
border-color: var(--checkbox-checked-hover-border-color);
}

.indeterminate:active:not(.disabled) .box {
.indeterminate:active:not(.disabled):not(.inactive) .box {
background-color: var(--checkbox-checked-active-bg-color);
border-color: var(--checkbox-checked-active-border-color);
}

.disabled {
.disabled,
.inactive {
cursor: var(--disabled-cursor);
}

Expand All @@ -153,6 +158,20 @@
color: var(--checkbox-disabled-color);
}

.inactive .box {
background-color: var(--checkbox-checked-disabled-bg-color);
border-color: var(--checkbox-inactive-border-color);
}

.inactive.checked .box {
color: var(--checkbox-checked-inactive-color);
border-color: var(--checkbox-checked-disabled-border-color);
}

.inactive.indeterminate .box {
border-color: var(--checkbox-checked-disabled-border-color);
}

.focused .box {
@mixin focus-outline;
}
Expand Down Expand Up @@ -182,6 +201,10 @@
color: var(--checkbox-hint-color);
}

.inactive .indeterminateLine {
background-color: var(--checkbox-checked-inactive-color);
}

.indeterminateLine {
position: absolute;
width: 10px;
Expand Down
24 changes: 24 additions & 0 deletions packages/radio/src/Component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,29 @@ describe('Radio', () => {

expect(cb).not.toBeCalled();
});

test('should not call `onChange` prop if inactive', () => {
const cb = jest.fn();

const { container } = render(<Radio onChange={cb} inactive={true} />);

if (container.firstElementChild) {
fireEvent.click(container.firstElementChild);
}

expect(cb).not.toBeCalled();
});

test('should not call `onChange` prop if inactive and checked', () => {
const cb = jest.fn();

const { container } = render(<Radio onChange={cb} checked={true} inactive={true} />);

if (container.firstElementChild) {
fireEvent.click(container.firstElementChild);
}

expect(cb).not.toBeCalled();
});
});
});
11 changes: 9 additions & 2 deletions packages/radio/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ export type RadioProps = Omit<
checked?: boolean;

/**
* Управление состоянием активен / не активен
* Управление состоянием включен / выключен
*/
disabled?: boolean;

/**
* Управление состоянием активен / неактивен
*/
inactive?: boolean;

/**
* Html аттрибут name инпута
*/
Expand Down Expand Up @@ -86,6 +91,7 @@ export const Radio = forwardRef<HTMLLabelElement, RadioProps>(
className,
name,
disabled,
inactive,
dataTestId,
label,
checked,
Expand Down Expand Up @@ -113,6 +119,7 @@ export const Radio = forwardRef<HTMLLabelElement, RadioProps>(
<label
className={cn(styles.container, styles[size], styles[align], className, {
[styles.disabled]: disabled,
[styles.inactive]: inactive,
[styles.checked]: checked,
[styles.focused]: focused,
[styles.block]: block,
Expand All @@ -123,7 +130,7 @@ export const Radio = forwardRef<HTMLLabelElement, RadioProps>(
type='radio'
onChange={handleChange}
data-test-id={dataTestId}
disabled={disabled}
disabled={disabled || inactive}
checked={checked}
name={name}
{...restProps}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/radio/src/component.screenshots.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Radio', () => {
hint: ['', 'Подсказка'],
checked: [false, true],
disabled: [false, true],
inactive: [false, true],
},
size: { width: 240, height: 60 },
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/radio/src/docs/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import styles from '!!raw-loader!../index.module.css';
const handleChange = () => setChecked(!checked);
const size = select('size', ['s', 'm'], 's');
const disabled = boolean('disabled', false);
const inactive = boolean('inactive', false);
const checkedKnob = boolean('checked', false);
const block = boolean('block', false);
const label = text('label', 'Text label')
Expand All @@ -33,6 +34,7 @@ import styles from '!!raw-loader!../index.module.css';
return (
<Radio
disabled={disabled}
inactive={inactive}
addons={addons}
block={block}
size={size}
Expand Down
24 changes: 21 additions & 3 deletions packages/radio/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
--radio-disabled-bg-color: var(--color-light-graphic-primary-inverted);
--radio-disabled-border-color: var(--color-light-graphic-neutral);

/* inactive */
--radio-inactive-border-color: var(--color-light-border-secondary-inverted);
--radio-checked-inactive-color: var(--color-light-graphic-tertiary);

/* disabled and checked */
--radio-checked-disabled-bg-color: var(--color-light-graphic-neutral);
--radio-checked-disabled-bg-color: var(--color-light-bg-neutral);
--radio-checked-disabled-border-color: transparent;

/* icon */
Expand All @@ -38,7 +42,7 @@
align-items: flex-start;
cursor: pointer;

&:not(.checked):not(.disabled) {
&:not(.checked):not(.disabled):not(.inactive) {
& .circle {
background-color: var(--radio-bg-color);
border: 1.5px solid var(--radio-border-color);
Expand Down Expand Up @@ -72,6 +76,19 @@
}
}

&.inactive {
cursor: var(--disabled-cursor);

& .circle {
background-color: var(--radio-checked-disabled-bg-color);
border: 1px solid var(--radio-inactive-border-color);
}

&.checked .circle:before {
background-color: var(--radio-checked-inactive-color);
}
}

&.checked:not(:active) .circle:before,
&.checked.disabled:active .circle:before {
opacity: 1;
Expand All @@ -98,7 +115,8 @@
@mixin focus-outline;
}

&.disabled.checked .circle {
&.disabled.checked .circle,
&.inactive.checked .circle {
background-color: var(--radio-checked-disabled-bg-color);
border-color: var(--radio-checked-disabled-border-color);
}
Expand Down

0 comments on commit ec02c89

Please sign in to comment.