Skip to content

Commit 29824ce

Browse files
committed
progress
1 parent 15ebc03 commit 29824ce

2 files changed

Lines changed: 39 additions & 22 deletions

File tree

src/includes/vscode-select/OptionListController.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export class OptionListController implements ReactiveController {
6363
}
6464

6565
set selectedIndex(index: number) {
66+
if (this._selectedIndex !== -1) {
67+
this._options[this._selectedIndex].selected ??= false;
68+
}
69+
6670
const op = this.getOptionByIndex(index);
6771

6872
this._selectedIndex = op ? index : -1;
@@ -74,7 +78,18 @@ export class OptionListController implements ReactiveController {
7478
}
7579

7680
set selectedIndexes(value: number[]) {
81+
this._selectedIndexes.forEach((v) => {
82+
this._options[v].selected = false;
83+
});
84+
7785
this._selectedIndexes = new Set(value);
86+
87+
value.forEach((v) => {
88+
if (this._options[v] !== undefined) {
89+
this._options[v].selected = true;
90+
}
91+
});
92+
7893
this._host.requestUpdate();
7994
}
8095

@@ -240,6 +255,18 @@ export class OptionListController implements ReactiveController {
240255
}
241256
}
242257

258+
expandMultiSelection(values: string[]) {
259+
values.forEach((v) => {
260+
const foundIndex = this._indexByValue.get(v) ?? -1;
261+
262+
if (foundIndex !== -1) {
263+
this._selectedIndexes.add(foundIndex);
264+
}
265+
});
266+
267+
this._host.requestUpdate();
268+
}
269+
243270
toggleComboboxMode(enabled: boolean) {
244271
this._combobox = enabled;
245272
this._host.requestUpdate();
@@ -265,6 +292,8 @@ export class OptionListController implements ReactiveController {
265292
} else {
266293
this._selectedIndexes.add(optIndex);
267294
}
295+
296+
this._host.requestUpdate();
268297
}
269298

270299
getActiveOption(): InternalOption | null {
@@ -279,6 +308,10 @@ export class OptionListController implements ReactiveController {
279308
return this._options[index] ?? null;
280309
}
281310

311+
findOptionIndex(value: string) {
312+
return this._indexByValue.get(value) ?? -1;
313+
}
314+
282315
getOptionByValue(
283316
value: string,
284317
includeHiddenOptions = false

src/vscode-multi-select/vscode-multi-select.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,10 @@ export class VscodeMultiSelect
242242
super._onSlotChange();
243243

244244
if (this._requestedValueToSetLater.length > 0) {
245-
console.log('aa', this._opts.value, this._requestedValueToSetLater);
246-
247-
const prevValue = this._opts.value as string[];
248-
249-
this._opts.value = [
250-
...this._opts.value,
251-
...this._requestedValueToSetLater,
252-
];
253-
254-
console.log(this._opts.value);
255-
256-
/* this.options.forEach((o, i) => {
257-
if (this._requestedValueToSetLater.includes(o.value)) {
258-
this._selectedIndexes.push(i);
259-
this._values.push(o.value);
260-
this._options[i].selected = true;
261-
this._requestedValueToSetLater =
262-
this._requestedValueToSetLater.filter((v) => v !== o.value);
263-
}
264-
}); */
245+
this._opts.expandMultiSelection(this._requestedValueToSetLater);
246+
this._requestedValueToSetLater = this._requestedValueToSetLater.filter(
247+
(v) => this._opts.findOptionIndex(v) === -1
248+
);
265249
}
266250
}
267251

@@ -339,7 +323,7 @@ export class VscodeMultiSelect
339323

340324
//#region render functions
341325
private _renderLabel() {
342-
switch (this._selectedIndexes.length) {
326+
switch (this._opts.selectedIndexes.length) {
343327
case 0:
344328
return html`<span class="select-face-badge no-item"
345329
>No items selected</span
@@ -348,7 +332,7 @@ export class VscodeMultiSelect
348332
return html`<span class="select-face-badge">1 item selected</span>`;
349333
default:
350334
return html`<span class="select-face-badge"
351-
>${this._selectedIndexes.length} items selected</span
335+
>${this._opts.selectedIndexes.length} items selected</span
352336
>`;
353337
}
354338
}

0 commit comments

Comments
 (0)