app-base-react
Version:
react development common base package.
1 lines • 9.79 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t,n=arguments[e];for(t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},_slicedToArray=function(r,e){if(Array.isArray(r))return r;if(Symbol.iterator in Object(r)){var t=e,n=[],a=!0,e=!1,i=void 0;try{for(var o,u=r[Symbol.iterator]();!(a=(o=u.next()).done)&&(n.push(o.value),!t||n.length!==t);a=!0);}catch(r){e=!0,i=r}finally{try{!a&&u.return&&u.return()}finally{if(e)throw i}}return n}throw new TypeError("Invalid attempt to destructure non-iterable instance")},_lodash=require("lodash"),_react=require("react"),_hooks=require("./hooks"),_processData=require("./processData"),_utils=require("./utils"),_validator=require("./validator");function _toConsumableArray(r){if(Array.isArray(r)){for(var e=0,t=Array(r.length);e<r.length;e++)t[e]=r[e];return t}return Array.from(r)}var useForm=function(r){var e=r||{},q=e.formData,t=e.onChange,n=e.onValidate,K=e.showValidate,a=e.logOnMount,e=e.logOnSubmit,a=a||window.FR_LOGGER&&window.FR_LOGGER.logOnMount,e=e||window.FR_LOGGER&&window.FR_LOGGER.logOnSubmit,i=(0,_react.useState)(0),i=_slicedToArray(i,2),j=i[0],k=i[1],i=(0,_hooks.useSet)({formData:{},submitData:{},errorFields:[],outErrorFields:[],isValidating:!1,outsideValidating:!1,isSubmitting:!1,isEditing:!1,allTouched:!1,touchedKeys:[],flatten:{},finalFlatten:{},firstMount:!0,validatingFields:[]}),i=_slicedToArray(i,2),o=i[0],u=i[1],c=(0,_react.useRef)(),s=(0,_react.useRef)(),l=(0,_react.useRef)("cn"),f=(0,_react.useRef)(),d=(0,_react.useRef)(),h=(0,_react.useRef)({}),m=(0,_react.useRef)({}),y=(0,_react.useRef)({}),g=(0,_react.useRef)([]),_=(0,_react.useRef)(),v=(0,_react.useRef)(),b=(0,_react.useRef)([]),p=(0,_react.useRef)([]),i=o.formData,G=o.submitData,F=o.errorFields,A=o.outErrorFields,A=void 0===A?[]:A,J=o.isValidating,N=o.outsideValidating,I=o.isSubmitting,L=o.isEditing,O=o.allTouched,H=o.touchedKeys,U=o.flatten,S=o.finalFlatten,D=o.firstMount;_.current=void 0===F?[]:F,v.current=A,g.current=H,m.current=U,y.current=S;function E(r){"function"==typeof t?t(r):u({formData:r})}function V(r){-1<g.current.indexOf(r)||(r=[].concat(_toConsumableArray(g.current),[r]),g.current=r,u({touchedKeys:r}))}function x(e){var r=g.current.filter(function(r){return-1===r.indexOf(e)});g.current=r,u({touchedKeys:r})}function R(r,e){"string"==typeof r&&("#"===r?E(_extends({},e)):((0,_lodash.set)(h.current,r,e),E(_extends({},h.current))))}function w(r){var e=[];Array.isArray(r)?e=[].concat(_toConsumableArray(r),_toConsumableArray(v.current)):r&&r.name?e=[r].concat(_toConsumableArray(v.current)):console.log("error format is wrong"),e=(0,_lodash.sortedUniqBy)(e,function(r){return r.name}),v.current=e,u({outErrorFields:e})}function T(){return u({isValidating:!0,allTouched:!0,isSubmitting:!1}),(0,_validator.validateAll)({formData:h.current,flatten:y.current,options:{locale:l.current,validateMessages:d.current},formInstance:{setFieldValidating:P,removeFieldValidating:M}}).then(function(r){u({errorFields:r});var e=(0,_lodash.sortedUniqBy)([].concat(_toConsumableArray(r||[]),_toConsumableArray(v.current)),function(r){return r.name});return"function"==typeof s.current?Promise.resolve((0,_processData.processData)(h.current,y.current,f.current)).then(function(r){return u({isValidating:!0,isSubmitting:!1,outsideValidating:!0,submitData:r}),{data:r,errors:e}}):Promise.resolve((0,_processData.processData)(h.current,y.current,f.current)).then(function(r){return u({isValidating:!1,isSubmitting:!0,submitData:r}),{data:r,errors:e}})}).catch(function(r){return console.log("submit error:",r),r})}function P(r){-1<p.current.indexOf(r)||(p.current=[].concat(_toConsumableArray(p.current),[r]))}function M(e){p.current=p.current.filter(function(r){return r!==e})}function C(r){return-1<p.current.indexOf(r)}var B=r&&r.hasOwnProperty("formData")?q:i;h.current=(0,_react.useMemo)(function(){return c.current?(0,_utils.generateDataSkeleton)(c.current,B):{}},[JSON.stringify(B),JSON.stringify(c.current)]),b.current=(0,_react.useMemo)(function(){var r;return Array.isArray(_.current)&&Array.isArray(v.current)&&0<v.current.length?(r=[].concat(_toConsumableArray(_.current),_toConsumableArray(v.current)),(0,_lodash.sortedUniqBy)(r,function(r){return r.name})):_.current},[JSON.stringify(_.current),JSON.stringify(v.current)]),(0,_react.useEffect)(function(){var r;c.current&&D&&(r=(0,_utils.flattenSchema)(c.current),u({flatten:r,firstMount:!1}))},[JSON.stringify(c.current),D]),(0,_react.useEffect)(function(){var a;D||(a=(0,_utils.clone)(m.current),Object.entries(m.current).forEach(function(r){var e,t,r=_slicedToArray(r,2),n=r[0],r=r[1];(0,_utils.schemaContainsExpression)(r.schema)&&(e=-1<(e=n.indexOf("]"))&&e<n.length-1,t=-1<JSON.stringify(r.schema).indexOf("rootValue"),e&&t||(a[n].schema=(0,_utils.parseAllExpression)(r.schema,h.current,n)))}),u({finalFlatten:a}))},[JSON.stringify(m.current),JSON.stringify(h.current),D]);function W(r){return-1<g.current.indexOf(r)}return{formData:h.current,schema:c.current,flatten:S,touchedKeys:g.current,allTouched:O,touchKey:V,removeTouched:x,changeTouchedKeys:function(r){u({touchedKeys:r})},onItemChange:R,setValueByPath:R,getSchemaByPath:function(r){try{return m.current[r].schema}catch(r){return console.log(r,"getSchemaByPath"),{}}},setSchemaByPath:function(r,e){if(m.current[r]){var t=(0,_utils.clone)(m.current);try{var n="function"==typeof e?e(t[r].schema):e;t[r].schema=_extends({},t[r].schema,n),u({flatten:t}),m.current=t}catch(r){console.error(r,"setSchemaByPath")}}else console.error("path:'"+r+"' 不存在(form.setSchemaByPath)")},setSchema:function(t,r){var r=r.override;if(void 0!==r&&r)r=(0,_utils.flattenSchema)(t),u({flatten:r});else{var n=(0,_utils.clone)(m.current);try{Object.keys(t).forEach(function(r){var e;m.current[r]?(e="function"==typeof(e=t[r])?e(n[r].schema):e,n[r].schema=_extends({},n[r].schema,e)):console.error("path:'"+r+"' 不存在(form.setSchemaByPath)")}),u({flatten:n}),m.current=n}catch(r){console.error(r,"setSchema")}}},setValues:function(r){r=(0,_processData.transformDataWithBind2)(r,m.current);E(r)},getValues:function(r,e){var t,n=y.current,a=h.current,i={},o=[];return Array.isArray(r)?r.forEach(function(r){(0,_lodash.set)(i,r,(0,_lodash.get)(a,r))}):i=a,e&&(0,_lodash.isFunction)(e)&&(Array.isArray&&(n={},r.forEach(function(r){n[r]=(0,_lodash.get)(y.current,r)})),t={},Object.keys(n).forEach(function(r){t[r]={touched:W(r),validating:C(r)}}),o=Object.keys(t).filter(function(r){return e(t[r])}),i={},(0,_lodash.isEmpty)(o)||o.forEach(function(r){(0,_lodash.set)(i,r,(0,_lodash.get)(a,r))})),(0,_processData.processData)(i,y.current,f.current)},getHiddenValues:function(){return(0,_utils.getHiddenData)(h.current,y.current)},resetFields:function(r){u({formData:r?r.formData:{},submitData:r?r.submitData:{},errorFields:r?r.errorFields:[],touchedKeys:r?r.touchedKeys:[],allTouched:!!r&&r.allTouched})},submit:T,init:T,submitData:G,errorFields:b.current,isValidating:J,outsideValidating:N,isSubmitting:I,endValidating:function(){return u({isValidating:!1,outsideValidating:!1,isSubmitting:!0})},endSubmitting:function(){return u({isSubmitting:!1,isValidating:!1,outsideValidating:!1})},setErrorFields:w,removeErrorField:function(e){var r=_.current.filter(function(r){return-1===r.name.indexOf(e)}),t=v.current.filter(function(r){return-1===r.name.indexOf(e)});u({errorFields:r,outErrorFields:t})},isEditing:L,setEditing:function(r){u({isEditing:r})},syncStuff:function(r){var e=r.schema,t=r.locale,n=r.validateMessages,a=r.beforeFinish,r=r.removeHiddenData;c.current=e,l.current=t,d.current=n,s.current=a,f.current=r,k(j+1)},showValidate:K,validateFields:function(r){var e=h.current;return Array.isArray(r)&&((0,_lodash.set)(e,{}),r.forEach(function(r){(0,_lodash.set)(e,r,(0,_lodash.get)(h.current,r))})),u({isValidating:!0}),(0,_validator.validateAll)({formData:e,flatten:y.current,options:{locale:l.current,validateMessages:d.current},formInstance:{setFieldValidating:P,removeFieldValidating:M}}).then(function(r){return u({isValidating:!1,errorFields:r}),(0,_lodash.isEmpty)(r)?Promise.resolve((0,_processData.processData)(e,y.current,f.current)):Promise.reject({errors:r,data:(0,_processData.processData)(e,y.current,f.current)})})},isFieldTouched:W,isFieldsTouched:function(){var r=arguments.length,e=[],t=!1,n=g.current;if(0===r)return 0<g.current.length;if(1===r){if(!Array.isArray(arguments[0]))return arguments[0]?O:0<g.current.length;e=arguments[0]}else e=Array.isArray(arguments[0])?arguments[0]:[],t=arguments[1];try{var a=function(r){return-1!==n.indexOf(r)};return t?e.every(a):e.some(a)}catch(r){console.error(">>>> isFieldsTouched error, check your input arguments",r)}},setFieldValidating:P,removeFieldValidating:M,isFieldValidating:C,scrollToPath:function(r){r=document.querySelector('[datapath="'+r+'"]');return!!r&&(r.scrollIntoView({behavior:"smooth",block:"end",inline:"nearest"}),!0)},getFieldError:function(e){return b.current.find(function(r){return r.name===e}).error||[]},getFieldsError:function(e){return e&&Array.isArray(e)?b.current.filter(function(r){return-1<e.indexOf(r.name)}):b.current},setFields:function(r){var e=r.filter(function(r){return r.error}).map(function(r){return{name:r.name,error:r.error}});(0,_lodash.isEmpty)(e)||w(e),r.forEach(function(r){var e=r.name,t=r.value,n=r.touched,a=r.validating;"value"in r&&R(e,t),"boolean"==typeof n&&(n?V:x)(e),"boolean"==typeof a&&(a?P:M)(e)})},setFirstMount:function(r){u({firstMount:r})},logOnMount:a,logOnSubmit:e,_setErrors:function(e){var r;"function"==typeof n&&(r=e?e.map(function(r){return r.name}):[],n(r)),u("function"==typeof e?function(r){r=r.errorFields;return{errorFields:e(r)}}:{errorFields:e})}}};exports.default=useForm;