@@ -25,7 +25,7 @@ interface SelectOptions {
25
25
toggleOne ?: boolean ;
26
26
selectOne ?: boolean ;
27
27
toggleAll ?: boolean ;
28
- selectFromAnchor ?: boolean ;
28
+ selectRange ?: boolean ;
29
29
}
30
30
31
31
/** Represents the required inputs for a listbox. */
@@ -135,41 +135,48 @@ export class ListboxPattern<V> {
135
135
}
136
136
137
137
if ( this . inputs . multi ( ) ) {
138
- // When the user holds down the shift key, they are selecting a range starting from the
139
- // index where the shift key begins being held down. Note that this is very different from
140
- // selecting from the current active or focused index.
138
+ // The following block implements range selection using a keyboard.
139
+ //
140
+ // Range selection is started by holding shift and navigating to the next item in a list.
141
+ //
142
+ // Wrapping is disabled during range selection since any decision on how to
143
+ // continue the range after wrapping would lead to an unintuitive user experience.
141
144
manager
142
145
. on ( Modifier . Any , 'Shift' , ( ) => this . shiftAnchorIndex . set ( this . inputs . activeIndex ( ) ) )
143
146
. on ( Modifier . Shift , this . prevKey , ( ) => {
144
147
if ( this . inputs . activeIndex ( ) === this . shiftAnchorIndex ( ) ) {
145
- this . selection . dropAnchor ( ) ;
148
+ this . selection . beginRangeSelection ( ) ;
146
149
}
147
150
this . wrap . set ( false ) ;
148
- this . prev ( { selectFromAnchor : true } ) ;
151
+ this . prev ( { selectRange : true } ) ;
149
152
this . wrap . set ( true ) ;
150
153
} )
151
154
. on ( Modifier . Shift , this . nextKey , ( ) => {
152
155
if ( this . inputs . activeIndex ( ) === this . shiftAnchorIndex ( ) ) {
153
- this . selection . dropAnchor ( ) ;
156
+ this . selection . beginRangeSelection ( ) ;
154
157
}
155
158
this . wrap . set ( false ) ;
156
- this . next ( { selectFromAnchor : true } ) ;
159
+ this . next ( { selectRange : true } ) ;
157
160
this . wrap . set ( true ) ;
158
161
} ) ;
159
162
160
163
manager
161
- . on ( Modifier . Shift , 'Enter' , ( ) => this . _updateSelection ( { selectFromAnchor : true } ) )
164
+ . on ( Modifier . Shift , 'Enter' , ( ) => {
165
+ this . _updateSelection ( { selectRange : true } ) ;
166
+ this . shiftAnchorIndex . set ( this . selection . rangeStartIndex ( ) ) ;
167
+ } )
168
+ . on ( Modifier . Shift , this . dynamicSpaceKey , ( ) => {
169
+ this . _updateSelection ( { selectRange : true } ) ;
170
+ this . shiftAnchorIndex . set ( this . selection . rangeStartIndex ( ) ) ;
171
+ } )
162
172
. on ( [ Modifier . Ctrl | Modifier . Shift , Modifier . Meta | Modifier . Shift ] , 'Home' , ( ) => {
163
- this . selection . dropAnchor ( ) ; // Make the current focused option the anchor.
164
- this . first ( { selectFromAnchor : true } ) ;
173
+ this . selection . beginRangeSelection ( ) ; // Make the current focused option the anchor.
174
+ this . first ( { selectRange : true } ) ;
165
175
} )
166
176
. on ( [ Modifier . Ctrl | Modifier . Shift , Modifier . Meta | Modifier . Shift ] , 'End' , ( ) => {
167
- this . selection . dropAnchor ( ) ; // Make the current focused option the anchor.
168
- this . last ( { selectFromAnchor : true } ) ;
169
- } )
170
- . on ( Modifier . Shift , this . dynamicSpaceKey , ( ) =>
171
- this . _updateSelection ( { selectFromAnchor : true } ) ,
172
- ) ;
177
+ this . selection . beginRangeSelection ( ) ; // Make the current focused option the anchor.
178
+ this . last ( { selectRange : true } ) ;
179
+ } ) ;
173
180
}
174
181
175
182
if ( ! this . followFocus ( ) && this . inputs . multi ( ) ) {
@@ -212,9 +219,9 @@ export class ListboxPattern<V> {
212
219
if ( this . multi ( ) ) {
213
220
manager . on ( Modifier . Shift , e => {
214
221
if ( this . inputs . activeIndex ( ) === this . shiftAnchorIndex ( ) ) {
215
- this . selection . dropAnchor ( ) ;
222
+ this . selection . beginRangeSelection ( ) ;
216
223
}
217
- this . goto ( e , { selectFromAnchor : true } ) ;
224
+ this . goto ( e , { selectRange : true } ) ;
218
225
} ) ;
219
226
}
220
227
@@ -330,8 +337,8 @@ export class ListboxPattern<V> {
330
337
if ( opts ?. toggleAll ) {
331
338
this . selection . toggleAll ( ) ;
332
339
}
333
- if ( opts ?. selectFromAnchor ) {
334
- this . selection . selectFromAnchor ( ) ;
340
+ if ( opts ?. selectRange ) {
341
+ this . selection . selectRange ( ) ;
335
342
}
336
343
}
337
344
0 commit comments