Skip to content

Commit 4358f78

Browse files
committed
wip: CheckboxGroup
1 parent a955d8c commit 4358f78

File tree

3 files changed

+16
-94
lines changed

3 files changed

+16
-94
lines changed

app/components/ui/checkbox-group.stories.tsx

Lines changed: 13 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useState } from 'react';
33

44
import { Checkbox } from '@/components/ui/checkbox';
55
import { CheckboxGroup } from '@/components/ui/checkbox-group';
6-
76
export default {
87
title: 'CheckboxGroup',
98
component: CheckboxGroup,
@@ -67,101 +66,24 @@ export const DisabledOption = () => {
6766
);
6867
};
6968

70-
const nestedBears = [
71-
{
72-
label: 'Bear 1',
73-
value: 'bear-1',
74-
children: null,
75-
},
76-
{
77-
label: 'Bear 2',
78-
value: 'bear-2',
79-
children: null,
80-
},
81-
{
82-
label: 'Bear 3',
83-
value: 'bear-3',
84-
children: [
85-
{
86-
label: 'Little bear 1',
87-
value: 'little-bear-1',
88-
},
89-
{
90-
label: 'Little bear 2',
91-
value: 'little-bear-2',
92-
},
93-
{
94-
label: 'Little bear 3',
95-
value: 'little-bear-3',
96-
},
97-
],
98-
},
99-
] as const;
100-
101-
const bears = nestedBears.map((bear) => bear.value);
102-
const littleBears = nestedBears[2].children.map((bear) => bear.value);
103-
export const WithNestedGroups = () => {
104-
const [bearsValue, setBearsValue] = useState<string[]>([]);
105-
const [littleBearsValue, setLittleBearsValue] = useState<string[]>([]);
69+
export const ParentCheckbox = () => {
70+
const [value, setValue] = useState<string[]>([]);
10671

10772
return (
10873
<CheckboxGroup
109-
value={bearsValue}
110-
onValueChange={(value) => {
111-
if (value.includes('bear-3')) {
112-
setLittleBearsValue(littleBears);
113-
} else if (littleBearsValue.length === littleBears.length) {
114-
setLittleBearsValue([]);
115-
}
116-
setBearsValue(value);
117-
}}
118-
allValues={bears}
119-
defaultValue={[]}
74+
value={value}
75+
onValueChange={setValue}
76+
allValues={astrobears.map((bear) => bear.value)}
12077
>
121-
<Checkbox parent>Astrobears</Checkbox>
78+
<Checkbox name="astrobears" parent>
79+
Astrobears
80+
</Checkbox>
12281
<div className="pl-4">
123-
{nestedBears.map((option) => {
124-
if (!option.children) {
125-
return (
126-
<Checkbox key={option.value} value={option.value}>
127-
{option.label}
128-
</Checkbox>
129-
);
130-
}
131-
132-
return (
133-
<CheckboxGroup
134-
key={option.value}
135-
value={littleBearsValue}
136-
onValueChange={(value) => {
137-
if (value.length === littleBears.length) {
138-
setBearsValue((prev) =>
139-
Array.from(new Set([...prev, 'bear-3']))
140-
);
141-
} else {
142-
setBearsValue((prev) => prev.filter((v) => v !== 'bear-3'));
143-
}
144-
setLittleBearsValue(value);
145-
}}
146-
allValues={option.children.map((bear) => bear.value)}
147-
defaultValue={[]}
148-
>
149-
<Checkbox parent>{option.label}</Checkbox>
150-
<div className="pl-4">
151-
{option.children.map((nestedOption) => {
152-
return (
153-
<Checkbox
154-
key={nestedOption.value}
155-
value={nestedOption.value}
156-
>
157-
{nestedOption.label}
158-
</Checkbox>
159-
);
160-
})}
161-
</div>
162-
</CheckboxGroup>
163-
);
164-
})}
82+
{astrobears.map((option) => (
83+
<Checkbox key={option.value} value={option.value}>
84+
{option.label}
85+
</Checkbox>
86+
))}
16587
</div>
16688
</CheckboxGroup>
16789
);

app/components/ui/checkbox-group.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { CheckboxGroup as CheckboxGroupPrimitive } from '@base-ui-components/rea
22

33
import { cn } from '@/lib/tailwind/utils';
44

5-
export type CheckboxGroupProps = CheckboxGroupPrimitive.Props;
5+
export type BaseCheckboxGroupProps = CheckboxGroupPrimitive.Props;
66

7-
export function CheckboxGroup(props: CheckboxGroupProps) {
7+
export function CheckboxGroup(props: BaseCheckboxGroupProps) {
88
return (
99
<CheckboxGroupPrimitive
1010
{...props}

app/components/ui/checkbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const labelVariants = cva('flex items-start gap-2.5 text-primary', {
1919
});
2020

2121
const checkboxVariants = cva(
22-
'flex flex-none cursor-pointer items-center justify-center rounded-sm outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:bg-muted-foreground disabled:opacity-20 data-checked:bg-primary data-checked:text-primary-foreground data-indeterminate:border data-indeterminate:border-primary/20 data-unchecked:border data-unchecked:border-primary/20',
22+
'flex flex-none cursor-pointer items-center justify-center rounded-sm outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:bg-muted-foreground disabled:opacity-20 aria-invalid:focus-visible:ring-destructive/50 data-checked:bg-primary data-checked:text-primary-foreground data-indeterminate:border data-indeterminate:border-primary/20 data-unchecked:border data-unchecked:border-primary/20 aria-invalid:data-unchecked:border-destructive',
2323
{
2424
variants: {
2525
size: {

0 commit comments

Comments
 (0)