Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: checkbox component

Brad Barbin5e3fcca37864
diff --git a/apps/carbon/app/components/Selectors/UserSelect/components/SelectionList.tsx b/apps/carbon/app/components/Selectors/UserSelect/components/SelectionList.tsx
index 141ae22531..509fb85481 100644
--- a/apps/carbon/app/components/Selectors/UserSelect/components/SelectionList.tsx
+++ b/apps/carbon/app/components/Selectors/UserSelect/components/SelectionList.tsx
@@ -1,12 +1,12 @@
import {
+ Checkbox,
+ HStack,
IconButton,
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
- useColor,
} from "@carbon/react";
-import { Checkbox, List, ListItem } from "@chakra-ui/react";
import { useMemo } from "react";
import { MdOutlineClear, MdPlaylistAdd } from "react-icons/md";
import { Avatar } from "~/components";
@@ -37,42 +37,35 @@ const SelectionList = () => {
[selectionItemsById]
);
- const background = useColor("gray.100");
-
return (
<TooltipProvider>
- <List
- w="full"
- maxW={width}
- mt={1}
- maxH={selectionsMaxHeight}
- overflowY={selectionsMaxHeight ? "auto" : undefined}
+ <ul
+ className="w-full mt-1"
+ style={{
+ maxWidth: width,
+ maxHeight: selectionsMaxHeight,
+ overflowY: selectionsMaxHeight ? "auto" : undefined,
+ }}
>
{selected.map((item) => {
const id = `UserSelection:SelectedItem-${item.id}`;
const canExpand = !checkedSelections && !readOnly && isGroup(item);
return (
- <ListItem
- key={item.id}
- p={2}
- borderRadius="md"
- _hover={{ background }}
- >
- <div className="flex space-x-2">
+ <li className="p-2 rounded-md hover:bg-accent" key={item.id}>
+ <div className="flex items-center space-x-2">
{checkedSelections ? (
- <>
+ <HStack className="w-full">
<Checkbox
id={`${instanceId}:${id}:checkbox`}
data-testid={id}
isChecked={item.isChecked}
- onChange={() => onToggleChecked(item)}
- size="lg"
- flexGrow={2}
- >
- <p className="text-sm line-clamp-1">{item.label}</p>
- </Checkbox>
- </>
+ onCheckedChange={() => onToggleChecked(item)}
+ />
+ <p className="flex-grow text-sm line-clamp-1">
+ {item.label}
+ </p>
+ </HStack>
) : (
<>
{"fullName" in item ? (
@@ -123,10 +116,10 @@ const SelectionList = () => {
</Tooltip>
)}
</div>
- </ListItem>
+ </li>
);
})}
- </List>
+ </ul>
</TooltipProvider>
);
};
diff --git a/apps/carbon/app/components/Table/components/IndeterminateCheckbox.tsx b/apps/carbon/app/components/Table/components/IndeterminateCheckbox.tsx
index e872cfb4a0..bd4faf3360 100644
--- a/apps/carbon/app/components/Table/components/IndeterminateCheckbox.tsx
+++ b/apps/carbon/app/components/Table/components/IndeterminateCheckbox.tsx
@@ -1,21 +1,31 @@
-import { Checkbox } from "@chakra-ui/react";
+import { Checkbox } from "@carbon/react";
+import type { MouseEvent } from "react";
type IndeterminateCheckboxProps = {
checked: boolean;
indeterminate: boolean;
+ onChange: (e: MouseEvent<HTMLInputElement>) => void;
[key: string]: any;
};
const IndeterminateCheckbox = ({
indeterminate,
checked,
+ onChange,
...rest
}: IndeterminateCheckboxProps) => {
+ const handleChange = (checked: boolean) => {
+ onChange({
+ target: { checked },
+ } as unknown as MouseEvent<HTMLInputElement>);
+ };
+
return (
<Checkbox
- isChecked={checked}
+ isChecked={!!checked || !!indeterminate}
isIndeterminate={indeterminate}
- ml={2}
+ className="ml-2"
+ onCheckedChange={handleChange}
{...rest}
>
<span className="sr-only">Select Row</span>
diff --git a/apps/carbon/app/modules/accounting/ui/ChartOfAccounts/ChartOfAccountsTable/ChartOfAccountsTable.tsx b/apps/carbon/app/modules/accounting/ui/ChartOfAccounts/ChartOfAccountsTable/ChartOfAccountsTable.tsx
index ba0d867472..921c1182bc 100644
--- a/apps/carbon/app/modules/accounting/ui/ChartOfAccounts/ChartOfAccountsTable/ChartOfAccountsTable.tsx
+++ b/apps/carbon/app/modules/accounting/ui/ChartOfAccounts/ChartOfAccountsTable/ChartOfAccountsTable.tsx
@@ -1,5 +1,5 @@
-import { Button, HStack } from "@carbon/react";
-import { Checkbox, Link, Text } from "@chakra-ui/react";
+import { Button, Checkbox, HStack, cn } from "@carbon/react";
+import { Link } from "@chakra-ui/react";
import { Link as RemixLink, useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useMemo } from "react";
@@ -54,12 +54,12 @@ const ChartOfAccountsTable = memo(({ data }: ChartOfAccountsTableProps) => {
cell: ({ row }) => {
const isPosting = row.original.type === "Posting";
return (
- <Text
- fontWeight={isPosting ? "normal" : "bold"}
- pl={`calc(${0.75 * row.original.level}rem)`}
+ <div
+ className={cn(!isPosting && "font-bold")}
+ style={{ paddingLeft: `calc(${0.75 * row.original.level}rem)` }}
>
{row.original.name}
- </Text>
+ </div>
);
},
},
diff --git a/apps/carbon/app/modules/accounting/ui/Currencies/CurrenciesTable/CurrenciesTable.tsx b/apps/carbon/app/modules/accounting/ui/Currencies/CurrenciesTable/CurrenciesTable.tsx
index b6b08ff5f4..406651fbbc 100644
--- a/apps/carbon/app/modules/accounting/ui/Currencies/CurrenciesTable/CurrenciesTable.tsx
+++ b/apps/carbon/app/modules/accounting/ui/Currencies/CurrenciesTable/CurrenciesTable.tsx
@@ -1,4 +1,5 @@
-import { Checkbox, Link, MenuItem } from "@chakra-ui/react";
+import { Checkbox } from "@carbon/react";
+import { Link, MenuItem } from "@chakra-ui/react";
import { useNavigate } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
import { memo, useCallback, useMemo } from "react";
@@ -36,9 +37,8 @@ const CurrenciesTable = memo(({ data, count }: CurrenciesTableProps) => {
cell: (item) => item.getValue(),
},
{
- accessorKey: "symbol",
header: "Symbol",
- cell: (item) => item.getValue(),
+ cell: ({ row }) => row.original.symbol,
},
{
accessorKey: "exchangeRate",
@@ -49,7 +49,7 @@ const CurrenciesTable = memo(({ data, count }: CurrenciesTableProps) => {
accessorKey: "isBaseCurrency",
header: "Default Currency",
cell: ({ row }) => (
- <Checkbox isChecked={row.original.isBaseCurrency} isReadOnly />
+ <Checkbox isChecked={row.original.isBaseCurrency} disabled />
),
},
];
diff --git a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
index f416a3cd7b..7e9709c317 100644
--- a/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLines/PurchaseOrderLines.tsx
@@ -5,16 +5,11 @@ import {
CardContent,
CardHeader,
CardTitle,
- HStack,
-} from "@carbon/react";
-import {
Checkbox,
+ HStack,
IconButton,
- Menu,
- MenuButton,
- MenuItem,
- MenuList,
-} from "@chakra-ui/react";
+} from "@carbon/react";
+import { Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { Link, Outlet, useNavigate, useParams } from "@remix-run/react";
import type { ColumnDef } from "@tanstack/react-table";
import { useMemo } from "react";
@@ -81,7 +76,7 @@ const PurchaseOrderLines = () => {
as={IconButton}
aria-label="Edit purchase order line type"
icon={<MdMoreHoriz />}
- size="sm"
+ size="md"
position="absolute"
right={-1}
top="-6px"
diff --git a/apps/carbon/app/modules/users/ui/EmployeeTypes/EmployeeTypeForm/EmployeeTypeForm.tsx b/apps/carbon/app/modules/users/ui/EmployeeTypes/EmployeeTypeForm/EmployeeTypeForm.tsx
index 6369a8b699..f089fa1248 100644
--- a/apps/carbon/app/modules/users/ui/EmployeeTypes/EmployeeTypeForm/EmployeeTypeForm.tsx
+++ b/apps/carbon/app/modules/users/ui/EmployeeTypes/EmployeeTypeForm/EmployeeTypeForm.tsx
@@ -9,7 +9,6 @@ import {
HStack,
VStack,
} from "@carbon/react";
-import { FormLabel } from "@chakra-ui/react";
import { useNavigate } from "@remix-run/react";
import { useState } from "react";
import { ValidatedForm } from "remix-validated-form";
@@ -89,18 +88,22 @@ const EmployeeTypeForm = ({ initialValues }: EmployeeTypeFormProps) => {
/>
</VStack>
<VStack>
- <FormLabel>Default Permissions</FormLabel>
- {Object.entries(permissions)
- .sort((a, b) => a[0].localeCompare(b[0]))
- .map(([module, data], index) => (
- <div key={index}>
- <PermissionCheckboxes
- module={module}
- permissions={data.permission}
- updatePermissions={updatePermissions}
- />
- </div>
- ))}
+ <label className="block text-sm font-medium leading-none">
+ Default Permissions
+ </label>
+ <VStack spacing={8}>
+ {Object.entries(permissions)
+ .sort((a, b) => a[0].localeCompare(b[0]))
+ .map(([module, data], index) => (
+ <div key={index}>
+ <PermissionCheckboxes
+ module={module}
+ permissions={data.permission}
+ updatePermissions={updatePermissions}
+ />
+ </div>
+ ))}
+ </VStack>
</VStack>
</DrawerBody>
<DrawerFooter>
diff --git a/apps/carbon/app/modules/users/ui/Employees/BulkEditPermissionsForm/BulkEditPermissionsForm.tsx b/apps/carbon/app/modules/users/ui/Employees/BulkEditPermissionsForm/BulkEditPermissionsForm.tsx
index feb00e518d..03f868bc45 100644
--- a/apps/carbon/app/modules/users/ui/Employees/BulkEditPermissionsForm/BulkEditPermissionsForm.tsx
+++ b/apps/carbon/app/modules/users/ui/Employees/BulkEditPermissionsForm/BulkEditPermissionsForm.tsx
@@ -114,17 +114,19 @@ const BulkEditPermissions = ({
<label className="block text-sm font-medium leading-none">
Permissions
</label>
- {Object.entries(permissions)
- .sort((a, b) => a[0].localeCompare(b[0]))
- .map(([module, data], index) => (
- <div key={index}>
- <PermissionCheckboxes
- module={module}
- permissions={data}
- updatePermissions={updatePermissions}
- />
- </div>
- ))}
+ <VStack spacing={8}>
+ {Object.entries(permissions)
+ .sort((a, b) => a[0].localeCompare(b[0]))
+ .map(([module, data], index) => (
+ <div key={index}>
+ <PermissionCheckboxes
+ module={module}
+ permissions={data}
+ updatePermissions={updatePermissions}
+ />
+ </div>
+ ))}
+ </VStack>
<Hidden name="data" value={JSON.stringify(permissions)} />
</VStack>
</DrawerBody>
diff --git a/apps/carbon/app/modules/users/ui/Employees/EmployeePermissionsForm/EmployeePermissionsForm.tsx b/apps/carbon/app/modules/users/ui/Employees/EmployeePermissionsForm/EmployeePermissionsForm.tsx
index 650e10c694..50a54cd9a4 100644
--- a/apps/carbon/app/modules/users/ui/Employees/EmployeePermissionsForm/EmployeePermissionsForm.tsx
+++ b/apps/carbon/app/modules/users/ui/Employees/EmployeePermissionsForm/EmployeePermissionsForm.tsx
@@ -79,17 +79,19 @@ const EmployeePermissionsForm = ({
<label className="block text-sm font-medium leading-none">
Permissions
</label>
- {Object.entries(permissions)
- .sort((a, b) => a[0].localeCompare(b[0]))
- .map(([module, data], index) => (
- <div key={index}>
- <PermissionCheckboxes
- module={module}
- permissions={data}
- updatePermissions={updatePermissions}
- />
- </div>
- ))}
+ <VStack spacing={8}>
+ {Object.entries(permissions)
+ .sort((a, b) => a[0].localeCompare(b[0]))
+ .map(([module, data], index) => (
+ <div key={index}>
+ <PermissionCheckboxes
+ module={module}
+ permissions={data}
+ updatePermissions={updatePermissions}
+ />
+ </div>
+ ))}
+ </VStack>
<Hidden name="id" />
<Hidden name="data" value={JSON.stringify(permissions)} />
</VStack>
diff --git a/apps/carbon/app/modules/users/ui/Groups/GroupsForm/GroupsForm.tsx b/apps/carbon/app/modules/users/ui/Groups/GroupsForm/GroupsForm.tsx
index a56470dfb5..53c944abeb 100644
--- a/apps/carbon/app/modules/users/ui/Groups/GroupsForm/GroupsForm.tsx
+++ b/apps/carbon/app/modules/users/ui/Groups/GroupsForm/GroupsForm.tsx
@@ -59,6 +59,7 @@ const GroupForm = ({ initialValues }: GroupFormProps) => {
<Users
name="selections"
selectionsMaxHeight={"calc(100vh - 330px)"}
+ checkedSelections
label="Group Members"
verbose
/>
diff --git a/apps/carbon/app/modules/users/ui/components/Permission/Permission.tsx b/apps/carbon/app/modules/users/ui/components/Permission/Permission.tsx
index 8b63388c0f..dbbb6af6e3 100644
--- a/apps/carbon/app/modules/users/ui/components/Permission/Permission.tsx
+++ b/apps/carbon/app/modules/users/ui/components/Permission/Permission.tsx
@@ -1,6 +1,6 @@
-import { Box, Checkbox, Stack } from "@chakra-ui/react";
-import { capitalize } from "~/utils/string";
+import { Checkbox, HStack, VStack } from "@carbon/react";
import type { Permission } from "~/modules/users";
+import { capitalize } from "~/utils/string";
type PermissionProps = {
module: string;
@@ -26,33 +26,35 @@ const PermissionCheckboxes = ({
};
return (
- <Box mb={8}>
- <Checkbox
- isChecked={allChecked}
- isIndeterminate={isIndeterminate}
- onChange={(e) =>
- updatePermissions(module, {
- view: e.target.checked,
- create: e.target.checked,
- update: e.target.checked,
- delete: e.target.checked,
- })
- }
- >
- {capitalize(module)}
- </Checkbox>
- <Stack pl={6} mt={2} spacing={2}>
+ <div>
+ <HStack>
+ <Checkbox
+ isChecked={allChecked || isIndeterminate}
+ isIndeterminate={isIndeterminate}
+ onCheckedChange={(checked) =>
+ updatePermissions(module, {
+ view: !!checked,
+ create: !!checked,
+ update: !!checked,
+ delete: !!checked,
+ })
+ }
+ />
+ <span>{capitalize(module)}</span>
+ </HStack>
+
+ <VStack className="pl-6 mt-2">
{labels.map((verb, index) => (
- <Checkbox
- key={index}
- isChecked={permissions[verb]}
- onChange={(e) => updateByPosition(index, e.target.checked)}
- >
- {`${capitalize(labels[index])} ${capitalize(module)}`}
- </Checkbox>
+ <HStack key={index}>
+ <Checkbox
+ isChecked={permissions[verb]}
+ onCheckedChange={(checked) => updateByPosition(index, !!checked)}
+ />
+ <span>{`${capitalize(labels[index])} ${capitalize(module)}`}</span>
+ </HStack>
))}
- </Stack>
- </Box>
+ </VStack>
+ </div>
);
};
diff --git a/packages/react/src/Checkbox.tsx b/packages/react/src/Checkbox.tsx
new file mode 100644
index 0000000000..83e3df8832
--- /dev/null
+++ b/packages/react/src/Checkbox.tsx
@@ -0,0 +1,43 @@
+import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
+import type { ComponentPropsWithoutRef, ElementRef } from "react";
+import { forwardRef } from "react";
+
+import { LuCheck } from "react-icons/lu";
+import { MdOutlineHorizontalRule } from "react-icons/md";
+
+import { cn } from "~/utils/cn";
+
+export interface CheckboxProps
+ extends ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root> {
+ isChecked?: boolean;
+ isIndeterminate?: boolean;
+}
+
+const Checkbox = forwardRef<
+ ElementRef<typeof CheckboxPrimitive.Root>,
+ CheckboxProps
+>(({ isChecked, isIndeterminate, className, ...props }, ref) => (
+ <CheckboxPrimitive.Root
+ ref={ref}
+ className={cn(
+ "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",
+ isIndeterminate && "bg-primary text-primary-foreground",
+ className
+ )}
+ {...props}
+ checked={typeof isChecked === "boolean" ? isChecked : props.checked}
+ >
+ <CheckboxPrimitive.Indicator
+ className={cn("flex items-center justify-center text-current mt-[-1px]")}
+ >
+ {isIndeterminate ? (
+ <MdOutlineHorizontalRule className="w-4 h-4" />
+ ) : (
+ <LuCheck className="w-4 h-4" />
+ )}
+ </CheckboxPrimitive.Indicator>
+ </CheckboxPrimitive.Root>
+));
+Checkbox.displayName = CheckboxPrimitive.Root.displayName;
+
+export { Checkbox };
diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx
index 538f79f605..4244afecc3 100644
--- a/packages/react/src/index.tsx
+++ b/packages/react/src/index.tsx
@@ -11,6 +11,7 @@ import {
CardHeader,
CardTitle,
} from "./Card";
+import { Checkbox } from "./Checkbox";
import { ClientOnly } from "./ClientOnly";
import {
Command,
@@ -134,6 +135,7 @@ export {
CardFooter,
CardHeader,
CardTitle,
+ Checkbox,
ClientOnly,
Command,
CommandDialog,