import type { ReactNode } from 'react'; import { Box, Field, MultiSelect, MultiSelectOption, Typography } from '@strapi/design-system'; import { useIntl } from 'react-intl'; type CheckboxListDefaultInputProps = { name: string; value?: unknown; onChange: (eventOrPath: { target: { name: string; value: string[] } }, value?: unknown) => void; intlLabel?: { id: string; defaultMessage: string; values?: Record; }; description?: { id: string; defaultMessage: string; values?: Record; }; labelAction?: ReactNode; required?: boolean; disabled?: boolean; error?: string; modifiedData?: { enum?: string[]; options?: { enum?: string[]; }; }; }; 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 CheckboxListDefaultInput = ({ name, value, onChange, intlLabel, description, labelAction, required = false, disabled = false, error, modifiedData, }: CheckboxListDefaultInputProps) => { const { formatMessage } = useIntl(); const enumValues = Array.isArray(modifiedData?.options?.enum) ? modifiedData.options.enum : Array.isArray(modifiedData?.enum) ? modifiedData.enum : []; const selectedValues = normalizeValue(value); const uniqueValues = Array.from( new Set(enumValues.filter((option) => typeof option === 'string' && option.trim().length > 0)) ); const label = intlLabel ? formatMessage(intlLabel, intlLabel.values ?? {}) : name; const hint = description ? formatMessage(description, description.values ?? {}) : undefined; const handleChange = (nextValues: string[] | undefined) => { onChange({ target: { name, value: Array.isArray(nextValues) ? nextValues : [], }, }); }; return ( {label} {uniqueValues.length > 0 ? ( {uniqueValues.map((option) => ( {option} ))} ) : ( {formatMessage({ id: 'checkbox-list.field.empty', defaultMessage: 'No values configured yet.', })} )} ); }; export { CheckboxListDefaultInput };