Skip to content

Commit

Permalink
fix: improve checkbox component
Browse files Browse the repository at this point in the history
  • Loading branch information
aizad-deriv committed Feb 8, 2024
1 parent 7f6cca0 commit c193b31
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
22 changes: 14 additions & 8 deletions lib/components/Checkbox/Checkbox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ $error_color: #ec3f3f;
.deriv-checkbox__wrapper {
display: inline-flex;
position: relative;
width: fit-content;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
}

.deriv-checkbox {
width: 16px;
height: 16px;
border-radius: $border_radius;
transition: background-color 0.3s ease-in-out;
cursor: pointer;
box-sizing: border-box;

&--unchecked {
background: transparent;
Expand All @@ -36,7 +35,6 @@ $error_color: #ec3f3f;

&--disabled {
opacity: 0.5;
cursor: not-allowed;
}

&__label {
Expand All @@ -46,12 +44,20 @@ $error_color: #ec3f3f;
line-height: 20px;
font-weight: 400;

&--disabled {
cursor: text;
}

&--error {
color: $error_color;
}
}
}

.deriv-checkbox:not(:disabled),
.deriv-checkbox:not(:disabled) + .deriv-checkbox__wrapper,
.deriv-checkbox:not(:disabled) ~ .deriv-checkbox__label {
cursor: pointer;
}

.deriv-checkbox:disabled,
.deriv-checkbox:disabled + .deriv-checkbox__wrapper,
.deriv-checkbox--disabled ~ .deriv-checkbox__label {
cursor: not-allowed;
}
5 changes: 4 additions & 1 deletion lib/components/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ComponentProps, ReactNode, Ref, forwardRef } from "react";
import clsx from "clsx";
import "./Checkbox.scss";

interface CheckboxProps extends Omit<ComponentProps<"input">, "placeholder"> {
interface CheckboxProps
extends Omit<ComponentProps<"input">, "placeholder" | "defaultChecked"> {
error?: boolean;
label?: ReactNode;
labelClassName?: string;
Expand Down Expand Up @@ -38,6 +39,8 @@ export const Checkbox = forwardRef(
className
)}
type="checkbox"
checked={!disabled && checked}
disabled={disabled}
ref={ref}
{...rest}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/stories/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export const Default: Story = {
label: "Get updates about Deriv products, services and events.",
},
render: (args) => {
const [checked, setChecked] = useState(false);
const [checked, setChecked] = useState(args.checked);

return (
<Checkbox
{...args}
checked={checked}
checked={args.checked || checked}
onChange={() => setChecked((previous) => !previous)}
/>
);
Expand Down

0 comments on commit c193b31

Please sign in to comment.