1- import { difference , head , take , union , without } from "ramda" ;
1+ import { difference , head , last , take , union , without } from "ramda" ;
22import { findAdjacentToPivotInSortedArray , findNextPivot } from "./arrayUtils" ;
33
44export type Context = {
@@ -12,6 +12,7 @@ type Command =
1212 | { type : "TOGGLE SELECTION" , id : string }
1313 | { type : "DESELECT ALL" }
1414 | { type : "SELECT ADJACENT" , id : string }
15+ | { type : "SELECT NEXT" }
1516
1617function listIncludesAndIsNotEmpty ( list : string [ ] , item : string ) {
1718 return list . length > 0 && list . includes ( item )
@@ -108,6 +109,44 @@ export function multiselect(context: Context, command: Command): Context {
108109 ...context ,
109110 selected : union ( without ( toRemove , context . selected ) , nextSelection )
110111 }
112+ } else if (
113+ command . type === "SELECT NEXT" &&
114+ context . list . length &&
115+ context . adjacentPivot === undefined
116+ ) {
117+ return {
118+ ...context ,
119+ selected : [ context . list [ 0 ] ] ,
120+ adjacentPivot : context . list [ 0 ] ,
121+ }
122+ } else if (
123+ command . type === "SELECT NEXT" &&
124+ context . list . length &&
125+ context . adjacentPivot !== undefined
126+ ) {
127+ const pivotIndex = context . list . indexOf ( context . adjacentPivot )
128+
129+ if ( pivotIndex < context . list . length - 1 ) {
130+ const nextItem = context . list [ pivotIndex + 1 ] ;
131+ return {
132+ ...context ,
133+ selected : [ nextItem ] ,
134+ adjacentPivot : nextItem
135+ }
136+ } else if (
137+ ! context . selected . length ||
138+ ! (
139+ context . selected . length === 1 &&
140+ last ( context . selected ) === last ( context . list )
141+ )
142+ ) {
143+ return {
144+ ...context ,
145+ selected : [ context . list [ pivotIndex ] ] ,
146+ }
147+ } else {
148+ return context ;
149+ }
111150 } else {
112151 return context ;
113152 }
0 commit comments