gform-react 1.5.1 → 1.9.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/README.md +2 -1
- package/dist/cjs/gform-react.development.js +48 -28
- 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.production.js +1 -1
- package/dist/esm/GForm.production.js.map +1 -1
- package/dist/esm/GInput.production.js +1 -1
- package/dist/esm/GInput.production.js.map +1 -1
- package/dist/esm/index.development.js +48 -28
- package/dist/esm/index.development.js.map +1 -1
- package/dist/esm/shared.production.js +1 -1
- package/dist/esm/shared.production.js.map +1 -1
- package/dist/index.d.ts +28 -11
- package/native/dist/cjs/gform-react-native.development.js +471 -0
- package/native/dist/cjs/gform-react-native.development.js.map +1 -0
- package/native/dist/cjs/gform-react-native.production.js +2 -0
- package/native/dist/cjs/gform-react-native.production.js.map +1 -0
- package/native/dist/cjs/index.js +7 -0
- package/native/dist/esm/RNGForm.production.js +2 -0
- package/native/dist/esm/RNGForm.production.js.map +1 -0
- package/native/dist/esm/RNGInput.production.js +2 -0
- package/native/dist/esm/RNGInput.production.js.map +1 -0
- package/native/dist/esm/index.development.js +461 -0
- package/native/dist/esm/index.development.js.map +1 -0
- package/native/dist/esm/index.js +2 -0
- package/native/dist/esm/shared.production.js +2 -0
- package/native/dist/esm/shared.production.js.map +1 -0
- package/native/dist/index.d.ts +77 -0
- package/native/package.json +15 -0
- package/package.json +33 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.production.js","sources":["../../src/helpers.ts","../../src/context.ts"],"sourcesContent":["import type { GInputInitialState, GInputProps, GInputState } from './fields';\r\nimport type { GChangeEvent, GDOMElement, IForm } from './form';\r\nimport type { GFormState, InitialState, RawData, ToFormDataOptions, ToRawDataOptions, ToURLSearchParamsOptions } from './state';\r\n\r\nexport const isObject = (o: any): o is object => (o && typeof o === 'object' && !Array.isArray(o));\r\n\r\nconst defaultFieldProps: { [key: string]: { value: string | number | boolean } } = {\r\n text: { value: '' },\r\n checkbox: { value: false },\r\n number: { value: 0 }\r\n};\r\n\r\nconst typeValueDict: { [key: string]: keyof HTMLFormElement | GDOMElement } = {\r\n checkbox: 'checked',\r\n number: 'valueAsNumber',\r\n};\r\n\r\nconst generateId = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);\r\n\r\nexport const _buildFormInitialValues = <T>(rows: JSX.Element | JSX.Element[] = []) => {\r\n const fields: { [key: string]: GInputInitialState } = {};\r\n\r\n if (!Array.isArray(rows)) rows = [rows];\r\n\r\n if (__DEBUG__) {\r\n console.log('[buildFormInitialValues] -', 'building initial values for ', rows);\r\n }\r\n\r\n rows.forEach(row => {\r\n const inputConfigs = _findInputs(row);\r\n\r\n inputConfigs.forEach(config => {\r\n if (__DEBUG__) {\r\n console.log('[buildFormInitialValues] -', 'building input', `(${config.formKey})`, config);\r\n }\r\n\r\n if (__DEV__ && fields[config.formKey]) {\r\n console.warn(`[Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);\r\n }\r\n\r\n const { required = false, max, maxLength, min, minLength, step, pattern, type = 'text', defaultValue, value, checked, defaultChecked, formKey, debounce, validatorKey } = config;\r\n const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;\r\n const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;\r\n\r\n fields[config.formKey] = {\r\n formKey,\r\n type,\r\n required,\r\n max,\r\n maxLength,\r\n min,\r\n minLength,\r\n step,\r\n pattern,\r\n value: inputValue,\r\n validatorKey,\r\n debounce,\r\n dirty: false,\r\n touched: false,\r\n gid: generateId()\r\n };\r\n\r\n Object.keys(fields[config.formKey]).forEach(key => {\r\n if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];\r\n });\r\n });\r\n });\r\n return { state: { fields, loading: false } as InitialState<T>, key: generateId() };\r\n};\r\n\r\nconst _findInputs = (root?: JSX.Element | JSX.Element[] | undefined[], total: GInputProps[] = []): GInputProps[] => {\r\n if (!root) return total;\r\n\r\n if (Array.isArray(root)) {\r\n root.forEach(element => _findInputs(element, total));\r\n return total;\r\n }\r\n\r\n if (root.props?.formKey) {\r\n if (__DEBUG__) {\r\n console.log('[findInputs] -', 'input config found', `(${root.props.formKey})`);\r\n }\r\n total.push(root.props);\r\n return total;\r\n }\r\n\r\n return _findInputs(root.props?.children, total);\r\n};\r\n\r\nexport const _findValidityKey = (validity: ValidityState): keyof ValidityState | undefined => {\r\n for (const key in validity) {\r\n if (key !== 'valid' && validity[key as keyof ValidityState]) {\r\n if (__DEBUG__) {\r\n console.log('[findValidityKey] -', 'found validity key:', key);\r\n }\r\n return key as keyof ValidityState;\r\n }\r\n }\r\n};\r\n\r\nexport const hasSubmitter = (form?: HTMLFormElement | null): boolean => {\r\n if (!form) return false;\r\n\r\n for (const element of form) {\r\n if ((element as HTMLInputElement).type === 'submit') return true;\r\n }\r\n\r\n return false;\r\n};\r\n\r\nexport const _checkIfFormIsValid = <T>(fields: IForm<T>): boolean => {\r\n for (const f in fields) {\r\n if (fields[f].error) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n};\r\n\r\nexport const _toRawData = <T>(fields: IForm<T> & { [key: string]: GInputState<any> }, options: ToRawDataOptions<T> = {}): RawData<T> => {\r\n const data: { [key: string]: unknown } = {};\r\n\r\n const { include, exclude, transform } = options;\r\n\r\n if (include) {\r\n include.forEach(key => data[key as string] = fields[key]?.value);\r\n } else for (const f in fields) {\r\n data[f] = fields[f].value;\r\n }\r\n\r\n exclude?.forEach(key => delete data[key as string]);\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n data[key] = set(fields[key]?.value);\r\n }\r\n }\r\n\r\n return data as RawData<T>;\r\n};\r\n\r\nexport const _toFormData = <T>(form: HTMLFormElement | null, options?: ToFormDataOptions<T>): FormData => {\r\n if (!form) return new FormData();\r\n\r\n if (options) {\r\n const { exclude, include, transform } = options;\r\n let formData: FormData;\r\n\r\n if (include) {\r\n formData = new FormData();\r\n include.forEach(key => formData.set(key as string, form[key as string]?.value));\r\n } else {\r\n formData = new FormData(form);\r\n exclude?.forEach(key => formData.delete(key as string));\r\n }\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n formData.set(key, set(form[key]?.value));\r\n }\r\n }\r\n return formData;\r\n\r\n }\r\n\r\n return new FormData(form);\r\n};\r\n\r\nexport function _toURLSearchParams<T>(this: GFormState<T>, options?: ToURLSearchParamsOptions<T>): URLSearchParams {\r\n let data: Record<keyof T, any>;\r\n if (options) {\r\n const { exclude, include, transform } = options;\r\n if (include) {\r\n data = {} as Record<keyof T, any>;\r\n include.forEach(key => (data[key] = this[key]?.value));\r\n } else {\r\n data = this.toRawData();\r\n exclude?.forEach(key => delete data[key]);\r\n }\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n (data[key] = set(this[key]?.value));\r\n }\r\n }\r\n }\r\n else data = this.toRawData();\r\n\r\n return new URLSearchParams(data); // this is ok because URLSearchParams will stringify the values (boolean/number)\r\n}\r\n\r\nfunction __debounce(this: { [key: string]: { timerId: NodeJS.Timeout } }, timeout: number, id: string): Promise<void> {\r\n return new Promise(resolve => {\r\n if (this[id]?.timerId)\r\n clearTimeout(this[id].timerId);\r\n\r\n const timerId = setTimeout(() => resolve(), timeout);\r\n\r\n if (this[id]) {\r\n this[id].timerId = timerId;\r\n } else this[id] = { timerId };\r\n });\r\n}\r\n\r\nexport const _debounce = __debounce.bind({});\r\n\r\nexport const _extractValue = <T>(e: GChangeEvent<GDOMElement | HTMLFormElement>, unknown?: { value: T } | string | number): undefined | string | number | boolean | T => {\r\n if (e.target) {\r\n if (Object.hasOwn(typeValueDict, e.target.type)) return e.target[typeValueDict[e.target.type] as 'value'];\r\n return e.target.value;\r\n }\r\n return (e.value as T) || (isObject(unknown) ? unknown.value : unknown);\r\n};\r\n\r\nexport const _checkResult = (handlerResult: boolean | RegExp, value: string | number | boolean): boolean => typeof handlerResult === 'boolean' ? handlerResult : typeof value === 'string' ? !handlerResult.test(value) : false;\r\n\r\nexport const _merge = <T extends object>(target: { [key: string]: any } = {}, ...nodes: ({ [key: string]: any } | undefined | void)[]): T => {\r\n if (!nodes.length) return target as T;\r\n\r\n const next = nodes.shift();\r\n if (isObject(next)) {\r\n for (const key in next) {\r\n target[key] = target[key] ? { ...target[key], ...next[key] } : next[key];\r\n }\r\n }\r\n\r\n return _merge(target, ...nodes);\r\n};","import { createContext, useContext } from \"react\";\r\nimport { useForm } from \"./useForm\";\r\n\r\nexport type GContext<T = any> = ReturnType<typeof useForm<T>>;\r\n\r\nconst gFormContext = createContext<GContext>({\r\n state: {\r\n fields: {},\r\n loading: false\r\n },\r\n _updateInputHandler: () => null,\r\n _validateInputHandler: () => null,\r\n _dispatchChanges: () => null,\r\n optimized: false,\r\n key: ''\r\n});\r\n\r\nexport const useGenericFormContext = () => useContext<GContext>(gFormContext);\r\n\r\nexport const GFormContextProvider = gFormContext.Provider;"],"names":["isObject","o","Array","isArray","defaultFieldProps","text","value","checkbox","number","typeValueDict","generateId","Date","toString","Math","random","substring","_buildFormInitialValues","rows","fields","forEach","row","_findInputs","config","required","max","maxLength","min","minLength","step","pattern","type","defaultValue","checked","defaultChecked","formKey","debounce","validatorKey","inputValue","dirty","touched","gid","Object","keys","key","state","loading","root","total","_root$props","_root$props2","element","props","push","children","_findValidityKey","validity","_checkIfFormIsValid","f","error","_toRawData","options","data","include","exclude","transform","_fields$key","_fields$key2","set","_toFormData","form","FormData","formData","_form","delete","_form$key","_toURLSearchParams","_this$key","this","toRawData","_this$key2","URLSearchParams","_debounce","timeout","id","Promise","resolve","_this$id","timerId","clearTimeout","setTimeout","bind","_extractValue","e","unknown","target","hasOwn","_checkResult","handlerResult","test","_merge","nodes","length","next","shift","_objectSpread","gFormContext","createContext","_updateInputHandler","_validateInputHandler","_dispatchChanges","optimized","useGenericFormContext","useContext","GFormContextProvider","Provider"],"mappings":"8GAIO,MAAMA,EAAYC,GAAyBA,GAAkB,iBAANA,IAAmBC,MAAMC,QAAQF,GAEzFG,EAA6E,CAC/EC,KAAM,CAAEC,MAAO,IACfC,SAAU,CAAED,OAAO,GACnBE,OAAQ,CAAEF,MAAO,IAGfG,EAAwE,CAC1EF,SAAU,UACVC,OAAQ,iBAGNE,EAAaA,MAAQ,IAAIC,MAAQC,SAAS,KAAO,EAAIC,KAAKC,UAAUF,SAAS,IAAIG,UAAU,EAAG,IAEvFC,EAA0BA,CAAIC,EAAoC,MAC3E,MAAMC,EAAgD,CAAA,EA+CtD,OA7CKhB,MAAMC,QAAQc,KAAOA,EAAO,CAACA,IAMlCA,EAAKE,SAAQC,IACYC,EAAYD,GAEpBD,SAAQG,IASjB,MAAMC,SAAEA,GAAW,EAAKC,IAAEA,EAAGC,UAAEA,EAASC,IAAEA,EAAGC,UAAEA,EAASC,KAAEA,EAAIC,QAAEA,EAAOC,KAAEA,EAAO,OAAMC,aAAEA,EAAYzB,MAAEA,EAAK0B,QAAEA,EAAOC,eAAEA,EAAcC,QAAEA,EAAOC,SAAEA,EAAQC,aAAEA,GAAiBd,EAEpKe,EAAa/B,GAASyB,GAAgBC,GAAWC,IADlC7B,EAAkB0B,IAAS1B,EAAkBC,MACoBC,MAEtFY,EAAOI,EAAOY,SAAW,CACrBA,UACAJ,OACAP,WACAC,MACAC,YACAC,MACAC,YACAC,OACAC,UACAvB,MAAO+B,EACPD,eACAD,WACAG,OAAO,EACPC,SAAS,EACTC,IAAK9B,KAGT+B,OAAOC,KAAKxB,EAAOI,EAAOY,UAAUf,SAAQwB,SACG,IAAhCzB,EAAOI,EAAOY,SAASS,WAA6BzB,EAAOI,EAAOY,SAASS,EAAI,GAC5F,GACJ,IAEC,CAAEC,MAAO,CAAE1B,SAAQ2B,SAAS,GAA4BF,I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A,GAARA,EAAUC,SACVC,aAAaX,KAAKM,GAAII,SAE1B,MAAMA,EAAUE,YAAW,IAAMJ,KAAWH,GAExCL,KAAKM,GACLN,KAAKM,GAAII,QAAUA,EAChBV,KAAKM,GAAM,CAAEI,UAAS,GAErC,EAEoCG,KAAK,IAE5BC,EAAgBA,CAAIC,EAAgDC,IACzED,EAAEE,OACErD,OAAOsD,OAAOtF,EAAemF,EAAEE,OAAOhE,MAAc8D,EAAEE,OAAOrF,EAAcmF,EAAEE,OAAOhE,OACjF8D,EAAEE,OAAOxF,MAEZsF,EAAEtF,QAAgBN,EAAS6F,GAAWA,EAAQvF,MAAQuF,GAGrDG,EAAeA,CAACC,EAAiC3F,IAAuE,kBAAlB2F,EAA8BA,EAAiC,iBAAV3F,IAAsB2F,EAAcC,KAAK5F,GAEpM6F,EAASA,CAAmBL,EAAiC,CAAE,KAAKM,KAC7E,IAAKA,EAAMC,OAAQ,OAAOP,EAE1B,MAAMQ,EAAOF,EAAMG,QACnB,GAAIvG,EAASsG,GACT,IAAK,MAAM3D,KAAO2D,EACdR,EAAOnD,GAAOmD,EAAOnD,GAAI6D,EAAAA,EAAA,CAAA,EAAQV,EAAOnD,IAAS2D,EAAK3D,IAAS2D,EAAK3D,GAI5E,OAAOwD,EAAOL,KAAWM,EAAM,EChO7BK,EAAeC,EAAwB,CACzC9D,MAAO,CACH1B,OAAQ,CAAE,EACV2B,SAAS,GAEb8D,EAAqBA,IAAM,KAC3BC,EAAuBA,IAAM,KAC7BC,EAAkBA,IAAM,KACxBC,WAAW,EACXnE,IAAK,KAGIoE,EAAwBA,IAAMC,EAAqBP,GAEnDQ,EAAuBR,EAAaS"}
|
|
1
|
+
{"version":3,"file":"shared.production.js","sources":["../../src/helpers.ts","../../src/context.ts"],"sourcesContent":["import type { GInputInitialState, GInputProps, GInputState, GInputStateMutable } from './fields';\r\nimport type { GChangeEvent, GDOMElement, IForm } from './form';\r\nimport type { GFormState, InitialState, RawData, ToFormDataOptions, ToRawDataOptions, ToURLSearchParamsOptions } from './state';\r\n\r\nexport const isObject = (o: any): o is object => (o && typeof o === 'object' && !Array.isArray(o));\r\n\r\nconst defaultFieldProps: { [key: string]: { value: string | number | boolean } } = {\r\n text: { value: '' },\r\n checkbox: { value: false },\r\n number: { value: 0 }\r\n};\r\n\r\nconst typeValueDict: { [key: string]: keyof HTMLFormElement | GDOMElement } = {\r\n checkbox: 'checked',\r\n number: 'valueAsNumber',\r\n};\r\n\r\nconst generateId = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);\r\n\r\nexport const _buildFormInitialValues = <T>(rows: JSX.Element | JSX.Element[] = []) => {\r\n const fields: { [key: string]: GInputInitialState } = {};\r\n\r\n if (!Array.isArray(rows)) rows = [rows];\r\n\r\n if (__DEBUG__) {\r\n console.log('[buildFormInitialValues] -', 'building initial values for ', rows);\r\n }\r\n\r\n rows.forEach(row => {\r\n const inputConfigs = _findInputs(row);\r\n\r\n inputConfigs.forEach(config => {\r\n if (__DEBUG__) {\r\n console.log('[buildFormInitialValues] -', 'building input', `(${config.formKey})`, config);\r\n }\r\n\r\n if (__DEV__ && fields[config.formKey]) {\r\n console.warn(`[Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);\r\n }\r\n\r\n const { required = false, max, maxLength, min, minLength, step, pattern, type = 'text', defaultValue, value, checked, defaultChecked, formKey, debounce, validatorKey } = config;\r\n const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;\r\n const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;\r\n\r\n fields[config.formKey] = {\r\n formKey,\r\n type,\r\n required,\r\n max,\r\n maxLength,\r\n min,\r\n minLength,\r\n step,\r\n pattern,\r\n value: inputValue,\r\n validatorKey,\r\n debounce,\r\n dirty: false,\r\n touched: false,\r\n gid: generateId()\r\n };\r\n\r\n Object.keys(fields[config.formKey]).forEach(key => {\r\n if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];\r\n });\r\n });\r\n });\r\n return { state: { fields, loading: false } as InitialState<T>, key: generateId() };\r\n};\r\n\r\nconst _findInputs = (root?: JSX.Element | JSX.Element[] | undefined[], total: (GInputProps & GInputStateMutable)[] = []): (GInputProps & GInputStateMutable)[] => {\r\n if (!root) return total;\r\n\r\n if (Array.isArray(root)) {\r\n root.forEach(element => _findInputs(element, total));\r\n return total;\r\n }\r\n\r\n if (root.props?.formKey) {\r\n if (__DEBUG__) {\r\n console.log('[findInputs] -', 'input config found', `(${root.props.formKey})`);\r\n }\r\n total.push(root.props);\r\n return total;\r\n }\r\n\r\n return _findInputs(root.props?.children, total);\r\n};\r\n\r\nexport const _findValidityKey = (validity: Partial<ValidityState>): keyof ValidityState | undefined => {\r\n for (const key in validity) {\r\n if (key !== 'valid' && validity[key as keyof ValidityState]) {\r\n if (__DEBUG__) {\r\n console.log('[findValidityKey] -', 'found validity key:', key);\r\n }\r\n return key as keyof ValidityState;\r\n }\r\n }\r\n};\r\n\r\nexport const hasSubmitter = (form?: HTMLFormElement | null): boolean => {\r\n if (!form) return false;\r\n\r\n for (const element of form) {\r\n if ((element as HTMLInputElement).type === 'submit') return true;\r\n }\r\n\r\n return false;\r\n};\r\n\r\nexport const _checkIfFormIsValid = <T>(fields: IForm<T>): boolean => {\r\n for (const f in fields) {\r\n if (fields[f].error) {\r\n return false;\r\n }\r\n }\r\n return true;\r\n};\r\n\r\nexport const _toRawData = <T>(fields: IForm<T> & { [key: string]: GInputState<any> }, options: ToRawDataOptions<T> = {}): RawData<T> => {\r\n const data: { [key: string]: unknown } = {};\r\n\r\n const { include, exclude, transform } = options;\r\n\r\n if (include) {\r\n include.forEach(key => data[key as string] = fields[key]?.value);\r\n } else for (const f in fields) {\r\n data[f] = fields[f].value;\r\n }\r\n\r\n exclude?.forEach(key => delete data[key as string]);\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n data[key] = set(fields[key]?.value);\r\n }\r\n }\r\n\r\n return data as RawData<T>;\r\n};\r\n\r\nexport const _toFormData = <T>(form: HTMLFormElement | null, options?: ToFormDataOptions<T>): FormData => {\r\n if (!form) return new FormData();\r\n\r\n if (options) {\r\n const { exclude, include, transform } = options;\r\n let formData: FormData;\r\n\r\n if (include) {\r\n formData = new FormData();\r\n include.forEach(key => formData.set(key as string, form[key as string]?.value));\r\n } else {\r\n formData = new FormData(form);\r\n exclude?.forEach(key => formData.delete(key as string));\r\n }\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n formData.set(key, set(form[key]?.value));\r\n }\r\n }\r\n return formData;\r\n\r\n }\r\n\r\n return new FormData(form);\r\n};\r\n\r\nexport function _toURLSearchParams<T>(this: GFormState<T>, options?: ToURLSearchParamsOptions<T>): URLSearchParams {\r\n let data: Record<keyof T, any>;\r\n if (options) {\r\n const { exclude, include, transform } = options;\r\n if (include) {\r\n data = {} as Record<keyof T, any>;\r\n include.forEach(key => (data[key] = this[key]?.value));\r\n } else {\r\n data = this.toRawData();\r\n exclude?.forEach(key => delete data[key]);\r\n }\r\n\r\n if (transform) {\r\n for (const key in transform) {\r\n const set = transform[key] as (value: GFormState<T>[typeof key]['value']) => any;\r\n (data[key] = set(this[key]?.value));\r\n }\r\n }\r\n }\r\n else data = this.toRawData();\r\n\r\n return new URLSearchParams(data); // this is ok because URLSearchParams will stringify the values (boolean/number)\r\n}\r\n\r\nfunction __debounce(this: { [key: string]: { timerId: NodeJS.Timeout } }, timeout: number, id: string): Promise<void> {\r\n return new Promise(resolve => {\r\n if (this[id]?.timerId)\r\n clearTimeout(this[id].timerId);\r\n\r\n const timerId = setTimeout(() => resolve(), timeout);\r\n\r\n if (this[id]) {\r\n this[id].timerId = timerId;\r\n } else this[id] = { timerId };\r\n });\r\n}\r\n\r\nexport const _debounce = __debounce.bind({});\r\n\r\nexport const _extractValue = <T>(e?: GChangeEvent<GDOMElement | HTMLFormElement>, unknown?: { value: T } | string | number): undefined | string | number | boolean | T => {\r\n if (e?.target) {\r\n if (Object.hasOwn(typeValueDict, e.target.type)) return e.target[typeValueDict[e.target.type] as 'value'];\r\n return e.target.value;\r\n }\r\n return (e?.value as T) || (isObject(unknown) ? unknown.value : unknown);\r\n};\r\n\r\nexport const _checkResult = (handlerResult: boolean | RegExp | string, value: string | number | boolean): boolean => typeof handlerResult === 'boolean' ? handlerResult : typeof value === 'string' ? typeof handlerResult === 'string' ? !new RegExp(handlerResult).test(value) : !handlerResult.test(value) : false;\r\n\r\nexport const _merge = <T extends object>(target: { [key: string]: any } = {}, ...nodes: ({ [key: string]: any } | undefined | void)[]): T => {\r\n if (!nodes.length) return target as T;\r\n\r\n const next = nodes.shift();\r\n if (isObject(next)) {\r\n for (const key in next) {\r\n target[key] = target[key] ? { ...target[key], ...next[key] } : next[key];\r\n }\r\n }\r\n\r\n return _merge(target, ...nodes);\r\n};","import { createContext, useContext } from \"react\";\r\nimport { useForm } from \"./useForm\";\r\n\r\nexport type GContext<T = any> = ReturnType<typeof useForm<T>>;\r\n\r\nconst gFormContext = createContext<GContext>({\r\n state: {\r\n fields: {},\r\n loading: false\r\n },\r\n _updateInputHandler: () => null,\r\n _viHandler: () => null,\r\n _dispatchChanges: () => null,\r\n _createInputChecker: () => false,\r\n optimized: false,\r\n key: ''\r\n});\r\n\r\nexport const useGenericFormContext = () => useContext<GContext>(gFormContext);\r\n\r\nexport const GFormContextProvider = gFormContext.Provider;"],"names":["isObject","o","Array","isArray","defaultFieldProps","text","value","checkbox","number","typeValueDict","generateId","Date","toString","Math","random","substring","_buildFormInitialValues","rows","fields","forEach","row","_findInputs","config","required","max","maxLength","min","minLength","step","pattern","type","defaultValue","checked","defaultChecked","formKey","debounce","validatorKey","inputValue","dirty","touched","gid","Object","keys","key","state","loading","root","total","_root$props","_root$props2","element","props","push","children","_findValidityKey","validity","_checkIfFormIsValid","f","error","_toRawData","options","data","include","exclude","transform","_fields$key","_fields$key2","set","_toFormData","form","FormData","formData","_form","delete","_form$key","_toURLSearchParams","_this$key","this","toRawData","_this$key2","URLSearchParams","_debounce","timeout","id","Promise","resolve","_this$id","timerId","clearTimeout","setTimeout","bind","_extractValue","e","unknown","target","hasOwn","_checkResult","handlerResult","RegExp","test","_merge","nodes","length","next","shift","_objectSpread","gFormContext","createContext","_updateInputHandler","_viHandler","_dispatchChanges","_createInputChecker","optimized","useGenericFormContext","useContext","GFormContextProvider","Provider"],"mappings":"8GAIO,MAAMA,EAAYC,GAAyBA,GAAkB,iBAANA,IAAmBC,MAAMC,QAAQF,GAEzFG,EAA6E,CAC/EC,KAAM,CAAEC,MAAO,IACfC,SAAU,CAAED,OAAO,GACnBE,OAAQ,CAAEF,MAAO,IAGfG,EAAwE,CAC1EF,SAAU,UACVC,OAAQ,iBAGNE,EAAaA,MAAQ,IAAIC,MAAQC,SAAS,KAAO,EAAIC,KAAKC,UAAUF,SAAS,IAAIG,UAAU,EAAG,IAEvFC,EAA0BA,CAAIC,EAAoC,MAC3E,MAAMC,EAAgD,CAAA,EA+CtD,OA7CKhB,MAAMC,QAAQc,KAAOA,EAAO,CAACA,IAMlCA,EAAKE,SAAQC,IACYC,EAAYD,GAEpBD,SAAQG,IASjB,MAAMC,SAAEA,GAAW,EAAKC,IAAEA,EAAGC,UAAEA,EAASC,IAAEA,EAAGC,UAAEA,EAASC,KAAEA,EAAIC,QAAEA,EAAOC,KAAEA,EAAO,OAAMC,aAAEA,EAAYzB,MAAEA,EAAK0B,QAAEA,EAAOC,eAAEA,EAAcC,QAAEA,EAAOC,SAAEA,EAAQC,aAAEA,GAAiBd,EAEpKe,EAAa/B,GAASyB,GAAgBC,GAAWC,IADlC7B,EAAkB0B,IAAS1B,EAAkBC,MACoBC,MAEtFY,EAAOI,EAAOY,SAAW,CACrBA,UACAJ,OACAP,WACAC,MACAC,YACAC,MACAC,YACAC,OACAC,UACAvB,MAAO+B,EACPD,eACAD,WACAG,OAAO,EACPC,SAAS,EACTC,IAAK9B,KAGT+B,OAAOC,KAAKxB,E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AAK,KAGIsE,EAAwBA,IAAMC,EAAqBR,GAEnDS,EAAuBT,EAAaU"}
|
package/dist/index.d.ts
CHANGED
|
@@ -35,9 +35,23 @@ export type BaseGenericFieldProps = {
|
|
|
35
35
|
/** refer to another input validator */
|
|
36
36
|
validatorKey?: string;
|
|
37
37
|
type?: HTMLInputTypeAttribute | undefined;
|
|
38
|
+
|
|
39
|
+
required?: boolean;
|
|
40
|
+
max?: number;
|
|
41
|
+
maxLength?: number;
|
|
42
|
+
min?: number;
|
|
43
|
+
minLength?: number;
|
|
44
|
+
checked?: boolean;
|
|
45
|
+
step?: number;
|
|
46
|
+
pattern?: string | RegExp;
|
|
38
47
|
};
|
|
39
48
|
|
|
40
|
-
export type GElementProps = Omit<InputHTMLAttributes<any>, 'color' | 'size' | 'onChange' | '
|
|
49
|
+
export type GElementProps<T> = Omit<InputHTMLAttributes<any>, 'color' | 'size' | 'onChange' | 'min' | 'max' | 'step'> & {
|
|
50
|
+
value: T;
|
|
51
|
+
step?: number
|
|
52
|
+
max?: number;
|
|
53
|
+
min?: number;
|
|
54
|
+
};
|
|
41
55
|
|
|
42
56
|
type PartialPick<T, P extends keyof T> = Omit<T, P> & Partial<Pick<T, P>>;
|
|
43
57
|
|
|
@@ -68,13 +82,6 @@ export type GInputInitialState = BaseGenericFieldProps & {
|
|
|
68
82
|
|
|
69
83
|
export type GInputState<T = string | number | boolean> = GInputInitialState & {
|
|
70
84
|
value: T;
|
|
71
|
-
required?: boolean;
|
|
72
|
-
max?: number | string | undefined;
|
|
73
|
-
maxLength?: number | undefined;
|
|
74
|
-
min?: number | string | undefined;
|
|
75
|
-
minLength?: number | undefined;
|
|
76
|
-
checked?: boolean | undefined;
|
|
77
|
-
step?: number | string | undefined;
|
|
78
85
|
error: boolean;
|
|
79
86
|
errorText: string;
|
|
80
87
|
checkValidity(): boolean;
|
|
@@ -82,11 +89,20 @@ export type GInputState<T = string | number | boolean> = GInputInitialState & {
|
|
|
82
89
|
debounce?: number;
|
|
83
90
|
};
|
|
84
91
|
|
|
85
|
-
export type GInputStateMutable<T=any> = GInputState<T> & {
|
|
92
|
+
export type GInputStateMutable<T = any> = GInputState<T> & {
|
|
93
|
+
max?: number;
|
|
94
|
+
maxLength?: number;
|
|
95
|
+
min?: number;
|
|
96
|
+
minLength?: number;
|
|
97
|
+
checked?: boolean;
|
|
98
|
+
step?: number;
|
|
99
|
+
pattern?: string | RegExp;
|
|
100
|
+
[key: string]: any;
|
|
101
|
+
};
|
|
86
102
|
|
|
87
|
-
type GInputElementHandler<T> = (input: GInputStateMutable<T>, props: GElementProps) => JSX.Element;
|
|
103
|
+
type GInputElementHandler<T> = (input: GInputStateMutable<T>, props: GElementProps<T>) => JSX.Element;
|
|
88
104
|
|
|
89
|
-
export type GInputProps = BaseGenericFieldProps & Omit<InputHTMLAttributes<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>, 'value'> & {
|
|
105
|
+
export type GInputProps = BaseGenericFieldProps & Omit<InputHTMLAttributes<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>, 'value' | 'step' | 'min' | 'max' | 'minLength' | 'maxLength'> & {
|
|
90
106
|
defaultChecked?: boolean;
|
|
91
107
|
defaultValue?: string | number;
|
|
92
108
|
checked?: boolean;
|
|
@@ -243,6 +259,7 @@ export type GFormProps<T> = Omit<HTMLAttributes<HTMLFormElement>, 'onSubmit' | '
|
|
|
243
259
|
* @optional
|
|
244
260
|
*/
|
|
245
261
|
optimized?: boolean;
|
|
262
|
+
ref?: MutableRefObject<HTMLFormElement | null>;
|
|
246
263
|
};
|
|
247
264
|
|
|
248
265
|
/**
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _extends = require('@babel/runtime/helpers/extends');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var reactNative = require('react-native');
|
|
6
|
+
|
|
7
|
+
const isObject = o => o && typeof o === 'object' && !Array.isArray(o);
|
|
8
|
+
const defaultFieldProps = {
|
|
9
|
+
text: {
|
|
10
|
+
value: ''
|
|
11
|
+
},
|
|
12
|
+
checkbox: {
|
|
13
|
+
value: false
|
|
14
|
+
},
|
|
15
|
+
number: {
|
|
16
|
+
value: 0
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const typeValueDict = {
|
|
20
|
+
checkbox: 'checked',
|
|
21
|
+
number: 'valueAsNumber'
|
|
22
|
+
};
|
|
23
|
+
const generateId = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);
|
|
24
|
+
const _buildFormInitialValues = (rows = []) => {
|
|
25
|
+
const fields = {};
|
|
26
|
+
if (!Array.isArray(rows)) rows = [rows];
|
|
27
|
+
rows.forEach(row => {
|
|
28
|
+
const inputConfigs = _findInputs(row);
|
|
29
|
+
inputConfigs.forEach(config => {
|
|
30
|
+
if (fields[config.formKey]) {
|
|
31
|
+
console.warn(`[Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);
|
|
32
|
+
}
|
|
33
|
+
const {
|
|
34
|
+
required = false,
|
|
35
|
+
max,
|
|
36
|
+
maxLength,
|
|
37
|
+
min,
|
|
38
|
+
minLength,
|
|
39
|
+
step,
|
|
40
|
+
pattern,
|
|
41
|
+
type = 'text',
|
|
42
|
+
defaultValue,
|
|
43
|
+
value,
|
|
44
|
+
checked,
|
|
45
|
+
defaultChecked,
|
|
46
|
+
formKey,
|
|
47
|
+
debounce,
|
|
48
|
+
validatorKey
|
|
49
|
+
} = config;
|
|
50
|
+
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
51
|
+
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
52
|
+
fields[config.formKey] = {
|
|
53
|
+
formKey,
|
|
54
|
+
type,
|
|
55
|
+
required,
|
|
56
|
+
max,
|
|
57
|
+
maxLength,
|
|
58
|
+
min,
|
|
59
|
+
minLength,
|
|
60
|
+
step,
|
|
61
|
+
pattern,
|
|
62
|
+
value: inputValue,
|
|
63
|
+
validatorKey,
|
|
64
|
+
debounce,
|
|
65
|
+
dirty: false,
|
|
66
|
+
touched: false,
|
|
67
|
+
gid: generateId()
|
|
68
|
+
};
|
|
69
|
+
Object.keys(fields[config.formKey]).forEach(key => {
|
|
70
|
+
if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
state: {
|
|
76
|
+
fields,
|
|
77
|
+
loading: false
|
|
78
|
+
},
|
|
79
|
+
key: generateId()
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const _findInputs = (root, total = []) => {
|
|
83
|
+
var _root$props, _root$props2;
|
|
84
|
+
if (!root) return total;
|
|
85
|
+
if (Array.isArray(root)) {
|
|
86
|
+
root.forEach(element => _findInputs(element, total));
|
|
87
|
+
return total;
|
|
88
|
+
}
|
|
89
|
+
if ((_root$props = root.props) !== null && _root$props !== void 0 && _root$props.formKey) {
|
|
90
|
+
total.push(root.props);
|
|
91
|
+
return total;
|
|
92
|
+
}
|
|
93
|
+
return _findInputs((_root$props2 = root.props) === null || _root$props2 === void 0 ? void 0 : _root$props2.children, total);
|
|
94
|
+
};
|
|
95
|
+
const _findValidityKey = validity => {
|
|
96
|
+
for (const key in validity) {
|
|
97
|
+
if (key !== 'valid' && validity[key]) {
|
|
98
|
+
return key;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const _checkIfFormIsValid = fields => {
|
|
103
|
+
for (const f in fields) {
|
|
104
|
+
if (fields[f].error) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
};
|
|
110
|
+
const _toRawData = (fields, options = {}) => {
|
|
111
|
+
const data = {};
|
|
112
|
+
const {
|
|
113
|
+
include,
|
|
114
|
+
exclude,
|
|
115
|
+
transform
|
|
116
|
+
} = options;
|
|
117
|
+
if (include) {
|
|
118
|
+
include.forEach(key => {
|
|
119
|
+
var _fields$key;
|
|
120
|
+
return data[key] = (_fields$key = fields[key]) === null || _fields$key === void 0 ? void 0 : _fields$key.value;
|
|
121
|
+
});
|
|
122
|
+
} else for (const f in fields) {
|
|
123
|
+
data[f] = fields[f].value;
|
|
124
|
+
}
|
|
125
|
+
exclude === null || exclude === void 0 || exclude.forEach(key => delete data[key]);
|
|
126
|
+
if (transform) {
|
|
127
|
+
for (const key in transform) {
|
|
128
|
+
var _fields$key2;
|
|
129
|
+
const set = transform[key];
|
|
130
|
+
data[key] = set((_fields$key2 = fields[key]) === null || _fields$key2 === void 0 ? void 0 : _fields$key2.value);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return data;
|
|
134
|
+
};
|
|
135
|
+
function _toURLSearchParams(options) {
|
|
136
|
+
let data;
|
|
137
|
+
if (options) {
|
|
138
|
+
const {
|
|
139
|
+
exclude,
|
|
140
|
+
include,
|
|
141
|
+
transform
|
|
142
|
+
} = options;
|
|
143
|
+
if (include) {
|
|
144
|
+
data = {};
|
|
145
|
+
include.forEach(key => {
|
|
146
|
+
var _this$key;
|
|
147
|
+
return data[key] = (_this$key = this[key]) === null || _this$key === void 0 ? void 0 : _this$key.value;
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
data = this.toRawData();
|
|
151
|
+
exclude === null || exclude === void 0 || exclude.forEach(key => delete data[key]);
|
|
152
|
+
}
|
|
153
|
+
if (transform) {
|
|
154
|
+
for (const key in transform) {
|
|
155
|
+
var _this$key2;
|
|
156
|
+
const set = transform[key];
|
|
157
|
+
data[key] = set((_this$key2 = this[key]) === null || _this$key2 === void 0 ? void 0 : _this$key2.value);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} else data = this.toRawData();
|
|
161
|
+
return new URLSearchParams(data);
|
|
162
|
+
}
|
|
163
|
+
function __debounce(timeout, id) {
|
|
164
|
+
return new Promise(resolve => {
|
|
165
|
+
var _this$id;
|
|
166
|
+
if ((_this$id = this[id]) !== null && _this$id !== void 0 && _this$id.timerId) clearTimeout(this[id].timerId);
|
|
167
|
+
const timerId = setTimeout(() => resolve(), timeout);
|
|
168
|
+
if (this[id]) {
|
|
169
|
+
this[id].timerId = timerId;
|
|
170
|
+
} else this[id] = {
|
|
171
|
+
timerId
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const _debounce = __debounce.bind({});
|
|
176
|
+
const _extractValue = (e, unknown) => {
|
|
177
|
+
if (e !== null && e !== void 0 && e.target) {
|
|
178
|
+
if (Object.hasOwn(typeValueDict, e.target.type)) return e.target[typeValueDict[e.target.type]];
|
|
179
|
+
return e.target.value;
|
|
180
|
+
}
|
|
181
|
+
return (e === null || e === void 0 ? void 0 : e.value) || (isObject(unknown) ? unknown.value : unknown);
|
|
182
|
+
};
|
|
183
|
+
const _checkResult = (handlerResult, value) => typeof handlerResult === 'boolean' ? handlerResult : typeof value === 'string' ? typeof handlerResult === 'string' ? !new RegExp(handlerResult).test(value) : !handlerResult.test(value) : false;
|
|
184
|
+
const _merge = (target = {}, ...nodes) => {
|
|
185
|
+
if (!nodes.length) return target;
|
|
186
|
+
const next = nodes.shift();
|
|
187
|
+
if (isObject(next)) {
|
|
188
|
+
for (const key in next) {
|
|
189
|
+
target[key] = target[key] ? {
|
|
190
|
+
...target[key],
|
|
191
|
+
...next[key]
|
|
192
|
+
} : next[key];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return _merge(target, ...nodes);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const useForm = (children, validators = {}, optimized = false) => {
|
|
199
|
+
const initialValues = React.useMemo(() => _buildFormInitialValues(typeof children === 'function' ? children({}) : children), [children]);
|
|
200
|
+
const [state, setState] = React.useState(initialValues.state);
|
|
201
|
+
const _viHandler = (input, e) => {
|
|
202
|
+
if (!input) return;
|
|
203
|
+
const element = e === null || e === void 0 ? void 0 : e.target;
|
|
204
|
+
if (typeof document !== 'undefined' && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement)) {
|
|
205
|
+
if (!input.checkValidity) input.checkValidity = () => element.checkValidity();
|
|
206
|
+
element.setCustomValidity('');
|
|
207
|
+
const validityKey = _findValidityKey(element.validity);
|
|
208
|
+
_validateInput(input, validityKey, v => element.setCustomValidity(v));
|
|
209
|
+
if (!validityKey && input.error) {
|
|
210
|
+
element.setCustomValidity(input.errorText || 'error');
|
|
211
|
+
}
|
|
212
|
+
_dispatchChanges(input, input.formKey);
|
|
213
|
+
} else {
|
|
214
|
+
input.checkValidity = () => _createInputChecker(input);
|
|
215
|
+
input.checkValidity();
|
|
216
|
+
_dispatchChanges(input, input.formKey);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
const _createInputChecker = input => {
|
|
220
|
+
let validityKey = _findValidityKey({
|
|
221
|
+
valueMissing: input.required && !input.value || false,
|
|
222
|
+
tooShort: input.minLength && input.value.toString().length < input.minLength || false,
|
|
223
|
+
tooLong: input.maxLength && input.value.toString().length > input.maxLength || false,
|
|
224
|
+
patternMismatch: input.pattern && _checkResult(input.pattern, input.value) || false,
|
|
225
|
+
rangeUnderflow: input.min && Number(input.value) < Number(input.min) || false,
|
|
226
|
+
rangeOverflow: input.max && Number(input.value) > Number(input.max) || false
|
|
227
|
+
});
|
|
228
|
+
if (!validityKey && input.error) {
|
|
229
|
+
validityKey = 'customError';
|
|
230
|
+
}
|
|
231
|
+
_validateInput(input, validityKey);
|
|
232
|
+
return !input.error;
|
|
233
|
+
};
|
|
234
|
+
const _updateInputHandler = (key, e, unknown) => {
|
|
235
|
+
const value = _extractValue(e, unknown);
|
|
236
|
+
const input = _updateInput(key, value);
|
|
237
|
+
_viHandler(input, e);
|
|
238
|
+
};
|
|
239
|
+
const _validateInput = (input, validityKey, setValidity) => {
|
|
240
|
+
const inputValidator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
241
|
+
inputValidator && __validateInput(input, inputValidator, validityKey, setValidity);
|
|
242
|
+
input.touched = true;
|
|
243
|
+
};
|
|
244
|
+
const _updateInput = (key, value) => {
|
|
245
|
+
const input = state.fields[key];
|
|
246
|
+
input.value = value;
|
|
247
|
+
input.dirty = true;
|
|
248
|
+
return input;
|
|
249
|
+
};
|
|
250
|
+
const _dispatchChanges = (changes, key) => setState(prev => {
|
|
251
|
+
if (key) {
|
|
252
|
+
return {
|
|
253
|
+
...prev,
|
|
254
|
+
fields: {
|
|
255
|
+
...prev.fields,
|
|
256
|
+
[key]: {
|
|
257
|
+
...prev.fields[key],
|
|
258
|
+
...changes
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
...prev,
|
|
265
|
+
...changes
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
const __validateInput = (input, inputValidator, validityKey, setValidity) => {
|
|
269
|
+
for (const index in inputValidator.constraintHandlers) {
|
|
270
|
+
const result = inputValidator.constraintHandlers[index](input, validityKey);
|
|
271
|
+
input.error = _checkResult(result, input.value);
|
|
272
|
+
if (input.error) return;
|
|
273
|
+
}
|
|
274
|
+
for (const index in inputValidator.handlers) {
|
|
275
|
+
const result = inputValidator.handlers[index](input, state.fields);
|
|
276
|
+
input.error = _checkResult(result, input.value);
|
|
277
|
+
if (input.error) return;
|
|
278
|
+
}
|
|
279
|
+
input.errorText = '';
|
|
280
|
+
if (inputValidator.asyncHandlers.length) {
|
|
281
|
+
input.error = true;
|
|
282
|
+
_debounce(input.debounce || 300, `${input.gid}-async`).then(() => {
|
|
283
|
+
const validateAsync = async () => {
|
|
284
|
+
for (const index in inputValidator.asyncHandlers) {
|
|
285
|
+
const result = await inputValidator.asyncHandlers[index](input, state.fields);
|
|
286
|
+
input.error = _checkResult(result, input.value);
|
|
287
|
+
if (input.error) break;
|
|
288
|
+
}
|
|
289
|
+
if (!input.error) input.errorText = '';
|
|
290
|
+
_dispatchChanges({
|
|
291
|
+
error: input.error,
|
|
292
|
+
errorText: input.errorText
|
|
293
|
+
}, input.formKey);
|
|
294
|
+
setValidity === null || setValidity === void 0 || setValidity(input.errorText);
|
|
295
|
+
};
|
|
296
|
+
validateAsync();
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
return {
|
|
301
|
+
state,
|
|
302
|
+
_updateInputHandler,
|
|
303
|
+
_viHandler,
|
|
304
|
+
_dispatchChanges,
|
|
305
|
+
optimized,
|
|
306
|
+
key: initialValues.key,
|
|
307
|
+
_createInputChecker
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const gFormContext = React.createContext({
|
|
312
|
+
state: {
|
|
313
|
+
fields: {},
|
|
314
|
+
loading: false
|
|
315
|
+
},
|
|
316
|
+
_updateInputHandler: () => null,
|
|
317
|
+
_viHandler: () => null,
|
|
318
|
+
_dispatchChanges: () => null,
|
|
319
|
+
_createInputChecker: () => false,
|
|
320
|
+
optimized: false,
|
|
321
|
+
key: ''
|
|
322
|
+
});
|
|
323
|
+
const useGenericFormContext = () => React.useContext(gFormContext);
|
|
324
|
+
const GFormContextProvider = gFormContext.Provider;
|
|
325
|
+
|
|
326
|
+
const RNGForm = (() => {
|
|
327
|
+
return React.forwardRef(({
|
|
328
|
+
loader = React.createElement("div", null, "loading"),
|
|
329
|
+
stateRef,
|
|
330
|
+
children,
|
|
331
|
+
validators,
|
|
332
|
+
onInit,
|
|
333
|
+
el: El,
|
|
334
|
+
...rest
|
|
335
|
+
}, ref) => {
|
|
336
|
+
const values = useForm(children, validators);
|
|
337
|
+
const {
|
|
338
|
+
state,
|
|
339
|
+
_dispatchChanges,
|
|
340
|
+
key
|
|
341
|
+
} = values;
|
|
342
|
+
const formState = React.useMemo(() => {
|
|
343
|
+
const _isFormValid = _checkIfFormIsValid(state.fields);
|
|
344
|
+
const formState = {
|
|
345
|
+
...state.fields,
|
|
346
|
+
isValid: _isFormValid,
|
|
347
|
+
isInvalid: !_isFormValid,
|
|
348
|
+
loading: state.loading,
|
|
349
|
+
toRawData: options => _toRawData(state.fields, options),
|
|
350
|
+
toURLSearchParams: _toURLSearchParams,
|
|
351
|
+
checkValidity: () => {
|
|
352
|
+
for (const i in state.fields) {
|
|
353
|
+
const valid = state.fields[i].checkValidity();
|
|
354
|
+
if (!valid) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
},
|
|
360
|
+
setLoading: p => _dispatchChanges({
|
|
361
|
+
loading: typeof p === 'function' ? p(state.loading) : p
|
|
362
|
+
}),
|
|
363
|
+
dispatchChanges: changes => _dispatchChanges({
|
|
364
|
+
fields: _merge({}, state.fields, changes)
|
|
365
|
+
})
|
|
366
|
+
};
|
|
367
|
+
if (stateRef) stateRef.current = formState;
|
|
368
|
+
return formState;
|
|
369
|
+
}, [state.fields]);
|
|
370
|
+
React.useEffect(() => {
|
|
371
|
+
const dispatchers = Object.keys(state.fields).reduce((fields, key) => {
|
|
372
|
+
fields[key] = {
|
|
373
|
+
dispatchChanges: changes => _dispatchChanges(changes, key)
|
|
374
|
+
};
|
|
375
|
+
return fields;
|
|
376
|
+
}, {});
|
|
377
|
+
if (onInit) {
|
|
378
|
+
const _handler = _c => {
|
|
379
|
+
_dispatchChanges({
|
|
380
|
+
fields: _merge({}, state.fields, dispatchers, _c)
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
const changes = onInit(formState);
|
|
384
|
+
changes instanceof Promise ? changes.then(_handler) : _handler(changes);
|
|
385
|
+
} else {
|
|
386
|
+
_dispatchChanges({
|
|
387
|
+
fields: _merge({}, state.fields, dispatchers)
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}, []);
|
|
391
|
+
const formComponent = React.useMemo(() => {
|
|
392
|
+
const formChildren = typeof children === 'function' ? children(formState) : children;
|
|
393
|
+
return React.createElement(El, _extends({}, rest, {
|
|
394
|
+
ref: ref
|
|
395
|
+
}), formChildren);
|
|
396
|
+
}, [formState, children]);
|
|
397
|
+
return React.createElement(GFormContextProvider, {
|
|
398
|
+
value: values,
|
|
399
|
+
key: key
|
|
400
|
+
}, state.loading ? loader : formComponent);
|
|
401
|
+
});
|
|
402
|
+
})();
|
|
403
|
+
|
|
404
|
+
const RNGInput = React.forwardRef(({
|
|
405
|
+
formKey,
|
|
406
|
+
element,
|
|
407
|
+
type,
|
|
408
|
+
validatorKey,
|
|
409
|
+
fetch,
|
|
410
|
+
fetchDeps = [],
|
|
411
|
+
defaultValue,
|
|
412
|
+
value,
|
|
413
|
+
debounce = 300,
|
|
414
|
+
...rest
|
|
415
|
+
}, ref) => {
|
|
416
|
+
const {
|
|
417
|
+
state: {
|
|
418
|
+
fields
|
|
419
|
+
},
|
|
420
|
+
_updateInputHandler,
|
|
421
|
+
_dispatchChanges,
|
|
422
|
+
_viHandler,
|
|
423
|
+
_createInputChecker
|
|
424
|
+
} = useGenericFormContext();
|
|
425
|
+
const inputState = fields[formKey];
|
|
426
|
+
const _element = React.useMemo(() => {
|
|
427
|
+
const value = inputState.value || '';
|
|
428
|
+
const _props = {
|
|
429
|
+
...rest,
|
|
430
|
+
value,
|
|
431
|
+
inputMode: type,
|
|
432
|
+
ref
|
|
433
|
+
};
|
|
434
|
+
_props.onEndEditing = e => {
|
|
435
|
+
_viHandler(inputState);
|
|
436
|
+
rest.onEndEditing && rest.onEndEditing(e);
|
|
437
|
+
};
|
|
438
|
+
_props.onChangeText = e => {
|
|
439
|
+
_updateInputHandler(formKey, undefined, {
|
|
440
|
+
value: e
|
|
441
|
+
});
|
|
442
|
+
rest.onChangeText && rest.onChangeText(e);
|
|
443
|
+
};
|
|
444
|
+
if (element) {
|
|
445
|
+
return element(inputState, _props);
|
|
446
|
+
}
|
|
447
|
+
return React.createElement(reactNative.TextInput, _props);
|
|
448
|
+
}, [inputState, element]);
|
|
449
|
+
const _fetchDeps = React.useMemo(() => fetchDeps.map(key => fields[key].value), [fields]);
|
|
450
|
+
React.useEffect(() => {
|
|
451
|
+
inputState.checkValidity = () => {
|
|
452
|
+
const result = _createInputChecker(inputState);
|
|
453
|
+
_dispatchChanges(inputState, formKey);
|
|
454
|
+
return result;
|
|
455
|
+
};
|
|
456
|
+
_dispatchChanges(inputState, formKey);
|
|
457
|
+
}, []);
|
|
458
|
+
React.useEffect(() => {
|
|
459
|
+
if (fetch) {
|
|
460
|
+
_debounce(debounce, `${inputState.gid}-fetch`).then(() => {
|
|
461
|
+
const res = fetch(inputState, fields);
|
|
462
|
+
res instanceof Promise ? res.then(state => state && _dispatchChanges(state, formKey)) : res && _dispatchChanges(res, formKey);
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}, _fetchDeps);
|
|
466
|
+
return _element;
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
exports.RNGForm = RNGForm;
|
|
470
|
+
exports.RNGInput = RNGInput;
|
|
471
|
+
//# sourceMappingURL=gform-react-native.development.js.map
|