Skip to content

Commit ff3cb8c

Browse files
committed
feat: implement Checkbox component with styles and assets
1 parent 2aca056 commit ff3cb8c

15 files changed

+370
-52
lines changed

src/Common/Checkbox.tsx

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/Common/Checkbox/Checkbox.scss

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
.form__checkbox-label {
2+
font-weight: 400;
3+
display: inline-block;
4+
margin-left: 12px;
5+
line-height: 16px;
6+
}
7+
8+
.form__checkbox__root {
9+
&--gap-8 {
10+
.form__checkbox-label {
11+
margin-left: 8px;
12+
}
13+
}
14+
}
15+
16+
.form__checkbox-check {
17+
display: none;
18+
}
19+
20+
$checkbox-hover-bg-image: url('./assets/ic-checkbox-hover.svg');
21+
22+
.form__checkbox-container {
23+
width: 20px;
24+
height: 20px;
25+
position: absolute;
26+
left: 0;
27+
background-size: contain;
28+
background-repeat: no-repeat;
29+
background-image: url('./assets/ic-checkbox-unselected.svg');
30+
transition: background-image 0.17s;
31+
32+
&:hover {
33+
background-image: url($checkbox-hover-bg-image);
34+
}
35+
}
36+
37+
// Class for checkbox parent container, if added to parent container, hover effect will be applied to checkbox
38+
.checkbox__parent-container {
39+
&:hover {
40+
.form__checkbox-container {
41+
background-image: $checkbox-hover-bg-image;
42+
}
43+
}
44+
}
45+
46+
.form__checkbox:checked[value='INTERMEDIATE'] ~ .form__checkbox-container {
47+
background-image: url('./assets/ic-checkbox-intermediate.svg');
48+
}
49+
50+
.form__checkbox:checked[value='CHECKED'] ~ .form__checkbox-container {
51+
background-image: url('./assets/ic-checkbox-selected.svg');
52+
}
53+
54+
.form__checkbox:checked[value='BULK_CHECKED'] ~ .form__checkbox-container {
55+
background-image: url('./assets/ic-bulk-check.svg');
56+
}
57+
58+
.form__checkbox:disabled ~ .form__checkbox-label,
59+
.form__checkbox:disabled ~ .form__checkbox-container {
60+
color: var(--N500);
61+
cursor: not-allowed;
62+
opacity: 0.5;
63+
}
64+
65+
// These are just hack, not to be further extended
66+
.theme {
67+
&__dark {
68+
$checkbox-hover-bg-image: url('./assets/dark/ic-checkbox-hover.svg');
69+
70+
.form__checkbox-container {
71+
background-image: url('./assets/dark/ic-checkbox-unselected.svg');
72+
73+
&:hover {
74+
background-image: $checkbox-hover-bg-image;
75+
}
76+
}
77+
78+
// Class for checkbox parent container, if added to parent container, hover effect will be applied to checkbox
79+
.checkbox__parent-container {
80+
&:hover {
81+
.form__checkbox-container {
82+
background-image: $checkbox-hover-bg-image;
83+
}
84+
}
85+
}
86+
87+
.form__checkbox:checked[value='INTERMEDIATE']~.form__checkbox-container {
88+
background-image: url('./assets/dark/ic-checkbox-intermediate.svg');
89+
}
90+
91+
.form__checkbox:checked[value='CHECKED']~.form__checkbox-container {
92+
background-image: url('./assets/dark/ic-checkbox-selected.svg');
93+
}
94+
95+
.form__checkbox:checked[value='BULK_CHECKED']~.form__checkbox-container {
96+
background-image: url('./assets/dark/ic-bulk-check.svg');
97+
}
98+
}
99+
}

src/Common/Checkbox/Checkbox.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { CheckboxProps } from '../Types'
18+
19+
import './Checkbox.scss'
20+
21+
/*
22+
Valid States of Checkbox:
23+
1. disabled: true, checked: false, value: XXX
24+
2. disabled: true, checked: true, value: INTERMIDIATE
25+
3. disabled: true, checked: true, value: CHECKED
26+
4. disabled: true, checked: false, value: XXX
27+
5. disabled: false, checked: true, value: INTERMIDIATE
28+
6. disabled: false, checked: true, value: CHECKED
29+
*/
30+
// TODO: Associate label with input element
31+
export const Checkbox = ({
32+
rootClassName,
33+
onClick,
34+
name,
35+
disabled,
36+
value,
37+
onChange,
38+
tabIndex,
39+
isChecked,
40+
id,
41+
dataTestId,
42+
children,
43+
}: CheckboxProps) => {
44+
const rootClass = `${rootClassName || ''}`
45+
46+
return (
47+
// eslint-disable-next-line jsx-a11y/label-has-associated-control
48+
<label className={`dc__position-rel flex left cursor ${rootClass}`} onClick={onClick}>
49+
<input
50+
{...(name ? { name } : {})}
51+
type="checkbox"
52+
className="form__checkbox"
53+
disabled={disabled}
54+
value={value}
55+
onChange={onChange}
56+
tabIndex={tabIndex}
57+
checked={isChecked}
58+
id={id}
59+
data-testid={dataTestId}
60+
/>
61+
<span className="form__checkbox-container" data-testid={`${dataTestId}-chk-span`} />
62+
<span className="form__checkbox-label">{children}</span>
63+
</label>
64+
)
65+
}
Lines changed: 21 additions & 0 deletions
Loading
Lines changed: 20 additions & 0 deletions
Loading
Lines changed: 22 additions & 0 deletions
Loading
Lines changed: 20 additions & 0 deletions
Loading
Lines changed: 19 additions & 0 deletions
Loading
Lines changed: 21 additions & 0 deletions
Loading
Lines changed: 20 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)