Skip to content

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

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

Open
wants to merge 6 commits 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
30 changes: 24 additions & 6 deletions packages/uui-input/lib/uui-input.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ export class UUIInputElement extends UUIFormControlMixin(
* @attr minlength-message
* @default
*/
@property({ type: String, attribute: 'minlength-message' })
minlengthMessage = 'This field need more characters';
@property({ attribute: 'minlength-message' })
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
`${charsLeft} characters left`;

/**
* Sets the max value of the input.
Expand All @@ -111,8 +112,11 @@ export class UUIInputElement extends UUIFormControlMixin(
* @attr maxlength-message
* @default
*/
@property({ type: String, attribute: 'maxlength-message' })
maxlengthMessage = 'This field exceeds the allowed amount of characters';
@property({ attribute: 'maxlength-message' })
maxlengthMessage: string | ((max: number, current: number) => string) = (
max,
current,
) => `Maximum ${max} characters, ${current} too many.`;

/**
* Specifies the interval between legal numbers of the input
Expand Down Expand Up @@ -216,12 +220,26 @@ export class UUIInputElement extends UUIFormControlMixin(

this.addValidator(
'tooShort',
() => this.minlengthMessage,
() => {
const label = this.minlengthMessage;
if (typeof label === 'function') {
return label(
this.minlength ? this.minlength - String(this.value).length : 0,
);
}
return label;
},
() => !!this.minlength && String(this.value).length < this.minlength,
);
this.addValidator(
'tooLong',
() => this.maxlengthMessage,
() => {
const label = this.maxlengthMessage;
if (typeof label === 'function') {
return label(this.maxlength ?? 0, String(this.value).length);
}
return label;
},
() => !!this.maxlength && String(this.value).length > this.maxlength,
);

Expand Down
33 changes: 24 additions & 9 deletions packages/uui-textarea/lib/uui-textarea.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {

/**
* Minlength validation message.
* @type {boolean}
* @attr
* @default
*/
@property({ type: String, attribute: 'minlength-message' })
minlengthMessage = 'This field need more characters';

@property({ attribute: 'minlength-message' })
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
`${charsLeft} characters left`;
/**
* This is a maximum value of the input.
* @type {number}
Expand All @@ -99,12 +98,14 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {

/**
* Maxlength validation message.
* @type {boolean}
* @attr
* @default
*/
@property({ type: String, attribute: 'maxlength-message' })
maxlengthMessage = 'This field exceeds the allowed amount of characters';
@property({ attribute: 'maxlength-message' })
maxlengthMessage: string | ((max: number, current: number) => string) = (
max,
current,
) => `Maximum ${max} characters, ${current} too many.`;

@query('#textarea')
protected _textarea!: HTMLInputElement;
Expand Down Expand Up @@ -163,12 +164,26 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {

this.addValidator(
'tooShort',
() => this.minlengthMessage,
() => {
const label = this.minlengthMessage;
if (typeof label === 'function') {
return label(
this.minlength ? this.minlength - String(this.value).length : 0,
);
}
return label;
},
() => !!this.minlength && (this.value as string).length < this.minlength,
);
this.addValidator(
'tooLong',
() => this.maxlengthMessage,
() => {
const label = this.maxlengthMessage;
if (typeof label === 'function') {
return label(this.maxlength ?? 0, String(this.value).length);
}
return label;
},
() => !!this.maxlength && (this.value as string).length > this.maxlength,
);
}
Expand Down
Loading