import React from 'react'; import useHandleMove from '../Common/useHandleMove'; import {DragAndDropItem, DragAndDropProvider} from "../index"; type MultiValueProps = { value?: T[]|null; onChange: (value: T[]) => void; children: (data: T|any|null, onChange: (data: T) => void, index: number) => React.ReactNode; add?: Boolean; move?: Boolean; index?: Boolean; }; export default function MultiValue({ value, move, onChange, children, add, index }: MultiValueProps) { const arr = value || []; const handleMove = useHandleMove(value, onChange); if (move) { return {arr.map((a, i) => {children(a, (val) => onChange(index ? (val === null ? arr.filter((b, a) => i !== a) : arr.map((b, a) => i === a ? val : b)) : (val === null ? arr.filter(b => b !== a) : arr.map(b => b === a ? val : b))), i)} ) .concat(add === false ? [] : [ {children(null, (val) => onChange(arr.concat([val])), (value && value.length) || 0)} ])} ; } return <> {arr.map((a, i) => {children(a, (val) => onChange(val === null ? arr.filter(b => b !== a) : arr.map(b => b === a ? val : b)), i)} ) .concat(add === false ? [] : [ {children(null, (val) => onChange(arr.concat([val])), (value && value.length) || 0)} ])} ; }