({
label,
value,
itemType,
minLength = 0,
maxLength = 100,
reorderable = true,
readOnly,
onChange
}: ArrayFieldProps)
| 1174 | } |
| 1175 | |
| 1176 | function ArrayField({ |
| 1177 | label, |
| 1178 | value, |
| 1179 | itemType, |
| 1180 | minLength = 0, |
| 1181 | maxLength = 100, |
| 1182 | reorderable = true, |
| 1183 | readOnly, |
| 1184 | onChange |
| 1185 | }: ArrayFieldProps) { |
| 1186 | const { t } = useLocale(); |
| 1187 | const [isExpanded, setIsExpanded] = useState(true); |
| 1188 | const [dragIndex, setDragIndex] = useState<number | null>(null); |
| 1189 | |
| 1190 | const safeValue = Array.isArray(value) ? value : []; |
| 1191 | const canAdd = !readOnly && safeValue.length < maxLength; |
| 1192 | const canRemove = !readOnly && safeValue.length > minLength; |
| 1193 | |
| 1194 | const handleAdd = () => { |
| 1195 | if (!canAdd) return; |
| 1196 | let defaultValue: any = ''; |
| 1197 | if (itemType?.type === 'number') defaultValue = 0; |
| 1198 | if (itemType?.type === 'boolean') defaultValue = false; |
| 1199 | onChange([...safeValue, defaultValue]); |
| 1200 | }; |
| 1201 | |
| 1202 | const handleRemove = (index: number) => { |
| 1203 | if (!canRemove) return; |
| 1204 | const newValue = [...safeValue]; |
| 1205 | newValue.splice(index, 1); |
| 1206 | onChange(newValue); |
| 1207 | }; |
| 1208 | |
| 1209 | const handleItemChange = (index: number, newItemValue: any) => { |
| 1210 | const newValue = [...safeValue]; |
| 1211 | newValue[index] = newItemValue; |
| 1212 | onChange(newValue); |
| 1213 | }; |
| 1214 | |
| 1215 | const handleDragStart = (index: number) => { |
| 1216 | if (!reorderable || readOnly) return; |
| 1217 | setDragIndex(index); |
| 1218 | }; |
| 1219 | |
| 1220 | const handleDragOver = (e: React.DragEvent, index: number) => { |
| 1221 | e.preventDefault(); |
| 1222 | if (dragIndex === null || dragIndex === index) return; |
| 1223 | |
| 1224 | const newValue = [...safeValue]; |
| 1225 | const [removed] = newValue.splice(dragIndex, 1); |
| 1226 | newValue.splice(index, 0, removed); |
| 1227 | onChange(newValue); |
| 1228 | setDragIndex(index); |
| 1229 | }; |
| 1230 | |
| 1231 | const handleDragEnd = () => { |
| 1232 | setDragIndex(null); |
| 1233 | }; |
nothing calls this directly
no test coverage detected