Skip to content

Commit 8704910

Browse files
authored
Merge pull request #61 from BeeInventor/develop
feature: add checkbox with light style
2 parents 62c9a10 + 2f1e2e5 commit 8704910

File tree

6 files changed

+211
-1
lines changed

6 files changed

+211
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@beeinventor/dasiot-react-component-lib",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"module": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"files": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { useState } from 'react';
2+
import { Meta, Story } from '@storybook/react';
3+
import CheckboxLight, { CheckboxLightProps } from './CheckboxLight';
4+
5+
export default {
6+
title: 'Components/Checkbox/CheckboxLight',
7+
component: CheckboxLight,
8+
argTypes: {
9+
disabled: {
10+
control: {
11+
type: 'boolean',
12+
},
13+
},
14+
},
15+
} as Meta;
16+
17+
const Template: Story<CheckboxLightProps> = (args) => {
18+
const [checked1, setChecked1] = useState(false);
19+
const [checked, setChecked] = useState(false);
20+
return (
21+
<div>
22+
<div>
23+
<CheckboxLight
24+
{...args}
25+
checked={checked1}
26+
onChange={(c) => setChecked1(c)}
27+
/>
28+
</div>
29+
<div>
30+
<CheckboxLight
31+
{...args}
32+
checked={checked}
33+
label="This is checkbox light"
34+
onChange={(c) => setChecked(c)}
35+
/>
36+
</div>
37+
<div>
38+
<CheckboxLight label="This is checkbox light with checked" checked />
39+
</div>
40+
<div>
41+
<CheckboxLight label="This is disabled checkbox light" disabled />
42+
</div>
43+
</div>
44+
);
45+
};
46+
47+
export const Default: Story<CheckboxLightProps> = Template.bind({});
48+
49+
Default.parameters = {
50+
backgrounds: {
51+
default: null,
52+
},
53+
};
54+
Default.args = {
55+
disabled: false,
56+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import React, { useRef } from 'react';
2+
import { styled, Box, BoxProps } from '@mui/material';
3+
4+
import CheckBoxSvgIcon from '../../../svg/CheckboxSvgIcon';
5+
6+
interface ContainerProps {
7+
disabled: boolean;
8+
isSelected: boolean;
9+
selectType: 'none' | 'one' | 'partial' | 'all';
10+
}
11+
12+
const Container = styled(Box, {
13+
shouldForwardProp: (prop) => {
14+
switch (prop) {
15+
case 'isSelected':
16+
case 'selectType':
17+
case 'sx':
18+
return false;
19+
default:
20+
return true;
21+
}
22+
},
23+
})<ContainerProps>`
24+
cursor: pointer;
25+
display: inline-flex;
26+
align-items: center;
27+
user-select: none;
28+
pointer-events: ${({ disabled }) => (disabled ? 'none' : 'auto')};
29+
30+
&:hover {
31+
& > .checkbox {
32+
border-color: ${({ theme }) => theme.color.primary.$60};
33+
}
34+
}
35+
36+
& > .checkbox {
37+
display: block;
38+
width: 1.5rem;
39+
height: 1.5rem;
40+
border: 2px solid ${({ theme }) => theme.color.secondary.$40};
41+
border-radius: 4px;
42+
margin: 8px;
43+
background: ${({ disabled, theme }) =>
44+
disabled ? theme.color.secondary.$40 : '#fff'};
45+
color: ${({ theme }) => theme.color.primary.$100};
46+
opacity: ${({ disabled }) => (disabled ? 0.3 : 1)};
47+
48+
${({ isSelected, selectType }) => {
49+
if (isSelected && selectType !== 'partial') {
50+
return {
51+
border: 'none',
52+
};
53+
}
54+
return undefined;
55+
}}
56+
57+
${({ theme, isSelected, selectType }) => {
58+
if (isSelected) {
59+
if (selectType === 'partial') {
60+
return {
61+
padding: '4px',
62+
'&:before': {
63+
display: 'block',
64+
content: '""',
65+
width: '100%',
66+
height: '100%',
67+
background: theme.color.primary.$100,
68+
borderRadius: '1px',
69+
},
70+
};
71+
}
72+
}
73+
return undefined;
74+
}};
75+
}
76+
77+
& > label {
78+
cursor: pointer;
79+
display: inline-block;
80+
margin-left: 8px;
81+
opacity: ${({ disabled }) => (disabled ? 0.6 : 1)};
82+
}
83+
`;
84+
85+
export interface CheckboxLightProps extends Omit<BoxProps, 'onChange'> {
86+
name?: string;
87+
label?: string;
88+
checked?: boolean;
89+
disabled?: boolean;
90+
selectType?: 'none' | 'one' | 'partial' | 'all';
91+
onChange?: (checked: boolean) => void;
92+
}
93+
94+
const CheckboxLight: React.FC<CheckboxLightProps> = ({
95+
name,
96+
label,
97+
sx,
98+
checked = false,
99+
disabled = false,
100+
selectType = 'none',
101+
onChange,
102+
}) => {
103+
const checkboxRef = useRef<HTMLInputElement>(null);
104+
105+
const handleOnClick = () => {
106+
onChange?.(!checked);
107+
};
108+
109+
return (
110+
<Container
111+
ref={checkboxRef}
112+
sx={sx}
113+
isSelected={checked}
114+
disabled={disabled}
115+
selectType={selectType}
116+
onClick={handleOnClick}
117+
data-cy={`checkbox-list-device-item${disabled ? 'disabled' : ''}`}
118+
>
119+
<div className="checkbox">
120+
{checked && selectType !== 'partial' && (
121+
<CheckBoxSvgIcon sx={{ width: '1.5rem', height: '1.5rem' }} />
122+
)}
123+
</div>
124+
{label && <label htmlFor={name}>{label}</label>}
125+
</Container>
126+
);
127+
};
128+
129+
export default CheckboxLight;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './CheckboxLight';

src/components/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { default as Step } from './Step';
1212
export { default as SearchTextField } from './TextField/SearchTextField';
1313
export { default as OrgText } from './OrgText';
1414
export { default as StatusDropdown } from './Dropdown/StatusDropdown';
15+
export { default as CheckboxLight } from './Checkbox/CheckboxLight';

src/svg/CheckboxSvgIcon.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2+
import * as React from 'react';
3+
import SvgIcon, { SvgIconProps } from '@mui/material/SvgIcon';
4+
5+
const CheckboxSvgIcon = (props: SvgIconProps) => (
6+
<SvgIcon
7+
fill="none"
8+
xmlns="http://www.w3.org/2000/svg"
9+
viewBox="0 0 24 24"
10+
{...props}
11+
>
12+
<rect width="24" height="24" rx="4" fill="#E6A600" />
13+
<path
14+
d="M7 12.6951L10.3049 16L17.5 9"
15+
stroke="white"
16+
strokeWidth="3"
17+
strokeLinecap="round"
18+
strokeLinejoin="round"
19+
/>
20+
</SvgIcon>
21+
);
22+
23+
export default CheckboxSvgIcon;

0 commit comments

Comments
 (0)