Skip to content

Commit d2dd511

Browse files
committed
added base checkbox
1 parent ede8b3d commit d2dd511

File tree

13 files changed

+192
-122
lines changed

13 files changed

+192
-122
lines changed
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
@use '../functions' as *;
22
@use '../variables' as *;
33

4+
@mixin checkbox-active-state {
5+
border-color: $color-primary-80;
6+
background-color: $color-primary-80;
7+
8+
&::after {
9+
border-color: $color-neutral-10;
10+
}
11+
12+
&:disabled {
13+
border-color: $color-primary-60;
14+
background-color: $color-primary-60;
15+
}
16+
17+
&.ids-input--error {
18+
border-color: $color-error-80;
19+
background-color: $color-error-80;
20+
21+
&::after {
22+
border-color: $color-neutral-10;
23+
}
24+
}
25+
}
26+
427
.ids-checkbox {
528
.ids-input--checkbox {
629
-webkit-appearance: none;
@@ -27,43 +50,8 @@
2750
transform: rotate(-45deg);
2851
}
2952

30-
&:checked,
31-
&:indeterminate {
32-
border-color: $color-primary-80;
33-
background-color: $color-primary-80;
34-
35-
&::after {
36-
border-color: $color-neutral-10;
37-
}
38-
39-
&:disabled {
40-
border-color: $color-primary-60;
41-
background-color: $color-primary-60;
42-
}
43-
44-
&.ids-input--error {
45-
border-color: $color-error-80;
46-
background-color: $color-error-80;
47-
48-
&::after {
49-
border-color: $color-neutral-10;
50-
}
51-
}
52-
}
53-
54-
&:indeterminate {
55-
&::after {
56-
background-color: $color-neutral-10;
57-
height: calculateRem(2px);
58-
top: 50%;
59-
transform: translateY(-50%);
60-
}
61-
62-
&.ids-input--error {
63-
&::after {
64-
background-color: $color-neutral-10;
65-
}
66-
}
53+
&:checked {
54+
@include checkbox-active-state;
6755
}
6856
}
6957
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@use '../functions' as *;
2+
@use '../variables' as *;
3+
@use './checkbox' as checkbox-mixins;
4+
5+
.ids-three-state-checkbox {
6+
@extend .ids-checkbox;
7+
8+
.ids-input--checkbox:indeterminate {
9+
@include checkbox-mixins.checkbox-active-state;
10+
11+
&::after {
12+
background-color: $color-neutral-10;
13+
height: calculateRem(2px);
14+
top: 50%;
15+
transform: translateY(-50%);
16+
}
17+
18+
&.ids-input--error {
19+
&::after {
20+
background-color: $color-neutral-10;
21+
}
22+
}
23+
}
24+
}

packages/assets/src/scss/styles.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313

1414
@use 'inputs/checkbox';
1515
@use 'inputs/input-text';
16+
@use 'inputs/three-state-checkbox';
1617

1718
@use 'ui/clear-btn';

packages/components/src/inputs/Checkbox/Checkbox.stories.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const meta: Meta<typeof CheckboxStateful> = {
1616
title: {
1717
control: 'text',
1818
},
19-
value: {
19+
checked: {
2020
control: 'boolean',
2121
},
2222
},
@@ -59,7 +59,7 @@ export const Checked: Story = {
5959
name: 'Checked',
6060
args: {
6161
name: 'default-input',
62-
value: true,
62+
checked: true,
6363
},
6464
};
6565

@@ -68,7 +68,7 @@ export const CheckedDisabled: Story = {
6868
args: {
6969
name: 'default-input',
7070
disabled: true,
71-
value: true,
71+
checked: true,
7272
},
7373
};
7474

@@ -77,6 +77,6 @@ export const CheckedError: Story = {
7777
args: {
7878
name: 'default-input',
7979
error: true,
80-
value: true,
80+
checked: true,
8181
},
8282
};
Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,25 @@
11
import React from 'react';
22

3-
import BaseInput from '@ids-internal/partials/BaseInput';
3+
import BaseCheckbox from '@ids-internal/partials/BaseCheckbox';
44
import { createCssClassNames } from '@ids-internal/shared/css.class.names';
5-
import withStateValue from '@ids-internal/hoc/withStateValue';
5+
import withStateChecked from '@ids-internal/hoc/withStateChecked';
66

77
import { CheckboxProps } from './Checkbox.types';
88

99
const Checkbox = ({
10-
name,
11-
onBlur = () => undefined,
12-
onChange = () => undefined,
13-
onFocus = () => undefined,
14-
onInput = () => undefined,
1510
className = '',
16-
disabled = false,
17-
error = false,
18-
extraAria = {},
19-
id = undefined,
20-
inputClassName = '',
21-
ref,
22-
required = false,
23-
title = '',
24-
value = false,
11+
...restProps
2512
}: CheckboxProps) => {
2613
const checkboxClassName = createCssClassNames({
2714
'ids-checkbox': true,
2815
[className]: true,
2916
});
30-
const componentOnBlur = (event: React.FocusEvent<HTMLInputElement>) => {
31-
onBlur(event);
32-
};
33-
const componentOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
34-
onChange(event.target.checked, event);
35-
};
36-
const componentOnFocus = (event: React.FocusEvent<HTMLInputElement>) => {
37-
onFocus(event);
38-
};
39-
const componentOnInput = (event: React.ChangeEvent<HTMLInputElement>) => {
40-
onInput(event.target.checked, event);
41-
};
4217

4318
return (
44-
<div className={checkboxClassName}>
45-
<BaseInput
46-
className={inputClassName}
47-
disabled={disabled}
48-
error={error}
49-
extraInputAttrs={{
50-
checked: value,
51-
onBlur: componentOnBlur,
52-
onChange: componentOnChange,
53-
onFocus: componentOnFocus,
54-
onInput: componentOnInput,
55-
...extraAria,
56-
}}
57-
id={id}
58-
name={name}
59-
ref={ref}
60-
required={required}
61-
title={title}
62-
type="checkbox"
63-
/>
64-
</div>
19+
<BaseCheckbox {...restProps} className={checkboxClassName} />
6520
);
6621
};
6722

6823
export default Checkbox;
6924

70-
export const CheckboxStateful = withStateValue<boolean>(Checkbox);
25+
export const CheckboxStateful = withStateChecked(Checkbox);
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
import { Ref } from 'react';
1+
import { BaseCheckboxProps } from '@ids-internal/partials/BaseCheckbox';
22

3-
import { BaseComponentAriaAttributes } from '@ids-types/general';
4-
5-
export interface CheckboxProps extends BaseComponentAriaAttributes {
6-
name: string;
7-
onBlur?: React.FocusEventHandler<HTMLInputElement>;
8-
onChange?: (value: boolean, event?: React.ChangeEvent<HTMLInputElement>) => void;
9-
onFocus?: React.FocusEventHandler<HTMLInputElement>;
10-
onInput?: (value: boolean, event?: React.ChangeEvent<HTMLInputElement>) => void;
11-
disabled?: boolean;
12-
error?: boolean;
13-
id?: string;
14-
inputClassName?: string;
15-
ref?: Ref<HTMLInputElement>;
16-
required?: boolean;
17-
value?: boolean;
18-
}
3+
export type CheckboxProps = BaseCheckboxProps;

packages/components/src/inputs/ThreeStateCheckbox/ThreeStateCheckbox.stories.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const meta: Meta<typeof ThreeStateCheckboxStateful> = {
1616
title: {
1717
control: 'text',
1818
},
19-
value: {
19+
checked: {
2020
control: 'boolean',
2121
},
2222
},
@@ -85,7 +85,7 @@ export const Checked: Story = {
8585
name: 'Checked',
8686
args: {
8787
name: 'default-input',
88-
value: true,
88+
checked: true,
8989
},
9090
};
9191

@@ -94,7 +94,7 @@ export const CheckedDisabled: Story = {
9494
args: {
9595
name: 'default-input',
9696
disabled: true,
97-
value: true,
97+
checked: true,
9898
},
9999
};
100100

@@ -103,6 +103,6 @@ export const CheckedError: Story = {
103103
args: {
104104
name: 'default-input',
105105
error: true,
106-
value: true,
106+
checked: true,
107107
},
108108
};

packages/components/src/inputs/ThreeStateCheckbox/ThreeStateCheckbox.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import React from 'react';
22

3-
import Checkbox from '../Checkbox';
3+
import BaseCheckbox from '@ids-internal/partials/BaseCheckbox';
44
import { createCssClassNames } from '@ids-internal/shared/css.class.names';
5-
import withStateValue from '@ids-internal/hoc/withStateValue';
5+
import withStateChecked from '@ids-internal/hoc/withStateChecked';
66

77
import { ThreeStateCheckboxProps } from './ThreeStateCheckbox.types';
88

99
const ThreeStateCheckbox = ({ className = '', indeterminate = false, ...restProps }: ThreeStateCheckboxProps) => {
1010
const checkboxClassName = createCssClassNames({
11-
'ids-checkbox--three-state': true,
11+
'ids-three-state-checkbox': true,
1212
[className]: true,
1313
});
1414
const inputClassName = createCssClassNames({
1515
'ids-input--indeterminate': indeterminate,
1616
});
1717

1818
return (
19-
<Checkbox
19+
<BaseCheckbox
2020
className={checkboxClassName}
2121
inputClassName={inputClassName}
2222
ref={(node) => {
@@ -31,4 +31,4 @@ const ThreeStateCheckbox = ({ className = '', indeterminate = false, ...restProp
3131

3232
export default ThreeStateCheckbox;
3333

34-
export const ThreeStateCheckboxStateful = withStateValue<boolean>(ThreeStateCheckbox);
34+
export const ThreeStateCheckboxStateful = withStateChecked(ThreeStateCheckbox);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { CheckboxProps } from '../Checkbox';
1+
import { BaseCheckboxProps } from '@ids-internal/partials/BaseCheckbox';
22

3-
interface ThreeStateCheckboxIsIndeterminateProps extends CheckboxProps {
3+
interface ThreeStateCheckboxIsIndeterminateProps extends BaseCheckboxProps {
44
indeterminate: true;
5-
value: false;
5+
checked: false;
66
}
77

8-
interface ThreeStateCheckboxIsNotIndeterminateProps extends CheckboxProps {
8+
interface ThreeStateCheckboxIsNotIndeterminateProps extends BaseCheckboxProps {
99
indeterminate: false;
10-
value: boolean;
10+
checked: boolean;
1111
}
1212

1313
export type ThreeStateCheckboxProps = ThreeStateCheckboxIsIndeterminateProps | ThreeStateCheckboxIsNotIndeterminateProps;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { FC, useState } from 'react';
2+
3+
type OnChangeFn = (checked: boolean, ...args: any[]) => any; // eslint-disable-line @typescript-eslint/no-explicit-any
4+
interface WrappedComponentProps {
5+
onChange?: OnChangeFn;
6+
checked: boolean;
7+
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
8+
}
9+
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
export default (WrappedComponent: FC<any>) => {
12+
const WrapperComponent = ({ checked, onChange, ...restProps }: WrappedComponentProps) => {
13+
const [componentChecked, setComponentChecked] = useState(checked);
14+
15+
const handleChange = (...args: Parameters<OnChangeFn>): ReturnType<OnChangeFn> => {
16+
setComponentChecked(args[0]);
17+
18+
if (onChange) {
19+
onChange(...args);
20+
}
21+
};
22+
23+
return <WrappedComponent {...restProps} checked={componentChecked} onChange={handleChange} />;
24+
};
25+
26+
WrapperComponent.displayName = `withStateChecked(${WrappedComponent.displayName ?? WrappedComponent.name})`;
27+
28+
return WrapperComponent;
29+
};

0 commit comments

Comments
 (0)