Skip to content

Commit 5ae070b

Browse files
committed
Refactor
1 parent ada6feb commit 5ae070b

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
import {VscElement} from '../VscElement.js';
2121
import {chevronDownIcon} from './template-elements.js';
2222

23-
const VISIBLE_OPTS = 10;
24-
const OPT_HEIGHT = 22;
23+
export const VISIBLE_OPTS = 10;
24+
export const OPT_HEIGHT = 22;
2525

2626
/**
2727
* @cssprop --dropdown-z-index - workaround for dropdown z-index issues
@@ -232,7 +232,7 @@ export class VscodeSelectBase extends VscElement {
232232
private _isBeingFiltered = false;
233233

234234
@state()
235-
private _optionListScrollPos = 0;
235+
protected _optionListScrollPos = 0;
236236

237237
/** @internal */
238238
protected _multiple = false;
@@ -308,28 +308,12 @@ export class VscodeSelectBase extends VscElement {
308308
this._value = values[0];
309309
this._values = values;
310310
}
311-
312-
if (!this._multiple && !this.combobox && selectedIndexes.length === 0) {
313-
this._selectedIndex = this._options.length > 0 ? 0 : -1;
314-
}
315311
}
316312

317313
protected _toggleDropdown(visible: boolean) {
318314
this.open = visible;
319315
this.ariaExpanded = String(visible);
320316

321-
if (visible && !this._multiple) {
322-
this._activeIndex = this._selectedIndex;
323-
}
324-
325-
if (visible && !this._multiple && !this.combobox) {
326-
this._activeIndex = this._selectedIndex;
327-
328-
if (this._activeIndex > VISIBLE_OPTS - 1) {
329-
this._optionListScrollPos = Math.floor(this._activeIndex * OPT_HEIGHT);
330-
}
331-
}
332-
333317
if (visible) {
334318
window.addEventListener('click', this._onClickOutside);
335319
} else {
@@ -505,7 +489,7 @@ export class VscodeSelectBase extends VscElement {
505489
}
506490
}
507491

508-
private _scrollActiveElementToTop() {
492+
protected _scrollActiveElementToTop() {
509493
this._optionListScrollPos = Math.floor(this._activeIndex * OPT_HEIGHT);
510494
}
511495

src/vscode-single-select/vscode-single-select.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {html, LitElement, TemplateResult} from 'lit';
22
import {property, query} from 'lit/decorators.js';
33
import {customElement} from '../includes/VscElement.js';
44
import {chevronDownIcon} from '../includes/vscode-select/template-elements.js';
5-
import {VscodeSelectBase} from '../includes/vscode-select/vscode-select-base.js';
5+
import {
6+
OPT_HEIGHT,
7+
VISIBLE_OPTS,
8+
VscodeSelectBase,
9+
} from '../includes/vscode-select/vscode-select-base.js';
610
import styles from './vscode-single-select.styles.js';
711
import {AssociatedFormControl} from '../includes/AssociatedFormControl.js';
812
import {
@@ -250,6 +254,30 @@ export class VscodeSingleSelect
250254
super._dispatchChangeEvent();
251255
}
252256

257+
protected override _setStateFromSlottedElements(): void {
258+
super._setStateFromSlottedElements();
259+
260+
if (!this.combobox && this._selectedIndexes.length === 0) {
261+
this._selectedIndex = this._options.length > 0 ? 0 : -1;
262+
}
263+
}
264+
265+
protected override _toggleDropdown(visible: boolean): void {
266+
super._toggleDropdown(visible);
267+
268+
if (visible) {
269+
this._activeIndex = this._selectedIndex;
270+
}
271+
272+
if (visible && !this.combobox) {
273+
this._activeIndex = this._selectedIndex;
274+
275+
if (this._activeIndex > VISIBLE_OPTS - 1) {
276+
this._optionListScrollPos = Math.floor(this._activeIndex * OPT_HEIGHT);
277+
}
278+
}
279+
}
280+
253281
protected override _onSlotChange(): void {
254282
super._onSlotChange();
255283

0 commit comments

Comments
 (0)