From 62b293715492522c3bec64350fee829a7d4263c2 Mon Sep 17 00:00:00 2001 From: Lan Nguyen Thuy Date: Mon, 31 Mar 2025 14:52:25 +0700 Subject: [PATCH 1/3] Fix bug text character length validation should be more helpful --- packages/uui-input/lib/uui-input.element.ts | 24 +++++++++++++++--- .../uui-textarea/lib/uui-textarea.element.ts | 25 +++++++++++++++---- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/uui-input/lib/uui-input.element.ts b/packages/uui-input/lib/uui-input.element.ts index bd9cd3b9a..47ba66155 100644 --- a/packages/uui-input/lib/uui-input.element.ts +++ b/packages/uui-input/lib/uui-input.element.ts @@ -85,7 +85,7 @@ export class UUIInputElement extends UUIFormControlMixin( * @default */ @property({ type: String, attribute: 'minlength-message' }) - minlengthMessage = 'This field need more characters'; + minlengthMessage = '{0} characters left'; /** * Sets the max value of the input. @@ -112,7 +112,7 @@ export class UUIInputElement extends UUIFormControlMixin( * @default */ @property({ type: String, attribute: 'maxlength-message' }) - maxlengthMessage = 'This field exceeds the allowed amount of characters'; + maxlengthMessage = 'Maximum {0} characters, {1} too many.'; /** * Specifies the interval between legal numbers of the input @@ -216,12 +216,21 @@ export class UUIInputElement extends UUIFormControlMixin( this.addValidator( 'tooShort', - () => this.minlengthMessage, + () => + this.formatString( + this.minlengthMessage, + this.minlength ? this.minlength - String(this.value).length : '', + ), () => !!this.minlength && String(this.value).length < this.minlength, ); this.addValidator( 'tooLong', - () => this.maxlengthMessage, + () => + this.formatString( + this.maxlengthMessage, + this.maxlength ?? '', + this.maxlength ? String(this.value).length - this.maxlength : '', + ), () => !!this.maxlength && String(this.value).length > this.maxlength, ); @@ -230,6 +239,13 @@ export class UUIInputElement extends UUIFormControlMixin( }); } + formatString(str: string, ...values: (string | number)[]): string { + return str.replace( + /{(\d+)}/g, + (_, index) => values[index]?.toString() ?? '', + ); + } + #onKeyDown(e: KeyboardEvent): void { if (this.type !== 'color' && e.key == 'Enter') { this.submit(); diff --git a/packages/uui-textarea/lib/uui-textarea.element.ts b/packages/uui-textarea/lib/uui-textarea.element.ts index b457fe9f1..61491a0ac 100644 --- a/packages/uui-textarea/lib/uui-textarea.element.ts +++ b/packages/uui-textarea/lib/uui-textarea.element.ts @@ -86,8 +86,7 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') { * @default */ @property({ type: String, attribute: 'minlength-message' }) - minlengthMessage = 'This field need more characters'; - + minlengthMessage = '{0} characters left'; /** * This is a maximum value of the input. * @type {number} @@ -104,7 +103,7 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') { * @default */ @property({ type: String, attribute: 'maxlength-message' }) - maxlengthMessage = 'This field exceeds the allowed amount of characters'; + maxlengthMessage = 'Maximum {0} characters, {1} too many.'; @query('#textarea') protected _textarea!: HTMLInputElement; @@ -163,12 +162,21 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') { this.addValidator( 'tooShort', - () => this.minlengthMessage, + () => + this.formatString( + this.minlengthMessage, + this.minlength ? this.minlength - String(this.value).length : '', + ), () => !!this.minlength && (this.value as string).length < this.minlength, ); this.addValidator( 'tooLong', - () => this.maxlengthMessage, + () => + this.formatString( + this.maxlengthMessage, + this.maxlength ?? '', + this.maxlength ? String(this.value).length - this.maxlength : '', + ), () => !!this.maxlength && (this.value as string).length > this.maxlength, ); } @@ -186,6 +194,13 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') { } } + formatString(str: string, ...values: (string | number)[]): string { + return str.replace( + /{(\d+)}/g, + (_, index) => values[index]?.toString() ?? '', + ); + } + /** * This method enables