@kws3/ui 4.3.1 → 4.3.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/CHANGELOG.mdx +7 -0
- package/form/index.d.ts +18 -15
- package/form/index.d.ts.map +1 -1
- package/form/index.js +42 -12
- package/package.json +2 -2
- package/types/index.d.ts +52 -4
package/CHANGELOG.mdx
CHANGED
package/form/index.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
/** @type {import("@kws3/ui/types").MakeForms} */
|
|
2
|
+
export const makeForms: typeof import("@kws3/ui/types").MakeForms;
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} v
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export function noDigits(v: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} v
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export function notEmpty(v: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} msg
|
|
15
|
+
* @param {function} fn
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export function withMsg(msg: string, fn: Function): (v: any, otherFields: object) => string;
|
|
16
19
|
//# sourceMappingURL=index.d.ts.map
|
package/form/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAGA,iDAAiD;AAEjD,kEAME;AAmKF;;;GAGG;AACH,4BAHW,MAAM,WAGoB;AAVrC;;;GAGG;AACH,4BAHW,MAAM,WAG2B;AAQ5C;;;;GAIG;AACH,6BAJW,MAAM,qBAKU,GAAG,eAAmB,MAAM,YACtB"}
|
package/form/index.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { cloneObject } from "
|
|
1
|
+
import { cloneObject } from "@kws3/ui/utils";
|
|
2
2
|
import { derived, get, writable } from "svelte/store";
|
|
3
3
|
|
|
4
|
+
/** @type {import("@kws3/ui/types").MakeForms} */
|
|
5
|
+
// @ts-ignore
|
|
4
6
|
const makeForms = (items) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
if (Array.isArray(items)) {
|
|
8
|
+
return items.map((item) => formMaker(item));
|
|
9
|
+
} else {
|
|
7
10
|
return formMaker(items);
|
|
8
11
|
}
|
|
9
|
-
|
|
10
|
-
let ret = [];
|
|
11
|
-
if (items.length) {
|
|
12
|
-
items.forEach((item) => ret.push(formMaker(item)));
|
|
13
|
-
}
|
|
14
|
-
return ret;
|
|
15
12
|
};
|
|
16
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @param {import("@kws3/ui/types").FormMakerConfig} config
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
17
18
|
const formMaker = (config) => {
|
|
18
19
|
let data = config.data || {};
|
|
19
20
|
let validators = config.validators || {};
|
|
@@ -32,6 +33,12 @@ const formMaker = (config) => {
|
|
|
32
33
|
let fields = Object.keys($formData);
|
|
33
34
|
let res = fields.reduce((o, v) => ((o[v] = false), o), {});
|
|
34
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} field
|
|
38
|
+
* @param {{ [x: string]: any }} oldData
|
|
39
|
+
* @param {{ [x: string]: any }} newData
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
35
42
|
let compare = (field, oldData, newData) => {
|
|
36
43
|
if (Array.isArray(newData[field])) {
|
|
37
44
|
if (
|
|
@@ -81,6 +88,11 @@ const formMaker = (config) => {
|
|
|
81
88
|
const errors = derived(formData, ($formData, set) => {
|
|
82
89
|
let fields = Object.keys(validators) || [];
|
|
83
90
|
let res = fields.reduce((o, v) => ((o[v] = ""), o), {});
|
|
91
|
+
/**
|
|
92
|
+
* @param {string} field
|
|
93
|
+
* @param {function | function[]} validator
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
84
96
|
let validate = (field, validator) => {
|
|
85
97
|
let message = "";
|
|
86
98
|
if (validator || typeof validator !== "undefined") {
|
|
@@ -122,17 +134,20 @@ const formMaker = (config) => {
|
|
|
122
134
|
Object.values($errors).every((v) => v === "")
|
|
123
135
|
);
|
|
124
136
|
|
|
137
|
+
/** @param {{ [key: string]: any }} newData */
|
|
125
138
|
function update(newData) {
|
|
126
139
|
data = newData;
|
|
127
140
|
reset();
|
|
128
141
|
}
|
|
129
142
|
|
|
143
|
+
/** @param {import("@kws3/ui/types").FormMakerConfig["validators"]} newValidators */
|
|
130
144
|
function setValidators(newValidators) {
|
|
131
145
|
validators = newValidators;
|
|
132
146
|
formData.set(get(formData));
|
|
133
147
|
}
|
|
134
148
|
|
|
135
|
-
|
|
149
|
+
/** @param {Event | null} e */
|
|
150
|
+
function reset(e = null) {
|
|
136
151
|
e && e.preventDefault();
|
|
137
152
|
formData.set(cloneObject(data));
|
|
138
153
|
tracker.set(cloneObject(tracker_data));
|
|
@@ -151,10 +166,25 @@ const formMaker = (config) => {
|
|
|
151
166
|
};
|
|
152
167
|
};
|
|
153
168
|
|
|
169
|
+
/**
|
|
170
|
+
* @param {string} v
|
|
171
|
+
* @returns
|
|
172
|
+
*/
|
|
154
173
|
const notEmpty = (v) => v && v.trim() !== "";
|
|
155
174
|
|
|
175
|
+
/**
|
|
176
|
+
* @param {string} v
|
|
177
|
+
* @returns
|
|
178
|
+
*/
|
|
156
179
|
const noDigits = (v) => !/\d/.test(v);
|
|
157
180
|
|
|
158
|
-
|
|
181
|
+
/**
|
|
182
|
+
* @param {string} msg
|
|
183
|
+
* @param {function} fn
|
|
184
|
+
* @returns
|
|
185
|
+
*/
|
|
186
|
+
const withMsg =
|
|
187
|
+
(msg, fn) => (/** @type {any} */ v, /** @type {object} */ otherFields) =>
|
|
188
|
+
fn(v, otherFields) ? "" : msg;
|
|
159
189
|
|
|
160
|
-
export { makeForms,
|
|
190
|
+
export { makeForms, noDigits, notEmpty, withMsg };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kws3/ui",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.3",
|
|
4
4
|
"description": "UI components for use with Svelte v3 applications.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"svelte": "index.js",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"typescript": "^5.2.2"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "c243fb9e0463b160c24f39ad4a2a1bf9cf48a4ce"
|
|
43
43
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Readable, Writable } from "svelte/store";
|
|
1
2
|
import type {
|
|
2
3
|
Colors,
|
|
3
4
|
BGColors,
|
|
@@ -110,10 +111,12 @@ export type DrawingPadEvents = {
|
|
|
110
111
|
};
|
|
111
112
|
|
|
112
113
|
export type ButtonEvent = {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
detail: {
|
|
115
|
+
doing: () => void;
|
|
116
|
+
done: (callback?: Function, timeout?: number) => void;
|
|
117
|
+
error: (callback?: Function, timeout?: number) => void;
|
|
118
|
+
context?: any;
|
|
119
|
+
};
|
|
117
120
|
};
|
|
118
121
|
|
|
119
122
|
export type SubmitButtonEvent = {
|
|
@@ -128,6 +131,51 @@ export type ButtonTracker = {
|
|
|
128
131
|
error: boolean;
|
|
129
132
|
};
|
|
130
133
|
|
|
134
|
+
export type FormMakerValidators = { [key: string]: Function | Function[] };
|
|
135
|
+
|
|
136
|
+
export type FormMakerReturnFormData = import("svelte/store").Writable<T>;
|
|
137
|
+
|
|
138
|
+
export type FormMakerReturnErrors = import("svelte/store").Readable<{
|
|
139
|
+
[K in keyof T]: string;
|
|
140
|
+
}>;
|
|
141
|
+
|
|
142
|
+
export type FormMakerReturnTouched = import("svelte/store").Readable<{
|
|
143
|
+
[K in keyof T]: boolean;
|
|
144
|
+
}>;
|
|
145
|
+
|
|
146
|
+
export type FormMakerReturnIsValid = import("svelte/store").Readable<boolean>;
|
|
147
|
+
export type FormMakerReturnIsTouched = import("svelte/store").Readable<boolean>;
|
|
148
|
+
export type FormMakerReturnTracker =
|
|
149
|
+
import("svelte/store").Writable<ButtonTracker>;
|
|
150
|
+
|
|
151
|
+
export type FormMakerConfig<T = Record<string, any>> = {
|
|
152
|
+
data: T;
|
|
153
|
+
validators?: FormMakerValidators;
|
|
154
|
+
strictMode?: boolean;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export type FormMakerReturn<T> = {
|
|
158
|
+
formData: FormMakerReturnFormData;
|
|
159
|
+
errors: FormMakerReturnErrors;
|
|
160
|
+
touched: FormMakerReturnTouched;
|
|
161
|
+
isValid: FormMakerReturnIsValid;
|
|
162
|
+
isTouched: FormMakerReturnIsTouched;
|
|
163
|
+
tracker: FormMakerReturnTracker;
|
|
164
|
+
update: (newData: { [key: string]: any }) => void;
|
|
165
|
+
reset: (e?: Event | null) => void;
|
|
166
|
+
setValidators: (newValidators: FormMakerValidators) => void;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export type CloneObject = <T>(obj: T) => T;
|
|
170
|
+
|
|
171
|
+
// Overloads
|
|
172
|
+
export declare function MakeForms<T extends Record<string, any>>(
|
|
173
|
+
items: FormMakerConfig<T>,
|
|
174
|
+
): FormMakerReturn<T>;
|
|
175
|
+
export declare function MakeForms<T extends Record<string, any>>(
|
|
176
|
+
items: FormMakerConfig<T>[],
|
|
177
|
+
): FormMakerReturn<T>[];
|
|
178
|
+
|
|
131
179
|
declare global {
|
|
132
180
|
interface Navigator {
|
|
133
181
|
readonly userAgentData: {
|