Skip to content

Commit ef8ed7e

Browse files
author
chouchouji
committed
refactor: optimize code
1 parent 6eed35d commit ef8ed7e

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/core/src/prompts/multi-select.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ export default class MultiSelectPrompt<T extends { value: any; disabled?: boolea
1313
> {
1414
options: T[];
1515
cursor = 0;
16+
#enabledOptions: T[] = [];
1617

1718
private get _value(): T['value'] {
1819
return this.options[this.cursor].value;
1920
}
2021

2122
private toggleAll() {
22-
const allSelected = this.value !== undefined && this.value.length === this.options.length;
23-
this.value = allSelected ? [] : this.options.filter((v) => !v.disabled).map((v) => v.value);
23+
const allSelected = this.value !== undefined && this.value.length === this.#enabledOptions.length;
24+
this.value = allSelected ? [] : this.#enabledOptions.map((v) => v.value);
2425
}
2526

2627
private toggleInvert() {
2728
const value = this.value;
2829
if (!value) {
2930
return;
3031
}
31-
const notSelected = this.options.filter((v) => !v.disabled && !value.includes(v.value));
32+
const notSelected = this.#enabledOptions.filter((v) => !value.includes(v.value));
3233
this.value = notSelected.map((v) => v.value);
3334
}
3435

@@ -46,8 +47,8 @@ export default class MultiSelectPrompt<T extends { value: any; disabled?: boolea
4647
super(opts, false);
4748

4849
this.options = opts.options;
49-
const disabledOptions = this.options.filter((option) => option.disabled);
50-
if (this.options.length === disabledOptions.length) return;
50+
this.#enabledOptions = this.options.filter((option) => !option.disabled);
51+
if (this.#enabledOptions.length === 0) return;
5152
this.value = [...(opts.initialValues ?? [])];
5253
const cursor = Math.max(
5354
this.options.findIndex(({ value }) => value === opts.cursorAt),

packages/core/src/prompts/select.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class SelectPrompt<T extends { value: any; disabled?: boolean }>
1111
> {
1212
options: T[];
1313
cursor = 0;
14+
#enabledOptions: T[] = [];
1415

1516
private get _selectedValue() {
1617
return this.options[this.cursor];
@@ -24,8 +25,8 @@ export default class SelectPrompt<T extends { value: any; disabled?: boolean }>
2425
super(opts, false);
2526

2627
this.options = opts.options;
27-
const disabledOptions = this.options.filter((option) => option.disabled);
28-
if (this.options.length === disabledOptions.length) return;
28+
this.#enabledOptions = this.options.filter((option) => !option.disabled);
29+
if (this.#enabledOptions.length === 0) return;
2930

3031
const initialCursor = this.options.findIndex(({ value }) => value === opts.initialValue);
3132
const cursor = initialCursor === -1 ? 0 : initialCursor

0 commit comments

Comments
 (0)