Skip to content

Commit a1552d3

Browse files
author
Lan Nguyen Thuy
committed
add variables to the message string validation
1 parent 0f1f140 commit a1552d3

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ export class UUIInputElement extends UUIFormControlMixin(
8484
* @attr minlength-message
8585
* @default
8686
*/
87-
@property({ type: String, attribute: 'minlength-message' })
88-
minlengthMessage = '{0} characters left';
87+
@property({ attribute: 'minlength-message' })
88+
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
89+
`${charsLeft} characters left`;
8990

9091
/**
9192
* Sets the max value of the input.
@@ -111,8 +112,11 @@ export class UUIInputElement extends UUIFormControlMixin(
111112
* @attr maxlength-message
112113
* @default
113114
*/
114-
@property({ type: String, attribute: 'maxlength-message' })
115-
maxlengthMessage = 'Maximum {0} characters, {1} too many.';
115+
@property({ attribute: 'maxlength-message' })
116+
maxlengthMessage: string | ((max: number, current: number) => string) = (
117+
max,
118+
current,
119+
) => `Maximum ${max} characters, ${current} too many.`;
116120

117121
/**
118122
* Specifies the interval between legal numbers of the input
@@ -216,21 +220,26 @@ export class UUIInputElement extends UUIFormControlMixin(
216220

217221
this.addValidator(
218222
'tooShort',
219-
() =>
220-
this.formatString(
221-
this.minlengthMessage,
222-
this.minlength ? this.minlength - String(this.value).length : '',
223-
),
223+
() => {
224+
const label = this.minlengthMessage;
225+
if (typeof label === 'function') {
226+
return label(
227+
this.minlength ? this.minlength - String(this.value).length : 0,
228+
);
229+
}
230+
return label;
231+
},
224232
() => !!this.minlength && String(this.value).length < this.minlength,
225233
);
226234
this.addValidator(
227235
'tooLong',
228-
() =>
229-
this.formatString(
230-
this.maxlengthMessage,
231-
this.maxlength ?? '',
232-
this.maxlength ? String(this.value).length - this.maxlength : '',
233-
),
236+
() => {
237+
const label = this.maxlengthMessage;
238+
if (typeof label === 'function') {
239+
return label(this.maxlength ?? 0, String(this.value).length);
240+
}
241+
return label;
242+
},
234243
() => !!this.maxlength && String(this.value).length > this.maxlength,
235244
);
236245

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +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 = '{0} characters left';
87+
@property({ attribute: 'minlength-message' })
88+
minlengthMessage: string | ((charsLeft: number) => string) = charsLeft =>
89+
`${charsLeft} characters left`;
9090
/**
9191
* This is a maximum value of the input.
9292
* @type {number}
@@ -98,12 +98,14 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {
9898

9999
/**
100100
* Maxlength validation message.
101-
* @type {boolean}
102101
* @attr
103102
* @default
104103
*/
105-
@property({ type: String, attribute: 'maxlength-message' })
106-
maxlengthMessage = 'Maximum {0} characters, {1} too many.';
104+
@property({ attribute: 'maxlength-message' })
105+
maxlengthMessage: string | ((max: number, current: number) => string) = (
106+
max,
107+
current,
108+
) => `Maximum ${max} characters, ${current} too many.`;
107109

108110
@query('#textarea')
109111
protected _textarea!: HTMLInputElement;
@@ -162,21 +164,26 @@ export class UUITextareaElement extends UUIFormControlMixin(LitElement, '') {
162164

163165
this.addValidator(
164166
'tooShort',
165-
() =>
166-
this.formatString(
167-
this.minlengthMessage,
168-
this.minlength ? this.minlength - String(this.value).length : '',
169-
),
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+
},
170176
() => !!this.minlength && (this.value as string).length < this.minlength,
171177
);
172178
this.addValidator(
173179
'tooLong',
174-
() =>
175-
this.formatString(
176-
this.maxlengthMessage,
177-
this.maxlength ?? '',
178-
this.maxlength ? String(this.value).length - this.maxlength : '',
179-
),
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+
},
180187
() => !!this.maxlength && (this.value as string).length > this.maxlength,
181188
);
182189
}

0 commit comments

Comments
 (0)