@stackshift-ui/checkbox 6.0.11-beta.2 → 7.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stackshift-ui/checkbox",
3
3
  "description": "",
4
- "version": "6.0.11-beta.2",
4
+ "version": "7.0.0-beta.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "main": "./dist/index.js",
@@ -29,20 +29,23 @@
29
29
  "typescript": "^5.6.2",
30
30
  "vite-tsconfig-paths": "^5.0.1",
31
31
  "vitest": "^2.1.1",
32
- "@stackshift-ui/typescript-config": "6.0.10-beta.2",
33
- "@stackshift-ui/eslint-config": "6.0.10-beta.2"
32
+ "@stackshift-ui/eslint-config": "6.0.10",
33
+ "@stackshift-ui/typescript-config": "6.0.10"
34
34
  },
35
35
  "dependencies": {
36
+ "@radix-ui/react-checkbox": "^1.3.2",
37
+ "@testing-library/user-event": "^14.6.1",
36
38
  "classnames": "^2.5.1",
37
- "@stackshift-ui/system": "6.0.11-beta.2",
38
- "@stackshift-ui/scripts": "6.0.10-beta.2"
39
+ "lucide-react": "^0.468.0",
40
+ "@stackshift-ui/system": "6.1.0-beta.0",
41
+ "@stackshift-ui/scripts": "6.1.0-beta.0"
39
42
  },
40
43
  "peerDependencies": {
44
+ "@stackshift-ui/system": ">=6.1.0-beta.0",
41
45
  "@types/react": "16.8 - 19",
42
46
  "next": "10 - 14",
43
47
  "react": "16.8 - 19",
44
- "react-dom": "16.8 - 19",
45
- "@stackshift-ui/system": ">=6.0.11-beta.2"
48
+ "react-dom": "16.8 - 19"
46
49
  },
47
50
  "peerDependenciesMeta": {
48
51
  "next": {
@@ -1,4 +1,5 @@
1
- import { cleanup, render, screen } from "@testing-library/react";
1
+ import { cleanup, render, screen, waitFor } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
2
3
  import { afterEach, describe, test } from "vitest";
3
4
  import { Checkbox } from "./checkbox";
4
5
 
@@ -7,7 +8,36 @@ describe.concurrent("checkbox", () => {
7
8
 
8
9
  test("Common: Checkbox - test if renders without errors", ({ expect }) => {
9
10
  const clx = "checkbox-class";
10
- render(<Checkbox className={clx} />);
11
- expect(screen.getByTestId("label").classList).toBeDefined();
11
+ const { unmount } = render(<Checkbox aria-label="Test checkbox" className={clx} />);
12
+
13
+ const checkbox = screen.getByRole("checkbox");
14
+ expect(checkbox.classList).toContain(clx);
15
+ expect(checkbox.ariaLabel).toBe("Test checkbox");
16
+ unmount();
17
+ });
18
+
19
+ test("Common: Checkbox - test if renders with correct state", ({ expect }) => {
20
+ const { unmount } = render(
21
+ <Checkbox data-testid="checked-checkbox" aria-label="Test checkbox" defaultChecked />,
22
+ );
23
+
24
+ const checkbox = screen.getByTestId<HTMLButtonElement>("checked-checkbox");
25
+ expect(checkbox.getAttribute("aria-checked")).toBe("true");
26
+ unmount();
27
+ });
28
+
29
+ test("Common: Checkbox - test if clicking checkbox changes state", async ({ expect }) => {
30
+ const user = userEvent.setup();
31
+ const { unmount } = render(
32
+ <Checkbox data-testid="unchecked-checkbox" aria-label="Test checkbox" />,
33
+ );
34
+
35
+ const checkbox = screen.getByTestId<HTMLButtonElement>("unchecked-checkbox");
36
+ expect(checkbox.getAttribute("aria-checked")).toBe("false");
37
+
38
+ await user.click(checkbox);
39
+
40
+ waitFor(() => expect(checkbox.getAttribute("aria-checked")).toBe("true"));
41
+ unmount();
12
42
  });
13
43
  });
package/src/checkbox.tsx CHANGED
@@ -1,71 +1,32 @@
1
- import { DefaultComponent, useStackShiftUIComponents } from "@stackshift-ui/system";
2
- import cn from "classnames";
3
- import type { ElementType, HTMLProps } from "react";
1
+ import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
2
+ import { Check } from "lucide-react";
3
+ import * as React from "react";
4
4
 
5
- export type StyleVariants<T extends string> = Record<T, string>;
6
- type Variant = "primary";
7
-
8
- export interface CheckboxProps extends Omit<HTMLProps<HTMLInputElement>, "as"> {
9
- required?: boolean;
10
- name?: string;
11
- ariaLabel?: string;
12
- label?: string;
13
- labelClass?: string;
14
- item?: string;
15
- variant?: Variant;
16
- onChange?: () => void;
17
- className?: string;
18
- as?: ElementType;
19
- }
5
+ import { cn, DefaultComponent, useStackShiftUIComponents } from "@stackshift-ui/system";
20
6
 
21
7
  const displayName = "Checkbox";
22
8
 
23
- export const Checkbox: React.FC<CheckboxProps> = ({
24
- required = false,
25
- name,
26
- ariaLabel,
27
- label,
28
- labelClass,
29
- item,
30
- variant = "primary",
31
- onChange,
32
- className,
33
- as = "label",
34
- ...props
35
- }) => {
9
+ const Checkbox = React.forwardRef<
10
+ React.ElementRef<typeof CheckboxPrimitive.Root>,
11
+ React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
12
+ >(({ className, ...props }, ref) => {
36
13
  const { [displayName]: Component = DefaultComponent } = useStackShiftUIComponents();
37
14
 
38
- const commonStyle = "";
39
- const primary = `${commonStyle}`;
40
-
41
- const variants: StyleVariants<Variant> = {
42
- primary,
43
- };
44
-
45
- const variantClass = variants[variant] ?? primary;
46
- const defaultLabelClass = "flex gap-2 items-center";
47
-
48
15
  return (
49
16
  <Component
50
- as={as}
51
- htmlFor={item}
52
- className={cn(defaultLabelClass, labelClass)}
53
- {...props}
54
- data-testid={displayName}>
55
- <input
56
- aria-label={ariaLabel || name}
57
- className={cn(variantClass, className)}
58
- name={name}
59
- type="checkbox"
60
- value={item}
61
- required={required}
62
- onChange={onChange}
63
- id={item}
64
- {...props}
65
- />
66
- {label || item}
17
+ as={CheckboxPrimitive.Root}
18
+ ref={ref}
19
+ className={cn(
20
+ "peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
21
+ className,
22
+ )}
23
+ {...props}>
24
+ <CheckboxPrimitive.Indicator className={cn("flex items-center justify-center text-current")}>
25
+ <Check className="h-4 w-4" />
26
+ </CheckboxPrimitive.Indicator>
67
27
  </Component>
68
28
  );
69
- };
70
-
29
+ });
71
30
  Checkbox.displayName = displayName;
31
+
32
+ export { Checkbox };