Skip to content

Commit 3f2e9e9

Browse files
committed
fix(text): cursor rendering bug
1 parent 0ada41e commit 3f2e9e9

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

packages/core/src/prompts/text.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ export interface TextOptions extends PromptOptions<TextPrompt> {
77
}
88

99
export default class TextPrompt extends Prompt {
10-
valueWithCursor = '';
10+
get valueWithCursor() {
11+
if (this.state === 'submit') {
12+
return this.value;
13+
}
14+
if (this.cursor >= this.value.length) {
15+
return `${this.value}${color.inverse(color.hidden('_'))}`;
16+
}
17+
const s1 = this.value.slice(0, this.cursor);
18+
const [s2, ...s3] = this.value.slice(this.cursor);
19+
return `${s1}${color.inverse(s2)}${s3.join('')}`;
20+
}
1121
get cursor() {
1222
return this._cursor;
1323
}
@@ -18,16 +28,6 @@ export default class TextPrompt extends Prompt {
1828
if (!this.value) {
1929
this.value = opts.defaultValue;
2030
}
21-
this.valueWithCursor = this.value;
22-
});
23-
this.on('value', () => {
24-
if (this.cursor >= this.value.length) {
25-
this.valueWithCursor = `${this.value}${color.inverse(color.hidden('_'))}`;
26-
} else {
27-
const s1 = this.value.slice(0, this.cursor);
28-
const s2 = this.value.slice(this.cursor);
29-
this.valueWithCursor = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`;
30-
}
3131
});
3232
}
3333
}

0 commit comments

Comments
 (0)