Skip to content

Commit da02e14

Browse files
committed
implement setCustomValidity
1 parent 6c2feed commit da02e14

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

packages/uui-base/lib/mixins/FormControlMixin.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ type FlagTypes =
4949
| 'badInput'
5050
| 'valid';
5151

52+
// Acceptable as an internal interface/type, BUT if exposed externally this should be turned into a public class in a separate file.
5253
interface Validator {
5354
flagKey: FlagTypes;
54-
getMessage: () => String;
55+
getMessageMethod: () => String;
5556
checkMethod: () => boolean;
5657
}
5758

@@ -212,19 +213,28 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
212213
* );
213214
* @method hasValue
214215
* @param {FlagTypes} flagKey the type of validation.
215-
* @param {method} getMessage method to retrieve relevant message. Is executed every time the validator is re-executed.
216+
* @param {method} getMessageMethod method to retrieve relevant message. Is executed every time the validator is re-executed.
216217
* @param {method} checkMethod method to determine if this validator should invalidate this form control. Return true if this should prevent submission.
217218
*/
218219
protected addValidator(
219220
flagKey: FlagTypes,
220221
getMessageMethod: () => String,
221222
checkMethod: () => boolean
222-
) {
223-
this._validators.push({
223+
): Validator {
224+
const obj = {
224225
flagKey: flagKey,
225-
getMessage: getMessageMethod,
226+
getMessageMethod: getMessageMethod,
226227
checkMethod: checkMethod,
227-
});
228+
};
229+
this._validators.push(obj);
230+
return obj;
231+
}
232+
233+
protected removeValidator(validator: Validator) {
234+
const index = this._validators.indexOf(validator);
235+
if (index !== -1) {
236+
this._validators.splice(index, 1);
237+
}
228238
}
229239

230240
/**
@@ -236,26 +246,28 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
236246
this._formCtrlElements.push(element);
237247
}
238248

249+
private _customValidityObject?: Validator;
250+
239251
/**
240252
* @method setCustomValidity
241253
* @description Set custom validity state, set to empty string to remove the custom message.
242254
* @param message {string} - The message to be shown
243255
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity|HTMLObjectElement:setCustomValidity}
244256
*/
245-
protected setCustomValidity(message: string) {
246-
if (message === '') {
247-
this._internals.setValidity({});
248-
this.dispatchEvent(new UUIFormControlEvent(UUIFormControlEvent.VALID));
249-
return;
257+
protected setCustomValidity(message: string | null) {
258+
if (this._customValidityObject) {
259+
this.removeValidator(this._customValidityObject);
250260
}
251261

252-
this._internals.setValidity(
253-
{ customError: true },
254-
message,
255-
this.getFormElement()
256-
);
262+
if (message != null && message !== '') {
263+
this._customValidityObject = this.addValidator(
264+
'customError',
265+
(): string => message,
266+
() => true
267+
);
268+
}
257269

258-
this.dispatchEvent(new UUIFormControlEvent(UUIFormControlEvent.INVALID));
270+
this._runValidators();
259271
}
260272

261273
private _runValidators() {
@@ -281,7 +293,7 @@ export const FormControlMixin = <T extends Constructor<LitElement>>(
281293
this._validityState[validator.flagKey] = true;
282294
this._internals.setValidity(
283295
this._validityState,
284-
validator.getMessage(),
296+
validator.getMessageMethod(),
285297
this.getFormElement()
286298
);
287299
}

0 commit comments

Comments
 (0)