From 362cbd57e75e02f12eff12925f7ec346a20721d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Acosta?= Date: Thu, 27 Jan 2022 11:33:27 -0500 Subject: [PATCH] fix: preserve original objects --- src/react-sortable.tsx | 55 ++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/src/react-sortable.tsx b/src/react-sortable.tsx index ca530a5..9d1e033 100644 --- a/src/react-sortable.tsx +++ b/src/react-sortable.tsx @@ -49,11 +49,14 @@ export class ReactSortable extends Component< this.ref = createRef(); // make all state false because we can't change sortable unless a mouse gesture is made. - const newList = props.list.map((item) => ({ - ...item, - chosen: false, - selected: false, - })); + const newList = [...props.list]; + + newList.forEach((item: T) => { + Object.assign(item, { + chosen: false, + selected: false, + }); + }) props.setList(newList, this.sortable, store); invariant( @@ -235,10 +238,14 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl const otherList = [...store.dragging!.props.list]; const customs = createCustoms(evt, otherList); removeNodes(customs); - const newList = handleStateAdd(customs, list, evt, clone).map((item) => ({ - ...item, - selected: false, - })); + + const newList = handleStateAdd(customs, list, evt, clone) + + newList.forEach((item) => { + Object.assign(item, { + selected: false, + }); + }); setList(newList, this.sortable, store); } @@ -289,7 +296,11 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl } // remove item.selected from list - newList = newList.map((item) => ({ ...item, selected: false })); + newList.forEach((item: T) => { + Object.assign(item, { + selected: false, + }); + }) setList(newList, this.sortable, store); } @@ -314,10 +325,9 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl const { list, setList } = this.props; const newList = list.map((item, index) => { if (index === evt.oldIndex) { - return { - ...item, + Object.assign(item, { chosen: true, - }; + }); } return item; }); @@ -328,10 +338,9 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl const { list, setList } = this.props; const newList = list.map((item, index) => { if (index === evt.oldIndex) { - return { - ...item, + Object.assign(item, { chosen: false, - }; + }); } return item; }); @@ -345,7 +354,12 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl onSelect(evt: MultiDragEvent): void { const { list, setList } = this.props; - const newList = list.map((item) => ({ ...item, selected: false })); + const newList = [...list]; + newList.forEach((item) => { + Object.assign(item, { + chosen: false, + }); + }); evt.newIndicies.forEach((curr) => { const index = curr.index; if (index === -1) { @@ -362,7 +376,12 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl onDeselect(evt: MultiDragEvent): void { const { list, setList } = this.props; - const newList = list.map((item) => ({ ...item, selected: false })); + const newList = [...list]; + newList.forEach((item) => { + Object.assign(item, { + chosen: false, + }); + }); evt.newIndicies.forEach((curr) => { const index = curr.index; if (index === -1) return;