From 5e3a6ad2b57ac78d985156c55ec3f353c90db55a Mon Sep 17 00:00:00 2001 From: Wahyu Kurniawan Date: Tue, 20 Feb 2024 18:24:28 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20feat:=20create=20checkbox?= =?UTF-8?q?=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shared/Checkbox/Checkbox.theme.ts | 68 +++++++++++++++++ .../components/shared/Checkbox/Checkbox.tsx | 76 +++++++++++++++++++ .../src/components/shared/Checkbox/index.ts | 1 + 3 files changed, 145 insertions(+) create mode 100644 packages/frontend/src/components/shared/Checkbox/Checkbox.theme.ts create mode 100644 packages/frontend/src/components/shared/Checkbox/Checkbox.tsx create mode 100644 packages/frontend/src/components/shared/Checkbox/index.ts diff --git a/packages/frontend/src/components/shared/Checkbox/Checkbox.theme.ts b/packages/frontend/src/components/shared/Checkbox/Checkbox.theme.ts new file mode 100644 index 00000000..7122f656 --- /dev/null +++ b/packages/frontend/src/components/shared/Checkbox/Checkbox.theme.ts @@ -0,0 +1,68 @@ +import { tv, type VariantProps } from 'tailwind-variants'; + +export const getCheckboxVariant = tv({ + slots: { + wrapper: ['group', 'flex', 'gap-3'], + indicator: [ + 'grid', + 'place-content-center', + 'text-transparent', + 'group-hover:text-controls-disabled', + 'focus-visible:text-controls-disabled', + 'group-focus-visible:text-controls-disabled', + 'data-[state=checked]:text-elements-on-primary', + 'data-[state=checked]:group-focus-visible:text-elements-on-primary', + 'data-[state=indeterminate]:text-elements-on-primary', + 'data-[state=checked]:data-[disabled]:text-elements-on-disabled-active', + ], + icon: ['w-3', 'h-3', 'stroke-current', 'text-current'], + input: [ + 'h-5', + 'w-5', + 'group', + 'border', + 'border-border-interactive/10', + 'bg-controls-tertiary', + 'rounded-md', + 'transition-all', + 'duration-150', + 'focus-ring', + 'shadow-button', + 'group-hover:border-border-interactive/[0.14]', + 'group-hover:bg-controls-tertiary', + 'data-[state=checked]:bg-controls-primary', + 'data-[state=checked]:hover:bg-controls-primary-hovered', + 'data-[state=checked]:focus-visible:bg-controls-primary-hovered', + 'data-[disabled]:bg-controls-disabled', + 'data-[disabled]:shadow-none', + 'data-[disabled]:hover:border-border-interactive/10', + 'data-[disabled]:cursor-not-allowed', + 'data-[state=checked]:data-[disabled]:bg-controls-disabled-active', + ], + label: [ + 'text-sm', + 'tracking-[-0.006em]', + 'text-elements-high-em', + 'flex', + 'flex-col', + 'gap-1', + 'px-1', + ], + description: ['text-xs', 'text-elements-low-em'], + }, + variants: { + disabled: { + true: { + wrapper: ['cursor-not-allowed'], + indicator: ['group-hover:text-current'], + input: [ + 'group-hover:border-border-interactive/[0.14]', + 'group-hover:bg-controls-disabled', + ], + label: ['cursor-not-allowed'], + }, + }, + }, +}); + +export type CheckboxVariants = VariantProps; diff --git a/packages/frontend/src/components/shared/Checkbox/Checkbox.tsx b/packages/frontend/src/components/shared/Checkbox/Checkbox.tsx new file mode 100644 index 00000000..0ba9d825 --- /dev/null +++ b/packages/frontend/src/components/shared/Checkbox/Checkbox.tsx @@ -0,0 +1,76 @@ +import React from 'react'; +import * as CheckboxRadix from '@radix-ui/react-checkbox'; +import { type CheckboxProps as CheckboxRadixProps } from '@radix-ui/react-checkbox'; + +import { getCheckboxVariant, type CheckboxVariants } from './Checkbox.theme'; +import { CheckIcon } from 'components/shared/CustomIcon'; + +interface CheckBoxProps extends CheckboxRadixProps, CheckboxVariants { + /** + * The label of the checkbox. + */ + label?: string; + /** + * The description of the checkbox. + */ + description?: string; +} + +/** + * Checkbox component is used to allow users to select one or more items from a set. + * It is a wrapper around the `@radix-ui/react-checkbox` component. + * + * It accepts all the props from `@radix-ui/react-checkbox` component and the variants from the theme. + * + * It also accepts `label` and `description` props to display the label and description of the checkbox. + * + * @example + * ```tsx + * + * ``` + */ +export const Checkbox = ({ + id, + className, + label, + description, + onCheckedChange, + ...props +}: CheckBoxProps) => { + const { + wrapper: wrapperStyle, + indicator: indicatorStyle, + icon: iconStyle, + input: inputStyle, + label: labelStyle, + description: descriptionStyle, + } = getCheckboxVariant({ + disabled: props?.disabled, + }); + return ( +
+ + + + + + {label && ( + + )} +
+ ); +}; diff --git a/packages/frontend/src/components/shared/Checkbox/index.ts b/packages/frontend/src/components/shared/Checkbox/index.ts new file mode 100644 index 00000000..f5c939fa --- /dev/null +++ b/packages/frontend/src/components/shared/Checkbox/index.ts @@ -0,0 +1 @@ +export * from './Checkbox';