MCPcopy Create free account
hub / github.com/bombshell-dev/clack / MultiSelectPrompt

Class MultiSelectPrompt

packages/core/src/prompts/multi-select.ts:16–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14 cursorAt?: T['value'];
15}
16export default class MultiSelectPrompt<T extends OptionLike> extends Prompt<T['value'][]> {
17 options: T[];
18 cursor = 0;
19
20 private get _value(): T['value'] {
21 return this.options[this.cursor].value;
22 }
23
24 private get _enabledOptions(): T[] {
25 return this.options.filter((option) => option.disabled !== true);
26 }
27
28 private toggleAll() {
29 const enabledOptions = this._enabledOptions;
30 const allSelected = this.value !== undefined && this.value.length === enabledOptions.length;
31 this.value = allSelected ? [] : enabledOptions.map((v) => v.value);
32 }
33
34 private toggleInvert() {
35 const value = this.value;
36 if (!value) {
37 return;
38 }
39 const notSelected = this._enabledOptions.filter((v) => !value.includes(v.value));
40 this.value = notSelected.map((v) => v.value);
41 }
42
43 private toggleValue() {
44 if (this.value === undefined) {
45 this.value = [];
46 }
47 const selected = this.value.includes(this._value);
48 this.value = selected
49 ? this.value.filter((value) => value !== this._value)
50 : [...this.value, this._value];
51 }
52
53 constructor(opts: MultiSelectOptions<T>) {
54 super(opts, false);
55
56 this.options = opts.options;
57 this.value = [...(opts.initialValues ?? [])];
58 const cursor = Math.max(
59 this.options.findIndex(({ value }) => value === opts.cursorAt),
60 0
61 );
62 this.cursor = this.options[cursor].disabled ? findCursor<T>(cursor, 1, this.options) : cursor;
63 this.on('key', (_char, key) => {
64 if (key.name === 'a') {
65 this.toggleAll();
66 }
67 if (key.name === 'i') {
68 this.toggleInvert();
69 }
70 });
71
72 this.on('cursor', (key) => {
73 switch (key) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected