From b9bd07c53d8c624f14f9b7da71ff1e4de099a029 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 5 Feb 2026 12:39:14 +0000 Subject: Refactor checkbox-list custom field implementation - Replaced the PluginIcon with EnumerationField in the custom field registration. - Introduced CheckboxListDefaultInput component for handling checkbox list inputs. - Updated the admin interface to include new settings for the checkbox-list custom field. - Enhanced server-side registration to support resizable input size for the checkbox-list. --- admin/src/components/CheckboxListDefaultInput.tsx | 109 ++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 admin/src/components/CheckboxListDefaultInput.tsx (limited to 'admin/src/components/CheckboxListDefaultInput.tsx') diff --git a/admin/src/components/CheckboxListDefaultInput.tsx b/admin/src/components/CheckboxListDefaultInput.tsx new file mode 100644 index 0000000..465d0d3 --- /dev/null +++ b/admin/src/components/CheckboxListDefaultInput.tsx @@ -0,0 +1,109 @@ +import type { ReactNode } from 'react'; + +import { Box, Checkbox, Field, Flex, 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[]; + }; +}; + +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?.enum) ? modifiedData.enum : []; + const selectedValues = normalizeValue(value); + + const label = intlLabel + ? formatMessage(intlLabel, intlLabel.values ?? {}) + : name; + const hint = description ? formatMessage(description, description.values ?? {}) : undefined; + + 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} + {enumValues.length > 0 ? ( + + {enumValues.map((option) => ( + + handleToggle(option, Boolean(checked)) + } + > + {option} + + ))} + + ) : ( + + + {formatMessage({ + id: 'checkbox-list.field.empty', + defaultMessage: 'No values configured yet.', + })} + + + )} + + + + ); +}; + +export { CheckboxListDefaultInput }; -- cgit v1.2.3