diff --git a/src/app/sandbox/checkbox/page.tsx b/src/app/sandbox/checkbox/page.tsx index 8afb02cf..0cea77da 100644 --- a/src/app/sandbox/checkbox/page.tsx +++ b/src/app/sandbox/checkbox/page.tsx @@ -5,37 +5,83 @@ import { useState } from "react"; function CheckboxPage() { const [checked, setChecked] = useState(false); + const [checkedA, setCheckedA] = useState(false); + const [checkedB, setCheckedB] = useState(true); return (

Checkbox demo

- {/* hui */}
- + A regular checkbox +
- + Checkbox is: {checked ? checked : not checked}
-
- +
+ Indeterminate checkbox + { + if (checkedA && checkedB) { + setCheckedA(false); + setCheckedB(false); + } else { + setCheckedA(true); + setCheckedB(true); + } + }} + /> + +
-
- + +
+ A disabled unchecked checkbox + {}} + />
-
+
+ A disabled checked checkbox setChecked(value)} - checked={checked} + label="Cannot uncheck me" + disabled + checked + onChange={() => {}} />
-
- State of last checkbox is:{" "} - {checked ? checked : not checked} + +
+ A disabled indeterminate checkbox + {}} + />
@@ -43,14 +89,11 @@ function CheckboxPage() {
  • Click area for checkbox is expanded with negative margins
  • -
  • - Indeterminate and error states are not defined (designed). They - will be added if/when needed. -
); } + export default CheckboxPage; diff --git a/src/components/ui/checkbox.tsx b/src/components/ui/checkbox.tsx index 08dd621b..2c4caa63 100644 --- a/src/components/ui/checkbox.tsx +++ b/src/components/ui/checkbox.tsx @@ -3,9 +3,10 @@ import { Switch } from "@headlessui/react"; type TCheckboxProps = { disabled?: boolean; label: string; - onChange?: (value: boolean) => void; - checked?: boolean; - hideLabel?: boolean; + onChange: (value: boolean) => void; + checked: boolean; + indeterminate?: boolean; + hideLabel?: boolean | string; }; function Checkbox({ @@ -13,48 +14,57 @@ function Checkbox({ label, onChange, checked = false, + indeterminate = false, hideLabel = false, }: TCheckboxProps) { + let hideLabelClass = ""; + if (hideLabel === true) { + hideLabelClass = "sr-only"; + } else if (hideLabel !== false) { + hideLabelClass = hideLabel; + } else { + hideLabelClass = ""; + } + + let squareStyle; + if (indeterminate || checked) { + if (disabled) { + squareStyle = "bg-neutral-300"; + } else { + squareStyle = "bg-blue-500 group-active:bg-blue-600"; + } + } else { + if (disabled) { + squareStyle = "border border-neutral-300 bg-neutral-100"; + } else { + squareStyle = + "border border-neutral-400 bg-white group-active:border-neutral-600"; + } + } + return (
- {({ checked }) => ( - <> -
- - - -
- - {label} - - - )} +
+ + {indeterminate ? ( + + ) : checked ? ( + + ) : null} + +
+ {label}
);