Skip to content

Commit 12cc5bc

Browse files
NguyenThuyLanLan Nguyen ThuyiOvergaard
authored
fix: Text character length validation should be more helpful (#1060)
* Fix bug text character length validation should be more helpful * add variables to the message string validation * remove formatstring * Update packages/uui-input/lib/uui-input.element.ts --------- Co-authored-by: Lan Nguyen Thuy <[email protected]> Co-authored-by: Jacob Overgaard <[email protected]>
1 parent df8b8b0 commit 12cc5bc

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

packages/uui-input/lib/uui-input.element.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ export class UUIInputElement extends UUIFormControlMixin(
9494
* @attr minlength-message
9595
* @default
9696
*/
97-
@property({ type: String, attribute: 'minlength-message' })
98-
minlengthMessage = 'This field need more characters';
97+
@property({ attribute: 'minlength-message' })
98+
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
99+
`${charsLeft} characters left`;
99100

100101
/**
101102
* Sets the max value of the input.
@@ -121,8 +122,11 @@ export class UUIInputElement extends UUIFormControlMixin(
121122
* @attr maxlength-message
122123
* @default
123124
*/
124-
@property({ type: String, attribute: 'maxlength-message' })
125-
maxlengthMessage = 'This field exceeds the allowed amount of characters';
125+
@property({ attribute: 'maxlength-message' })
126+
maxlengthMessage: string | ((max: number, current: number) => string) = (
127+
max,
128+
current,
129+
) => `Maximum length exceeded (${current}/${max} characters)`;
126130

127131
/**
128132
* Specifies the interval between legal numbers of the input
@@ -228,12 +232,26 @@ export class UUIInputElement extends UUIFormControlMixin(
228232

229233
this.addValidator(
230234
'tooShort',
231-
() => this.minlengthMessage,
235+
() => {
236+
const label = this.minlengthMessage;
237+
if (typeof label === 'function') {
238+
return label(
239+
this.minlength ? this.minlength - String(this.value).length : 0,
240+
);
241+
}
242+
return label;
243+
},
232244
() => !!this.minlength && String(this.value).length < this.minlength,
233245
);
234246
this.addValidator(
235247
'tooLong',
236-
() => this.maxlengthMessage,
248+
() => {
249+
const label = this.maxlengthMessage;
250+
if (typeof label === 'function') {
251+
return label(this.maxlength ?? 0, String(this.value).length);
252+
}
253+
return label;
254+
},
237255
() => !!this.maxlength && String(this.value).length > this.maxlength,
238256
);
239257

packages/uui-textarea/lib/uui-textarea.element.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {
8181

8282
/**
8383
* Minlength validation message.
84-
* @type {boolean}
8584
* @attr
8685
* @default
8786
*/
88-
@property({ type: String, attribute: 'minlength-message' })
89-
minlengthMessage = 'This field need more characters';
90-
87+
@property({ attribute: 'minlength-message' })
88+
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
89+
`${charsLeft} characters left`;
9190
/**
9291
* This is a maximum value of the input.
9392
* @type {number}
@@ -99,12 +98,14 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {
9998

10099
/**
101100
* Maxlength validation message.
102-
* @type {boolean}
103101
* @attr
104102
* @default
105103
*/
106-
@property({ type: String, attribute: 'maxlength-message' })
107-
maxlengthMessage = 'This field exceeds the allowed amount of characters';
104+
@property({ attribute: 'maxlength-message' })
105+
maxlengthMessage: string | ((max: number, current: number) => string) = (
106+
max,
107+
current,
108+
) => `Maximum ${max} characters, ${current} too many.`;
108109

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

164165
this.addValidator(
165166
'tooShort',
166-
() => this.minlengthMessage,
167+
() => {
168+
const label = this.minlengthMessage;
169+
if (typeof label === 'function') {
170+
return label(
171+
this.minlength ? this.minlength - String(this.value).length : 0,
172+
);
173+
}
174+
return label;
175+
},
167176
() => !!this.minlength && (this.value as string).length < this.minlength,
168177
);
169178
this.addValidator(
170179
'tooLong',
171-
() => this.maxlengthMessage,
180+
() => {
181+
const label = this.maxlengthMessage;
182+
if (typeof label === 'function') {
183+
return label(this.maxlength ?? 0, String(this.value).length);
184+
}
185+
return label;
186+
},
172187
() => !!this.maxlength && (this.value as string).length > this.maxlength,
173188
);
174189
}

0 commit comments

Comments
 (0)