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: switch component

Brad Barbind3e548691bc4
diff --git a/apps/carbon/app/components/Form/Boolean.tsx b/apps/carbon/app/components/Form/Boolean.tsx
index 595ebb5ecc..6e35c3d429 100644
--- a/apps/carbon/app/components/Form/Boolean.tsx
+++ b/apps/carbon/app/components/Form/Boolean.tsx
@@ -4,15 +4,10 @@ import {
FormHelperText,
FormLabel,
HStack,
+ Switch,
} from "@carbon/react";
-import type { SwitchProps, SystemStyleObject } from "@chakra-ui/react";
-import {
- chakra,
- omitThemingProps,
- useCheckbox,
- useMultiStyleConfig,
-} from "@chakra-ui/react";
-import { forwardRef, useMemo } from "react";
+import type { SwitchProps } from "@chakra-ui/react";
+import { forwardRef } from "react";
import { useControlField, useField } from "remix-validated-form";
type FormBooleanProps = Omit<SwitchProps, "onChange"> & {
@@ -28,80 +23,26 @@ const Boolean = forwardRef<HTMLInputElement, FormBooleanProps>(
const { getInputProps, error } = useField(name);
const [value, setValue] = useControlField<boolean>(name);
- const styles = useMultiStyleConfig("Switch", {
- colorScheme: "green",
- ...props,
- });
- const { ...ownProps } = omitThemingProps(props);
-
- const { getCheckboxProps, getRootProps, getLabelProps } = useCheckbox({
- ...ownProps,
- isChecked: value,
- });
-
- const containerStyles: SystemStyleObject = useMemo(
- () => ({
- display: "inline-block",
- position: "relative",
- verticalAlign: "middle",
- lineHeight: 0,
- ...styles.container,
- }),
- [styles.container]
- );
-
- const trackStyles: SystemStyleObject = useMemo(
- () => ({
- display: "inline-flex",
- flexShrink: 0,
- justifyContent: "flex-start",
- boxSizing: "content-box",
- cursor: "pointer",
- ...styles.track,
- }),
- [styles.track]
- );
-
return (
<FormControl isInvalid={!!error} className="pt-2">
{label && <FormLabel htmlFor={name}>{label}</FormLabel>}
- <HStack className="w-full">
- <chakra.label
- {...getRootProps()}
- className="chakra-switch"
- __css={containerStyles}
- >
- <span className="sr-only">
- <input
- ref={ref}
- type="checkbox"
- disabled={props.isDisabled}
- {...getInputProps({
- type: "checkbox",
- id: name,
- })}
- onChange={(e) => {
- setValue(e.target.checked);
- onChange?.(e.target.checked);
- }}
- />
- </span>
- <HStack>
- <chakra.span
- {...getCheckboxProps()}
- className="chakra-switch__track"
- __css={trackStyles}
- >
- <chakra.span
- __css={styles.thumb}
- className="chakra-switch__thumb"
- data-checked={value ? "" : undefined}
- />
- </chakra.span>
- {description && <p {...getLabelProps()}>{description}</p>}
- </HStack>
- </chakra.label>
+ <HStack>
+ {/* @ts-ignore */}
+ <Switch
+ {...getInputProps()}
+ checked={value}
+ onCheckedChange={(checked) => {
+ setValue(checked);
+ onChange?.(checked);
+ }}
+ aria-label={label}
+ {...props}
+ />
+ {description && (
+ <p className="text-muted-foreground text-sm">{description}</p>
+ )}
</HStack>
+
{error ? (
<FormErrorMessage>{error}</FormErrorMessage>
) : (
diff --git a/apps/carbon/app/components/Table/components/Sort/Sort.tsx b/apps/carbon/app/components/Table/components/Sort/Sort.tsx
index 3bee9d3b6f..8c882b7cf5 100644
--- a/apps/carbon/app/components/Table/components/Sort/Sort.tsx
+++ b/apps/carbon/app/components/Table/components/Sort/Sort.tsx
@@ -1,4 +1,4 @@
-import { Button, HStack, IconButton } from "@carbon/react";
+import { Button, HStack, IconButton, Switch } from "@carbon/react";
import {
Menu,
MenuButton,
@@ -11,7 +11,6 @@ import {
PopoverFooter,
PopoverHeader,
PopoverTrigger,
- Switch,
} from "@chakra-ui/react";
import { Reorder } from "framer-motion";
import { BsChevronDown, BsListUl } from "react-icons/bs";
@@ -75,8 +74,8 @@ const Sort = ({ columnAccessors }: SortProps) => {
<>{columnAccessors[column] ?? ""}</>
</span>
<Switch
- isChecked={direction === "asc"}
- onChange={() => toggleSortByDirection(column)}
+ checked={direction === "asc"}
+ onCheckedChange={() => toggleSortByDirection(column)}
/>
<span className="text-sm text-muted-foreground">
Ascending
diff --git a/apps/carbon/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx b/apps/carbon/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
index cab576b9ec..3bb3995b54 100644
--- a/apps/carbon/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
+++ b/apps/carbon/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
@@ -1,5 +1,12 @@
-import { Button, HStack, VStack, useColor, useDisclosure } from "@carbon/react";
-import { Grid, Switch } from "@chakra-ui/react";
+import {
+ Button,
+ HStack,
+ Switch,
+ VStack,
+ useColor,
+ useDisclosure,
+} from "@carbon/react";
+import { Grid } from "@chakra-ui/react";
import { parseDate } from "@internationalized/date";
import { useFetcher, useParams } from "@remix-run/react";
import { useState } from "react";
@@ -438,11 +445,11 @@ function renderTypedDisplay({
pt={3}
w="full"
>
- <p className="text-muted-foreground self-center">{attribute.name}</p>
+ <p className="text-muted-foreground items-center">{attribute.name}</p>
{displayValue === "N/A" ? (
<p className="self-center">{displayValue}</p>
) : (
- <Switch isReadOnly isChecked={displayValue === true} />
+ <Switch disabled checked={displayValue === true} />
)}
<HStack className="justify-between w-full self-center">
<Button
diff --git a/apps/carbon/app/modules/resources/ui/Attributes/AttributeForm/AttributeForm.tsx b/apps/carbon/app/modules/resources/ui/Attributes/AttributeForm/AttributeForm.tsx
index dff7cc1b65..e9f25df14f 100644
--- a/apps/carbon/app/modules/resources/ui/Attributes/AttributeForm/AttributeForm.tsx
+++ b/apps/carbon/app/modules/resources/ui/Attributes/AttributeForm/AttributeForm.tsx
@@ -49,7 +49,7 @@ const AttributeForm = ({
const options =
dataTypes?.map((dt) => ({
- value: dt.id,
+ value: dt.id.toString(),
label: dt.label,
})) ?? [];
@@ -63,7 +63,7 @@ const AttributeForm = ({
);
const onChangeCheckForListType = (selected: {
- value: string | number;
+ value: string;
label: string;
}) => {
setIsList(Number(selected.value) === DataType.List);
diff --git a/packages/react/src/Switch.tsx b/packages/react/src/Switch.tsx
new file mode 100644
index 0000000000..d3c572fe8b
--- /dev/null
+++ b/packages/react/src/Switch.tsx
@@ -0,0 +1,30 @@
+"use client";
+
+import * as SwitchPrimitives from "@radix-ui/react-switch";
+import type { ComponentPropsWithoutRef, ElementRef } from "react";
+import { forwardRef } from "react";
+
+import { cn } from "~/utils/cn";
+
+const Switch = forwardRef<
+ ElementRef<typeof SwitchPrimitives.Root>,
+ ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
+>(({ className, ...props }, ref) => (
+ <SwitchPrimitives.Root
+ className={cn(
+ "peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
+ className
+ )}
+ {...props}
+ ref={ref}
+ >
+ <SwitchPrimitives.Thumb
+ className={cn(
+ "pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0"
+ )}
+ />
+ </SwitchPrimitives.Root>
+));
+Switch.displayName = SwitchPrimitives.Root.displayName;
+
+export { Switch };
diff --git a/packages/react/src/index.tsx b/packages/react/src/index.tsx
index 94f71c6b8d..5399b96d57 100644
--- a/packages/react/src/index.tsx
+++ b/packages/react/src/index.tsx
@@ -98,6 +98,7 @@ import {
} from "./Select";
import { Status } from "./Status";
import Submit from "./Submit";
+import { Switch } from "./Switch";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./Tabs";
import ThemeProvider, { theme } from "./Theme";
import {
@@ -209,6 +210,7 @@ export {
SelectValue,
Status,
Submit,
+ Switch,
Tabs,
TabsContent,
TabsList,