gform-react 2.7.5 → 2.8.1
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 +30 -30
- package/dist/cjs/gform-react.development.js +165 -140
- package/dist/cjs/gform-react.development.js.map +1 -1
- package/dist/cjs/gform-react.production.js +1 -1
- package/dist/cjs/gform-react.production.js.map +1 -1
- package/dist/esm/GForm.development.js +13 -10
- package/dist/esm/GForm.development.js.map +1 -1
- package/dist/esm/GForm.production.js +1 -1
- package/dist/esm/GForm.production.js.map +1 -1
- package/dist/esm/GInput.development.js +23 -22
- package/dist/esm/GInput.development.js.map +1 -1
- package/dist/esm/GInput.production.js +1 -1
- package/dist/esm/GInput.production.js.map +1 -1
- package/dist/esm/GValidator.development.js +1 -111
- package/dist/esm/GValidator.development.js.map +1 -1
- package/dist/esm/GValidator.production.js +1 -1
- package/dist/esm/GValidator.production.js.map +1 -1
- package/dist/esm/shared.development.js +134 -113
- package/dist/esm/shared.development.js.map +1 -1
- package/dist/esm/shared.development2.js +116 -0
- package/dist/esm/shared.development2.js.map +1 -0
- package/dist/esm/shared.production.js +1 -1
- package/dist/esm/shared.production.js.map +1 -1
- package/dist/esm/useFormSelector.development.js +4 -3
- package/dist/esm/useFormSelector.development.js.map +1 -1
- package/dist/esm/useFormSelector.production.js +1 -1
- package/dist/index.d.ts +7 -7
- package/native/dist/cjs/gform-react.development.js +151 -133
- package/native/dist/cjs/gform-react.development.js.map +1 -1
- package/native/dist/cjs/gform-react.production.js +1 -1
- package/native/dist/cjs/gform-react.production.js.map +1 -1
- package/native/dist/esm/RNGForm.development.js +4 -4
- package/native/dist/esm/RNGForm.development.js.map +1 -1
- package/native/dist/esm/RNGForm.production.js +1 -1
- package/native/dist/esm/RNGForm.production.js.map +1 -1
- package/native/dist/esm/RNGInput.development.js +23 -21
- package/native/dist/esm/RNGInput.development.js.map +1 -1
- package/native/dist/esm/RNGInput.production.js +1 -1
- package/native/dist/esm/RNGInput.production.js.map +1 -1
- package/native/dist/esm/shared.development.js +133 -112
- package/native/dist/esm/shared.development.js.map +1 -1
- package/native/dist/esm/shared.production.js +1 -1
- package/native/dist/esm/shared.production.js.map +1 -1
- package/native/dist/index.d.ts +7 -7
- package/package.json +3 -3
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
|
|
2
2
|
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
|
3
|
-
import React, { forwardRef,
|
|
4
|
-
import {
|
|
5
|
-
import './
|
|
3
|
+
import React, { forwardRef, useEffect, useMemo, memo } from 'react';
|
|
4
|
+
import { e as createSelector, u as useFormStore, a as useFormSelector, f as _debounce } from './shared.development.js';
|
|
5
|
+
import './shared.development2.js';
|
|
6
6
|
|
|
7
7
|
const selectFields = [state => state.fields];
|
|
8
8
|
const makeSelectFields = (keys = []) => createSelector(selectFields, fields => {
|
|
@@ -13,8 +13,9 @@ const makeSelectFields = (keys = []) => createSelector(selectFields, fields => {
|
|
|
13
13
|
const _excluded = ["formKey", "element", "title", "type", "fetch", "fetchDeps", "optimized", "debounce", "defaultChecked", "defaultValue", "checked", "validatorKey", "value"];
|
|
14
14
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
15
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
-
const _GInput = forwardRef((
|
|
17
|
-
|
|
16
|
+
const _GInput = forwardRef((props, ref) => {
|
|
17
|
+
const store = useFormStore();
|
|
18
|
+
const {
|
|
18
19
|
formKey,
|
|
19
20
|
element,
|
|
20
21
|
title,
|
|
@@ -28,13 +29,26 @@ const _GInput = forwardRef((_ref, ref) => {
|
|
|
28
29
|
checked,
|
|
29
30
|
validatorKey,
|
|
30
31
|
value
|
|
31
|
-
} =
|
|
32
|
-
rest = _objectWithoutProperties(
|
|
32
|
+
} = props,
|
|
33
|
+
rest = _objectWithoutProperties(props, _excluded);
|
|
34
|
+
if (!store.getState().fields[formKey]) {
|
|
35
|
+
store.registerField(props);
|
|
36
|
+
}
|
|
33
37
|
const inputState = useFormSelector(state => state.fields[formKey]);
|
|
34
|
-
const
|
|
38
|
+
const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (inputState.value) {
|
|
41
|
+
store.handlers._viHandler(inputState, {
|
|
42
|
+
target: store.getInputElement(formKey)
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return () => {
|
|
46
|
+
store.unregisterField(formKey);
|
|
47
|
+
};
|
|
48
|
+
}, []);
|
|
35
49
|
const _element = useMemo(() => {
|
|
36
50
|
let value, checked;
|
|
37
|
-
if (type === 'checkbox') checked = inputState.value || false;else value = inputState.value || '';
|
|
51
|
+
if (type === 'checkbox') checked = inputState.value || false;else if (type === 'number') value = inputState.value || 0;else value = inputState.value || '';
|
|
38
52
|
const _props = _objectSpread(_objectSpread({}, rest), {}, {
|
|
39
53
|
type,
|
|
40
54
|
name: formKey,
|
|
@@ -66,25 +80,12 @@ const _GInput = forwardRef((_ref, ref) => {
|
|
|
66
80
|
} : (e, unknown) => {
|
|
67
81
|
store.handlers._updateInputHandler(inputState, e, unknown);
|
|
68
82
|
};
|
|
69
|
-
if (!inputState.touched && inputState.dispatchChanges) {
|
|
70
|
-
_props.onFocus = rest.onFocus ? e => {
|
|
71
|
-
rest.onFocus(e);
|
|
72
|
-
inputState.dispatchChanges({
|
|
73
|
-
touched: true
|
|
74
|
-
});
|
|
75
|
-
} : () => {
|
|
76
|
-
inputState.dispatchChanges({
|
|
77
|
-
touched: true
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
83
|
}
|
|
82
84
|
if (element) {
|
|
83
85
|
return element(inputState, _props);
|
|
84
86
|
}
|
|
85
87
|
return React.createElement("input", _props);
|
|
86
88
|
}, [inputState, element]);
|
|
87
|
-
const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));
|
|
88
89
|
useEffect(() => {
|
|
89
90
|
if (fetch) {
|
|
90
91
|
_debounce(debounce, `${inputState.gid}-fetch`).then(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GInput.development.js","sources":["../../src/selectors.ts","../../src/fields/GInput.tsx"],"sourcesContent":["import {createSelector} from \"./form-context\";\r\nimport type {InitialState} from \"./state\";\r\n\r\nconst selectFields = [(state: InitialState) => state.fields];\r\n\r\nexport const makeSelectFields = (keys: string[] = []) =>\r\n createSelector(\r\n selectFields,\r\n (fields) => {\r\n const selected = keys.map((key) => JSON.stringify(fields[key].value)).join(', ');\r\n return selected.length ? selected : null;\r\n }\r\n );","import React, {forwardRef, memo, type ReactNode, useEffect, useMemo} from 'react';\r\n\r\nimport {_debounce} from '../helpers';\r\nimport type {GInputProps, GInputState, GElementProps} from '.';\r\nimport {useFormSelector, useFormStore} from \"../form-context\";\r\nimport {makeSelectFields} from \"../selectors\";\r\n\r\nconst _GInput = forwardRef<HTMLInputElement, GInputProps>(({\r\n formKey,\r\n element,\r\n title,\r\n type = 'text',\r\n fetch,\r\n fetchDeps,\r\n optimized,\r\n debounce = 300,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultChecked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultValue,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n checked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n validatorKey,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n value,\r\n ...rest\r\n}, ref) => {\r\n const inputState = useFormSelector(state => state.fields[formKey]);\r\n const store = useFormStore();\r\n\r\n const _element = useMemo(() => {\r\n let value: any, checked;\r\n\r\n if (type === 'checkbox') checked = inputState.value || false;\r\n else value = inputState.value || '';\r\n\r\n const _props = {\r\n ...rest,\r\n type,\r\n name: formKey,\r\n value,\r\n checked,\r\n ref,\r\n 'aria-invalid': inputState.error,\r\n 'aria-required': inputState.required,\r\n title: title || inputState.errorText\r\n };\r\n\r\n if (!store.handlers.optimized || !optimized) {\r\n _props.onBlur = rest.onBlur ?\r\n (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n rest.onBlur!(e);\r\n } : (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onInvalid = rest.onInvalid ?\r\n (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n rest.onInvalid!(e);\r\n } : (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onChange = rest.onChange ?\r\n (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n rest.onChange!(e);\r\n } : (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n };\r\n\r\n if (!inputState.touched && inputState.dispatchChanges) {\r\n _props.onFocus = rest.onFocus ?\r\n (e) => {\r\n rest.onFocus!(e);\r\n inputState.dispatchChanges({touched: true});\r\n }\r\n :\r\n () => {\r\n inputState.dispatchChanges({touched: true});\r\n };\r\n }\r\n }\r\n\r\n if (element) {\r\n return (element as (input: GInputState, props: GElementProps<typeof value>) => ReactNode)(inputState, _props);\r\n }\r\n\r\n return (\r\n <input {..._props} />\r\n );\r\n }, [inputState, element]);\r\n\r\n const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));\r\n\r\n useEffect(() => {\r\n if (fetch) {\r\n _debounce(debounce, `${inputState.gid}-fetch`).then(() => {\r\n const res = fetch(inputState, store.getState().fields);\r\n if (res instanceof Promise) {\r\n res.then((state) => state && store.handlers._dispatchChanges(state, formKey));\r\n } else if (res) {\r\n store.handlers._dispatchChanges(res, formKey);\r\n }\r\n });\r\n }\r\n }, [_fetchDeps]);\r\n\r\n return _element;\r\n});\r\n\r\nexport const GInput = memo(_GInput);"],"names":["selectFields","state","fields","makeSelectFields","keys","createSelector","selected","map","key","JSON","stringify","value","join","length","_GInput","forwardRef","_ref","ref","formKey","element","title","type","fetch","fetchDeps","optimized","debounce","defaultChecked","defaultValue","checked","validatorKey","rest","_objectWithoutProperties","_excluded","inputState","useFormSelector","store","useFormStore","_element","useMemo","_props","_objectSpread","name","error","required","errorText","handlers","onBlur","e","_viHandler","onInvalid","preventDefault","onChange","unknown","_updateInputHandler","touched","dispatchChanges","onFocus","React","createElement","_fetchDeps","useEffect","_debounce","gid","then","res","getState","Promise","_dispatchChanges","GInput","memo"],"mappings":";;;;;;AAGA,MAAMA,YAAY,GAAG,CAAEC,KAAmB,IAAKA,KAAK,CAACC,MAAM,CAAC;AAErD,MAAMC,gBAAgB,GAAGA,CAACC,IAAc,GAAG,EAAE,KAChDC,cAAc,CACVL,YAAY,EACXE,MAAM,IAAK;EACR,MAAMI,QAAQ,GAAGF,IAAI,CAACG,GAAG,CAAEC,GAAG,IAAKC,IAAI,CAACC,SAAS,CAACR,MAAM,CAACM,GAAG,CAAC,CAACG,KAAK,CAAC,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC;AAChF,EAAA,OAAON,QAAQ,CAACO,MAAM,GAAGP,QAAQ,GAAG,IAAI;AAC5C,CACJ,CAAC;;;;;ACLL,MAAMQ,OAAO,GAAGC,UAAU,CAAgC,CAAAC,IAAA,EAoBvDC,GAAG,KAAK;EAAA,IApBgD;MACvDC,OAAO;MACPC,OAAO;MACPC,KAAK;AACLC,MAAA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"],"sourcesContent":["import {createSelector} from \"./form-context\";\r\nimport type {InitialState} from \"./state\";\r\n\r\nconst selectFields = [(state: InitialState) => state.fields];\r\n\r\nexport const makeSelectFields = (keys: string[] = []) =>\r\n createSelector(\r\n selectFields,\r\n (fields) => {\r\n const selected = keys.map((key) => JSON.stringify(fields[key].value)).join(', ');\r\n return selected.length ? selected : null;\r\n }\r\n );","import React, {FormEvent, forwardRef, memo, type ReactNode, useEffect, useMemo} from 'react';\r\n\r\nimport {_debounce} from '../helpers';\r\nimport type {GInputProps, GInputState, GElementProps} from '.';\r\nimport {useFormSelector, useFormStore} from \"../form-context\";\r\nimport {makeSelectFields} from \"../selectors\";\r\nimport {type GDOMElement} from \"../form\";\r\n\r\nconst _GInput = forwardRef<HTMLInputElement, GInputProps>((props, ref) => {\r\n const store = useFormStore();\r\n\r\n const {\r\n formKey,\r\n element,\r\n title,\r\n type = 'text',\r\n fetch,\r\n fetchDeps,\r\n optimized,\r\n debounce = 300,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultChecked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultValue,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n checked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n validatorKey,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n value,\r\n ...rest\r\n } = props;\r\n if (!store.getState().fields[formKey]) {\r\n if (__DEBUG__) {\r\n console.log('[GInput] -', 'registering input', `(${formKey})`, props);\r\n }\r\n store.registerField(props);\r\n }\r\n\r\n const inputState = useFormSelector(state => state.fields[formKey]);\r\n const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));\r\n\r\n useEffect(() => {\r\n if (inputState.value) {\r\n store.handlers._viHandler(inputState, {target: store.getInputElement(formKey)} as unknown as FormEvent<GDOMElement>);\r\n }\r\n return () => {\r\n if (__DEBUG__) {\r\n console.log('[GInput] -', 'unregistering input', `(${formKey})`);\r\n }\r\n store.unregisterField(formKey);\r\n };\r\n }, []);\r\n\r\n const _element = useMemo(() => {\r\n let value: any, checked;\r\n\r\n if (type === 'checkbox') checked = inputState.value || false;\r\n else if (type === 'number') value = inputState.value || 0;\r\n else value = inputState.value || '';\r\n\r\n const _props = {\r\n ...rest,\r\n type,\r\n name: formKey,\r\n value,\r\n checked,\r\n ref,\r\n 'aria-invalid': inputState.error,\r\n 'aria-required': inputState.required,\r\n title: title || inputState.errorText\r\n };\r\n\r\n if (!store.handlers.optimized || !optimized) {\r\n _props.onBlur = rest.onBlur ?\r\n (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n rest.onBlur!(e);\r\n } : (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onInvalid = rest.onInvalid ?\r\n (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n rest.onInvalid!(e);\r\n } : (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onChange = rest.onChange ?\r\n (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n rest.onChange!(e);\r\n } : (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n };\r\n }\r\n\r\n if (element) {\r\n return (element as (input: GInputState, props: GElementProps<typeof value>) => ReactNode)(inputState, _props);\r\n }\r\n\r\n return (\r\n <input {..._props} />\r\n );\r\n }, [inputState, element]);\r\n\r\n useEffect(() => {\r\n if (fetch) {\r\n _debounce(debounce, `${inputState.gid}-fetch`).then(() => {\r\n const res = fetch(inputState, store.getState().fields);\r\n if (res instanceof Promise) {\r\n res.then((state) => state && store.handlers._dispatchChanges(state, formKey));\r\n } else if (res) {\r\n store.handlers._dispatchChanges(res, formKey);\r\n }\r\n });\r\n }\r\n }, [_fetchDeps]);\r\n\r\n return _element;\r\n});\r\n\r\nexport const GInput = memo(_GInput);"],"names":["selectFields","state","fields","makeSelectFields","keys","createSelector","selected","map","key","JSON","stringify","value","join","length","_GInput","forwardRef","props","ref","store","useFormStore","formKey","element","title","type","fetch","fetchDeps","optimized","debounce","defaultChecked","defaultValue","checked","validatorKey","rest","_objectWithoutProperties","_excluded","getState","registerField","inputState","useFormSelector","_fetchDeps","useEffect","handlers","_viHandler","target","getInputElement","unregisterField","_element","useMemo","_props","_objectSpread","name","error","required","errorText","onBlur","e","onInvalid","preventDefault","onChange","unknown","_updateInputHandler","React","createElement","_debounce","gid","then","res","Promise","_dispatchChanges","GInput","memo"],"mappings":";;;;;;AAGA,MAAMA,YAAY,GAAG,CAAEC,KAAmB,IAAKA,KAAK,CAACC,MAAM,CAAC;AAErD,MAAMC,gBAAgB,GAAGA,CAACC,IAAc,GAAG,EAAE,KAChDC,cAAc,CACVL,YAAY,EACXE,MAAM,IAAK;EACR,MAAMI,QAAQ,GAAGF,IAAI,CAACG,GAAG,CAAEC,GAAG,IAAKC,IAAI,CAACC,SAAS,CAACR,MAAM,CAACM,GAAG,CAAC,CAACG,KAAK,CAAC,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC;AAChF,EAAA,OAAON,QAAQ,CAACO,MAAM,GAAGP,QAAQ,GAAG,IAAI;AAC5C,CACJ,CAAC;;;;;ACJL,MAAMQ,OAAO,GAAGC,UAAU,CAAgC,CAACC,KAAK,EAAEC,GAAG,KAAK;AACtE,EAAA,MAAMC,KAAK,GAAGC,YAAY,EAAE;EAE5B,MAAM;MACFC,OAAO;MACPC,OAAO;MACPC,KAAK;AACLC,MAAAA,IAAI,GAAG,MAAM;MACbC,KAAK;MACLC,SAAS;MACTC,SAAS;AACTC,MAAAA,QAAQ,GAAG,GAAG;MAEdC,cAAc;MAEdC,YAAY;MAEZC,OAAO;MAEPC,YAAY;AAEZpB,MAAAA;AAEJ,KAAC,GAAGK,KAAK;AADFgB,IAAAA,IAAI,GAAAC,wBAAA,CACPjB,KAAK,EAAAkB,SA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,EAAEkB,CAAC,EAAEI,OAAO,CAAC;MAC9D,CAAC;AACT,IAAA;AAEA,IAAA,IAAItC,OAAO,EAAE;AACT,MAAA,OAAQA,OAAO,CAA2EgB,UAAU,EAAEW,MAAM,CAAC;AACjH,IAAA;AAEA,IAAA,OACIa,KAAA,CAAAC,aAAA,CAAA,OAAA,EAAWd,MAAS,CAAC;AAE7B,EAAA,CAAC,EAAE,CAACX,UAAU,EAAEhB,OAAO,CAAC,CAAC;AAEzBmB,EAAAA,SAAS,CAAC,MAAM;AACZ,IAAA,IAAIhB,KAAK,EAAE;AACPuC,MAAAA,SAAS,CAACpC,QAAQ,EAAE,CAAA,EAAGU,UAAU,CAAC2B,GAAG,CAAA,MAAA,CAAQ,CAAC,CAACC,IAAI,CAAC,MAAM;AACtD,QAAA,MAAMC,GAAG,GAAG1C,KAAK,CAACa,UAAU,EAAEnB,KAAK,CAACiB,QAAQ,EAAE,CAACjC,MAAM,CAAC;QACtD,IAAIgE,GAAG,YAAYC,OAAO,EAAE;AACxBD,UAAAA,GAAG,CAACD,IAAI,CAAEhE,KAAK,IAAKA,KAAK,IAAIiB,KAAK,CAACuB,QAAQ,CAAC2B,gBAAgB,CAACnE,KAAK,EAAEmB,OAAO,CAAC,CAAC;QACjF,CAAC,MAAM,IAAI8C,GAAG,EAAE;UACZhD,KAAK,CAACuB,QAAQ,CAAC2B,gBAAgB,CAACF,GAAG,EAAE9C,OAAO,CAAC;AACjD,QAAA;AACJ,MAAA,CAAC,CAAC;AACN,IAAA;AACJ,EAAA,CAAC,EAAE,CAACmB,UAAU,CAAC,CAAC;AAEhB,EAAA,OAAOO,QAAQ;AACnB,CAAC,CAAC;MAEWuB,MAAM,GAAGC,IAAI,CAACxD,OAAO;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"@babel/runtime/helpers/esm/defineProperty";import t from"@babel/runtime/helpers/esm/objectWithoutProperties";import r,{forwardRef as n,
|
|
1
|
+
import e from"@babel/runtime/helpers/esm/defineProperty";import t from"@babel/runtime/helpers/esm/objectWithoutProperties";import r,{forwardRef as n,useEffect as a,useMemo as l,memo as o}from"react";import{d as i,u as d,a as s,e as c}from"./shared.production.js";const u=[e=>e.fields],p=["formKey","element","title","type","fetch","fetchDeps","optimized","debounce","defaultChecked","defaultValue","checked","validatorKey","value"];function h(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function f(t){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?h(Object(n),!0).forEach((function(r){e(t,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):h(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}const m=o(n(((e,n)=>{const o=d(),{formKey:h,element:m,title:v,type:b="text",fetch:g,fetchDeps:y,optimized:O,debounce:j=300,defaultChecked:P,defaultValue:_,checked:D,validatorKey:k,value:C}=e,H=t(e,p);o.getState().fields[h]||o.registerField(e);const w=s((e=>e.fields[h])),I=s(((e=[])=>i(u,(t=>{const r=e.map((e=>JSON.stringify(t[e].value))).join(", ");return r.length?r:null})))(y));a((()=>(w.value&&o.handlers._viHandler(w,{target:o.getInputElement(h)}),()=>{o.unregisterField(h)})),[]);const S=l((()=>{let e,t;"checkbox"===b?t=w.value||!1:e="number"===b?w.value||0:w.value||"";const a=f(f({},H),{},{type:b,name:h,value:e,checked:t,ref:n,"aria-invalid":w.error,"aria-required":w.required,title:v||w.errorText});return o.handlers.optimized&&O||(a.onBlur=H.onBlur?e=>{o.handlers._viHandler(w,e),H.onBlur(e)}:e=>{o.handlers._viHandler(w,e)},a.onInvalid=H.onInvalid?e=>{e.preventDefault(),o.handlers._viHandler(w,e),H.onInvalid(e)}:e=>{e.preventDefault(),o.handlers._viHandler(w,e)},a.onChange=H.onChange?(e,t)=>{o.handlers._updateInputHandler(w,e,t),H.onChange(e)}:(e,t)=>{o.handlers._updateInputHandler(w,e,t)}),m?m(w,a):r.createElement("input",a)}),[w,m]);return a((()=>{g&&c(j,`${w.gid}-fetch`).then((()=>{const e=g(w,o.getState().fields);e instanceof Promise?e.then((e=>e&&o.handlers._dispatchChanges(e,h))):e&&o.handlers._dispatchChanges(e,h)}))}),[I]),S})));export{m as GInput};
|
|
2
2
|
//# sourceMappingURL=GInput.production.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GInput.production.js","sources":["../../src/selectors.ts","../../src/fields/GInput.tsx"],"sourcesContent":["import {createSelector} from \"./form-context\";\r\nimport type {InitialState} from \"./state\";\r\n\r\nconst selectFields = [(state: InitialState) => state.fields];\r\n\r\nexport const makeSelectFields = (keys: string[] = []) =>\r\n createSelector(\r\n selectFields,\r\n (fields) => {\r\n const selected = keys.map((key) => JSON.stringify(fields[key].value)).join(', ');\r\n return selected.length ? selected : null;\r\n }\r\n );","import React, {forwardRef, memo, type ReactNode, useEffect, useMemo} from 'react';\r\n\r\nimport {_debounce} from '../helpers';\r\nimport type {GInputProps, GInputState, GElementProps} from '.';\r\nimport {useFormSelector, useFormStore} from \"../form-context\";\r\nimport {makeSelectFields} from \"../selectors\";\r\n\r\nconst _GInput = forwardRef<HTMLInputElement, GInputProps>(({\r\n formKey,\r\n
|
|
1
|
+
{"version":3,"file":"GInput.production.js","sources":["../../src/selectors.ts","../../src/fields/GInput.tsx"],"sourcesContent":["import {createSelector} from \"./form-context\";\r\nimport type {InitialState} from \"./state\";\r\n\r\nconst selectFields = [(state: InitialState) => state.fields];\r\n\r\nexport const makeSelectFields = (keys: string[] = []) =>\r\n createSelector(\r\n selectFields,\r\n (fields) => {\r\n const selected = keys.map((key) => JSON.stringify(fields[key].value)).join(', ');\r\n return selected.length ? selected : null;\r\n }\r\n );","import React, {FormEvent, forwardRef, memo, type ReactNode, useEffect, useMemo} from 'react';\r\n\r\nimport {_debounce} from '../helpers';\r\nimport type {GInputProps, GInputState, GElementProps} from '.';\r\nimport {useFormSelector, useFormStore} from \"../form-context\";\r\nimport {makeSelectFields} from \"../selectors\";\r\nimport {type GDOMElement} from \"../form\";\r\n\r\nconst _GInput = forwardRef<HTMLInputElement, GInputProps>((props, ref) => {\r\n const store = useFormStore();\r\n\r\n const {\r\n formKey,\r\n element,\r\n title,\r\n type = 'text',\r\n fetch,\r\n fetchDeps,\r\n optimized,\r\n debounce = 300,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultChecked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n defaultValue,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n checked,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n validatorKey,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n value,\r\n ...rest\r\n } = props;\r\n if (!store.getState().fields[formKey]) {\r\n if (__DEBUG__) {\r\n console.log('[GInput] -', 'registering input', `(${formKey})`, props);\r\n }\r\n store.registerField(props);\r\n }\r\n\r\n const inputState = useFormSelector(state => state.fields[formKey]);\r\n const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));\r\n\r\n useEffect(() => {\r\n if (inputState.value) {\r\n store.handlers._viHandler(inputState, {target: store.getInputElement(formKey)} as unknown as FormEvent<GDOMElement>);\r\n }\r\n return () => {\r\n if (__DEBUG__) {\r\n console.log('[GInput] -', 'unregistering input', `(${formKey})`);\r\n }\r\n store.unregisterField(formKey);\r\n };\r\n }, []);\r\n\r\n const _element = useMemo(() => {\r\n let value: any, checked;\r\n\r\n if (type === 'checkbox') checked = inputState.value || false;\r\n else if (type === 'number') value = inputState.value || 0;\r\n else value = inputState.value || '';\r\n\r\n const _props = {\r\n ...rest,\r\n type,\r\n name: formKey,\r\n value,\r\n checked,\r\n ref,\r\n 'aria-invalid': inputState.error,\r\n 'aria-required': inputState.required,\r\n title: title || inputState.errorText\r\n };\r\n\r\n if (!store.handlers.optimized || !optimized) {\r\n _props.onBlur = rest.onBlur ?\r\n (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n rest.onBlur!(e);\r\n } : (e) => {\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onInvalid = rest.onInvalid ?\r\n (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n rest.onInvalid!(e);\r\n } : (e) => {\r\n e.preventDefault(); // hide default browser validation tooltip\r\n store.handlers._viHandler(inputState, e);\r\n };\r\n\r\n _props.onChange = rest.onChange ?\r\n (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n rest.onChange!(e);\r\n } : (e, unknown?: { value: unknown } | string | number) => {\r\n store.handlers._updateInputHandler(inputState, e, unknown);\r\n };\r\n }\r\n\r\n if (element) {\r\n return (element as (input: GInputState, props: GElementProps<typeof value>) => ReactNode)(inputState, _props);\r\n }\r\n\r\n return (\r\n <input {..._props} />\r\n );\r\n }, [inputState, element]);\r\n\r\n useEffect(() => {\r\n if (fetch) {\r\n _debounce(debounce, `${inputState.gid}-fetch`).then(() => {\r\n const res = fetch(inputState, store.getState().fields);\r\n if (res instanceof Promise) {\r\n res.then((state) => state && store.handlers._dispatchChanges(state, formKey));\r\n } else if (res) {\r\n store.handlers._dispatchChanges(res, formKey);\r\n }\r\n });\r\n }\r\n }, [_fetchDeps]);\r\n\r\n return _element;\r\n});\r\n\r\nexport const GInput = memo(_GInput);"],"names":["selectFields","state","fields","GInput","memo","forwardRef","props","ref","store","useFormStore","formKey","element","title","type","fetch","fetchDeps","optimized","debounce","defaultChecked","defaultValue","checked","validatorKey","value","rest","_objectWithoutProperties","_excluded","getState","registerField","inputState","useFormSelector","_fetchDeps","makeSelectFields","keys","createSelector","selected","map","key","JSON","stringify","join","length","useEffect","handlers","_viHandler","target","getInputElement","unregisterField","_element","useMemo","_props","_objectSpread","name","error","required","errorText","onBlur","e","onInvalid","preventDefault","onChange","unknown","_updateInputHandler","React","createElement","_debounce","gid","then","res","Promise","_dispatchChanges"],"mappings":"uQAGA,MAAMA,EAAe,CAAEC,GAAwBA,EAAMC,0tBCKrD,MAsHaC,EAASC,EAtHNC,GAA0C,CAACC,EAAOC,KAC9D,MAAMC,EAAQC,KAERC,QACFA,EAAOC,QACPA,EAAOC,MACPA,EAAKC,KACLA,EAAO,OAAMC,MACbA,EAAKC,UACLA,EAASC,UACTA,EAASC,SACTA,EAAW,IAAGC,eAEdA,EAAcC,aAEdA,EAAYC,QAEZA,EAAOC,aAEPA,EAAYC,MAEZA,GAEAhB,EADGiB,EAAIC,EACPlB,EAAKmB,GACJjB,EAAMkB,WAAWxB,OAAOQ,IAIzBF,EAAMmB,cAAcrB,GAGxB,MAAMsB,EAAaC,GAAgB5B,GAASA,EAAMC,OAAOQ,KACnDoB,EAAaD,EDnCSE,EAACC,EAAiB,KAC9CC,EACIjC,GACCE,IACG,MAAMgC,EAAWF,EAAKG,KAAKC,GAAQC,KAAKC,UAAUpC,EAAOkC,GAAKd,SAAQiB,KAAK,MAC3E,OAAOL,EAASM,OAASN,EAAW,IAAI,IC8BbH,CAAiBhB,IAEpD0B,GAAU,KACFb,EAAWN,OACXd,EAAMkC,SAASC,WAAWf,EAAY,CAACgB,OAAQpC,EAAMqC,gBAAgBnC,KAElE,KAIHF,EAAMsC,gBAAgBpC,EAAQ,IAEnC,IAEH,MAAMqC,EAAWC,GAAQ,KACrB,IAAI1B,EAAYF,EAEH,aAATP,EAAqBO,EAAUQ,EAAWN,QAAS,EAC3BA,EAAV,WAATT,EAA2Be,EAAWN,OAAS,EAC3CM,EAAWN,OAAS,GAEjC,MAAM2B,EAAMC,EAAAA,KACL3B,GAAI,GAAA,CACPV,OACAsC,KAAMzC,EACNY,QACAF,UACAb,MACA,eAAgBqB,EAAWwB,MAC3B,gBAAiBxB,EAAWyB,SAC5BzC,MAAOA,GAASgB,EAAW0B,YA+B/B,OA5BK9C,EAAMkC,SAAS1B,WAAcA,IAC9BiC,EAAOM,OAAShC,EAAKgC,OAChBC,IACGhD,EAAMkC,SAASC,WAAWf,EAAY4B,GACtCjC,EAAKgC,OAAQC,EAAE,EACdA,IACDhD,EAAMkC,SAASC,WAAWf,EAAY4B,EAAE,EAGhDP,EAAOQ,UAAYlC,EAAKkC,UACnBD,IACGA,EAAEE,iBACFlD,EAAMkC,SAASC,WAAWf,EAAY4B,GACtCjC,EAAKkC,UAAWD,EAAE,EACjBA,IACDA,EAAEE,iBACFlD,EAAMkC,SAASC,WAAWf,EAAY4B,EAAE,EAGhDP,EAAOU,SAAWpC,EAAKoC,SACnB,CAACH,EAAGI,KACApD,EAAMkC,SAASmB,oBAAoBjC,EAAY4B,EAAGI,GAClDrC,EAAKoC,SAAUH,EAAE,EACjB,CAACA,EAAGI,KACJpD,EAAMkC,SAASmB,oBAAoBjC,EAAY4B,EAAGI,EAAQ,GAIlEjD,EACQA,EAAkFiB,EAAYqB,GAItGa,EAAAC,cAAA,QAAWd,EAAU,GAE1B,CAACrB,EAAYjB,IAehB,OAbA8B,GAAU,KACF3B,GACAkD,EAAU/C,EAAU,GAAGW,EAAWqC,aAAaC,MAAK,KAChD,MAAMC,EAAMrD,EAAMc,EAAYpB,EAAMkB,WAAWxB,QAC3CiE,aAAeC,QACfD,EAAID,MAAMjE,GAAUA,GAASO,EAAMkC,SAAS2B,iBAAiBpE,EAAOS,KAC7DyD,GACP3D,EAAMkC,SAAS2B,iBAAiBF,EAAKzD,EACzC,GAER,GACD,CAACoB,IAEGiB,CAAQ"}
|
|
@@ -1,112 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
let validityMap;
|
|
3
|
-
{
|
|
4
|
-
handlersMap = {
|
|
5
|
-
minLength: 'withMinLengthMessage',
|
|
6
|
-
maxLength: 'withMaxLengthMessage',
|
|
7
|
-
required: 'withRequiredMessage',
|
|
8
|
-
pattern: 'withPatternMismatchMessage',
|
|
9
|
-
min: 'withRangeUnderflowMessage',
|
|
10
|
-
max: 'withRangeOverflowMessage',
|
|
11
|
-
step: 'withStepMismatchMessage',
|
|
12
|
-
type: 'withTypeMismatchMessage'
|
|
13
|
-
};
|
|
14
|
-
validityMap = {
|
|
15
|
-
tooShort: 'minLength',
|
|
16
|
-
valueMissing: 'required',
|
|
17
|
-
tooLong: 'maxLength',
|
|
18
|
-
patternMismatch: 'pattern',
|
|
19
|
-
rangeOverflow: 'max',
|
|
20
|
-
rangeUnderflow: 'min',
|
|
21
|
-
stepMismatch: 'step',
|
|
22
|
-
typeMismatch: 'type'
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
class GValidator {
|
|
26
|
-
get handlers() {
|
|
27
|
-
return this._handlers;
|
|
28
|
-
}
|
|
29
|
-
get constraintHandlers() {
|
|
30
|
-
return this._constraintHandlers;
|
|
31
|
-
}
|
|
32
|
-
get asyncHandlers() {
|
|
33
|
-
return this._asyncHandlers;
|
|
34
|
-
}
|
|
35
|
-
constructor(baseValidator) {
|
|
36
|
-
const baseHandlers = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.handlers) || [];
|
|
37
|
-
const baseConstraintHandlers = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.constraintHandlers) || [];
|
|
38
|
-
const baseHandlersAsync = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.asyncHandlers) || [];
|
|
39
|
-
this._handlers = [].concat(baseHandlers);
|
|
40
|
-
this._constraintHandlers = [].concat(baseConstraintHandlers);
|
|
41
|
-
this._asyncHandlers = [].concat(baseHandlersAsync);
|
|
42
|
-
{
|
|
43
|
-
this.track = [];
|
|
44
|
-
if (baseValidator !== null && baseValidator !== void 0 && baseValidator.track) {
|
|
45
|
-
this.track = this.track.concat(baseValidator.track);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
hasConstraint(constraint) {
|
|
50
|
-
var _this$track;
|
|
51
|
-
return ((_this$track = this.track) === null || _this$track === void 0 ? void 0 : _this$track.includes(constraint)) || false;
|
|
52
|
-
}
|
|
53
|
-
withRequiredMessage(message) {
|
|
54
|
-
return this.__addConstraintValidationHandler('valueMissing', message);
|
|
55
|
-
}
|
|
56
|
-
withMaxLengthMessage(message) {
|
|
57
|
-
return this.__addConstraintValidationHandler('tooLong', message);
|
|
58
|
-
}
|
|
59
|
-
withMinLengthMessage(message) {
|
|
60
|
-
return this.__addConstraintValidationHandler('tooShort', message);
|
|
61
|
-
}
|
|
62
|
-
withPatternMismatchMessage(message) {
|
|
63
|
-
return this.__addConstraintValidationHandler('patternMismatch', message);
|
|
64
|
-
}
|
|
65
|
-
withBadInputMessage(message) {
|
|
66
|
-
return this.__addConstraintValidationHandler('badInput', message);
|
|
67
|
-
}
|
|
68
|
-
withRangeUnderflowMessage(message) {
|
|
69
|
-
return this.__addConstraintValidationHandler('rangeUnderflow', message);
|
|
70
|
-
}
|
|
71
|
-
withRangeOverflowMessage(message) {
|
|
72
|
-
return this.__addConstraintValidationHandler('rangeOverflow', message);
|
|
73
|
-
}
|
|
74
|
-
withTypeMismatchMessage(message) {
|
|
75
|
-
return this.__addConstraintValidationHandler('typeMismatch', message);
|
|
76
|
-
}
|
|
77
|
-
withStepMismatchMessage(message) {
|
|
78
|
-
return this.__addConstraintValidationHandler('stepMismatch', message);
|
|
79
|
-
}
|
|
80
|
-
withCustomValidation(handler) {
|
|
81
|
-
this._handlers.push(handler);
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
withCustomValidationAsync(handler) {
|
|
85
|
-
this._asyncHandlers.push(handler);
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
__addConstraintValidationHandler(validityKey, message) {
|
|
89
|
-
if (this.track) {
|
|
90
|
-
if (this.track.includes(validityKey)) {
|
|
91
|
-
console.warn(`DEV ONLY - [Duplicate Handlers] - handler for '${validityKey}' has already been defined`);
|
|
92
|
-
}
|
|
93
|
-
this.track.push(validityKey);
|
|
94
|
-
}
|
|
95
|
-
this._constraintHandlers.push((input, key) => {
|
|
96
|
-
{
|
|
97
|
-
if (validityKey && validityMap[validityKey] && typeof input[validityMap[validityKey]] === 'undefined') {
|
|
98
|
-
console.warn(`DEV ONLY - [Missing Prop] - the input '${input.formKey}' has registered validator for the violation '${validityKey}' but the input hasn't described the constraint '${validityMap[validityKey]}'.\nadd '${validityMap[validityKey]}' to the input props.\nexample:\n<GInput formKey='${input.formKey}' ${validityMap[validityKey]}={...} />\n\nor either remove '.${handlersMap[validityMap[validityKey]]}(...)' validation`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (key === validityKey) {
|
|
102
|
-
input.errorText = typeof message === 'string' ? message : message(input);
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
return false;
|
|
106
|
-
});
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export { GValidator, handlersMap, validityMap };
|
|
1
|
+
export { G as GValidator } from './shared.development2.js';
|
|
112
2
|
//# sourceMappingURL=GValidator.development.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GValidator.development.js","sources":["../../src/validations/GValidator.ts"],"sourcesContent":["import type { GConstraintValidator, GConstraintValidatorHandler, GCustomValidatorHandler, GCustomValidatorHandlerAsync } from \".\";\r\n\r\nexport let handlersMap: { [key: string]: string };\r\nexport let validityMap: { [key in keyof Partial<ValidityState>]: any };\r\n\r\nif (__DEV__) {\r\n handlersMap = {\r\n minLength: 'withMinLengthMessage',\r\n maxLength: 'withMaxLengthMessage',\r\n required: 'withRequiredMessage',\r\n pattern: 'withPatternMismatchMessage',\r\n min: 'withRangeUnderflowMessage',\r\n max: 'withRangeOverflowMessage',\r\n step: 'withStepMismatchMessage',\r\n type: 'withTypeMismatchMessage',\r\n };\r\n validityMap = {\r\n tooShort: 'minLength',\r\n valueMissing: 'required',\r\n tooLong: 'maxLength',\r\n patternMismatch: 'pattern',\r\n rangeOverflow: 'max',\r\n rangeUnderflow: 'min',\r\n stepMismatch: 'step',\r\n typeMismatch: 'type'\r\n };\r\n}\r\n\r\n/**a class for handling validations for input(s)\r\n * @example\r\n * const baseValidator = new GValidator().withRequiredMessage('this field is required');\r\n *\r\n * const validators: GValidators<SignInForm> = {\r\n * username: new GValidator(baseValidator).withMinLengthMessage('...'),\r\n * '*': baseValidator // a default validator for all other fields in the form\r\n * };\r\n */\r\nexport class GValidator<T = any> {\r\n private _handlers: GCustomValidatorHandler<T>[];\r\n private _constraintHandlers: GConstraintValidatorHandler[];\r\n private _asyncHandlers: GCustomValidatorHandlerAsync<T>[];\r\n track?: (keyof ValidityState)[];\r\n\r\n get handlers() {\r\n return this._handlers;\r\n }\r\n\r\n get constraintHandlers() {\r\n return this._constraintHandlers;\r\n }\r\n\r\n get asyncHandlers() {\r\n return this._asyncHandlers;\r\n }\r\n\r\n constructor(baseValidator?: GValidator<T>) {\r\n const baseHandlers = baseValidator?.handlers || [];\r\n const baseConstraintHandlers = baseValidator?.constraintHandlers || [];\r\n const baseHandlersAsync = baseValidator?.asyncHandlers || [];\r\n\r\n this._handlers = [].concat(baseHandlers as any);\r\n this._constraintHandlers = [].concat(baseConstraintHandlers as any);\r\n this._asyncHandlers = [].concat(baseHandlersAsync as any);\r\n\r\n if (__DEV__) {\r\n this.track = [];\r\n if (baseValidator?.track) {\r\n this.track = this.track.concat(baseValidator.track);\r\n }\r\n } else {\r\n delete this.track;\r\n }\r\n }\r\n\r\n public hasConstraint(constraint: keyof ValidityState): boolean {\r\n return this.track?.includes(constraint) || false;\r\n }\r\n\r\n /**register a `valueMissing` violation handler (use this with `required` attribute) */\r\n withRequiredMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('valueMissing', message);\r\n }\r\n\r\n /**register a `tooLong` violation handler (use this with `maxLength` attribute) */\r\n withMaxLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooLong', message);\r\n }\r\n\r\n /**register a `tooShort` violation handler (use this with `minLength` attribute)*/\r\n withMinLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooShort', message);\r\n }\r\n\r\n /**register a `patternMismatch` violation handler (use this with `pattern` attribute)*/\r\n withPatternMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('patternMismatch', message);\r\n }\r\n\r\n /**register a `badInput` violation handler */\r\n withBadInputMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('badInput', message);\r\n }\r\n\r\n /**register a `rangeUnderflow` violation handler (use this with `min` attribute) */\r\n withRangeUnderflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeUnderflow', message);\r\n }\r\n\r\n /**register a `rangeOverflow` violation handler (use this with `max` attribute) */\r\n withRangeOverflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeOverflow', message);\r\n }\r\n\r\n /**\r\n * register a `typeMismatch` violation handler<br />\r\n * if its possible use `pattern` attribute (and `withPatternMismatchMessage`) or `custom validation` instead.<br/>\r\n * use the `type` attribute to set the input's keyboard (for example type `'tel'` will show on mobile phones only numpads)\r\n * and then with `pattern` or `custom validation` you can validate it.<br/>\r\n * the reason for that is `type` is not a solid validation and likely will be replaced anyway.<br />\r\n * if `pattern` or `custom` are used, then `withTypeMismatchMessage` is ignored\r\n * */\r\n withTypeMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('typeMismatch', message);\r\n }\r\n\r\n /**register a `stepMismatch` violation handler (use this with `step` attribute)*/\r\n withStepMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('stepMismatch', message);\r\n }\r\n\r\n /**register a custom validation handler */\r\n withCustomValidation(handler: GCustomValidatorHandler<T>): GValidator<T> {\r\n this._handlers.push(handler);\r\n return this;\r\n }\r\n /**register a custom validation async handler */\r\n withCustomValidationAsync(handler: GCustomValidatorHandlerAsync<T>): GValidator<T> {\r\n this._asyncHandlers.push(handler);\r\n return this;\r\n }\r\n\r\n private __addConstraintValidationHandler(validityKey: keyof ValidityState, message: string | GConstraintValidator): GValidator<T> {\r\n if (__DEV__ && this.track) {\r\n if (this.track.includes(validityKey)) {\r\n console.warn(`DEV ONLY - [Duplicate Handlers] - handler for '${validityKey}' has already been defined`);\r\n }\r\n this.track.push(validityKey);\r\n }\r\n this._constraintHandlers.push((input, key) => {\r\n if (__DEV__) {\r\n if (validityKey && validityMap[validityKey] && typeof input[validityMap[validityKey]] === 'undefined') {\r\n console.warn(`DEV ONLY - [Missing Prop] - the input '${input.formKey}' has registered validator for the violation '${validityKey}' but the input hasn't described the constraint '${validityMap[validityKey]}'.\\nadd '${validityMap[validityKey]}' to the input props.\\nexample:\\n<GInput formKey='${input.formKey}' ${validityMap[validityKey]}={...} />\\n\\nor either remove '.${handlersMap[validityMap[validityKey]]}(...)' validation`);\r\n }\r\n }\r\n\r\n if (key === validityKey) {\r\n input.errorText = typeof message === 'string' ? message : message(input);\r\n return true;\r\n }\r\n return false;\r\n });\r\n\r\n return this;\r\n }\r\n}"],"names":["handlersMap","validityMap","minLength","maxLength","required","pattern","min","max","step","type","tooShort","valueMissing","tooLong","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","typeMismatch","GValidator","handlers","_handlers","constraintHandlers","_constraintHandlers","asyncHandlers","_asyncHandlers","constructor","baseValidator","baseHandlers","baseConstraintHandlers","baseHandlersAsync","concat","track","hasConstraint","constraint","_this$track","includes","withRequiredMessage","message","__addConstraintValidationHandler","withMaxLengthMessage","withMinLengthMessage","withPatternMismatchMessage","withBadInputMessage","withRangeUnderflowMessage","withRangeOverflowMessage","withTypeMismatchMessage","withStepMismatchMessage","withCustomValidation","handler","push","withCustomValidationAsync","validityKey","console","warn","input","key","formKey","errorText"],"mappings":"IAEWA;IACAC;AAEE;AACTD,EAAAA,WAAW,GAAG;AACVE,IAAAA,S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KAAK;AAC1C,MAAa;AACT,QAAA,IAAIJ,WAAW,IAAIlD,WAAW,CAACkD,WAAW,CAAC,IAAI,OAAOG,KAAK,CAACrD,WAAW,CAACkD,WAAW,CAAC,CAAC,KAAK,WAAW,EAAE;AACnGC,UAAAA,OAAO,CAACC,IAAI,CAAC,CAAA,uCAAA,EAA0CC,KAAK,CAACE,OAAO,CAAA,8CAAA,EAAiDL,WAAW,CAAA,iDAAA,EAAoDlD,WAAW,CAACkD,WAAW,CAAC,CAAA,SAAA,EAAYlD,WAAW,CAACkD,WAAW,CAAC,CAAA,kDAAA,EAAqDG,KAAK,CAACE,OAAO,CAAA,EAAA,EAAKvD,WAAW,CAACkD,WAAW,CAAC,CAAA,gCAAA,EAAmCnD,WAAW,CAACC,WAAW,CAACkD,WAAW,CAAC,CAAC,mBAAmB,CAAC;AAC/a,QAAA;AACJ,MAAA;MAEA,IAAII,GAAG,KAAKJ,WAAW,EAAE;AACrBG,QAAAA,KAAK,CAACG,SAAS,GAAG,OAAOpB,OAAO,KAAK,QAAQ,GAAGA,OAAO,GAAGA,OAAO,CAACiB,KAAK,CAAC;AACxE,QAAA,OAAO,IAAI;AACf,MAAA;AACA,MAAA,OAAO,KAAK;AAChB,IAAA,CAAC,CAAC;AAEF,IAAA,OAAO,IAAI;AACf,EAAA;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"GValidator.development.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
class t{get handlers(){return this._handlers}get constraintHandlers(){return this._constraintHandlers}get asyncHandlers(){return this._asyncHandlers}constructor(t){const n=(null==t?void 0:t.handlers)||[],a=(null==t?void 0:t.constraintHandlers)||[],s=(null==t?void 0:t.asyncHandlers)||[];this._handlers=[].concat(n),this._constraintHandlers=[].concat(a),this._asyncHandlers=[].concat(s),delete this.track}hasConstraint(t){var n;return(null===(n=this.track)||void 0===n?void 0:n.includes(t))||!1}withRequiredMessage(t){return this.__addConstraintValidationHandler("valueMissing",t)}withMaxLengthMessage(t){return this.__addConstraintValidationHandler("tooLong",t)}withMinLengthMessage(t){return this.__addConstraintValidationHandler("tooShort",t)}withPatternMismatchMessage(t){return this.__addConstraintValidationHandler("patternMismatch",t)}withBadInputMessage(t){return this.__addConstraintValidationHandler("badInput",t)}withRangeUnderflowMessage(t){return this.__addConstraintValidationHandler("rangeUnderflow",t)}withRangeOverflowMessage(t){return this.__addConstraintValidationHandler("rangeOverflow",t)}withTypeMismatchMessage(t){return this.__addConstraintValidationHandler("typeMismatch",t)}withStepMismatchMessage(t){return this.__addConstraintValidationHandler("stepMismatch",t)}withCustomValidation(t){return this._handlers.push(t),this}withCustomValidationAsync(t){return this._asyncHandlers.push(t),this}__addConstraintValidationHandler(t,n){const a=(a,s)=>s===t&&(a.errorText="string"==typeof n?n:n(a),!0);return Object.defineProperty(a,"name",{value:`constraintHandler_${t}`}),this._constraintHandlers.push(a),this}}export{t as GValidator};
|
|
2
2
|
//# sourceMappingURL=GValidator.production.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GValidator.production.js","sources":["../../src/validations/GValidator.ts"],"sourcesContent":["import type { GConstraintValidator, GConstraintValidatorHandler, GCustomValidatorHandler, GCustomValidatorHandlerAsync } from \".\";\r\n\r\nexport let handlersMap: { [key: string]: string };\r\nexport let validityMap: { [key in keyof Partial<ValidityState>]: any };\r\n\r\nif (__DEV__) {\r\n handlersMap = {\r\n minLength: 'withMinLengthMessage',\r\n maxLength: 'withMaxLengthMessage',\r\n required: 'withRequiredMessage',\r\n pattern: 'withPatternMismatchMessage',\r\n min: 'withRangeUnderflowMessage',\r\n max: 'withRangeOverflowMessage',\r\n step: 'withStepMismatchMessage',\r\n type: 'withTypeMismatchMessage',\r\n };\r\n validityMap = {\r\n tooShort: 'minLength',\r\n valueMissing: 'required',\r\n tooLong: 'maxLength',\r\n patternMismatch: 'pattern',\r\n rangeOverflow: 'max',\r\n rangeUnderflow: 'min',\r\n stepMismatch: 'step',\r\n typeMismatch: 'type'\r\n };\r\n}\r\n\r\n/**a class for handling validations for input(s)\r\n * @example\r\n * const baseValidator = new GValidator().withRequiredMessage('this field is required');\r\n *\r\n * const validators: GValidators<SignInForm> = {\r\n * username: new GValidator(baseValidator).withMinLengthMessage('...'),\r\n * '*': baseValidator // a default validator for all other fields in the form\r\n * };\r\n */\r\nexport class GValidator<T = any> {\r\n private _handlers: GCustomValidatorHandler<T>[];\r\n private _constraintHandlers: GConstraintValidatorHandler[];\r\n private _asyncHandlers: GCustomValidatorHandlerAsync<T>[];\r\n track?: (keyof ValidityState)[];\r\n\r\n get handlers() {\r\n return this._handlers;\r\n }\r\n\r\n get constraintHandlers() {\r\n return this._constraintHandlers;\r\n }\r\n\r\n get asyncHandlers() {\r\n return this._asyncHandlers;\r\n }\r\n\r\n constructor(baseValidator?: GValidator<T>) {\r\n const baseHandlers = baseValidator?.handlers || [];\r\n const baseConstraintHandlers = baseValidator?.constraintHandlers || [];\r\n const baseHandlersAsync = baseValidator?.asyncHandlers || [];\r\n\r\n this._handlers = [].concat(baseHandlers as any);\r\n this._constraintHandlers = [].concat(baseConstraintHandlers as any);\r\n this._asyncHandlers = [].concat(baseHandlersAsync as any);\r\n\r\n if (__DEV__) {\r\n this.track = [];\r\n if (baseValidator?.track) {\r\n this.track = this.track.concat(baseValidator.track);\r\n }\r\n } else {\r\n delete this.track;\r\n }\r\n }\r\n\r\n public hasConstraint(constraint: keyof ValidityState): boolean {\r\n return this.track?.includes(constraint) || false;\r\n }\r\n\r\n /**register a `valueMissing` violation handler (use this with `required` attribute) */\r\n withRequiredMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('valueMissing', message);\r\n }\r\n\r\n /**register a `tooLong` violation handler (use this with `maxLength` attribute) */\r\n withMaxLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooLong', message);\r\n }\r\n\r\n /**register a `tooShort` violation handler (use this with `minLength` attribute)*/\r\n withMinLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooShort', message);\r\n }\r\n\r\n /**register a `patternMismatch` violation handler (use this with `pattern` attribute)*/\r\n withPatternMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('patternMismatch', message);\r\n }\r\n\r\n /**register a `badInput` violation handler */\r\n withBadInputMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('badInput', message);\r\n }\r\n\r\n /**register a `rangeUnderflow` violation handler (use this with `min` attribute) */\r\n withRangeUnderflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeUnderflow', message);\r\n }\r\n\r\n /**register a `rangeOverflow` violation handler (use this with `max` attribute) */\r\n withRangeOverflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeOverflow', message);\r\n }\r\n\r\n /**\r\n * register a `typeMismatch` violation handler
|
|
1
|
+
{"version":3,"file":"GValidator.production.js","sources":["../../src/validations/GValidator.ts"],"sourcesContent":["import type { GConstraintValidator, GConstraintValidatorHandler, GCustomValidatorHandler, GCustomValidatorHandlerAsync } from \".\";\r\nimport type {GInputState} from \"../fields\";\r\n\r\nexport let handlersMap: { [key: string]: string };\r\nexport let validityMap: { [key in keyof Partial<ValidityState>]: any };\r\n\r\nif (__DEV__) {\r\n handlersMap = {\r\n minLength: 'withMinLengthMessage',\r\n maxLength: 'withMaxLengthMessage',\r\n required: 'withRequiredMessage',\r\n pattern: 'withPatternMismatchMessage',\r\n min: 'withRangeUnderflowMessage',\r\n max: 'withRangeOverflowMessage',\r\n step: 'withStepMismatchMessage',\r\n type: 'withTypeMismatchMessage',\r\n };\r\n validityMap = {\r\n tooShort: 'minLength',\r\n valueMissing: 'required',\r\n tooLong: 'maxLength',\r\n patternMismatch: 'pattern',\r\n rangeOverflow: 'max',\r\n rangeUnderflow: 'min',\r\n stepMismatch: 'step',\r\n typeMismatch: 'type'\r\n };\r\n}\r\n\r\n/**a class for handling validations for input(s)\r\n * @example\r\n * const baseValidator = new GValidator().withRequiredMessage('this field is required');\r\n *\r\n * const validators: GValidators<SignInForm> = {\r\n * username: new GValidator(baseValidator).withMinLengthMessage('...'),\r\n * '*': baseValidator // a default validator for all other fields in the form\r\n * };\r\n */\r\nexport class GValidator<T = any> {\r\n private _handlers: GCustomValidatorHandler<T>[];\r\n private _constraintHandlers: GConstraintValidatorHandler[];\r\n private _asyncHandlers: GCustomValidatorHandlerAsync<T>[];\r\n track?: (keyof ValidityState)[];\r\n\r\n get handlers() {\r\n return this._handlers;\r\n }\r\n\r\n get constraintHandlers() {\r\n return this._constraintHandlers;\r\n }\r\n\r\n get asyncHandlers() {\r\n return this._asyncHandlers;\r\n }\r\n\r\n constructor(baseValidator?: GValidator<T>) {\r\n const baseHandlers = baseValidator?.handlers || [];\r\n const baseConstraintHandlers = baseValidator?.constraintHandlers || [];\r\n const baseHandlersAsync = baseValidator?.asyncHandlers || [];\r\n\r\n this._handlers = [].concat(baseHandlers as any);\r\n this._constraintHandlers = [].concat(baseConstraintHandlers as any);\r\n this._asyncHandlers = [].concat(baseHandlersAsync as any);\r\n\r\n if (__DEV__) {\r\n this.track = [];\r\n if (baseValidator?.track) {\r\n this.track = this.track.concat(baseValidator.track);\r\n }\r\n } else {\r\n delete this.track;\r\n }\r\n }\r\n\r\n public hasConstraint(constraint: keyof ValidityState): boolean {\r\n return this.track?.includes(constraint) || false;\r\n }\r\n\r\n /**register a `valueMissing` violation handler (use this with `required` attribute) */\r\n withRequiredMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('valueMissing', message);\r\n }\r\n\r\n /**register a `tooLong` violation handler (use this with `maxLength` attribute) */\r\n withMaxLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooLong', message);\r\n }\r\n\r\n /**register a `tooShort` violation handler (use this with `minLength` attribute)*/\r\n withMinLengthMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('tooShort', message);\r\n }\r\n\r\n /**register a `patternMismatch` violation handler (use this with `pattern` attribute)*/\r\n withPatternMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('patternMismatch', message);\r\n }\r\n\r\n /**register a `badInput` violation handler */\r\n withBadInputMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('badInput', message);\r\n }\r\n\r\n /**register a `rangeUnderflow` violation handler (use this with `min` attribute) */\r\n withRangeUnderflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeUnderflow', message);\r\n }\r\n\r\n /**register a `rangeOverflow` violation handler (use this with `max` attribute) */\r\n withRangeOverflowMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('rangeOverflow', message);\r\n }\r\n\r\n /**\r\n * register a `typeMismatch` violation handler (use this with `type` attributes like `email` or `url`)\r\n * <br />\r\n * Use the `type` attribute to set the appropriate virtual keyboard (e.g., `type=\"tel\"` for a numeric pad on mobile).\r\n * Since native browser validation for certain types can vary, consider using `pattern` or custom validators for\r\n * consistent cross-browser behavior. Ensure your custom validation is compatible with the native one to avoid\r\n * blocking form submission.\r\n */\r\n withTypeMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('typeMismatch', message);\r\n }\r\n\r\n /**register a `stepMismatch` violation handler (use this with `step` attribute)*/\r\n withStepMismatchMessage(message: string | GConstraintValidator): GValidator<T> {\r\n return this.__addConstraintValidationHandler('stepMismatch', message);\r\n }\r\n\r\n /**register a custom validation handler */\r\n withCustomValidation(handler: GCustomValidatorHandler<T>): GValidator<T> {\r\n this._handlers.push(handler);\r\n return this;\r\n }\r\n /**register a custom validation async handler */\r\n withCustomValidationAsync(handler: GCustomValidatorHandlerAsync<T>): GValidator<T> {\r\n this._asyncHandlers.push(handler);\r\n return this;\r\n }\r\n\r\n private __addConstraintValidationHandler(validityKey: keyof ValidityState, message: string | GConstraintValidator): GValidator<T> {\r\n if (__DEV__ && this.track) {\r\n if (this.track.includes(validityKey)) {\r\n console.warn(`DEV ONLY - [Duplicate Handlers] - handler for '${validityKey}' has already been defined`);\r\n }\r\n this.track.push(validityKey);\r\n }\r\n\r\n const constraintHandler = (input: GInputState<T>, key?: keyof ValidityState) => {\r\n if (__DEV__) {\r\n if (validityKey && validityMap[validityKey] && typeof input[validityMap[validityKey]] === 'undefined') {\r\n console.warn(`DEV ONLY - [Missing Prop] - the input '${input.formKey}' has registered validator for the violation '${validityKey}' but the input hasn't described the constraint '${validityMap[validityKey]}'.\\nadd '${validityMap[validityKey]}' to the input props.\\nexample:\\n<GInput formKey='${input.formKey}' ${validityMap[validityKey]}={...} />\\n\\nor either remove '.${handlersMap[validityMap[validityKey]]}(...)' validation`);\r\n }\r\n }\r\n\r\n if (key === validityKey) {\r\n input.errorText = typeof message === 'string' ? message : message(input);\r\n return true;\r\n }\r\n return false;\r\n };\r\n\r\n Object.defineProperty(constraintHandler, 'name', {value: `constraintHandler_${validityKey}`});\r\n\r\n this._constraintHandlers.push(constraintHandler);\r\n\r\n return this;\r\n }\r\n}"],"names":["GValidator","handlers","this","_handlers","constraintHandlers","_constraintHandlers","asyncHandlers","_asyncHandlers","constructor","baseValidator","baseHandlers","baseConstraintHandlers","baseHandlersAsync","concat","track","hasConstraint","constraint","_this$track","includes","withRequiredMessage","message","__addConstraintValidationHandler","withMaxLengthMessage","withMinLengthMessage","withPatternMismatchMessage","withBadInputMessage","withRangeUnderflowMessage","withRangeOverflowMessage","withTypeMismatchMessage","withStepMismatchMessage","withCustomValidation","handler","push","withCustomValidationAsync","validityKey","constraintHandler","input","key","errorText","Object","defineProperty","value"],"mappings":"AAsCO,MAAMA,EAMLC,eACA,OAAOC,KAAKC,SAChB,CAEIC,yBACA,OAAOF,KAAKG,mBAChB,CA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