Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug text character length validation should be more helpful #1060

Open
wants to merge 1 commit into
base: v1/contrib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions packages/uui-input/lib/uui-input.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
);

Expand All @@ -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();
Expand Down
25 changes: 20 additions & 5 deletions packages/uui-textarea/lib/uui-textarea.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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;
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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 <label for="..."> to focus the select
*/
Expand Down
Loading