Skip to content

Commit a59b77e

Browse files
authored
Merge pull request #164 from DZakh-forks/master
Fix work with frozen item objects
2 parents 2e27b36 + 33b03a6 commit a59b77e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/react-sortable.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ReactSortable<T extends ItemInterface> extends Component<
4747
this.ref = createRef<HTMLElement>();
4848

4949
// make all state false because we can't change sortable unless a mouse gesture is made.
50-
const newList = [...props.list].map(item => ({
50+
const newList = props.list.map(item => ({
5151
...item,
5252
chosen: false,
5353
selected: false
@@ -293,15 +293,29 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl
293293

294294
onChoose(evt: SortableEvent) {
295295
const { list, setList } = this.props;
296-
const newList = [...list];
297-
newList[evt.oldIndex!].chosen = true;
296+
const newList = list.map((item, index) => {
297+
if (index === evt.oldIndex) {
298+
return {
299+
...item,
300+
chosen: true,
301+
}
302+
}
303+
return item;
304+
});
298305
setList(newList, this.sortable, store);
299306
}
300307

301308
onUnchoose(evt: SortableEvent) {
302309
const { list, setList } = this.props;
303-
const newList = [...list];
304-
newList[evt.oldIndex!].chosen = false;
310+
const newList = list.map((item, index) => {
311+
if (index === evt.oldIndex) {
312+
return {
313+
...item,
314+
chosen: false,
315+
}
316+
}
317+
return item;
318+
});
305319
setList(newList, this.sortable, store);
306320
}
307321

@@ -312,7 +326,7 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl
312326

313327
onSelect(evt: MultiDragEvent) {
314328
const { list, setList } = this.props;
315-
const newList = [...list].map(item => ({ ...item, selected: false }));
329+
const newList = list.map(item => ({ ...item, selected: false }));
316330
evt.newIndicies.forEach(curr => {
317331
const index = curr.index;
318332
if (index === -1) {
@@ -329,7 +343,7 @@ Please read the updated README.md at https://github.com/SortableJS/react-sortabl
329343

330344
onDeselect(evt: MultiDragEvent) {
331345
const { list, setList } = this.props;
332-
const newList = [...list].map(item => ({ ...item, selected: false }));
346+
const newList = list.map(item => ({ ...item, selected: false }));
333347
evt.newIndicies.forEach(curr => {
334348
const index = curr.index;
335349
if (index === -1) return;

0 commit comments

Comments
 (0)