@fluid-topics/ft-wc-utils 1.2.69 → 1.2.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ import { Constructor } from "../generic-types";
2
+ import { ValidationRule } from "./ValidationRules";
3
+ import { FtLitElement } from "../FtLitElement";
4
+ export type FtInputInterface<V> = {
5
+ warningRules: Array<ValidationRule<V>>;
6
+ errorRules: Array<ValidationRule<V>>;
7
+ validateOn: ValidateOn;
8
+ validateAtRender: boolean;
9
+ label: string;
10
+ name: string;
11
+ disabled: boolean;
12
+ helperText?: string;
13
+ forceStatus?: Status;
14
+ value?: V;
15
+ hideLabel: boolean;
16
+ errorMessages?: string[];
17
+ warningMessages?: string[];
18
+ status: Status;
19
+ additionalWarningRules: Array<ValidationRule<V>>;
20
+ additionalErrorRules: Array<ValidationRule<V>>;
21
+ focused: boolean;
22
+ onInput(): void;
23
+ onFocus(): void;
24
+ onBlur(): void;
25
+ triggerValidation(): void;
26
+ };
27
+ export declare enum ValidateOn {
28
+ blur = "blur",
29
+ blurWithStatusResolutionOnInput = "blurWithStatusResolutionOnInput",
30
+ input = "input"
31
+ }
32
+ export declare enum Status {
33
+ default = "default",
34
+ warning = "warning",
35
+ error = "error"
36
+ }
37
+ export type FtInputType<T extends Constructor<FtLitElement>, V> = T & Constructor<FtInputInterface<V>>;
38
+ export declare class InputValidationEvent extends CustomEvent<{
39
+ level: string;
40
+ }> {
41
+ constructor(level: string);
42
+ }
43
+ export declare function toFtInput<T extends Constructor<FtLitElement>, V>(ElementClass: T, ValueClass: Constructor<V>): FtInputType<T, V>;
@@ -0,0 +1,134 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { property, state, } from "lit/decorators.js";
8
+ export var ValidateOn;
9
+ (function (ValidateOn) {
10
+ ValidateOn["blur"] = "blur";
11
+ ValidateOn["blurWithStatusResolutionOnInput"] = "blurWithStatusResolutionOnInput";
12
+ ValidateOn["input"] = "input";
13
+ })(ValidateOn || (ValidateOn = {}));
14
+ export var Status;
15
+ (function (Status) {
16
+ Status["default"] = "default";
17
+ Status["warning"] = "warning";
18
+ Status["error"] = "error";
19
+ })(Status || (Status = {}));
20
+ export class InputValidationEvent extends CustomEvent {
21
+ constructor(level) {
22
+ super("input-validated", { detail: { level: level } });
23
+ }
24
+ }
25
+ export function toFtInput(ElementClass, ValueClass) {
26
+ class FtInput extends ElementClass {
27
+ constructor() {
28
+ super(...arguments);
29
+ this.warningRules = [];
30
+ this.errorRules = [];
31
+ this.validateOn = ValidateOn.input;
32
+ this.validateAtRender = false;
33
+ this.label = "";
34
+ this.hideLabel = false;
35
+ this.name = "";
36
+ this.disabled = false;
37
+ this.errorMessages = [];
38
+ this.warningMessages = [];
39
+ this.status = Status.default;
40
+ this.focused = false;
41
+ this.additionalWarningRules = [];
42
+ this.additionalErrorRules = [];
43
+ }
44
+ connectedCallback() {
45
+ super.connectedCallback();
46
+ if (this.validateAtRender) {
47
+ this.triggerValidation();
48
+ }
49
+ }
50
+ onBlur() {
51
+ this.focused = false;
52
+ if (this.validateOn === ValidateOn.blur || this.validateOn === ValidateOn.blurWithStatusResolutionOnInput) {
53
+ this.triggerValidation();
54
+ }
55
+ }
56
+ onFocus() {
57
+ this.focused = true;
58
+ }
59
+ onInput() {
60
+ if ((this.status !== Status.default && this.validateOn === ValidateOn.blurWithStatusResolutionOnInput) || this.validateOn === ValidateOn.input) {
61
+ this.triggerValidation();
62
+ }
63
+ }
64
+ triggerValidation() {
65
+ this.errorMessages = this.processRules([...this.errorRules, ...this.additionalErrorRules]);
66
+ this.warningMessages = this.processRules([...this.warningRules, ...this.additionalWarningRules]);
67
+ this.status = this.resolveStatus();
68
+ this.dispatchEvent(new InputValidationEvent(this.status));
69
+ }
70
+ resolveStatus() {
71
+ return this.forceStatus ? this.forceStatus :
72
+ this.errorMessages.length ? Status.error :
73
+ this.warningMessages.length ? Status.warning :
74
+ Status.default;
75
+ }
76
+ processRules(rules) {
77
+ return rules.map(r => r(this.value))
78
+ .filter(s => s !== true)
79
+ .map(e => e);
80
+ }
81
+ }
82
+ __decorate([
83
+ property()
84
+ ], FtInput.prototype, "warningRules", void 0);
85
+ __decorate([
86
+ property()
87
+ ], FtInput.prototype, "errorRules", void 0);
88
+ __decorate([
89
+ property()
90
+ ], FtInput.prototype, "validateOn", void 0);
91
+ __decorate([
92
+ property({ type: Boolean })
93
+ ], FtInput.prototype, "validateAtRender", void 0);
94
+ __decorate([
95
+ property()
96
+ ], FtInput.prototype, "label", void 0);
97
+ __decorate([
98
+ property({ type: Boolean })
99
+ ], FtInput.prototype, "hideLabel", void 0);
100
+ __decorate([
101
+ property()
102
+ ], FtInput.prototype, "name", void 0);
103
+ __decorate([
104
+ property({ type: Boolean })
105
+ ], FtInput.prototype, "disabled", void 0);
106
+ __decorate([
107
+ property()
108
+ ], FtInput.prototype, "helperText", void 0);
109
+ __decorate([
110
+ property()
111
+ ], FtInput.prototype, "forceStatus", void 0);
112
+ __decorate([
113
+ property({ reflect: true })
114
+ ], FtInput.prototype, "value", void 0);
115
+ __decorate([
116
+ state()
117
+ ], FtInput.prototype, "errorMessages", void 0);
118
+ __decorate([
119
+ state()
120
+ ], FtInput.prototype, "warningMessages", void 0);
121
+ __decorate([
122
+ state()
123
+ ], FtInput.prototype, "status", void 0);
124
+ __decorate([
125
+ state()
126
+ ], FtInput.prototype, "focused", void 0);
127
+ __decorate([
128
+ state()
129
+ ], FtInput.prototype, "additionalWarningRules", void 0);
130
+ __decorate([
131
+ state()
132
+ ], FtInput.prototype, "additionalErrorRules", void 0);
133
+ return FtInput;
134
+ }
@@ -0,0 +1,6 @@
1
+ export type ValidationRule<V> = (value?: V) => true | string;
2
+ export declare const ValidationRules: {
3
+ required: (message: string) => ValidationRule<string>;
4
+ email: (message: string) => ValidationRule<String>;
5
+ url: (message: string) => ValidationRule<String>;
6
+ };
@@ -0,0 +1,13 @@
1
+ const URL_REGEX = /^[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
2
+ const EMAIL_REGEX = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
3
+ export const ValidationRules = {
4
+ required: function (message) {
5
+ return (v) => (!!v || message);
6
+ },
7
+ email: function (message) {
8
+ return (v) => ((!v || EMAIL_REGEX.test(v.toString())) || message);
9
+ },
10
+ url: function (message) {
11
+ return (v) => ((!v || URL_REGEX.test(v.toString())) || message);
12
+ },
13
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./FtInput";
2
+ export * from "./ValidationRules";
@@ -0,0 +1,2 @@
1
+ export * from "./FtInput";
2
+ export * from "./ValidationRules";