@steveesamson/microform 1.0.5 → 1.0.6

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 CHANGED
@@ -31,7 +31,8 @@ import uform from "@steveesamson/microform";
31
31
  let defaultData:any = $props();
32
32
 
33
33
  // Instatiate microform
34
- const { form, values, errors, submit, sanity } = uform({
34
+ // const { form, values, errors, submit, sanity } = uform({
35
+ const { form, values, errors, submit, sanity, setValue, setError } = uform({
35
36
  // Set default form data
36
37
  data:{...defaultData},
37
38
  // Set a global event for validation, can be overriden on a each field.
@@ -63,6 +64,8 @@ On the instantiation of `microform`, we have access to:
63
64
  - `sanity`, a `FormSanity`, which tells us if a form is clean/without errors by it's `ok` property.
64
65
  - `reset`, a function to reset form
65
66
  - `onsubmit`, a function to handle form submission.
67
+ - `setValue`, a function to mutate state in order to avoid `ownership_invalid_mutation` errors, especially, when values is fed as props to other components that intend to mutate `values` states.
68
+ - `setError`, is a function introduced for the same reason `setValue` was added; it give privileges to components authored on `microform` that may mutate the `errors` state.
66
69
 
67
70
  ### In the view Html
68
71
 
@@ -70,11 +73,11 @@ On the instantiation of `microform`, we have access to:
70
73
  <form use:submit={onSubmit}>
71
74
  <label for="fullname">
72
75
  Name:
73
- <input
74
- type="text"
75
- name="fullname"
76
- id="fullname"
77
- use:form={{ validations: ['required'] }}
76
+ <input
77
+ type="text"
78
+ name="fullname"
79
+ id="fullname"
80
+ use:form={{ validations: ['required'] }}
78
81
  />
79
82
  {#if errors.fullname}
80
83
  <small>{errors.fullname}</small>
@@ -82,11 +85,11 @@ On the instantiation of `microform`, we have access to:
82
85
  </label>
83
86
  <label for="dob">
84
87
  DOB:
85
- <input
86
- type="date"
87
- name="dob"
88
- id="dob"
89
- use:form={{ validations: ['required'] }}
88
+ <input
89
+ type="date"
90
+ name="dob"
91
+ id="dob"
92
+ use:form={{ validations: ['required'] }}
90
93
  />
91
94
  {#if errors.dob}
92
95
  <small>{errors.dob}</small>
@@ -149,9 +152,9 @@ On the instantiation of `microform`, we have access to:
149
152
  type="file"
150
153
  name="resume"
151
154
  id="resume"
152
- use:form={{
153
- validateEvent:'change',
154
- validations: ['required', 'file-size-mb:3']
155
+ use:form={{
156
+ validateEvent:'change',
157
+ validations: ['required', 'file-size-mb:3']
155
158
  }}
156
159
  />
157
160
  {#if errors.resume}
@@ -160,9 +163,9 @@ On the instantiation of `microform`, we have access to:
160
163
  </label>
161
164
  <label for="comment">
162
165
  Comment:
163
- <textarea
164
- name="comment"
165
- id="comment"
166
+ <textarea
167
+ name="comment"
168
+ id="comment"
166
169
  use:form={{ validations: ['required'] }}
167
170
  ></textarea>
168
171
  {#if errors.comment}
@@ -209,11 +212,11 @@ On the instantiation of `microform`, we have access to:
209
212
  Story:
210
213
  <div
211
214
  contenteditable="true"
212
- use:form={{
213
- validateEvent: 'input',
214
- validations: ['required'],
215
- name: 'story',
216
- html: true
215
+ use:form={{
216
+ validateEvent: 'input',
217
+ validations: ['required'],
218
+ name: 'story',
219
+ html: true
217
220
  }}
218
221
  ></div>
219
222
  {#if errors.story}
@@ -235,10 +238,10 @@ While the above example uses the `submit` action of `microform`, form could also
235
238
  <form>
236
239
  <label for="password">
237
240
  Password:
238
- <input
239
- type="text"
240
- name="password"
241
- id="password"
241
+ <input
242
+ type="text"
243
+ name="password"
244
+ id="password"
242
245
  use:form={{
243
246
  validations: ['required']
244
247
  }}
@@ -254,7 +257,7 @@ While the above example uses the `submit` action of `microform`, form could also
254
257
  name="confirm_password"
255
258
  id="confirm_password"
256
259
  use:form={{
257
- validations: ['required','match:password'],
260
+ validations: ['required','match:password'],
258
261
  }}
259
262
  />
260
263
  {#if errors.confirm_password}
@@ -297,10 +300,7 @@ While the above example uses the `submit` action of `microform`, form could also
297
300
  You need not bind the `values` to your fields except when there is a definite need for it as `form` will coordinate all value changes based on the `data` passed at instantiation, if any. Therefore, constructs like the following might not be neccessary:
298
301
 
299
302
  ```html
300
- <input
301
- ...
302
- value="{values.email_account}"
303
- />
303
+ <input ... value="{values.email_account}" />
304
304
  ```
305
305
 
306
306
  ### 1. Validations
@@ -395,9 +395,11 @@ Finally, the validations can be combined to form a complex graph of validations
395
395
  ```
396
396
 
397
397
  # Overriding validators
398
+
398
399
  Validators could be overriden to provide custom validation and/or messages besides the default ones. For instance, let us overide the `len` validation rule. Every non-empty string/message returned from a validator's call becomes the error to be displayed to the user; and it shows up in the `errors` keyed by the field name.
399
400
 
400
401
  ### Approach 1
402
+
401
403
  ```ts
402
404
  <script>
403
405
  import uform, { type FieldProps } from "@steveesamson/microform";
@@ -427,7 +429,9 @@ const { form, values, errors, submit, sanity } = uform({
427
429
  });
428
430
  </script>
429
431
  ```
432
+
430
433
  Instead of using the literal validation keys like `len`, `required` etc., while overriding validators, the exported key namee could be used. The key names are:
434
+
431
435
  - IS_REQUIRED = `required`
432
436
  - IS_EMAIL = `email`
433
437
  - IS_URL = `url`
@@ -447,6 +451,7 @@ Instead of using the literal validation keys like `len`, `required` etc., while
447
451
  Therefore, the following is equivalent to the configuration in `Approach 1`:
448
452
 
449
453
  ### Approach 2
454
+
450
455
  ```ts
451
456
  <script>
452
457
  import uform, { type FieldProps, IS_LEN } from "@steveesamson/microform";
@@ -488,4 +493,4 @@ The validation will be used on the view as below:
488
493
  validations:['required','len:30']
489
494
  }}
490
495
  />
491
- ```
496
+ ```
@@ -1,4 +1,3 @@
1
- /// <reference types="svelte" />
2
1
  import { type Writable } from "svelte/store";
3
2
  import type { FormOptions, FormAction } from "./types.js";
4
3
  import type { Params } from "./internal.js";
@@ -1,4 +1,3 @@
1
- /// <reference types="svelte" />
2
1
  import { type Writable } from "svelte/store";
3
2
  import type { ValidateArgs, ValidatorType, ValidatorMap } from "./types.js";
4
3
  import type { Params } from "./internal.js";
@@ -0,0 +1,3 @@
1
+ import type { Microform } from './types.js';
2
+ declare const microform: Microform;
3
+ export default microform;
@@ -2,7 +2,6 @@ import { writable, derived, get } from 'svelte/store';
2
2
  import { formAction } from './form-action.js';
3
3
  import { bindStateToStore } from "./utils.js";
4
4
  const microform = (props) => {
5
- // form default values
6
5
  const data = props?.data || {};
7
6
  // form values
8
7
  const _values = writable({ ...data });
@@ -62,6 +61,12 @@ const microform = (props) => {
62
61
  const values = $state({ ...data });
63
62
  const errors = $state({});
64
63
  const sanity = $state({ ok: get(_valid) });
64
+ const setValue = (field, value) => {
65
+ values[field] = value;
66
+ };
67
+ const setError = (field, value) => {
68
+ errors[field] = value;
69
+ };
65
70
  bindStateToStore(values, _values);
66
71
  bindStateToStore(errors, _errors);
67
72
  _valid.subscribe((changes) => {
@@ -75,6 +80,8 @@ const microform = (props) => {
75
80
  submit,
76
81
  onsubmit,
77
82
  reset,
83
+ setError,
84
+ setValue
78
85
  };
79
86
  };
80
87
  export default microform;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="svelte" />
2
1
  import { type Writable } from "svelte/store";
3
2
  import type { Params } from "./internal.js";
4
3
  export type Validator = `max:${number}` | `min:${number}` | `len:${number}` | `minlen:${number}` | `maxlen:${number}` | `file-size-mb:${number}` | `match:${string}` | 'required' | 'email' | 'integer' | 'number' | 'alpha' | 'alphanum' | 'url' | 'ip';
@@ -62,5 +61,7 @@ export type MicroFormReturn = {
62
61
  submit: (formNode: HTMLFormElement, handler: FormSubmit) => void;
63
62
  onsubmit: (handler: FormSubmit) => (e: Event) => Promise<void>;
64
63
  reset: () => void;
64
+ setValue: (key: string, value: unknown) => void;
65
+ setError: (key: string, value: unknown) => void;
65
66
  };
66
67
  export type Microform = (props?: MicroFormProps) => MicroFormReturn;
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="svelte" />
2
1
  import type { Writable } from "svelte/store";
3
2
  import type { Params } from "./internal.js";
4
3
  type TEvent = {
package/package.json CHANGED
@@ -1,22 +1,24 @@
1
1
  {
2
2
  "name": "@steveesamson/microform",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "postbuild": "touch ./docs/.nojekyll",
7
7
  "prebuild": "rm -fr ./docs & rm -fr ./dist",
8
- "build": "vite build && npm run package",
8
+ "build": "vite build && npm run prepack",
9
9
  "preview": "vite preview",
10
- "package": "svelte-kit sync && svelte-package && publint",
11
- "prepublishOnly": "npm run package",
10
+ "prepare": "svelte-kit sync || echo ''",
11
+ "prepack": "svelte-kit sync && svelte-package && publint",
12
12
  "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
13
13
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
14
+ "format": "prettier --write .",
14
15
  "lint": "prettier --check . && eslint .",
15
- "format": "prettier --write ."
16
+ "test:unit": "vitest",
17
+ "test": "npm run test:unit -- --run"
16
18
  },
17
19
  "repository": {
18
20
  "type": "git",
19
- "url": "https://github.com/steveesamson/microform.git"
21
+ "url": "git+https://github.com/steveesamson/microform.git"
20
22
  },
21
23
  "exports": {
22
24
  ".": {
@@ -31,31 +33,41 @@
31
33
  "!dist/**/*.test.*",
32
34
  "!dist/**/*.spec.*"
33
35
  ],
36
+ "sideEffects": [
37
+ "**/*.css"
38
+ ],
39
+ "type": "module",
40
+ "peerDependencies": {
41
+ "svelte": "^5.0.0"
42
+ },
34
43
  "devDependencies": {
35
- "@sveltejs/adapter-auto": "^3.0.0",
36
- "@sveltejs/adapter-static": "^3.0.1",
37
- "@sveltejs/kit": "^2.0.0",
44
+ "@eslint/compat": "^1.2.5",
45
+ "@eslint/js": "^9.18.0",
46
+ "@sveltejs/adapter-static": "^3.0.8",
47
+ "@sveltejs/kit": "^2.16.0",
38
48
  "@sveltejs/package": "^2.0.0",
39
- "@sveltejs/vite-plugin-svelte": "^3.0.0",
40
- "@types/eslint": "8.56.0",
41
- "@typescript-eslint/eslint-plugin": "^6.0.0",
42
- "@typescript-eslint/parser": "^6.0.0",
43
- "eslint": "^8.56.0",
44
- "eslint-config-prettier": "^9.1.0",
45
- "eslint-plugin-svelte": "^2.35.1",
46
- "mdsvex": "^0.11.0",
47
- "prettier": "^3.1.1",
48
- "prettier-plugin-svelte": "^3.1.2",
49
- "publint": "^0.1.9",
49
+ "@sveltejs/vite-plugin-svelte": "^5.0.0",
50
+ "@testing-library/jest-dom": "^6.6.3",
51
+ "@testing-library/svelte": "^5.2.4",
52
+ "eslint": "^9.18.0",
53
+ "eslint-config-prettier": "^10.0.1",
54
+ "eslint-plugin-svelte": "^3.0.0",
55
+ "globals": "^16.0.0",
56
+ "jsdom": "^26.0.0",
57
+ "mdsvex": "^0.12.3",
58
+ "prettier": "^3.4.2",
59
+ "prettier-plugin-svelte": "^3.3.3",
60
+ "publint": "^0.3.2",
50
61
  "shiki": "^0.14.7",
51
- "svelte-check": "^3.6.0",
52
- "tslib": "^2.4.1",
62
+ "svelte": "^5.0.0",
63
+ "svelte-check": "^4.0.0",
53
64
  "typescript": "^5.0.0",
54
- "vite": "^5.0.3"
65
+ "typescript-eslint": "^8.20.0",
66
+ "vite": "^6.0.0",
67
+ "vitest": "^3.0.0"
55
68
  },
56
69
  "default": "./dist/index.js",
57
70
  "types": "./dist/index.d.ts",
58
- "type": "module",
59
71
  "keywords": [
60
72
  "microform",
61
73
  "microform-svelte",
@@ -68,8 +80,5 @@
68
80
  "sveltekit",
69
81
  "svelte-kit",
70
82
  "sveltekit-microform"
71
- ],
72
- "dependencies": {
73
- "svelte": "^5.0.0-next.210"
74
- }
83
+ ]
75
84
  }