import type { ReactNode } from 'react'; import { Box, Checkbox, Field, Flex, Typography } from '@strapi/design-system'; import { useIntl } from 'react-intl'; type CheckboxListInputProps = { name: string; value?: unknown; onChange: (eventOrPath: { target: { name: string; value: string[] } }, value?: unknown) => void; attribute?: { enum?: string[]; options?: { enum?: string[]; }; } | null; label?: ReactNode; hint?: ReactNode; required?: boolean; disabled?: boolean; error?: string; labelAction?: ReactNode; }; const getEnumValues = (attribute: CheckboxListInputProps['attribute']): string[] => { if (!attribute) { return []; } if (Array.isArray(attribute.options?.enum)) { return attribute.options.enum; } if (Array.isArray(attribute.enum)) { return attribute.enum; } return []; }; const normalizeValue = (value: unknown): string[] => { if (Array.isArray(value)) { return value.filter((item): item is string => typeof item === 'string'); } if (typeof value === 'string' && value.length > 0) { return [value]; } return []; }; const CheckboxListInput = ({ name, value, onChange, attribute, label, hint, required = false, disabled = false, error, labelAction, }: CheckboxListInputProps) => { const { formatMessage } = useIntl(); const enumValues = getEnumValues(attribute); const selectedValues = normalizeValue(value); const handleToggle = (option: string, isChecked: boolean) => { const nextValues = isChecked ? Array.from(new Set([...selectedValues, option])) : selectedValues.filter((item) => item !== option); onChange({ target: { name, value: nextValues, }, }); }; return ( {label ?? name} {enumValues.length > 0 ? ( {enumValues.map((option) => ( handleToggle(option, Boolean(checked)) } > {option} ))} ) : ( {formatMessage({ id: 'checkbox-list.field.empty', defaultMessage: 'No values configured yet.', })} )} ); }; export default CheckboxListInput;