Skip to content
Snippets Groups Projects
Commit ee9562f6 authored by Alissa Cheng's avatar Alissa Cheng
Browse files

feat: toggle disabled state

parent ae89d321
No related branches found
No related tags found
1 merge request!17form components
Pipeline #66992 passed
import { Dispatch, SetStateAction } from "react"; import { Dispatch, SetStateAction } from "react";
import { Switch } from "@nextui-org/react"; import { Switch } from "@nextui-org/react";
import { disabledCursor } from "./utils/classes.ts";
type ToggleProps = { type ToggleProps = {
isDisabled?: boolean;
isSelected: boolean; isSelected: boolean;
setIsSelected: Dispatch<SetStateAction<boolean>>; setIsSelected?: Dispatch<SetStateAction<boolean>>;
label?: string; label?: string;
}; };
const Toggle = ({ isSelected, setIsSelected, label }: ToggleProps) => { const Toggle = ({
isDisabled = false,
isSelected,
setIsSelected,
label,
}: ToggleProps) => {
const selectedClass = isSelected const selectedClass = isSelected
? "group-data-[selected=true]:ml-0 bg-primary" ? "group-data-[selected=true]:ml-0 bg-primary"
: "ml-[11px] bg-grey"; : "ml-[11px] bg-grey";
return ( return (
<Switch <Switch
isDisabled={isDisabled}
// Does not support "isRequired": https://github.com/nextui-org/nextui/issues/1610 // Does not support "isRequired": https://github.com/nextui-org/nextui/issues/1610
isSelected={isSelected} isSelected={isSelected}
onValueChange={setIsSelected} onValueChange={setIsSelected}
color="default" color="default"
classNames={{ classNames={{
base: disabledCursor(isDisabled),
thumb: `h-[14px] w-[14px] ${selectedClass}`, thumb: `h-[14px] w-[14px] ${selectedClass}`,
wrapper: wrapper:
"h-[8px] w-[25px] p-0 overflow-visible bg-lightGrey dark:bg-mediumGrey", "h-[8px] w-[25px] p-0 overflow-visible bg-lightGrey dark:bg-mediumGrey",
......
export const disabledCursor = (isDisabled: boolean) => { export const disabledCursor = (isDisabled: boolean) => {
if (!isDisabled) return ""; if (!isDisabled) return "";
return "pointer-events-auto cursor-not-allowed opacity-100"; return "pointer-events-auto cursor-not-allowed opacity-50";
}; };
export const disabledBg = (isDisabled: boolean) => { export const disabledBg = (isDisabled: boolean) => {
......
...@@ -19,3 +19,12 @@ export const SimpleToggle = () => { ...@@ -19,3 +19,12 @@ export const SimpleToggle = () => {
/> />
); );
}; };
export const DisabledToggle = () => {
return (
<div className="flex flex-col gap-4">
<Toggle isDisabled={true} isSelected={true} label="I'm always right!" />
<Toggle isDisabled={true} isSelected={false} label="I'm always left!" />
</div>
);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment