@kws3/ui 4.3.1 → 4.3.2

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 CHANGED
@@ -1,3 +1,7 @@
1
+ ## 4.3.2
2
+ - Extended typings for form helper
3
+ - Fix typings for ButtonEvent
4
+
1
5
  ## 4.3.1
2
6
  - Include bulma 0.9 as peer dependency
3
7
  - Include svelte 4 as peer dependency
package/form/index.d.ts CHANGED
@@ -1,16 +1,19 @@
1
- /// <reference types="svelte" />
2
- export function makeForms(items: any): any[] | {
3
- formData: import("svelte/store").Writable<any>;
4
- errors: import("svelte/store").Readable<any>;
5
- touched: import("svelte/store").Readable<any>;
6
- isValid: import("svelte/store").Readable<boolean>;
7
- isTouched: import("svelte/store").Readable<any>;
8
- tracker: import("svelte/store").Writable<any>;
9
- update: (newData: any) => void;
10
- reset: (e: any) => void;
11
- setValidators: (newValidators: any) => void;
12
- };
13
- export function notEmpty(v: any): boolean;
14
- export function noDigits(v: any): boolean;
15
- export function withMsg(msg: any, fn: any): (v: any, otherFields: any) => any;
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":";AAGA;;;;;;;;;;EAWC;AA2ID,0CAA4C;AAE5C,0CAAqC;AAErC,8EAA8E"}
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 "../utils/index";
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
- var res = Array.isArray(items);
6
- if (!res) {
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
- function reset(e) {
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
- const withMsg = (msg, fn) => (v, otherFields) => fn(v, otherFields) ? "" : msg;
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, notEmpty, noDigits, withMsg };
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.1",
3
+ "version": "4.3.2",
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": "e21174fbfbe5370abdfcf4862b3978cffc35659f"
42
+ "gitHead": "a8f22c35003d283bc5b76ee2fd44a800931effc6"
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
- doing: () => void;
114
- done: (callback?: Function, timeout?: number) => void;
115
- error: (callback?: Function, timeout?: number) => void;
116
- context: any;
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,34 @@ export type ButtonTracker = {
128
131
  error: boolean;
129
132
  };
130
133
 
134
+ export type FormMakerConfig<T = Record<string, any>> = {
135
+ data: T;
136
+ validators?: { [key: string]: Function | Function[] };
137
+ strictMode?: boolean;
138
+ };
139
+
140
+ export type FormMakerReturnType<T> = {
141
+ formData: import("svelte/store").Writable<T>;
142
+ errors: import("svelte/store").Readable<{ [K in keyof T]: string }>;
143
+ touched: import("svelte/store").Readable<{ [K in keyof T]: boolean }>;
144
+ isValid: import("svelte/store").Readable<boolean>;
145
+ isTouched: import("svelte/store").Readable<boolean>;
146
+ tracker: import("svelte/store").Writable<ButtonTracker>;
147
+ update: (newData: { [key: string]: any }) => void;
148
+ reset: (e?: Event | null) => void;
149
+ setValidators: (newValidators: FormMakerConfig["validators"]) => void;
150
+ };
151
+
152
+ export type CloneObject = <T>(obj: T) => T;
153
+
154
+ // Overloads
155
+ export declare function MakeForms<T extends Record<string, any>>(
156
+ items: FormMakerConfig<T>,
157
+ ): FormMakerReturnType<T>;
158
+ export declare function MakeForms<T extends Record<string, any>>(
159
+ items: FormMakerConfig<T>[],
160
+ ): FormMakerReturnType<T>[];
161
+
131
162
  declare global {
132
163
  interface Navigator {
133
164
  readonly userAgentData: {