Skip to content

Commit cc7e25c

Browse files
fix: sort correctly when selecting multiple items (#10)
When items are moved from unselected to selected list, they should be in the same order as they were presented in the unselected (not the order in which they were clicked on) [DHIS2-6651]
1 parent 7493ae9 commit cc7e25c

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/components/ItemSelector/modules/__tests__/toggler.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ describe('using the toggler ', () => {
102102
]
103103
})
104104

105+
it('should return the highlighted ids in the same order as original item list', () => {
106+
const actual = toggler('id', false, true, 2, 0, ['ones'], items)
107+
108+
expect(actual.ids).toEqual(['ones', 'some', 'id'])
109+
})
110+
105111
it('should not update the lastClickedIndex', () => {
106112
const expectedResult = {
107113
ids: items,
@@ -125,7 +131,7 @@ describe('using the toggler ', () => {
125131

126132
it('should keep the highlighted ids and not add duplicate items', () => {
127133
const expectedResult = {
128-
ids: ['some', 'ones', 'stuff', 'here'],
134+
ids: ['ones', 'some', 'stuff', 'here'],
129135
lastClickedIndex: lastClickedIndex,
130136
}
131137

@@ -144,7 +150,7 @@ describe('using the toggler ', () => {
144150

145151
it('should add items from lastClickedIndex to current index into the array', () => {
146152
const expectedResult = {
147-
ids: ['some', 'ones', 'stuff', 'here'],
153+
ids: ['ones', 'some', 'stuff', 'here'],
148154
lastClickedIndex: lastClickedIndex,
149155
}
150156

@@ -196,7 +202,7 @@ describe('using the toggler ', () => {
196202

197203
it('should be able to add one item and update the lastClickedIndex', () => {
198204
const expectedResult = {
199-
ids: highlightedIds.concat(id),
205+
ids: ['ones', 'stuff', 'here'],
200206
lastClickedIndex: index,
201207
}
202208

src/components/ItemSelector/modules/toggler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export const toggler = (
2424
newIndex = newArr.newIndex
2525
}
2626

27+
const orderedIds = items.filter(item => ids.includes(item))
28+
2729
return {
28-
ids,
30+
ids: orderedIds,
2931
lastClickedIndex: newIndex,
3032
}
3133
}

stories/ItemSelector.stories.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ const updateStore = (unselectedIds, selectedIds) => {
4747

4848
const onSelect = newIds => {
4949
const selectedIds = [
50-
...new Set(newIds.concat(store.get('selected').items.map(i => i.id))),
50+
...new Set(
51+
store
52+
.get('selected')
53+
.items.map(i => i.id)
54+
.concat(newIds)
55+
),
5156
]
5257

5358
const unselectedIds = Object.keys(items).filter(
@@ -58,10 +63,14 @@ const onSelect = newIds => {
5863
}
5964

6065
const onDeselect = newIds => {
61-
const unselectedIds = [
66+
const unorderedUnselectedIds = [
6267
...new Set(newIds.concat(store.get('unselected').items.map(i => i.id))),
6368
]
6469

70+
const unselectedIds = Object.keys(items).filter(id =>
71+
unorderedUnselectedIds.includes(id)
72+
)
73+
6574
const selectedIds = Object.keys(items).filter(
6675
id => !unselectedIds.includes(id)
6776
)

0 commit comments

Comments
 (0)