Skip to content

Commit c8b7b1e

Browse files
committed
select next does not work if select adjacent from bottom to top
1 parent 936c3d4 commit c8b7b1e

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macos-multi-select",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "Given a list of ids, and an action, return a list of selected items with the same behaviour of macOS finder list view selection.",
55
"main": "dist/index.js",
66
"repository": "[email protected]:codingedgar/macos-multi-select.git",

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function multiselect(context: Context, command: Command): Context {
104104
Math.max(pivotIndex, selectionIndex) + 1
105105
);
106106

107+
if (pivotIndex > selectionIndex){
108+
nextSelection.reverse()
109+
}
110+
107111
const toRemove = difference(adjacent, nextSelection);
108112

109113
return {
@@ -124,7 +128,7 @@ export function multiselect(context: Context, command: Command): Context {
124128
command.type === "SELECT NEXT" &&
125129
context.list.length &&
126130
context.selected.length
127-
) {
131+
) {
128132
const pivotIndex = context.list.indexOf(last(context.selected)!)
129133

130134
if (pivotIndex < context.list.length - 1) {

src/spec/selectNext.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,78 @@ describe('Select Next Item', () => {
164164
)
165165
)
166166
})
167+
168+
test('Should select next from the last selected even when the selection is bottom to top', () => {
169+
fc.assert(
170+
fc.property(
171+
fc.set(
172+
fc.string(),
173+
{ minLength: 2 }
174+
)
175+
.chain(list =>
176+
fc.tuple(
177+
fc.nat(list.length - 1),
178+
fc.nat(list.length - 1),
179+
)
180+
.map(([first, second]) => ({
181+
list,
182+
adjacentPivot: undefined,
183+
first: list[first],
184+
second: list[second],
185+
}))
186+
)
187+
,
188+
({
189+
adjacentPivot,
190+
list,
191+
first,
192+
second
193+
}) => {
194+
195+
const context1 = multiselect(
196+
{
197+
list,
198+
adjacentPivot: adjacentPivot,
199+
selected: []
200+
},
201+
{
202+
type: "SELECT ONE",
203+
id: first,
204+
}
205+
)
206+
207+
const context2 = multiselect(
208+
context1,
209+
{
210+
type: "SELECT ADJACENT",
211+
id: second,
212+
}
213+
)
214+
215+
const currentSelectionPivot = list.indexOf(second)
216+
const nextSelection = currentSelectionPivot < list.length - 1
217+
? currentSelectionPivot + 1
218+
: list.length - 1;
219+
220+
const nextPivot = list[nextSelection];
221+
222+
expect(
223+
multiselect(
224+
context2,
225+
{
226+
type: "SELECT NEXT",
227+
}
228+
)
229+
)
230+
.toEqual({
231+
list,
232+
selected: [nextPivot],
233+
adjacentPivot: nextPivot,
234+
})
235+
236+
}
237+
),
238+
)
239+
})
167240

168241
})

src/spec/selectPrevious.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,76 @@ describe('Select Previous Item', () => {
169169
)
170170
})
171171

172+
test('Should select previous from the last selected even when the selection is bottom to top', () => {
173+
fc.assert(
174+
fc.property(
175+
fc.set(
176+
fc.string(),
177+
{ minLength: 2 }
178+
)
179+
.chain(list =>
180+
fc.tuple(
181+
fc.nat(list.length - 1),
182+
fc.nat(list.length - 1),
183+
)
184+
.map(([first, second]) => ({
185+
list,
186+
adjacentPivot: undefined,
187+
first: list[first],
188+
second: list[second],
189+
}))
190+
)
191+
,
192+
({
193+
adjacentPivot,
194+
list,
195+
first,
196+
second
197+
}) => {
198+
199+
const context1 = multiselect(
200+
{
201+
list,
202+
adjacentPivot: adjacentPivot,
203+
selected: []
204+
},
205+
{
206+
type: "SELECT ONE",
207+
id: first,
208+
}
209+
)
210+
211+
const context2 = multiselect(
212+
context1,
213+
{
214+
type: "SELECT ADJACENT",
215+
id: second,
216+
}
217+
)
218+
219+
const currentSelectionPivot = list.indexOf(second)
220+
const nextSelection = currentSelectionPivot > 0
221+
? currentSelectionPivot - 1
222+
: 0;
223+
const nextPivot = list[nextSelection];
224+
225+
expect(
226+
multiselect(
227+
context2,
228+
{
229+
type: "SELECT PREVIOUS",
230+
}
231+
)
232+
)
233+
.toEqual({
234+
list,
235+
selected: [nextPivot],
236+
adjacentPivot: nextPivot,
237+
})
238+
239+
}
240+
),
241+
)
242+
})
243+
172244
});

0 commit comments

Comments
 (0)