Skip to content

Commit d2d8b88

Browse files
author
Marcella Malune
committed
checkbox migration to ChakraUI completed
1 parent 15b9295 commit d2d8b88

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

src/components/Checkbox.tsx

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
11
import React from "react"
2-
import {
3-
Checkbox as ChakraCheckbox,
4-
Icon,
5-
VisuallyHiddenInput,
6-
Text,
7-
VStack,
8-
} from "@chakra-ui/react"
2+
import { Box, VisuallyHiddenInput, Text } from "@chakra-ui/react"
93

10-
// Checkbox Container
11-
const containerProps = {
12-
display: "inline-block",
13-
verticalAlign: "middle",
14-
}
15-
16-
// Hidden input
17-
18-
// StyledCheckbox
19-
20-
// Icon
21-
const svgProps = {
22-
fill: "none",
23-
stroke: `${(props) => props.theme.colors.white}`,
24-
strokeWidth: "2px",
25-
visibility: `${(props) => (props.checked ? "visible" : "hidden")}`,
26-
}
27-
// Label
28-
29-
// Props
304
export interface IProps {
315
children?: React.ReactNode
326
callback?: () => void
@@ -35,7 +9,6 @@ export interface IProps {
359
size?: number
3610
}
3711

38-
// Checkbox
3912
const Checkbox: React.FC<IProps> = ({
4013
callback,
4114
checked,
@@ -44,38 +17,69 @@ const Checkbox: React.FC<IProps> = ({
4417
size = 2,
4518
...rest
4619
}) => {
47-
console.log({
48-
checkboxProps: {
49-
callback,
50-
checked,
51-
children,
52-
className,
53-
size,
54-
...rest,
55-
},
56-
})
57-
5820
const handleClick = () => {
5921
if (callback) {
6022
callback()
6123
}
6224
}
6325

26+
const checkboxContainerAttributes = {
27+
display: "inline-block",
28+
verticalAlign: "middle",
29+
className: className,
30+
onClick: handleClick,
31+
}
32+
33+
const hiddenCheckboxAttributes = {
34+
type: "checkbox",
35+
checked: checked,
36+
readOnly: true,
37+
...rest,
38+
}
39+
40+
const styledCheckboxAttributes = {
41+
"aria-hidden": true,
42+
className: "styled-checkbox",
43+
display: "inline-block",
44+
w: `${size}rem`,
45+
h: `${size}rem`,
46+
minW: `${size}rem`,
47+
bg: `${checked ? "primary400" : "background"}`,
48+
border: "px",
49+
borderStyle: "solid",
50+
borderColor: "black50",
51+
borderRadius: "3px",
52+
transition: "all 150ms",
53+
_hover: {
54+
boxShadow: "tableItemBoxShadow",
55+
border: "px",
56+
borderStyle: "solid",
57+
borderColor: "primary600",
58+
transition: "transform 0.1s",
59+
transform: "scale(1.02)",
60+
},
61+
}
62+
6463
return (
65-
<VStack className={className} onClick={handleClick} {...containerProps}>
66-
<VisuallyHiddenInput type="checkbox" checked={checked} readOnly />
67-
<ChakraCheckbox
68-
aria-hidden="true"
69-
checked={checked}
70-
className="styled-checkbox"
71-
size={size}
72-
>
73-
<svg checked={checked} viewBox="0 0 24 24" {...svgProps}>
64+
<Box {...checkboxContainerAttributes}>
65+
<VisuallyHiddenInput {...hiddenCheckboxAttributes} />
66+
<Box {...styledCheckboxAttributes}>
67+
<svg
68+
viewBox="0 0 24 24"
69+
fill="none"
70+
stroke="white"
71+
strokeWidth={0.5}
72+
visibility={`${checked ? "visible" : "hidden"}`}
73+
>
7474
<polyline points="20 6 9 17 4 12" />
7575
</svg>
76-
</ChakraCheckbox>
77-
{children && <Text>{children}</Text>}
78-
</VStack>
76+
</Box>
77+
{children && (
78+
<Text as="span" ml={2}>
79+
{children}
80+
</Text>
81+
)}
82+
</Box>
7983
)
8084
}
8185

0 commit comments

Comments
 (0)