-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.tsx
More file actions
83 lines (72 loc) · 2.29 KB
/
index.tsx
File metadata and controls
83 lines (72 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { useState } from 'react';
import { Label } from '@/components/ui/label';
import { Checkbox } from '@/components/ui/checkbox';
interface Option {
id: string | number;
label: string;
}
interface MultiSelectProps {
id: number;
question: string;
description?: string;
options: Option[];
required?: boolean;
defaultValue?: Array<string | number>;
onChange?: (value: Array<string | number>) => void;
onSubmit?: (questionId: number, value: Array<string | number>) => void;
disabled?: boolean;
}
const MultiSelect: React.FC<MultiSelectProps> = ({
id,
question,
description,
options,
required = false,
defaultValue = [],
onChange,
onSubmit,
disabled = false,
}) => {
const [selectedOptions, setSelectedOptions] = useState<Array<string | number>>(defaultValue);
const handleChange = (optionId: string | number, checked: boolean) => {
let newSelectedOptions: Array<string | number> = [];
if (checked) {
newSelectedOptions = [...selectedOptions, optionId];
} else {
newSelectedOptions = selectedOptions.filter(id => id !== optionId);
}
setSelectedOptions(newSelectedOptions);
if (onChange) onChange(newSelectedOptions);
if (onSubmit) onSubmit(id, newSelectedOptions);
};
return (
<div className="mb-6">
<div className="flex items-center mb-2">
<Label className="text-lg font-medium">{question}</Label>
{required && <span className="ml-1 text-red-500">*</span>}
</div>
{description && (
<p className="mb-4 text-sm text-muted-foreground">{description}</p>
)}
<div className="space-y-3">
{options.map((option) => (
<div key={option.id} className="flex items-center space-x-2">
<Checkbox
id={`option-${id}-${option.id}`}
checked={selectedOptions.includes(option.id)}
onCheckedChange={(checked) => handleChange(option.id, !!checked)}
disabled={disabled}
/>
<Label
htmlFor={`option-${id}-${option.id}`}
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{option.label}
</Label>
</div>
))}
</div>
</div>
);
};
export default MultiSelect;