@mobx-ecosystem/mobx-form 2.0.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/README.md +7 -0
- package/dist/configure-form.d.ts +9 -0
- package/dist/field-service.d.ts +26 -0
- package/dist/form-service.d.ts +58 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/utils/check-types.d.ts +7 -0
- package/dist/utils/form.d.ts +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type ValidateFunction = (fieldValues: Record<string, unknown>, validationSchema: unknown) => Promise<Record<string, string>>;
|
|
2
|
+
export declare let validate: ValidateFunction | undefined;
|
|
3
|
+
export interface ConfigurationForm {
|
|
4
|
+
validation: {
|
|
5
|
+
validate: ValidateFunction;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export declare const configureForm: (configuration: ConfigurationForm) => void;
|
|
9
|
+
export declare const _checkConfiguration: () => void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type FieldOptionsType = {
|
|
2
|
+
onError?: boolean;
|
|
3
|
+
};
|
|
4
|
+
type Nullable<T> = T | null;
|
|
5
|
+
export declare class FieldService<T> {
|
|
6
|
+
_serviceType: string;
|
|
7
|
+
_initValue?: Nullable<T>;
|
|
8
|
+
_value?: Nullable<T>;
|
|
9
|
+
_error?: string;
|
|
10
|
+
validate?(): Promise<void>;
|
|
11
|
+
_disabled: boolean;
|
|
12
|
+
options?: FieldOptionsType;
|
|
13
|
+
constructor(initValue?: T, options?: FieldOptionsType);
|
|
14
|
+
get initValue(): Nullable<T> | undefined;
|
|
15
|
+
set initValue(initValue: Nullable<T> | undefined);
|
|
16
|
+
get value(): Nullable<T> | undefined;
|
|
17
|
+
set value(value: Nullable<T> | undefined);
|
|
18
|
+
get error(): string | undefined;
|
|
19
|
+
set error(error: string | undefined);
|
|
20
|
+
get disabled(): boolean;
|
|
21
|
+
set disabled(disabled: boolean);
|
|
22
|
+
get isValid(): boolean;
|
|
23
|
+
get isInit(): boolean;
|
|
24
|
+
get props(): any;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { FieldService } from './field-service';
|
|
2
|
+
export declare class FormService<T extends Record<string, FieldService<unknown>>> {
|
|
3
|
+
fields: T;
|
|
4
|
+
validationSchema?: unknown;
|
|
5
|
+
constructor(fields: T, validationSchema?: unknown);
|
|
6
|
+
/***
|
|
7
|
+
* Validate the form
|
|
8
|
+
*
|
|
9
|
+
* *Configure this method with configureForm from mobx-form
|
|
10
|
+
*/
|
|
11
|
+
validate: () => Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Return field keys
|
|
14
|
+
*/
|
|
15
|
+
get keys(): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Check each field if its isValid = true
|
|
18
|
+
*/
|
|
19
|
+
get isValid(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Check each field if its isInit = false
|
|
22
|
+
*/
|
|
23
|
+
get isTouched(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Check if isTouched = true && isValid = true
|
|
26
|
+
*/
|
|
27
|
+
get canBeSubmitted(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @returns Object of field values
|
|
31
|
+
*/
|
|
32
|
+
getValues: () => Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* Set field values to init values
|
|
35
|
+
*/
|
|
36
|
+
setValuesAsInit: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Set field errors to undefined
|
|
39
|
+
*/
|
|
40
|
+
resetErrors: () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Set errors for fields
|
|
43
|
+
* @param errors object of string which provides errors for fields
|
|
44
|
+
*/
|
|
45
|
+
setErrors(errors: Record<string, string>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Reset fields to their own initial values
|
|
48
|
+
*/
|
|
49
|
+
reset: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* Pass true to the property 'disabled'
|
|
52
|
+
*/
|
|
53
|
+
disable: () => void;
|
|
54
|
+
/**
|
|
55
|
+
* Pass false to the property 'disabled'
|
|
56
|
+
*/
|
|
57
|
+
enable: () => void;
|
|
58
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isEqual as e}from"lodash";import{makeAutoObservable as t}from"mobx";var r=function(){return r=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},r.apply(this,arguments)};function n(e,t,r,n){return new(r||(r=Promise))((function(i,o){function u(e){try{s(n.next(e))}catch(e){o(e)}}function a(e){try{s(n.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,a)}s((n=n.apply(e,t||[])).next())}))}function i(e,t){var r,n,i,o,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(s){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(u=0)),u;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,n=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(i=u.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){u.label=a[1];break}if(6===a[0]&&u.label<i[1]){u.label=i[1],i=a;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(a);break}i[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(e,u)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}var o,u=function(){function n(e,r){this._serviceType="field-service",this._disabled=!1,t(this),this.initValue=e,this.options=r}return Object.defineProperty(n.prototype,"initValue",{get:function(){return this._initValue},set:function(e){this._initValue=e,this._value=e,this.validate&&this.validate()},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"value",{get:function(){return this._value},set:function(e){this._value=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"error",{get:function(){return this._error},set:function(e){this._error=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"disabled",{get:function(){return this._disabled},set:function(e){this._disabled=e},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isValid",{get:function(){return!this._error},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"isInit",{get:function(){return null!==(t=this.value)&&"object"==typeof t?e(this.value,this._initValue):this._value===this._initValue;var t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"props",{get:function(){var e,t=this,n={value:this.value,error:this.error,disabled:this.disabled};return(null===(e=this.options)||void 0===e?void 0:e.onError)&&(n=r(r({},n),{onError:function(e){t.error=t.error||(null==e?void 0:e.toString())}})),r(r({},n),{onChange:function(e,r){t.value=r,t.validate&&t.validate()}})},enumerable:!1,configurable:!0}),n}(),a=function(e){o=e.validation.validate},s=function(){if(!o)throw new Error("You must define configureForm to configure mobx-form")},l=function(){function e(e,r){var u=this;this.validate=function(){return n(u,void 0,void 0,(function(){var e,t;return i(this,(function(r){switch(r.label){case 0:return e=this.getValues(),[4,null==o?void 0:o(e,this.validationSchema)];case 1:return(t=r.sent())&&0!=Object.keys(t||[]).length?this.setErrors(t):this.resetErrors(),[2]}}))}))},this.getValues=function(){var e={};return u.keys.forEach((function(t){e[t]=u.fields[t].value})),e},this.setValuesAsInit=function(){u.keys.forEach((function(e){u.fields[e].initValue=u.fields[e].value}))},this.resetErrors=function(){u.keys.forEach((function(e){return u.fields[e].error=void 0}))},this.reset=function(){u.keys.forEach((function(e){return u.fields[e].value=u.fields[e].initValue})),u.validate()},this.disable=function(){u.keys.forEach((function(e){return u.fields[e].disabled=!0}))},this.enable=function(){u.keys.forEach((function(e){return u.fields[e].disabled=!1}))},s(),t(this),this.fields=e,this.validationSchema=r,this.keys.forEach((function(e){u.fields[e].validate=u.validate}))}return Object.defineProperty(e.prototype,"keys",{get:function(){return Object.keys(this.fields)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isValid",{get:function(){var e=this;return this.keys.every((function(t){return e.fields[t].isValid}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"isTouched",{get:function(){var e=this;return this.keys.some((function(t){return!e.fields[t].isInit}))},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"canBeSubmitted",{get:function(){return this.isTouched&&this.isValid},enumerable:!1,configurable:!0}),e.prototype.setErrors=function(e){var t=this;this.keys.forEach((function(r){t.fields[r].error=null==e?void 0:e[r]}))},e}();export{u as FieldService,l as FormService,s as _checkConfiguration,a as configureForm,o as validate};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const isObject: (obj: unknown) => obj is Record<string, unknown>;
|
|
2
|
+
export declare const isInteger: (obj: unknown) => boolean;
|
|
3
|
+
export declare const isString: (obj: unknown) => boolean;
|
|
4
|
+
export declare const isBlob: (obj: unknown) => boolean;
|
|
5
|
+
export declare const isFile: (obj: unknown) => boolean;
|
|
6
|
+
export declare const isNumber: (value: unknown) => boolean;
|
|
7
|
+
export declare const isBoolean: (value: unknown) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@mobx-ecosystem/mobx-form",
|
|
4
|
+
"private": false,
|
|
5
|
+
"version": "2.0.2",
|
|
6
|
+
"description": "provides the ability to use forms with validation in MobX stores",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "tsc --watch",
|
|
20
|
+
"_clear": "rimraf dist",
|
|
21
|
+
"tsc": "tsc",
|
|
22
|
+
"_make-bundle": "rollup -c",
|
|
23
|
+
"build": "run-s _clear _make-bundle",
|
|
24
|
+
"pub": "yarn build && yarn publish --access public",
|
|
25
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"mobx-formik",
|
|
32
|
+
"mobx-forms",
|
|
33
|
+
"form",
|
|
34
|
+
"mobx-form"
|
|
35
|
+
],
|
|
36
|
+
"author": "RudikS",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"mobx": "^4 || ^5 || ^6"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@rollup/plugin-commonjs": "^24.0.1",
|
|
43
|
+
"@rollup/plugin-node-resolve": "^15.0.2",
|
|
44
|
+
"@rollup/plugin-terser": "^0.4.0",
|
|
45
|
+
"@rollup/plugin-typescript": "^11.1.0",
|
|
46
|
+
"@types/lodash": "^4.14.192",
|
|
47
|
+
"mobx": "^6.9.0",
|
|
48
|
+
"npm-run-all": "^4.1.5",
|
|
49
|
+
"rimraf": "^5.0.0",
|
|
50
|
+
"rollup": "^3.20.2",
|
|
51
|
+
"rollup-plugin-dts": "^5.3.0",
|
|
52
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
53
|
+
"tslib": "^2.5.0",
|
|
54
|
+
"typescript": "^5.0.4"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "https://github.com/RudikS-git/mobx-ecosystem"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"lodash": "^4.17.21"
|
|
62
|
+
}
|
|
63
|
+
}
|