foorm 0.0.2-alpha.9 → 0.0.3
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.
- package/README.md +1 -1
- package/dist/index.cjs +36 -23
- package/dist/index.d.ts +19 -14
- package/dist/index.mjs +36 -23
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
# @foormjs/foorm
|
|
1
|
+
# @foormjs/foorm
|
package/dist/index.cjs
CHANGED
|
@@ -4,8 +4,8 @@ var ftring$1 = require('@prostojs/ftring');
|
|
|
4
4
|
|
|
5
5
|
function isFtring(input) {
|
|
6
6
|
return (typeof input === 'object' &&
|
|
7
|
-
|
|
8
|
-
typeof
|
|
7
|
+
input.__is_ftring__ &&
|
|
8
|
+
typeof input.v === 'string');
|
|
9
9
|
}
|
|
10
10
|
function ftring(strings, __type__) {
|
|
11
11
|
return {
|
|
@@ -41,13 +41,13 @@ class Foorm {
|
|
|
41
41
|
* @param replaceContext a context to be transported along with metadata
|
|
42
42
|
* @returns form metadata without functions
|
|
43
43
|
*/
|
|
44
|
-
transportable(replaceContext) {
|
|
44
|
+
transportable(replaceContext, replaceValues) {
|
|
45
45
|
var _a, _b;
|
|
46
46
|
return {
|
|
47
47
|
title: (_a = this.title) !== null && _a !== void 0 ? _a : '',
|
|
48
48
|
submit: (_b = this.submit) !== null && _b !== void 0 ? _b : { text: 'Submit' },
|
|
49
49
|
context: replaceContext || this.context,
|
|
50
|
-
entries: this.entries.map(
|
|
50
|
+
entries: this.entries.map(e => (Object.assign(Object.assign({}, e), { value: replaceValues ? replaceValues[e.field] : e.value, validators: (e.validators || []).filter(v => isFtring(v)) }))),
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
normalizeEntry(e) {
|
|
@@ -60,8 +60,9 @@ class Foorm {
|
|
|
60
60
|
*/
|
|
61
61
|
executable() {
|
|
62
62
|
var _a, _b;
|
|
63
|
-
if (!this.fns)
|
|
63
|
+
if (!this.fns) {
|
|
64
64
|
this.fns = new ftring$1.FtringsPool();
|
|
65
|
+
}
|
|
65
66
|
return {
|
|
66
67
|
title: transformFtrings(this.title || '', this.fns),
|
|
67
68
|
submit: {
|
|
@@ -70,27 +71,41 @@ class Foorm {
|
|
|
70
71
|
},
|
|
71
72
|
context: this.context,
|
|
72
73
|
entries: this.entries
|
|
73
|
-
.map(
|
|
74
|
-
.map(
|
|
74
|
+
.map(e => this.normalizeEntry(e))
|
|
75
|
+
.map(e => (Object.assign(Object.assign({}, e), {
|
|
75
76
|
// strings
|
|
76
77
|
label: transformFtrings(e.label, this.fns), description: transformFtrings(e.description, this.fns), hint: transformFtrings(e.hint, this.fns), placeholder: transformFtrings(e.placeholder, this.fns),
|
|
77
78
|
// strings || objects
|
|
78
79
|
classes: transformFtringsInObj(e.classes, this.fns), styles: transformFtringsInObj(e.styles, this.fns),
|
|
79
80
|
// booleans
|
|
80
|
-
optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators)
|
|
81
|
+
optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators),
|
|
82
|
+
// options
|
|
83
|
+
options: transformFtrings(e.options, this.fns),
|
|
84
|
+
// attrs
|
|
85
|
+
attrs: transformFtringsInObj(e.attrs, this.fns) }))),
|
|
81
86
|
};
|
|
82
87
|
}
|
|
88
|
+
createFormData() {
|
|
89
|
+
const data = {};
|
|
90
|
+
for (const entry of this.entries) {
|
|
91
|
+
if (entry.type !== 'action') {
|
|
92
|
+
data[entry.field] = (entry.value || undefined);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
83
97
|
prepareValidators(_validators) {
|
|
84
|
-
const validators = (_validators || []).map(
|
|
98
|
+
const validators = (_validators || []).map(v => (isFtring(v) ? this.fns.getFn(v.v) : v));
|
|
85
99
|
validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
|
|
86
100
|
return validators;
|
|
87
101
|
}
|
|
88
102
|
supportsAltAction(altAction) {
|
|
89
|
-
return !!this.entries.
|
|
103
|
+
return !!this.entries.some(e => e.altAction === altAction);
|
|
90
104
|
}
|
|
91
105
|
getFormValidator() {
|
|
92
|
-
if (!this.fns)
|
|
106
|
+
if (!this.fns) {
|
|
93
107
|
this.fns = new ftring$1.FtringsPool();
|
|
108
|
+
}
|
|
94
109
|
const entries = this.executable().entries;
|
|
95
110
|
const fields = {};
|
|
96
111
|
for (const entry of entries) {
|
|
@@ -121,22 +136,19 @@ class Foorm {
|
|
|
121
136
|
};
|
|
122
137
|
if (scope.entry) {
|
|
123
138
|
if (typeof evalEntry.disabled === 'function') {
|
|
124
|
-
scope.entry.disabled = evalEntry.disabled =
|
|
125
|
-
evalEntry.disabled(scope);
|
|
139
|
+
scope.entry.disabled = evalEntry.disabled = evalEntry.disabled(scope);
|
|
126
140
|
}
|
|
127
141
|
else {
|
|
128
142
|
scope.entry.disabled = evalEntry.disabled;
|
|
129
143
|
}
|
|
130
144
|
if (typeof evalEntry.optional === 'function') {
|
|
131
|
-
scope.entry.optional = evalEntry.optional =
|
|
132
|
-
evalEntry.optional(scope);
|
|
145
|
+
scope.entry.optional = evalEntry.optional = evalEntry.optional(scope);
|
|
133
146
|
}
|
|
134
147
|
else {
|
|
135
148
|
scope.entry.optional = evalEntry.optional;
|
|
136
149
|
}
|
|
137
150
|
if (typeof evalEntry.hidden === 'function') {
|
|
138
|
-
scope.entry.hidden = evalEntry.hidden =
|
|
139
|
-
evalEntry.hidden(scope);
|
|
151
|
+
scope.entry.hidden = evalEntry.hidden = evalEntry.hidden(scope);
|
|
140
152
|
}
|
|
141
153
|
else {
|
|
142
154
|
scope.entry.hidden = evalEntry.hidden;
|
|
@@ -182,17 +194,18 @@ function validate(opts) {
|
|
|
182
194
|
return { passed: true };
|
|
183
195
|
}
|
|
184
196
|
function transformFtrings(value, fns) {
|
|
185
|
-
if (
|
|
197
|
+
if (value === undefined) {
|
|
186
198
|
return value;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
: value;
|
|
199
|
+
}
|
|
200
|
+
return isFtring(value) ? fns.getFn(value.v) : value;
|
|
190
201
|
}
|
|
191
202
|
function transformFtringsInObj(value, fns) {
|
|
192
|
-
if (isFtring(value))
|
|
203
|
+
if (isFtring(value)) {
|
|
193
204
|
return transformFtrings(value, fns);
|
|
194
|
-
|
|
205
|
+
}
|
|
206
|
+
if (typeof value === 'function') {
|
|
195
207
|
return value;
|
|
208
|
+
}
|
|
196
209
|
if (typeof value === 'object' && value !== null) {
|
|
197
210
|
const obj = {};
|
|
198
211
|
for (const [key, val] of Object.entries(value)) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
interface TFtring {
|
|
2
2
|
__is_ftring__: true;
|
|
3
3
|
v: string;
|
|
4
4
|
__type__?: 'boolean' | 'string' | 'number';
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
6
|
type StringOrFtring = string | TFtring;
|
|
7
7
|
type ObjSOF = Record<string, StringOrFtring>;
|
|
8
|
-
|
|
8
|
+
interface TFoormFnScope<T = string> {
|
|
9
9
|
v?: T;
|
|
10
10
|
data: Record<string, unknown>;
|
|
11
11
|
context: Record<string, unknown>;
|
|
@@ -15,11 +15,15 @@ type TFoormFnScope<T = string> = {
|
|
|
15
15
|
hidden?: boolean;
|
|
16
16
|
};
|
|
17
17
|
action?: string;
|
|
18
|
-
}
|
|
18
|
+
}
|
|
19
19
|
type TFoormValidatorFn<T = string> = (ctx: TFoormFnScope<T>) => string | boolean;
|
|
20
20
|
type TFoormFn<T = string, R = string | boolean> = (ctx: TFoormFnScope<T>) => R;
|
|
21
21
|
type TRelevantFields = 'field' | 'type' | 'component' | 'name' | 'attrs' | 'length';
|
|
22
|
-
|
|
22
|
+
type TFoormEntryOptions = {
|
|
23
|
+
key: string;
|
|
24
|
+
label: string;
|
|
25
|
+
} | string;
|
|
26
|
+
interface TFoormEntry<T = string, O = TFoormEntryOptions, SFTR = TFtring, BFTR = TFtring, FNFTR = TFtring, OFTR = TFtring> {
|
|
23
27
|
field: string;
|
|
24
28
|
altAction?: string;
|
|
25
29
|
label?: string | SFTR;
|
|
@@ -33,20 +37,20 @@ interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtring, FN
|
|
|
33
37
|
autocomplete?: string;
|
|
34
38
|
name?: string;
|
|
35
39
|
value?: T;
|
|
36
|
-
options?: O[];
|
|
37
|
-
attrs?: Record<string,
|
|
40
|
+
options?: O[] | OFTR;
|
|
41
|
+
attrs?: Record<string, string | SFTR>;
|
|
38
42
|
optional?: boolean | BFTR;
|
|
39
43
|
disabled?: boolean | BFTR;
|
|
40
44
|
hidden?: boolean | BFTR;
|
|
41
45
|
length?: number;
|
|
42
|
-
validators?:
|
|
46
|
+
validators?: Array<FNFTR | TFoormValidatorFn<T>>;
|
|
43
47
|
}
|
|
44
|
-
type TFoormEntryExecutable<T = unknown, O =
|
|
48
|
+
type TFoormEntryExecutable<T = unknown, O = TFoormEntryOptions> = TFoormEntry<T, O, TFoormFn<T, string>, TFoormFn<T, boolean>, TFoormValidatorFn<T>, TFoormFn<T, O[]>> & {
|
|
45
49
|
name: string;
|
|
46
50
|
label: string | TFoormFn<T, string>;
|
|
47
51
|
type: string;
|
|
48
52
|
};
|
|
49
|
-
|
|
53
|
+
interface TFoormMetaExecutable {
|
|
50
54
|
title: string | TFoormFn<undefined, string>;
|
|
51
55
|
submit: {
|
|
52
56
|
text: string | TFoormFn<undefined, string>;
|
|
@@ -54,7 +58,7 @@ type TFoormMetaExecutable = {
|
|
|
54
58
|
};
|
|
55
59
|
context: Record<string, unknown>;
|
|
56
60
|
entries: TFoormEntryExecutable[];
|
|
57
|
-
}
|
|
61
|
+
}
|
|
58
62
|
|
|
59
63
|
interface TFoormSubmit<S = TFtring, B = TFtring> {
|
|
60
64
|
text: string | S;
|
|
@@ -84,7 +88,7 @@ declare class Foorm {
|
|
|
84
88
|
* @param replaceContext a context to be transported along with metadata
|
|
85
89
|
* @returns form metadata without functions
|
|
86
90
|
*/
|
|
87
|
-
transportable<T extends Record<string, unknown>>(replaceContext?: T): Required<TFoormOptions> & {
|
|
91
|
+
transportable<T extends Record<string, unknown>>(replaceContext?: T, replaceValues?: Record<string, unknown>): Required<TFoormOptions> & {
|
|
88
92
|
context?: Record<string, unknown>;
|
|
89
93
|
};
|
|
90
94
|
protected normalizeEntry<T, O>(e: TFoormEntry<T, O>): TFoormEntry<T, O> & {
|
|
@@ -98,6 +102,7 @@ declare class Foorm {
|
|
|
98
102
|
* @returns form metadata with functions
|
|
99
103
|
*/
|
|
100
104
|
executable(): TFoormMetaExecutable;
|
|
105
|
+
createFormData<T extends Record<string, unknown>>(): T;
|
|
101
106
|
prepareValidators(_validators: TFoormEntry['validators']): TFoormValidatorFn<string>[];
|
|
102
107
|
supportsAltAction(altAction: string): boolean;
|
|
103
108
|
getFormValidator(): (inputs: Record<string, unknown>) => {
|
|
@@ -106,7 +111,7 @@ declare class Foorm {
|
|
|
106
111
|
};
|
|
107
112
|
}
|
|
108
113
|
declare function validate<T = string>(opts: TFoormFnScope<T> & {
|
|
109
|
-
validators: TFoormValidatorFn<T
|
|
114
|
+
validators: Array<TFoormValidatorFn<T>>;
|
|
110
115
|
}): {
|
|
111
116
|
passed: boolean;
|
|
112
117
|
error: string;
|
|
@@ -118,4 +123,4 @@ declare function validate<T = string>(opts: TFoormFnScope<T> & {
|
|
|
118
123
|
declare function isFtring(input: unknown): input is TFtring;
|
|
119
124
|
declare function ftring(strings: TemplateStringsArray, __type__?: TFtring['__type__']): TFtring;
|
|
120
125
|
|
|
121
|
-
export { Foorm, type ObjSOF, type StringOrFtring, type TFoormEntry, type TFoormEntryExecutable, type TFoormFn, type TFoormFnScope, type TFoormMetaExecutable, type TFoormOptions, type TFoormSubmit, type TFoormValidatorFn, type TFtring, ftring, isFtring, validate };
|
|
126
|
+
export { Foorm, type ObjSOF, type StringOrFtring, type TFoormEntry, type TFoormEntryExecutable, type TFoormEntryOptions, type TFoormFn, type TFoormFnScope, type TFoormMetaExecutable, type TFoormOptions, type TFoormSubmit, type TFoormValidatorFn, type TFtring, ftring, isFtring, validate };
|
package/dist/index.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { FtringsPool } from '@prostojs/ftring';
|
|
|
2
2
|
|
|
3
3
|
function isFtring(input) {
|
|
4
4
|
return (typeof input === 'object' &&
|
|
5
|
-
|
|
6
|
-
typeof
|
|
5
|
+
input.__is_ftring__ &&
|
|
6
|
+
typeof input.v === 'string');
|
|
7
7
|
}
|
|
8
8
|
function ftring(strings, __type__) {
|
|
9
9
|
return {
|
|
@@ -39,13 +39,13 @@ class Foorm {
|
|
|
39
39
|
* @param replaceContext a context to be transported along with metadata
|
|
40
40
|
* @returns form metadata without functions
|
|
41
41
|
*/
|
|
42
|
-
transportable(replaceContext) {
|
|
42
|
+
transportable(replaceContext, replaceValues) {
|
|
43
43
|
var _a, _b;
|
|
44
44
|
return {
|
|
45
45
|
title: (_a = this.title) !== null && _a !== void 0 ? _a : '',
|
|
46
46
|
submit: (_b = this.submit) !== null && _b !== void 0 ? _b : { text: 'Submit' },
|
|
47
47
|
context: replaceContext || this.context,
|
|
48
|
-
entries: this.entries.map(
|
|
48
|
+
entries: this.entries.map(e => (Object.assign(Object.assign({}, e), { value: replaceValues ? replaceValues[e.field] : e.value, validators: (e.validators || []).filter(v => isFtring(v)) }))),
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
normalizeEntry(e) {
|
|
@@ -58,8 +58,9 @@ class Foorm {
|
|
|
58
58
|
*/
|
|
59
59
|
executable() {
|
|
60
60
|
var _a, _b;
|
|
61
|
-
if (!this.fns)
|
|
61
|
+
if (!this.fns) {
|
|
62
62
|
this.fns = new FtringsPool();
|
|
63
|
+
}
|
|
63
64
|
return {
|
|
64
65
|
title: transformFtrings(this.title || '', this.fns),
|
|
65
66
|
submit: {
|
|
@@ -68,27 +69,41 @@ class Foorm {
|
|
|
68
69
|
},
|
|
69
70
|
context: this.context,
|
|
70
71
|
entries: this.entries
|
|
71
|
-
.map(
|
|
72
|
-
.map(
|
|
72
|
+
.map(e => this.normalizeEntry(e))
|
|
73
|
+
.map(e => (Object.assign(Object.assign({}, e), {
|
|
73
74
|
// strings
|
|
74
75
|
label: transformFtrings(e.label, this.fns), description: transformFtrings(e.description, this.fns), hint: transformFtrings(e.hint, this.fns), placeholder: transformFtrings(e.placeholder, this.fns),
|
|
75
76
|
// strings || objects
|
|
76
77
|
classes: transformFtringsInObj(e.classes, this.fns), styles: transformFtringsInObj(e.styles, this.fns),
|
|
77
78
|
// booleans
|
|
78
|
-
optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators)
|
|
79
|
+
optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators),
|
|
80
|
+
// options
|
|
81
|
+
options: transformFtrings(e.options, this.fns),
|
|
82
|
+
// attrs
|
|
83
|
+
attrs: transformFtringsInObj(e.attrs, this.fns) }))),
|
|
79
84
|
};
|
|
80
85
|
}
|
|
86
|
+
createFormData() {
|
|
87
|
+
const data = {};
|
|
88
|
+
for (const entry of this.entries) {
|
|
89
|
+
if (entry.type !== 'action') {
|
|
90
|
+
data[entry.field] = (entry.value || undefined);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
81
95
|
prepareValidators(_validators) {
|
|
82
|
-
const validators = (_validators || []).map(
|
|
96
|
+
const validators = (_validators || []).map(v => (isFtring(v) ? this.fns.getFn(v.v) : v));
|
|
83
97
|
validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
|
|
84
98
|
return validators;
|
|
85
99
|
}
|
|
86
100
|
supportsAltAction(altAction) {
|
|
87
|
-
return !!this.entries.
|
|
101
|
+
return !!this.entries.some(e => e.altAction === altAction);
|
|
88
102
|
}
|
|
89
103
|
getFormValidator() {
|
|
90
|
-
if (!this.fns)
|
|
104
|
+
if (!this.fns) {
|
|
91
105
|
this.fns = new FtringsPool();
|
|
106
|
+
}
|
|
92
107
|
const entries = this.executable().entries;
|
|
93
108
|
const fields = {};
|
|
94
109
|
for (const entry of entries) {
|
|
@@ -119,22 +134,19 @@ class Foorm {
|
|
|
119
134
|
};
|
|
120
135
|
if (scope.entry) {
|
|
121
136
|
if (typeof evalEntry.disabled === 'function') {
|
|
122
|
-
scope.entry.disabled = evalEntry.disabled =
|
|
123
|
-
evalEntry.disabled(scope);
|
|
137
|
+
scope.entry.disabled = evalEntry.disabled = evalEntry.disabled(scope);
|
|
124
138
|
}
|
|
125
139
|
else {
|
|
126
140
|
scope.entry.disabled = evalEntry.disabled;
|
|
127
141
|
}
|
|
128
142
|
if (typeof evalEntry.optional === 'function') {
|
|
129
|
-
scope.entry.optional = evalEntry.optional =
|
|
130
|
-
evalEntry.optional(scope);
|
|
143
|
+
scope.entry.optional = evalEntry.optional = evalEntry.optional(scope);
|
|
131
144
|
}
|
|
132
145
|
else {
|
|
133
146
|
scope.entry.optional = evalEntry.optional;
|
|
134
147
|
}
|
|
135
148
|
if (typeof evalEntry.hidden === 'function') {
|
|
136
|
-
scope.entry.hidden = evalEntry.hidden =
|
|
137
|
-
evalEntry.hidden(scope);
|
|
149
|
+
scope.entry.hidden = evalEntry.hidden = evalEntry.hidden(scope);
|
|
138
150
|
}
|
|
139
151
|
else {
|
|
140
152
|
scope.entry.hidden = evalEntry.hidden;
|
|
@@ -180,17 +192,18 @@ function validate(opts) {
|
|
|
180
192
|
return { passed: true };
|
|
181
193
|
}
|
|
182
194
|
function transformFtrings(value, fns) {
|
|
183
|
-
if (
|
|
195
|
+
if (value === undefined) {
|
|
184
196
|
return value;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
: value;
|
|
197
|
+
}
|
|
198
|
+
return isFtring(value) ? fns.getFn(value.v) : value;
|
|
188
199
|
}
|
|
189
200
|
function transformFtringsInObj(value, fns) {
|
|
190
|
-
if (isFtring(value))
|
|
201
|
+
if (isFtring(value)) {
|
|
191
202
|
return transformFtrings(value, fns);
|
|
192
|
-
|
|
203
|
+
}
|
|
204
|
+
if (typeof value === 'function') {
|
|
193
205
|
return value;
|
|
206
|
+
}
|
|
194
207
|
if (typeof value === 'object' && value !== null) {
|
|
195
208
|
const obj = {};
|
|
196
209
|
for (const [key, val] of Object.entries(value)) {
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foorm",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "foorm",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
10
11
|
".": {
|
|
11
12
|
"import": "./dist/index.mjs",
|
|
12
13
|
"require": "./dist/index.cjs",
|
|
@@ -34,6 +35,6 @@
|
|
|
34
35
|
},
|
|
35
36
|
"homepage": "https://github.com/foormjs/foormjs/tree/main/packages/foorm#readme",
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@prostojs/ftring": "^0.0.
|
|
38
|
+
"@prostojs/ftring": "^0.0.4"
|
|
38
39
|
}
|
|
39
40
|
}
|