Skip to content

Commit 409e711

Browse files
authored
Merge pull request #62 from BeeInventor/feature/dropdown-checkbox
add: dropdown checkbox
2 parents 8704910 + 0e0cde7 commit 409e711

File tree

9 files changed

+450
-11152
lines changed

9 files changed

+450
-11152
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.7.0",
3+
"version": "1.7.1",
44
"module": "lib/index.js",
55
"types": "lib/index.d.ts",
66
"files": [
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+
value?: any;
92+
onChange?: (value: any, checked: boolean) => void;
93+
}
94+
95+
const CheckboxLight: React.FC<CheckboxLightProps> = ({
96+
name,
97+
label,
98+
sx,
99+
value,
100+
checked = false,
101+
disabled = false,
102+
selectType = 'none',
103+
onChange,
104+
}) => {
105+
const checkboxRef = useRef<HTMLInputElement>(null);
106+
107+
return (
108+
<Container
109+
ref={checkboxRef}
110+
sx={sx}
111+
isSelected={checked}
112+
disabled={disabled}
113+
selectType={selectType}
114+
onClick={() => {
115+
onChange?.(value, !checked);
116+
}}
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,72 @@
1+
import { Meta, Story } from '@storybook/react';
2+
import React, { useState } from 'react';
3+
import DropdownCheckbox from './DropdownCheckbox';
4+
import {
5+
DropdownCheckboxItem,
6+
DropdownCheckboxProps,
7+
} from './DropdownColor.type';
8+
9+
const list: DropdownCheckboxItem[] = [
10+
{
11+
id: 'floor-1',
12+
value: 'floor-1',
13+
name: 'Floor 1',
14+
},
15+
{
16+
id: 'floor-2',
17+
value: 'floor-2',
18+
name: 'Floor 2',
19+
},
20+
{
21+
id: 'floor-3',
22+
value: 'floor-3',
23+
name: 'Floor 3',
24+
},
25+
];
26+
27+
export default {
28+
title: 'Components/Dropdown/DropdownCheckbox',
29+
component: DropdownCheckbox,
30+
argTypes: {
31+
onSelect: { action: 'onSelected' },
32+
disabled: {
33+
control: 'boolean',
34+
},
35+
className: {
36+
control: 'text',
37+
},
38+
listClassName: {
39+
control: 'text',
40+
},
41+
itemClassName: {
42+
control: 'text',
43+
},
44+
selectedId: {
45+
control: 'text',
46+
},
47+
mode: {
48+
control: 'radio',
49+
options: ['dark', 'light'],
50+
},
51+
},
52+
} as Meta;
53+
54+
export const Default: Story<DropdownCheckboxProps> = (args) => {
55+
const [selectedIds, setSelectedIds] = useState<string[]>();
56+
57+
return (
58+
<div>
59+
<DropdownCheckbox
60+
{...args}
61+
selectedIds={selectedIds}
62+
onSelect={(values) => setSelectedIds(values as string[])}
63+
/>
64+
</div>
65+
);
66+
};
67+
68+
Default.args = {
69+
mode: 'dark',
70+
list,
71+
placeholder: 'Please Select Item',
72+
};

0 commit comments

Comments
 (0)