Skip to content

Commit

Permalink
[breaking] Remove support for legacy browsers (See readme).
Browse files Browse the repository at this point in the history
Remove custom CSS class for invalid elements (Use :invalid) instead.
  • Loading branch information
RillingDev committed Mar 15, 2021
1 parent 150f2c8 commit 7c969a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 45 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ new Ok({}, "myClass");
new Ok({}, false);
```

## Legacy Browsers
## Compatibility

For browsers not supporting the HTML5 Validation API (<https://caniuse.com/#feat=form-validation>), ok.js will still work, but the validation message will not be shown.
Ok should work in all browsers that support the following:

- <https://caniuse.com/form-validation>
- <https://caniuse.com/mdn-api_htmlinputelement_setcustomvalidity>
18 changes: 3 additions & 15 deletions src/Ok.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Validator } from "./validator/Validator";
import type { ValidatorDictionary } from "./validator/ValidatorDictionary";
import { setCustomValidity } from "./dom/setCustomValidity";
import type { ValidatableElement } from "./dom/ValidatableElement";
import { getValidatableElementValue } from "./dom/ValidatableElement";

Expand All @@ -16,21 +15,15 @@ type ValidatorMap = Map<string, Validator>;
*/
export class Ok {
private readonly map: ValidatorMap;
private readonly invalidClass: string | null;

/**
* Ok constructor.
*
* @public
* @param validators Object containing the validators to use.
* @param invalidClass CSS class for invalid elements, or null if none should be set.
*/
public constructor(
validators: ValidatorDictionary,
invalidClass: string | null = "invalid"
) {
public constructor(validators: ValidatorDictionary) {
this.map = new Map(Object.entries(validators));
this.invalidClass = invalidClass;
}

/**
Expand Down Expand Up @@ -70,16 +63,11 @@ export class Ok {
typeof validator.msg === "function"
? validator.msg(value, element, e)
: validator.msg;
setCustomValidity(element, msg);
element.setCustomValidity(msg);
}
}
if (valid) {
setCustomValidity(element, "");
if (this.invalidClass != null) {
element.classList.remove(this.invalidClass);
}
} else if (this.invalidClass != null) {
element.classList.add(this.invalidClass);
element.setCustomValidity("");
}

return valid;
Expand Down
28 changes: 0 additions & 28 deletions src/dom/setCustomValidity.ts

This file was deleted.

0 comments on commit 7c969a7

Please sign in to comment.