diff --git a/lib/elements/autocomplete.js b/lib/elements/autocomplete.js index b7a0d4c..36a65da 100644 --- a/lib/elements/autocomplete.js +++ b/lib/elements/autocomplete.js @@ -93,12 +93,7 @@ class AutocompletePrompt extends Prompt { if (this.clearFirst && this.input.length > 0) { this.reset(); } else { - this.done = this.exited = true; - this.aborted = false; - this.fire(); - this.render(); - this.out.write('\n'); - this.close(); + this.abort(); } } diff --git a/lib/elements/number.js b/lib/elements/number.js index 253b846..00c9436 100644 --- a/lib/elements/number.js +++ b/lib/elements/number.js @@ -185,10 +185,11 @@ class NumberPrompt extends Prompt { render() { if (this.closed) return; if (!this.firstRender && this.outputError) { + this.out.write(cursor.restore); this.out.write( - cursor.down(lines(this.outputError, this.out.columns) - 1) + - clear(this.outputError, this.out.columns), + cursor.down(lines(this.outputError, this.out.columns) - 1), ); + this.clear = clear(this.outputError, this.out.columns); } super.render(); this.outputError = ''; diff --git a/lib/elements/text.js b/lib/elements/text.js index 37ab8e1..26b1f44 100644 --- a/lib/elements/text.js +++ b/lib/elements/text.js @@ -187,10 +187,11 @@ class TextPrompt extends Prompt { render() { if (this.closed) return; if (!this.firstRender && this.outputError) { + this.out.write(cursor.restore); this.out.write( - cursor.down(lines(this.outputError, this.out.columns) - 1) + - clear(this.outputError, this.out.columns), + cursor.down(lines(this.outputError, this.out.columns) - 1), ); + this.clear = clear(this.outputError, this.out.columns); } super.render(); this.outputError = ''; diff --git a/src/index.ts b/src/index.ts index 1ee37be..4c9fb16 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import type { BaseConfig, BaseReturn, MultiSelectReturn, + NumberReturn, SelectConfig, SelectReturn, ToggleConfig, @@ -92,8 +93,20 @@ const prompts = { async number( config: BaseConfig, options?: Options, - ): BaseReturn { - return await $_({ ...config, type: 'number' }, options); + ): NumberReturn { + return await $_( + { + validate(input) { + return typeof input === 'number' || 'Enter a valid number'; + }, + ...config, + type: 'number', + format(input) { + return Number(input); + }, + }, + options, + ); }, async list( diff --git a/src/types.ts b/src/types.ts index f003f13..0f61283 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,6 +35,10 @@ export type MultiSelectReturn< [K in Name]: C['value'][] | undefined; }>; +export type NumberReturn = Promise<{ + [K in Name]: number | undefined; +}>; + /* ~~~ TOGGLE ~~~ */ export interface ToggleConfig extends BaseConfig { active?: string;