Skip to content

Commit

Permalink
fix(prompt): disabled item gets selected when it's the first option (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jan 13, 2021
1 parent ad779f4 commit f5c9be5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
22 changes: 20 additions & 2 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export interface GenericListSettings<T, V>
/** Generic list prompt representation. */
export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
extends GenericInput<T, V, S> {
protected listOffset = 0;
protected listIndex = 0;
protected options: S["options"] = this.settings.options;
protected listIndex: number = this.getListIndex();
protected listOffset: number = this.getPageOffset(this.listIndex);

/**
* Create list separator.
Expand Down Expand Up @@ -201,6 +201,24 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
);
}

protected getListIndex(value?: string) {
return typeof value === "undefined"
? this.options.findIndex((item: GenericListOptionSettings) =>
!item.disabled
) || 0
: this.options.findIndex((item: GenericListOptionSettings) =>
item.value === value
) || 0;
}

protected getPageOffset(index: number) {
if (index === 0) {
return 0;
}
const height: number = this.getListHeight();
return Math.floor(index / height) * height;
}

/**
* Find option by value.
* @param value Value of the option.
Expand Down
17 changes: 0 additions & 17 deletions prompt/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export interface SelectSettings extends GenericListSettings<string, string> {
export class Select<S extends SelectSettings = SelectSettings>
extends GenericList<string, string, S> {
protected listIndex: number = this.getListIndex(this.settings.default);
protected listOffset: number = this.getPageOffset(this.listIndex);

/**
* Inject prompt value. Can be used for unit tests or pre selections.
Expand Down Expand Up @@ -99,22 +98,6 @@ export class Select<S extends SelectSettings = SelectSettings>
return this.options[this.listIndex]?.value ?? this.settings.default;
}

protected getListIndex(value: string | undefined) {
return typeof value === "undefined"
? 0
: this.options.findIndex((item: SelectOptionSettings) =>
item.value === value
) || 0;
}

protected getPageOffset(index: number) {
if (index === 0) {
return 0;
}
const height: number = this.getListHeight();
return Math.floor(index / height) * height;
}

/**
* Validate input value.
* @param value User input value.
Expand Down

0 comments on commit f5c9be5

Please sign in to comment.