Skip to content

Commit 28eeeca

Browse files
authored
core: Fix removeItems for indices greater than 9 (#2181)
In mapDispatchToArrayControlProps, the removeItems method sorted indices as strings. This broke removing items for indix combinations like [2, 11] because 11 was sorted before 2. This is fixed with this commit by properly sorting as numbers.
1 parent 8fc90d3 commit 28eeeca

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/core/src/util/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ export const mapDispatchToArrayControlProps = (
799799
dispatch(
800800
update(path, (array) => {
801801
toDelete
802-
.sort()
802+
.sort((a, b) => a - b)
803803
.reverse()
804804
.forEach((s) => array.splice(s, 1));
805805
return array;

packages/core/test/util/renderer.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,31 @@ test('mapDispatchToArrayControlProps should remove items from array', (t) => {
625625
t.is(getCore().data[0], 'quux');
626626
});
627627

628+
test('mapDispatchToArrayControlProps should remove items from array with more than 10 items', (t) => {
629+
const data = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
630+
const schema: JsonSchema = {
631+
type: 'array',
632+
items: {
633+
type: 'string',
634+
},
635+
};
636+
const uischema: ControlElement = {
637+
type: 'Control',
638+
scope: '#',
639+
};
640+
const initCore: JsonFormsCore = {
641+
uischema,
642+
schema,
643+
data,
644+
errors: [] as ErrorObject[],
645+
};
646+
const [getCore, dispatch] = mockDispatch(initCore);
647+
dispatch(init(data, schema, uischema));
648+
const props = mapDispatchToArrayControlProps(dispatch);
649+
props.removeItems('', [11, 2])();
650+
t.is(getCore().data.length, 10);
651+
});
652+
628653
test('mapStateToLayoutProps - visible via state with path from ownProps ', (t) => {
629654
const uischema = {
630655
type: 'VerticalLayout',

0 commit comments

Comments
 (0)