Skip to content

Commit bf92805

Browse files
committed
Fix highlight last option
1 parent bf93090 commit bf92805

2 files changed

Lines changed: 26 additions & 58 deletions

File tree

src/includes/vscode-select/OptionListController.ts

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class OptionListController implements ReactiveController {
1212
private _activeIndex = -1;
1313
private _host: ReactiveControllerHost;
1414
private _options: InternalOption[] = [];
15+
private _filteredOptions: InternalOption[] | null = null;
1516
private _filterPattern = '';
1617
private _filterMethod: FilterMethod = 'fuzzy';
1718
private _combobox = false;
@@ -107,25 +108,39 @@ export class OptionListController implements ReactiveController {
107108
}
108109

109110
set filterPattern(pattern: string) {
110-
this._filterPattern = pattern;
111-
this._updateState();
111+
if (pattern !== this._filterPattern) {
112+
this._filterPattern = pattern;
113+
this._updateState();
114+
this._filteredOptions = null;
115+
}
112116
}
113117

114118
get filterMethod(): FilterMethod {
115119
return this._filterMethod;
116120
}
117121

118122
set filterMethod(method: FilterMethod) {
119-
this._filterMethod = method;
120-
this._updateState();
123+
if (method !== this._filterMethod) {
124+
this._filterMethod = method;
125+
this._updateState();
126+
this._filteredOptions = null;
127+
}
121128
}
122129

123130
get options(): InternalOption[] {
124131
return this._options;
125132
}
126133

127134
get filteredOptions(): InternalOption[] {
128-
return this._options.filter((o) => o.visible);
135+
if (this._filterPattern === '') {
136+
return this._options;
137+
}
138+
139+
if (this._filteredOptions === null) {
140+
this._filteredOptions = this._options.filter((o) => o.visible);
141+
}
142+
143+
return this._filteredOptions;
129144
}
130145

131146
get numOfVisibleOptions() {
@@ -169,7 +184,7 @@ export class OptionListController implements ReactiveController {
169184
let visible = true;
170185
let ranges: [number, number][] = [];
171186

172-
if (this._combobox) {
187+
if (this._combobox && this._filterPattern !== '') {
173188
const res = this._searchByPattern(label);
174189
visible = res.match;
175190
ranges = res.ranges;
@@ -266,48 +281,8 @@ export class OptionListController implements ReactiveController {
266281
return this._options[index];
267282
}
268283

269-
next(fromIndex?: number): {value: InternalOption; last: boolean} {
284+
next(fromIndex?: number): InternalOption | null {
270285
const from = fromIndex ?? this._activeIndex;
271-
let last = false;
272-
273-
let nextIndex = -1;
274-
275-
for (let i = from + 1; i < this._options.length; i++) {
276-
if (
277-
this._options[i] &&
278-
!this._options[i].disabled &&
279-
this._options[i].visible
280-
) {
281-
nextIndex = i;
282-
break;
283-
}
284-
}
285-
286-
const value =
287-
nextIndex > -1 ? this._options[nextIndex] : (this._options[from] ?? null);
288-
last = nextIndex === -1;
289-
290-
return {
291-
value,
292-
last,
293-
};
294-
}
295-
296-
getNextSelectableOption(fromIndex?: number): InternalOption | null {
297-
const from = fromIndex ?? this._activeIndex;
298-
299-
/* if (this._options.length === 0) {
300-
return null;
301-
}
302-
303-
if (this._options.length === 1) {
304-
return this._options[0];
305-
}
306-
307-
if (from !== -1 && !this._options[from + 1]) {
308-
return this._options[from];
309-
} */
310-
311286
let nextIndex = -1;
312287

313288
for (let i = from + 1; i < this._options.length; i++) {
@@ -369,13 +344,6 @@ export class OptionListController implements ReactiveController {
369344
this._host.requestUpdate();
370345
}
371346

372-
activateNext() {
373-
const nextOp = this.getNextSelectableOption();
374-
this._activeIndex = nextOp?.index ?? -1;
375-
this._host.requestUpdate();
376-
return nextOp;
377-
}
378-
379347
activatePrev() {
380348
const prevOp = this.getPrevSelectableOption();
381349
this._activeIndex = prevOp?.index ?? -1;

src/includes/vscode-select/vscode-select-base.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,13 @@ export class VscodeSelectBase extends VscElement {
531531

532532
const nextOp = this._opts.next();
533533

534-
if (suggestedOptionVisible && nextOp.last) {
534+
if (suggestedOptionVisible && nextOp === null) {
535535
this._isPlaceholderOptionActive = true;
536536
this._adjustOptionListScrollPos('down', numOpts - 1);
537537
this._opts.activeIndex = -1;
538-
} else {
539-
const nextSelectableIndex = nextOp?.value.relativeIndex ?? -1;
540-
this._opts.activeIndex = nextOp.value.index;
538+
} else if (nextOp !== null) {
539+
const nextSelectableIndex = nextOp?.relativeIndex ?? -1;
540+
this._opts.activeIndex = nextOp?.index ?? -1;
541541

542542
if (nextSelectableIndex > -1) {
543543
this._adjustOptionListScrollPos('down', nextSelectableIndex);

0 commit comments

Comments
 (0)