diff --git a/dist/react-formutil.cjs.development.js b/dist/react-formutil.cjs.development.js index f0b8519..75a1227 100644 --- a/dist/react-formutil.cjs.development.js +++ b/dist/react-formutil.cjs.development.js @@ -901,6 +901,7 @@ var Form = /*#__PURE__*/function (_Component) { var pathData = pathExist($parsedTree, name); if (force || pathData) { + var $curState = handler.$getState(); var $newState = handler && processer(pathData && pathData.data, handler); if ($newState) { @@ -916,11 +917,11 @@ var Form = /*#__PURE__*/function (_Component) { if (findItem) { findItem.$newValue = $newValue; - } else if (name in _this.$formutil.$weakParams) { + } else { _this.$$fieldChangedQueue.push({ name: name, $newValue: $newValue, - $prevValue: _this.$formutil.$weakParams[name] + $prevValue: $curState.$value }); } } diff --git a/dist/react-formutil.cjs.production.js b/dist/react-formutil.cjs.production.js index fac7085..ca197af 100644 --- a/dist/react-formutil.cjs.production.js +++ b/dist/react-formutil.cjs.production.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:true});var e=require("react");var t=require("warning");var r=require("react-dom");var n=require("react-is");var a=require("hoist-non-react-statics");var i=require("react-fast-compare");function u(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var o=u(e);var s=u(t);var l=u(n);var f=u(a);var $=u(i);function c(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}var d=c;function v(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t)n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}));r.push.apply(r,n)}return r}function p(e){for(var t=1;te.length)t=e.length;for(var r=0,n=new Array(t);r1?r-1:0),a=1;a-1}var ye=function e(t){try{var r=new Function("origin","global","return typeof ".concat(t," === 'number' || (typeof ").concat(t," !== 'undefined' && !(origin in global)) ? ").concat(t," : origin"));return r(t,ue)}catch(e){return t}};function be(){for(var e=arguments.length,t=new Array(e),r=0;r=0?[]:{}:Array.isArray(l[p])?O(l[p]):h({},l[p]);break;default:l[p]=i;break}}}}catch(e){s["default"](false,"The name '%s' of Field seems is not a legal expression.",a)}if(t.length>2){return n}}function we(e,t){var r=t.split(ie).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,a=r.length;n should always return None(null,undefined) or an object contains error message of Field.");return t.$render(r)}return t.$$setStates(e||{},(function(e,t){var r=t.$getState(),n=r.$error,a=n===void 0?{}:n;if(e){return{$error:h(h({},a),{},d({},_e,e))}}if(a[_e]){delete a[_e];return{$error:a}}}),r,true)};t.$getField=function(e){var r=t.$$getRegister(e);s["default"](!e||r,"$getField('".concat(e,"') fail to find the matched Field. Maybe it has been unmounted."));s["default"](e,"You should pass a name of the mounted Field to $getField().");if(r){var n;return(n=r.$new)===null||n===void 0?void 0:n.call(r)}};t.$$onChange=function(e,r,n){return t.$setStates(d({},e,r),n)};t.$$setStates=function(){var e=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var n=arguments.length>1?arguments[1]:undefined;var a=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var u=t.$$deepParseObject(e);var o=[];Ve(t.$$registers,(function(e,r){var a=we(u,r);if(i||a){var s=e&&n(a&&a.data,e);if(s){var l=e.$$merge(s),f=l.$value;o.push([e,s]);if("$value"in s||"$viewValue"in s){var $=Ce(t.$$fieldChangedQueue,(function(e){return e.name===r}));if($){$.$newValue=f}else if(r in t.$formutil.$weakParams){t.$$fieldChangedQueue.push({name:r,$newValue:f,$prevValue:t.$formutil.$weakParams[r]})}}t.$$formShouldUpdateFields[r]=true}}}));return r.unstable_batchedUpdates((function(){o.forEach((function(e){var t=N(e,2),r=t[0],n=t[1];return r.$$detectChange(n)}));return t.$render(a)}))};t.$render=function(e){return new Promise((function(r){return t.forceUpdate((function(){return r(pe(e,t.$formutil))}))}))};t.$validates=function(){var e;for(var r=arguments.length,n=new Array(r),a=0;a1&&arguments[1]!==undefined?arguments[1]:{};Ve(t,(function(e,t){return be(r,t,e)}));return r}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){Fe(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(t){Fe(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){Fe(this.props.$ref,null)}},{key:"_render",value:function t(){var r=this.$formutil;var n=this.props,a=n.children,i=n.render,u=n.component;if(u){return e.createElement(u,{$formutil:r})}if(se(i)){return i(r)}if(se(a)){return a(r)}return e.Children.map(a,(function(t){return t&&de(t.type)?e.cloneElement(t,{$formutil:r}):t}))}},{key:"render",value:function e(){var t=this;var r=this.props.$processer;var n=Object.keys(this.$$registers).map((function(e){return{path:e,$state:t.$$registers[e].$getState()}}));var a=this.$$formShouldUpdateAll;var i=this.$formutil||{};var u=n.some((function(e){var t=e.$state;return t.$invalid}));var s=n.some((function(e){var t=e.$state;return t.$dirty}));var l=n.some((function(e){var t=e.$state;return t.$touched}));var f=n.some((function(e){var t=e.$state;return t.$focused}));var $=this.$$formPending||n.some((function(e){var t=e.$state;return t.$pending}));var c=a?{}:h({},i.$pureParams);var d=a?{}:h({},i.$states);var v=a?{}:h({},i.$errors);var p=a?{}:h({},i.$dirts);var m=a?{}:h({},i.$touches);var g=a?{}:h({},i.$focuses);var y=a?{}:h({},i.$pendings);var b=a?{}:h({},i.$weakStates);var w=a?{}:h({},i.$weakParams);var F=a?{}:h({},i.$weakErrors);var C=a?{}:h({},i.$weakDirts);var P=a?{}:h({},i.$weakFocuses);var V=a?{}:h({},i.$weakTouches);var k=a?{}:h({},i.$weakPendings);for(var S=0,O=n.length;S=0)continue;r[a]=e[a]}return r}var De=Ae;function Ne(e,t){if(e==null)return{};var r=De(e,t);var n,a;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;r[n]=e[n]}}return r}var Ue=Ne;var Te=["render","component","children","$defaultValues","$defaultStates","$onFormChange","$validator","$processer","$ref"];function Ie(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(e){G(a,e);var n=te(a);function a(){var e;T(this,a);for(var r=arguments.length,i=new Array(r),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var a=Object.assign({},e.$state.$error);if(qe(r)){a[t]=r||t;Be(r,t,e.props.name)}else{delete a[t]}return p(a,n)}function g(t,r){return e.$setState({$pending:t},r)}function y(){var t=e.$state.$error,r=t===void 0?{}:t;for(var n in r){if(r.hasOwnProperty(n)){return r[n]instanceof Error?r[n].message:r[n]}}}function b(t){var r=Object.assign({},t);if("$error"in r){if(!r.$error){r.$error={}}r.$valid=Object.keys(r.$error).length===0}var n=e.props,a=n.$parser,i=n.$formatter;if("$viewValue"in r&&!("$value"in r)){var u=function e(t){return r.$viewValue=t};r.$value=a?a(r.$viewValue,u):r.$viewValue}else if("$value"in r&&!("$viewValue"in r)){var o=function e(t){return r.$value=t};r.$viewValue=i?i(r.$value,o):r.$value}if("$valid"in r){r.$invalid=!r.$valid}else if("$invalid"in r){r.$dirty=!r.$invalid}if("$dirty"in r){r.$pristine=!r.$dirty}else if("$pristine"in r){r.$dirty=!r.$pristine}if("$touched"in r){r.$untouched=!r.$touched}else if("$untouched"in r){r.$touched=!r.$untouched}e.$state=h(h({},e.$state),r);return s()}return r}var Ke=function(e){G(r,e);var t=te(r);function r(){var e;T(this,r);for(var n=arguments.length,a=new Array(n),i=0;i with the name '".concat(t,"' must be used underneath a
component or withForm() HOC, otherwise it's isolated."));s["default"](t,"You should assign a name to , otherwise it will be isolated!");if(r.$$register){r.$$register(t,this.$fieldHandler)}this.$prevState=this.$state;Fe(this.props.$ref,this.$fieldutil)}},{key:"componentWillUnmount",value:function e(){if(this.$formContext.$$unregister){this.$formContext.$$unregister(this.props.name,this.$fieldHandler,this.props.$reserveOnUnmount)}this.isMounting=false;Fe(this.props.$ref,null)}},{key:"componentDidUpdate",value:function e(t){var r=this.props.name;if(r!==t.name){if(this.$formContext.$$register){this.$formContext.$$register(r,this.$fieldHandler,t.name)}}Fe(this.props.$ref,this.$fieldutil);if(this.$state.$value!==this.$prevState.$value){if(!(r in(this.$formContext.$$registers||{}))){this.$registered.$$triggerChange({$newValue:this.$state.$value,$prevValue:this.$prevState.$value})}}this.$prevState=this.$state}},{key:"shouldComponentUpdate",value:function e(t){var r=t.$memo;return!r||!Ee(this.$registered.$getState(),this.$prevState)||!(Array.isArray(r)?$["default"](r,this.props.$memo):$["default"](this.props,t))}},{key:"_render",value:function e(){var t=this.$fieldutil=h(h(h({$name:this.props.name},this.$registered.$getState()),this.$registered),{},{$$formutil:this.$formContext.$formutil});return Ge(t,this.props)}},{key:"render",value:function e(){var t=this;this.shouldRendered=true;return o["default"].createElement(re.Consumer,null,(function(e){var r=!t.$formContext;t.$formContext=e();if(!t.$fieldHandler){t.$fieldHandler=ze(t,t)}t.$registered=(t.$formContext.$$registers||{})[t.$fieldHandler.$name]||t.$fieldHandler;if(r){t.$fieldHandler.$$reset();t.$fieldHandler.$validate()}return t._render()}))}}]);return r}(e.Component);Ke.displayName=Ye;var We=["name","$defaultValue","$defaultState","$onFieldChange","$validators","$asyncValidators","$validateLazy","$memo","$reserveOnUnmount","$ref","$parser","$formatter","render","component","children"];function Je(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(e){G(a,e);var n=te(a);function a(){var e;T(this,a);for(var r=arguments.length,i=new Array(r),u=0;u.")}},{key:"render",value:function e(){var t=this.props,r=t.$value,n=t.onChange,a=t.onFocus,i=t.onBlur,u=Ue(t,["$value","onChange","onFocus","onBlur"]);return o["default"].createElement(rt,null,(function(e){var t=e();var s=t.type,l=t.name;var f=s==="radio"?{checked:t.value===r,onChange:function e(a){t.onChange(r,a);n&&n(a)}}:s==="checkbox"?{checked:t.value.indexOf(r)>-1,onChange:function e(a){t.onChange(a.target.checked?t.value.concat(r):t.value.filter((function(e){return e!==r})),a);n&&n(a)}}:{value:t.value,onChange:function e(r){t.onChange(r);n&&n(r)}};return o["default"].createElement("input",Object.assign({name:l},u,f,{type:s,onFocus:function e(r){t.onFocus(r);a&&a(r)},onBlur:function e(r){t.onBlur(r);i&&i(r)}}))}))}}]);return r}(e.Component);at.displayName="React.Formutil.EasyField.Group.Option";var it=function(e){G(r,e);var t=te(r);function r(){T(this,r);return t.apply(this,arguments)}M(r,[{key:"componentDidMount",value:function e(){s["default"](false,'The "Field" property in EasyField\'s children-props has been deprecated. Please use "GroupOption" instead.')}},{key:"render",value:function e(){return o["default"].createElement(at,this.props)}}]);return r}(e.Component);it.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var ut=o["default"].Frament||"div";var ot=function(e){G(r,e);var t=te(r);function r(e){var n;T(this,r);n=t.call(this,e);n.id=0;n.latestValue=n.props.value;n.$formutil=void 0;n.FieldValidators={required:function e(t){return t!==null}};n.$onFormChange=function(e){e.$onValidates((function(e){var t=e.$invalid,r=e.$params;if(t){if(n.props.value.length){n.props.onChange(n.latestValue=[])}}else if(!$["default"](n.props.value,r.list)){n.props.onChange(n.latestValue=r.list)}}))};n.swap=function(e,t,r){return n.$setState((function(r){var n=r.items;var a=[n[e],n[t]];n[t]=a[0];n[e]=a[1];return n}),r)};n.insert=function(){var e,t,r;for(var a=arguments.length,i=new Array(a),u=0;u=t*1}],["max",function(e,t){return le(e)||e*1<=t*1}],["min",function(e,t){return le(e)||e*1>=t*1}],["pattern",function(e,t){return le(e)||t.test(e)}],["enum",function(e,t){return le(e)||t.indexOf(e)>-1}],["checker",function(e,t,r){return t(e,r)}]].reduce((function(e,t){var r=N(t,2),n=r[0],a=r[1];e[n]=function e(t,r,i){var u=i.validMessage,o=u===void 0?{}:u;return a.apply(void 0,arguments)||o[n]||"Error input: ".concat(n)};return e}),{});var ft="React.Formutil.EasyField";var $t={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(t){return typeof t==="string"?t.trim():t}};function ct(e,t,r){var n;var a=t.valuePropName,i=t.changePropName,u=t.focusPropName,o=t.blurPropName,s=t.getValueFromEvent,l=t.passUtil;var f=function e(t){return t&&t.target?t.target[a]:t};var $=h(h({},r),{},(n={},d(n,a,e.$viewValue),d(n,i,(function(r){var n;for(var a=arguments.length,u=new Array(a>1?a-1:0),o=1;o=16.8, Please upgrade your reactjs.")}var e=o["default"].useContext;var t=e(re);return t()}function gt(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!o["default"].useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var r=o["default"].useState,n=o["default"].useLayoutEffect,a=o["default"].useRef;var i;if(e){if(typeof e==="string"){i=e;t.name=i}else{t=e;i=t.name}}var u=mt();var l=a({}).current;var f=a([]);var $;l.$formContext=u;l.props=t;l.$setState=p;l.shouldRendered=true;var c=r((function(){l.$$FIELD_UUID=Qe();l.$fieldHandler=$=ze(l);l.$fieldHandler.$$reset();l.$fieldHandler.$validate()})),d=N(c,2),v=d[1];if(!$){$=(u.$$registers||{})[l.$fieldHandler.$name]||l.$fieldHandler}n((function(){var e=l.$state;if(l.isMounting){if(!(i in(u.$$registers||{}))){$.$$triggerChange({$newValue:e.$value,$prevValue:l.$prevState.$value})}}l.$prevState=e}),[l.$state.$value]);n((function(){l.isMounting=true;s["default"](!i||u.$formutil,"You should enusre that the useField() with the name '".concat(i,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));s["default"](i,"You should pass a name argument to useField(), otherwise it will be isolated!");return function(){l.isMounting=false;Fe(t.$ref,null)}}),[]);n((function(){if(u.$$register){u.$$register(i,l.$fieldHandler)}return function(){if(u.$$unregister){u.$$unregister(i,l.$fieldHandler,!l.isMounting&&t.$reserveOnUnmount)}}}),[i]);n((function(){Fe(t.$ref,l.$fieldutil)}));n((function(){if(f.current.length>0){var e=O(f.current);f.current.length=0;while(e.length){e.pop()(l.$fieldutil)}}}));function p(e,t){return new Promise((function(r){var n=function e(){return r(pe(t,l.$fieldutil))};if(l.isMounting){if(i in(u.$$registers||{})){l.shouldRendered=false;u.$$onChange(i,e,n);if(!l.shouldRendered){v({})}}else{$.$$merge(e);$.$$detectChange(e);v({});f.current.push(n)}}else{$.$$merge(e);n()}}))}return l.$fieldutil=h(h(h({$name:i},$.$getState()),$),{},{$$formutil:u.$formutil})}function yt(){var e=mt(),t=e.$formutil;return t}function bt(e){e=h(h(h({},$t),e),{},{children:null});var t=dt(e),r=t.fieldProps,n=t.childProps;var a=gt(r);return ct(a,r,n)}exports.EasyField=pt;exports.Field=Ke;exports.Form=Re;exports.connect=ht;exports.formContext=re;exports.useField=gt;exports.useForm=yt;exports.useHandler=bt;exports.withField=Xe;exports.withForm=He; +"use strict";Object.defineProperty(exports,"__esModule",{value:true});var e=require("react");var t=require("warning");var r=require("react-dom");var n=require("react-is");var a=require("hoist-non-react-statics");var i=require("react-fast-compare");function u(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var o=u(e);var s=u(t);var l=u(n);var f=u(a);var $=u(i);function c(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}var d=c;function v(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t)n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}));r.push.apply(r,n)}return r}function p(e){for(var t=1;te.length)t=e.length;for(var r=0,n=new Array(t);r1?r-1:0),a=1;a-1}var ye=function e(t){try{var r=new Function("origin","global","return typeof ".concat(t," === 'number' || (typeof ").concat(t," !== 'undefined' && !(origin in global)) ? ").concat(t," : origin"));return r(t,ue)}catch(e){return t}};function be(){for(var e=arguments.length,t=new Array(e),r=0;r=0?[]:{}:Array.isArray(l[p])?O(l[p]):h({},l[p]);break;default:l[p]=i;break}}}}catch(e){s["default"](false,"The name '%s' of Field seems is not a legal expression.",a)}if(t.length>2){return n}}function we(e,t){var r=t.split(ie).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,a=r.length;n should always return None(null,undefined) or an object contains error message of Field.");return t.$render(r)}return t.$$setStates(e||{},(function(e,t){var r=t.$getState(),n=r.$error,a=n===void 0?{}:n;if(e){return{$error:h(h({},a),{},d({},_e,e))}}if(a[_e]){delete a[_e];return{$error:a}}}),r,true)};t.$getField=function(e){var r=t.$$getRegister(e);s["default"](!e||r,"$getField('".concat(e,"') fail to find the matched Field. Maybe it has been unmounted."));s["default"](e,"You should pass a name of the mounted Field to $getField().");if(r){var n;return(n=r.$new)===null||n===void 0?void 0:n.call(r)}};t.$$onChange=function(e,r,n){return t.$setStates(d({},e,r),n)};t.$$setStates=function(){var e=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var n=arguments.length>1?arguments[1]:undefined;var a=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var u=t.$$deepParseObject(e);var o=[];Ve(t.$$registers,(function(e,r){var a=we(u,r);if(i||a){var s=e.$getState();var l=e&&n(a&&a.data,e);if(l){var f=e.$$merge(l),$=f.$value;o.push([e,l]);if("$value"in l||"$viewValue"in l){var c=Ce(t.$$fieldChangedQueue,(function(e){return e.name===r}));if(c){c.$newValue=$}else{t.$$fieldChangedQueue.push({name:r,$newValue:$,$prevValue:s.$value})}}t.$$formShouldUpdateFields[r]=true}}}));return r.unstable_batchedUpdates((function(){o.forEach((function(e){var t=N(e,2),r=t[0],n=t[1];return r.$$detectChange(n)}));return t.$render(a)}))};t.$render=function(e){return new Promise((function(r){return t.forceUpdate((function(){return r(pe(e,t.$formutil))}))}))};t.$validates=function(){var e;for(var r=arguments.length,n=new Array(r),a=0;a1&&arguments[1]!==undefined?arguments[1]:{};Ve(t,(function(e,t){return be(r,t,e)}));return r}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){Fe(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(t){Fe(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){Fe(this.props.$ref,null)}},{key:"_render",value:function t(){var r=this.$formutil;var n=this.props,a=n.children,i=n.render,u=n.component;if(u){return e.createElement(u,{$formutil:r})}if(se(i)){return i(r)}if(se(a)){return a(r)}return e.Children.map(a,(function(t){return t&&de(t.type)?e.cloneElement(t,{$formutil:r}):t}))}},{key:"render",value:function e(){var t=this;var r=this.props.$processer;var n=Object.keys(this.$$registers).map((function(e){return{path:e,$state:t.$$registers[e].$getState()}}));var a=this.$$formShouldUpdateAll;var i=this.$formutil||{};var u=n.some((function(e){var t=e.$state;return t.$invalid}));var s=n.some((function(e){var t=e.$state;return t.$dirty}));var l=n.some((function(e){var t=e.$state;return t.$touched}));var f=n.some((function(e){var t=e.$state;return t.$focused}));var $=this.$$formPending||n.some((function(e){var t=e.$state;return t.$pending}));var c=a?{}:h({},i.$pureParams);var d=a?{}:h({},i.$states);var v=a?{}:h({},i.$errors);var p=a?{}:h({},i.$dirts);var m=a?{}:h({},i.$touches);var g=a?{}:h({},i.$focuses);var y=a?{}:h({},i.$pendings);var b=a?{}:h({},i.$weakStates);var w=a?{}:h({},i.$weakParams);var F=a?{}:h({},i.$weakErrors);var C=a?{}:h({},i.$weakDirts);var P=a?{}:h({},i.$weakFocuses);var V=a?{}:h({},i.$weakTouches);var S=a?{}:h({},i.$weakPendings);for(var k=0,O=n.length;k=0)continue;r[a]=e[a]}return r}var De=Ae;function Ne(e,t){if(e==null)return{};var r=De(e,t);var n,a;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;r[n]=e[n]}}return r}var Ue=Ne;var Te=["render","component","children","$defaultValues","$defaultStates","$onFormChange","$validator","$processer","$ref"];function Ie(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(e){G(a,e);var n=te(a);function a(){var e;T(this,a);for(var r=arguments.length,i=new Array(r),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var a=Object.assign({},e.$state.$error);if(qe(r)){a[t]=r||t;Be(r,t,e.props.name)}else{delete a[t]}return p(a,n)}function g(t,r){return e.$setState({$pending:t},r)}function y(){var t=e.$state.$error,r=t===void 0?{}:t;for(var n in r){if(r.hasOwnProperty(n)){return r[n]instanceof Error?r[n].message:r[n]}}}function b(t){var r=Object.assign({},t);if("$error"in r){if(!r.$error){r.$error={}}r.$valid=Object.keys(r.$error).length===0}var n=e.props,a=n.$parser,i=n.$formatter;if("$viewValue"in r&&!("$value"in r)){var u=function e(t){return r.$viewValue=t};r.$value=a?a(r.$viewValue,u):r.$viewValue}else if("$value"in r&&!("$viewValue"in r)){var o=function e(t){return r.$value=t};r.$viewValue=i?i(r.$value,o):r.$value}if("$valid"in r){r.$invalid=!r.$valid}else if("$invalid"in r){r.$dirty=!r.$invalid}if("$dirty"in r){r.$pristine=!r.$dirty}else if("$pristine"in r){r.$dirty=!r.$pristine}if("$touched"in r){r.$untouched=!r.$touched}else if("$untouched"in r){r.$touched=!r.$untouched}e.$state=h(h({},e.$state),r);return s()}return r}var Ke=function(e){G(r,e);var t=te(r);function r(){var e;T(this,r);for(var n=arguments.length,a=new Array(n),i=0;i with the name '".concat(t,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));s["default"](t,"You should assign a name to , otherwise it will be isolated!");if(r.$$register){r.$$register(t,this.$fieldHandler)}this.$prevState=this.$state;Fe(this.props.$ref,this.$fieldutil)}},{key:"componentWillUnmount",value:function e(){if(this.$formContext.$$unregister){this.$formContext.$$unregister(this.props.name,this.$fieldHandler,this.props.$reserveOnUnmount)}this.isMounting=false;Fe(this.props.$ref,null)}},{key:"componentDidUpdate",value:function e(t){var r=this.props.name;if(r!==t.name){if(this.$formContext.$$register){this.$formContext.$$register(r,this.$fieldHandler,t.name)}}Fe(this.props.$ref,this.$fieldutil);if(this.$state.$value!==this.$prevState.$value){if(!(r in(this.$formContext.$$registers||{}))){this.$registered.$$triggerChange({$newValue:this.$state.$value,$prevValue:this.$prevState.$value})}}this.$prevState=this.$state}},{key:"shouldComponentUpdate",value:function e(t){var r=t.$memo;return!r||!Ee(this.$registered.$getState(),this.$prevState)||!(Array.isArray(r)?$["default"](r,this.props.$memo):$["default"](this.props,t))}},{key:"_render",value:function e(){var t=this.$fieldutil=h(h(h({$name:this.props.name},this.$registered.$getState()),this.$registered),{},{$$formutil:this.$formContext.$formutil});return Ge(t,this.props)}},{key:"render",value:function e(){var t=this;this.shouldRendered=true;return o["default"].createElement(re.Consumer,null,(function(e){var r=!t.$formContext;t.$formContext=e();if(!t.$fieldHandler){t.$fieldHandler=ze(t,t)}t.$registered=(t.$formContext.$$registers||{})[t.$fieldHandler.$name]||t.$fieldHandler;if(r){t.$fieldHandler.$$reset();t.$fieldHandler.$validate()}return t._render()}))}}]);return r}(e.Component);Ke.displayName=Ye;var We=["name","$defaultValue","$defaultState","$onFieldChange","$validators","$asyncValidators","$validateLazy","$memo","$reserveOnUnmount","$ref","$parser","$formatter","render","component","children"];function Je(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(e){G(a,e);var n=te(a);function a(){var e;T(this,a);for(var r=arguments.length,i=new Array(r),u=0;u.")}},{key:"render",value:function e(){var t=this.props,r=t.$value,n=t.onChange,a=t.onFocus,i=t.onBlur,u=Ue(t,["$value","onChange","onFocus","onBlur"]);return o["default"].createElement(rt,null,(function(e){var t=e();var s=t.type,l=t.name;var f=s==="radio"?{checked:t.value===r,onChange:function e(a){t.onChange(r,a);n&&n(a)}}:s==="checkbox"?{checked:t.value.indexOf(r)>-1,onChange:function e(a){t.onChange(a.target.checked?t.value.concat(r):t.value.filter((function(e){return e!==r})),a);n&&n(a)}}:{value:t.value,onChange:function e(r){t.onChange(r);n&&n(r)}};return o["default"].createElement("input",Object.assign({name:l},u,f,{type:s,onFocus:function e(r){t.onFocus(r);a&&a(r)},onBlur:function e(r){t.onBlur(r);i&&i(r)}}))}))}}]);return r}(e.Component);at.displayName="React.Formutil.EasyField.Group.Option";var it=function(e){G(r,e);var t=te(r);function r(){T(this,r);return t.apply(this,arguments)}M(r,[{key:"componentDidMount",value:function e(){s["default"](false,'The "Field" property in EasyField\'s children-props has been deprecated. Please use "GroupOption" instead.')}},{key:"render",value:function e(){return o["default"].createElement(at,this.props)}}]);return r}(e.Component);it.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var ut=o["default"].Frament||"div";var ot=function(e){G(r,e);var t=te(r);function r(e){var n;T(this,r);n=t.call(this,e);n.id=0;n.latestValue=n.props.value;n.$formutil=void 0;n.FieldValidators={required:function e(t){return t!==null}};n.$onFormChange=function(e){e.$onValidates((function(e){var t=e.$invalid,r=e.$params;if(t){if(n.props.value.length){n.props.onChange(n.latestValue=[])}}else if(!$["default"](n.props.value,r.list)){n.props.onChange(n.latestValue=r.list)}}))};n.swap=function(e,t,r){return n.$setState((function(r){var n=r.items;var a=[n[e],n[t]];n[t]=a[0];n[e]=a[1];return n}),r)};n.insert=function(){var e,t,r;for(var a=arguments.length,i=new Array(a),u=0;u=t*1}],["max",function(e,t){return le(e)||e*1<=t*1}],["min",function(e,t){return le(e)||e*1>=t*1}],["pattern",function(e,t){return le(e)||t.test(e)}],["enum",function(e,t){return le(e)||t.indexOf(e)>-1}],["checker",function(e,t,r){return t(e,r)}]].reduce((function(e,t){var r=N(t,2),n=r[0],a=r[1];e[n]=function e(t,r,i){var u=i.validMessage,o=u===void 0?{}:u;return a.apply(void 0,arguments)||o[n]||"Error input: ".concat(n)};return e}),{});var ft="React.Formutil.EasyField";var $t={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(t){return typeof t==="string"?t.trim():t}};function ct(e,t,r){var n;var a=t.valuePropName,i=t.changePropName,u=t.focusPropName,o=t.blurPropName,s=t.getValueFromEvent,l=t.passUtil;var f=function e(t){return t&&t.target?t.target[a]:t};var $=h(h({},r),{},(n={},d(n,a,e.$viewValue),d(n,i,(function(r){var n;for(var a=arguments.length,u=new Array(a>1?a-1:0),o=1;o=16.8, Please upgrade your reactjs.")}var e=o["default"].useContext;var t=e(re);return t()}function gt(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!o["default"].useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var r=o["default"].useState,n=o["default"].useLayoutEffect,a=o["default"].useRef;var i;if(e){if(typeof e==="string"){i=e;t.name=i}else{t=e;i=t.name}}var u=mt();var l=a({}).current;var f=a([]);var $;l.$formContext=u;l.props=t;l.$setState=p;l.shouldRendered=true;var c=r((function(){l.$$FIELD_UUID=Qe();l.$fieldHandler=$=ze(l);l.$fieldHandler.$$reset();l.$fieldHandler.$validate()})),d=N(c,2),v=d[1];if(!$){$=(u.$$registers||{})[l.$fieldHandler.$name]||l.$fieldHandler}n((function(){var e=l.$state;if(l.isMounting){if(!(i in(u.$$registers||{}))){$.$$triggerChange({$newValue:e.$value,$prevValue:l.$prevState.$value})}}l.$prevState=e}),[l.$state.$value]);n((function(){l.isMounting=true;s["default"](!i||u.$formutil,"You should enusre that the useField() with the name '".concat(i,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));s["default"](i,"You should pass a name argument to useField(), otherwise it will be isolated!");return function(){l.isMounting=false;Fe(t.$ref,null)}}),[]);n((function(){if(u.$$register){u.$$register(i,l.$fieldHandler)}return function(){if(u.$$unregister){u.$$unregister(i,l.$fieldHandler,!l.isMounting&&t.$reserveOnUnmount)}}}),[i]);n((function(){Fe(t.$ref,l.$fieldutil)}));n((function(){if(f.current.length>0){var e=O(f.current);f.current.length=0;while(e.length){e.pop()(l.$fieldutil)}}}));function p(e,t){return new Promise((function(r){var n=function e(){return r(pe(t,l.$fieldutil))};if(l.isMounting){if(i in(u.$$registers||{})){l.shouldRendered=false;u.$$onChange(i,e,n);if(!l.shouldRendered){v({})}}else{$.$$merge(e);$.$$detectChange(e);v({});f.current.push(n)}}else{$.$$merge(e);n()}}))}return l.$fieldutil=h(h(h({$name:i},$.$getState()),$),{},{$$formutil:u.$formutil})}function yt(){var e=mt(),t=e.$formutil;return t}function bt(e){e=h(h(h({},$t),e),{},{children:null});var t=dt(e),r=t.fieldProps,n=t.childProps;var a=gt(r);return ct(a,r,n)}exports.EasyField=pt;exports.Field=Ke;exports.Form=Re;exports.connect=ht;exports.formContext=re;exports.useField=gt;exports.useForm=yt;exports.useHandler=bt;exports.withField=Xe;exports.withForm=He; diff --git a/dist/react-formutil.esm.development.js b/dist/react-formutil.esm.development.js index ab5ef4a..27e8a4e 100644 --- a/dist/react-formutil.esm.development.js +++ b/dist/react-formutil.esm.development.js @@ -834,6 +834,7 @@ var Form = /*#__PURE__*/function (_Component) { var pathData = pathExist($parsedTree, name); if (force || pathData) { + var $curState = handler.$getState(); var $newState = handler && processer(pathData && pathData.data, handler); if ($newState) { @@ -849,11 +850,11 @@ var Form = /*#__PURE__*/function (_Component) { if (findItem) { findItem.$newValue = $newValue; - } else if (name in _this.$formutil.$weakParams) { + } else { _this.$$fieldChangedQueue.push({ name: name, $newValue: $newValue, - $prevValue: _this.$formutil.$weakParams[name] + $prevValue: $curState.$value }); } } diff --git a/dist/react-formutil.esm.production.js b/dist/react-formutil.esm.production.js index d448e30..38a028f 100644 --- a/dist/react-formutil.esm.production.js +++ b/dist/react-formutil.esm.production.js @@ -1 +1 @@ -import e,{createContext as t,createElement as r,Children as n,cloneElement as i,Component as a,forwardRef as o}from"react";import u from"warning";import{unstable_batchedUpdates as s}from"react-dom";import l from"react-is";import f from"hoist-non-react-statics";import $ from"react-fast-compare";function c(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t)n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}));r.push.apply(r,n)}return r}function v(e){for(var t=1;te.length)t=e.length;for(var r=0,n=new Array(t);r1?r-1:0),i=1;i-1}var Z=function e(t){try{var r=new Function("origin","global","return typeof ".concat(t," === 'number' || (typeof ").concat(t," !== 'undefined' && !(origin in global)) ? ").concat(t," : origin"));return r(t,H)}catch(e){return t}};function ee(){for(var e=arguments.length,t=new Array(e),r=0;r=0?[]:{}:Array.isArray(l[h])?b(l[h]):v({},l[h]);break;default:l[h]=a;break}}}}catch(e){u(false,"The name '%s' of Field seems is not a legal expression.",i)}if(t.length>2){return n}}function te(e,t){var r=t.split(I).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,i=r.length;n should always return None(null,undefined) or an object contains error message of Field.");return t.$render(r)}return t.$$setStates(e||{},(function(e,t){var r=t.$getState(),n=r.$error,i=n===void 0?{}:n;if(e){return{$error:v(v({},i),{},c({},fe,e))}}if(i[fe]){delete i[fe];return{$error:i}}}),r,true)};t.$getField=function(e){var r=t.$$getRegister(e);u(!e||r,"$getField('".concat(e,"') fail to find the matched Field. Maybe it has been unmounted."));u(e,"You should pass a name of the mounted Field to $getField().");if(r){var n;return(n=r.$new)===null||n===void 0?void 0:n.call(r)}};t.$$onChange=function(e,r,n){return t.$setStates(c({},e,r),n)};t.$$setStates=function(){var e=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var r=arguments.length>1?arguments[1]:undefined;var n=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var a=t.$$deepParseObject(e);var o=[];ae(t.$$registers,(function(e,n){var u=te(a,n);if(i||u){var s=e&&r(u&&u.data,e);if(s){var l=e.$$merge(s),f=l.$value;o.push([e,s]);if("$value"in s||"$viewValue"in s){var $=ne(t.$$fieldChangedQueue,(function(e){return e.name===n}));if($){$.$newValue=f}else if(n in t.$formutil.$weakParams){t.$$fieldChangedQueue.push({name:n,$newValue:f,$prevValue:t.$formutil.$weakParams[n]})}}t.$$formShouldUpdateFields[n]=true}}}));return s((function(){o.forEach((function(e){var t=V(e,2),r=t[0],n=t[1];return r.$$detectChange(n)}));return t.$render(n)}))};t.$render=function(e){return new Promise((function(r){return t.forceUpdate((function(){return r(K(e,t.$formutil))}))}))};t.$validates=function(){var e;for(var r=arguments.length,n=new Array(r),i=0;i1&&arguments[1]!==undefined?arguments[1]:{};ae(t,(function(e,t){return ee(r,t,e)}));return r}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){re(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(t){re(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){re(this.props.$ref,null)}},{key:"_render",value:function e(){var t=this.$formutil;var a=this.props,o=a.children,u=a.render,s=a.component;if(s){return r(s,{$formutil:t})}if(L(u)){return u(t)}if(L(o)){return o(t)}return n.map(o,(function(e){return e&&z(e.type)?i(e,{$formutil:t}):e}))}},{key:"render",value:function t(){var r=this;var n=this.props.$processer;var i=Object.keys(this.$$registers).map((function(e){return{path:e,$state:r.$$registers[e].$getState()}}));var a=this.$$formShouldUpdateAll;var o=this.$formutil||{};var u=i.some((function(e){var t=e.$state;return t.$invalid}));var s=i.some((function(e){var t=e.$state;return t.$dirty}));var l=i.some((function(e){var t=e.$state;return t.$touched}));var f=i.some((function(e){var t=e.$state;return t.$focused}));var $=this.$$formPending||i.some((function(e){var t=e.$state;return t.$pending}));var c=a?{}:v({},o.$pureParams);var d=a?{}:v({},o.$states);var p=a?{}:v({},o.$errors);var h=a?{}:v({},o.$dirts);var m=a?{}:v({},o.$touches);var g=a?{}:v({},o.$focuses);var y=a?{}:v({},o.$pendings);var b=a?{}:v({},o.$weakStates);var w=a?{}:v({},o.$weakParams);var F=a?{}:v({},o.$weakErrors);var P=a?{}:v({},o.$weakDirts);var V=a?{}:v({},o.$weakFocuses);var k=a?{}:v({},o.$weakTouches);var S=a?{}:v({},o.$weakPendings);for(var C=0,O=i.length;C=0)continue;r[i]=e[i]}return r}function pe(e,t){if(e==null)return{};var r=ve(e,t);var n,i;if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(i=0;i=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;r[n]=e[n]}}return r}var he=["render","component","children","$defaultValues","$defaultStates","$onFormChange","$validator","$processer","$ref"];function me(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(n){_(a,n);var i=x(a);function a(){var r;k(this,a);for(var n=arguments.length,o=new Array(n),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var i=Object.assign({},e.$state.$error);if(we(r)){i[t]=r||t;Fe(r,t,e.props.name)}else{delete i[t]}return h(i,n)}function g(t,r){return e.$setState({$pending:t},r)}function y(){var t=e.$state.$error,r=t===void 0?{}:t;for(var n in r){if(r.hasOwnProperty(n)){return r[n]instanceof Error?r[n].message:r[n]}}}function b(t){var r=Object.assign({},t);if("$error"in r){if(!r.$error){r.$error={}}r.$valid=Object.keys(r.$error).length===0}var n=e.props,i=n.$parser,a=n.$formatter;if("$viewValue"in r&&!("$value"in r)){var o=function e(t){return r.$viewValue=t};r.$value=i?i(r.$viewValue,o):r.$viewValue}else if("$value"in r&&!("$viewValue"in r)){var u=function e(t){return r.$value=t};r.$viewValue=a?a(r.$value,u):r.$value}if("$valid"in r){r.$invalid=!r.$valid}else if("$invalid"in r){r.$dirty=!r.$invalid}if("$dirty"in r){r.$pristine=!r.$dirty}else if("$pristine"in r){r.$dirty=!r.$pristine}if("$touched"in r){r.$untouched=!r.$touched}else if("$untouched"in r){r.$touched=!r.$untouched}e.$state=v(v({},e.$state),r);return s()}return r}var Ce=function(t){_(n,t);var r=x(n);function n(){var e;k(this,n);for(var t=arguments.length,i=new Array(t),a=0;a with the name '".concat(t,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));u(t,"You should assign a name to , otherwise it will be isolated!");if(r.$$register){r.$$register(t,this.$fieldHandler)}this.$prevState=this.$state;re(this.props.$ref,this.$fieldutil)}},{key:"componentWillUnmount",value:function e(){if(this.$formContext.$$unregister){this.$formContext.$$unregister(this.props.name,this.$fieldHandler,this.props.$reserveOnUnmount)}this.isMounting=false;re(this.props.$ref,null)}},{key:"componentDidUpdate",value:function e(t){var r=this.props.name;if(r!==t.name){if(this.$formContext.$$register){this.$formContext.$$register(r,this.$fieldHandler,t.name)}}re(this.props.$ref,this.$fieldutil);if(this.$state.$value!==this.$prevState.$value){if(!(r in(this.$formContext.$$registers||{}))){this.$registered.$$triggerChange({$newValue:this.$state.$value,$prevValue:this.$prevState.$value})}}this.$prevState=this.$state}},{key:"shouldComponentUpdate",value:function e(t){var r=t.$memo;return!r||!le(this.$registered.$getState(),this.$prevState)||!(Array.isArray(r)?$(r,this.props.$memo):$(this.props,t))}},{key:"_render",value:function e(){var t=this.$fieldutil=v(v(v({$name:this.props.name},this.$registered.$getState()),this.$registered),{},{$$formutil:this.$formContext.$formutil});return ke(t,this.props)}},{key:"render",value:function t(){var r=this;this.shouldRendered=true;return e.createElement(N.Consumer,null,(function(e){var t=!r.$formContext;r.$formContext=e();if(!r.$fieldHandler){r.$fieldHandler=Se(r,r)}r.$registered=(r.$formContext.$$registers||{})[r.$fieldHandler.$name]||r.$fieldHandler;if(t){r.$fieldHandler.$$reset();r.$fieldHandler.$validate()}return r._render()}))}}]);return n}(a);Ce.displayName=Pe;var Oe=["name","$defaultValue","$defaultState","$onFieldChange","$validators","$asyncValidators","$validateLazy","$memo","$reserveOnUnmount","$ref","$parser","$formatter","render","component","children"];function Ee(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(n){_(a,n);var i=x(a);function a(){var r;k(this,a);for(var n=arguments.length,o=new Array(n),u=0;u.")}},{key:"render",value:function t(){var r=this.props,n=r.$value,i=r.onChange,a=r.onFocus,o=r.onBlur,u=pe(r,["$value","onChange","onFocus","onBlur"]);return e.createElement(Re,null,(function(t){var r=t();var s=r.type,l=r.name;var f=s==="radio"?{checked:r.value===n,onChange:function e(t){r.onChange(n,t);i&&i(t)}}:s==="checkbox"?{checked:r.value.indexOf(n)>-1,onChange:function e(t){r.onChange(t.target.checked?r.value.concat(n):r.value.filter((function(e){return e!==n})),t);i&&i(t)}}:{value:r.value,onChange:function e(t){r.onChange(t);i&&i(t)}};return e.createElement("input",Object.assign({name:l},u,f,{type:s,onFocus:function e(t){r.onFocus(t);a&&a(t)},onBlur:function e(t){r.onBlur(t);o&&o(t)}}))}))}}]);return n}(a);Ne.displayName="React.Formutil.EasyField.Group.Option";var Ue=function(t){_(n,t);var r=x(n);function n(){k(this,n);return r.apply(this,arguments)}C(n,[{key:"componentDidMount",value:function e(){u(false,'The "Field" property in EasyField\'s children-props has been deprecated. Please use "GroupOption" instead.')}},{key:"render",value:function t(){return e.createElement(Ne,this.props)}}]);return n}(a);Ue.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var Te=e.Frament||"div";var Ie=function(t){_(n,t);var r=x(n);function n(e){var t;k(this,n);t=r.call(this,e);t.id=0;t.latestValue=t.props.value;t.$formutil=void 0;t.FieldValidators={required:function e(t){return t!==null}};t.$onFormChange=function(e){e.$onValidates((function(e){var r=e.$invalid,n=e.$params;if(r){if(t.props.value.length){t.props.onChange(t.latestValue=[])}}else if(!$(t.props.value,n.list)){t.props.onChange(t.latestValue=n.list)}}))};t.swap=function(e,r,n){return t.$setState((function(t){var n=t.items;var i=[n[e],n[r]];n[r]=i[0];n[e]=i[1];return n}),n)};t.insert=function(){var e,r,n;for(var i=arguments.length,a=new Array(i),o=0;o=t*1}],["max",function(e,t){return B(e)||e*1<=t*1}],["min",function(e,t){return B(e)||e*1>=t*1}],["pattern",function(e,t){return B(e)||t.test(e)}],["enum",function(e,t){return B(e)||t.indexOf(e)>-1}],["checker",function(e,t,r){return t(e,r)}]].reduce((function(e,t){var r=V(t,2),n=r[0],i=r[1];e[n]=function e(t,r,a){var o=a.validMessage,u=o===void 0?{}:o;return i.apply(void 0,arguments)||u[n]||"Error input: ".concat(n)};return e}),{});var Le="React.Formutil.EasyField";var Be={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(t){return typeof t==="string"?t.trim():t}};function Ye(e,t,r){var n;var i=t.valuePropName,a=t.changePropName,o=t.focusPropName,u=t.blurPropName,s=t.getValueFromEvent,l=t.passUtil;var f=function e(t){return t&&t.target?t.target[i]:t};var $=v(v({},r),{},(n={},c(n,i,e.$viewValue),c(n,a,(function(r){var n;for(var i=arguments.length,o=new Array(i>1?i-1:0),u=1;u=16.8, Please upgrade your reactjs.")}var t=e.useContext;var r=t(N);return r()}function We(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!e.useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var n=e.useState,i=e.useLayoutEffect,a=e.useRef;var o;if(t){if(typeof t==="string"){o=t;r.name=o}else{r=t;o=r.name}}var s=Ke();var l=a({}).current;var f=a([]);var $;l.$formContext=s;l.props=r;l.$setState=h;l.shouldRendered=true;var c=n((function(){l.$$FIELD_UUID=Ve();l.$fieldHandler=$=Se(l);l.$fieldHandler.$$reset();l.$fieldHandler.$validate()})),d=V(c,2),p=d[1];if(!$){$=(s.$$registers||{})[l.$fieldHandler.$name]||l.$fieldHandler}i((function(){var e=l.$state;if(l.isMounting){if(!(o in(s.$$registers||{}))){$.$$triggerChange({$newValue:e.$value,$prevValue:l.$prevState.$value})}}l.$prevState=e}),[l.$state.$value]);i((function(){l.isMounting=true;u(!o||s.$formutil,"You should enusre that the useField() with the name '".concat(o,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));u(o,"You should pass a name argument to useField(), otherwise it will be isolated!");return function(){l.isMounting=false;re(r.$ref,null)}}),[]);i((function(){if(s.$$register){s.$$register(o,l.$fieldHandler)}return function(){if(s.$$unregister){s.$$unregister(o,l.$fieldHandler,!l.isMounting&&r.$reserveOnUnmount)}}}),[o]);i((function(){re(r.$ref,l.$fieldutil)}));i((function(){if(f.current.length>0){var e=b(f.current);f.current.length=0;while(e.length){e.pop()(l.$fieldutil)}}}));function h(e,t){return new Promise((function(r){var n=function e(){return r(K(t,l.$fieldutil))};if(l.isMounting){if(o in(s.$$registers||{})){l.shouldRendered=false;s.$$onChange(o,e,n);if(!l.shouldRendered){p({})}}else{$.$$merge(e);$.$$detectChange(e);p({});f.current.push(n)}}else{$.$$merge(e);n()}}))}return l.$fieldutil=v(v(v({$name:o},$.$getState()),$),{},{$$formutil:s.$formutil})}function Je(){var e=Ke(),t=e.$formutil;return t}function Xe(e){e=v(v(v({},Be),e),{},{children:null});var t=Qe(e),r=t.fieldProps,n=t.childProps;var i=We(r);return Ye(i,r,n)}export{ze as EasyField,Ce as Field,de as Form,qe as connect,N as formContext,We as useField,Je as useForm,Xe as useHandler,_e as withField,ge as withForm}; +import e,{createContext as t,createElement as r,Children as n,cloneElement as i,Component as a,forwardRef as o}from"react";import u from"warning";import{unstable_batchedUpdates as s}from"react-dom";import l from"react-is";import f from"hoist-non-react-statics";import $ from"react-fast-compare";function c(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t)n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}));r.push.apply(r,n)}return r}function v(e){for(var t=1;te.length)t=e.length;for(var r=0,n=new Array(t);r1?r-1:0),i=1;i-1}var Z=function e(t){try{var r=new Function("origin","global","return typeof ".concat(t," === 'number' || (typeof ").concat(t," !== 'undefined' && !(origin in global)) ? ").concat(t," : origin"));return r(t,H)}catch(e){return t}};function ee(){for(var e=arguments.length,t=new Array(e),r=0;r=0?[]:{}:Array.isArray(l[h])?b(l[h]):v({},l[h]);break;default:l[h]=a;break}}}}catch(e){u(false,"The name '%s' of Field seems is not a legal expression.",i)}if(t.length>2){return n}}function te(e,t){var r=t.split(I).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,i=r.length;n should always return None(null,undefined) or an object contains error message of Field.");return t.$render(r)}return t.$$setStates(e||{},(function(e,t){var r=t.$getState(),n=r.$error,i=n===void 0?{}:n;if(e){return{$error:v(v({},i),{},c({},fe,e))}}if(i[fe]){delete i[fe];return{$error:i}}}),r,true)};t.$getField=function(e){var r=t.$$getRegister(e);u(!e||r,"$getField('".concat(e,"') fail to find the matched Field. Maybe it has been unmounted."));u(e,"You should pass a name of the mounted Field to $getField().");if(r){var n;return(n=r.$new)===null||n===void 0?void 0:n.call(r)}};t.$$onChange=function(e,r,n){return t.$setStates(c({},e,r),n)};t.$$setStates=function(){var e=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var r=arguments.length>1?arguments[1]:undefined;var n=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var a=t.$$deepParseObject(e);var o=[];ae(t.$$registers,(function(e,n){var u=te(a,n);if(i||u){var s=e.$getState();var l=e&&r(u&&u.data,e);if(l){var f=e.$$merge(l),$=f.$value;o.push([e,l]);if("$value"in l||"$viewValue"in l){var c=ne(t.$$fieldChangedQueue,(function(e){return e.name===n}));if(c){c.$newValue=$}else{t.$$fieldChangedQueue.push({name:n,$newValue:$,$prevValue:s.$value})}}t.$$formShouldUpdateFields[n]=true}}}));return s((function(){o.forEach((function(e){var t=P(e,2),r=t[0],n=t[1];return r.$$detectChange(n)}));return t.$render(n)}))};t.$render=function(e){return new Promise((function(r){return t.forceUpdate((function(){return r(K(e,t.$formutil))}))}))};t.$validates=function(){var e;for(var r=arguments.length,n=new Array(r),i=0;i1&&arguments[1]!==undefined?arguments[1]:{};ae(t,(function(e,t){return ee(r,t,e)}));return r}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){re(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(t){re(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){re(this.props.$ref,null)}},{key:"_render",value:function e(){var t=this.$formutil;var a=this.props,o=a.children,u=a.render,s=a.component;if(s){return r(s,{$formutil:t})}if(L(u)){return u(t)}if(L(o)){return o(t)}return n.map(o,(function(e){return e&&z(e.type)?i(e,{$formutil:t}):e}))}},{key:"render",value:function t(){var r=this;var n=this.props.$processer;var i=Object.keys(this.$$registers).map((function(e){return{path:e,$state:r.$$registers[e].$getState()}}));var a=this.$$formShouldUpdateAll;var o=this.$formutil||{};var u=i.some((function(e){var t=e.$state;return t.$invalid}));var s=i.some((function(e){var t=e.$state;return t.$dirty}));var l=i.some((function(e){var t=e.$state;return t.$touched}));var f=i.some((function(e){var t=e.$state;return t.$focused}));var $=this.$$formPending||i.some((function(e){var t=e.$state;return t.$pending}));var c=a?{}:v({},o.$pureParams);var d=a?{}:v({},o.$states);var p=a?{}:v({},o.$errors);var h=a?{}:v({},o.$dirts);var m=a?{}:v({},o.$touches);var g=a?{}:v({},o.$focuses);var y=a?{}:v({},o.$pendings);var b=a?{}:v({},o.$weakStates);var w=a?{}:v({},o.$weakParams);var F=a?{}:v({},o.$weakErrors);var V=a?{}:v({},o.$weakDirts);var P=a?{}:v({},o.$weakFocuses);var S=a?{}:v({},o.$weakTouches);var k=a?{}:v({},o.$weakPendings);for(var C=0,O=i.length;C=0)continue;r[i]=e[i]}return r}function pe(e,t){if(e==null)return{};var r=ve(e,t);var n,i;if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(i=0;i=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;r[n]=e[n]}}return r}var he=["render","component","children","$defaultValues","$defaultStates","$onFormChange","$validator","$processer","$ref"];function me(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(n){_(a,n);var i=x(a);function a(){var r;S(this,a);for(var n=arguments.length,o=new Array(n),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var i=Object.assign({},e.$state.$error);if(we(r)){i[t]=r||t;Fe(r,t,e.props.name)}else{delete i[t]}return h(i,n)}function g(t,r){return e.$setState({$pending:t},r)}function y(){var t=e.$state.$error,r=t===void 0?{}:t;for(var n in r){if(r.hasOwnProperty(n)){return r[n]instanceof Error?r[n].message:r[n]}}}function b(t){var r=Object.assign({},t);if("$error"in r){if(!r.$error){r.$error={}}r.$valid=Object.keys(r.$error).length===0}var n=e.props,i=n.$parser,a=n.$formatter;if("$viewValue"in r&&!("$value"in r)){var o=function e(t){return r.$viewValue=t};r.$value=i?i(r.$viewValue,o):r.$viewValue}else if("$value"in r&&!("$viewValue"in r)){var u=function e(t){return r.$value=t};r.$viewValue=a?a(r.$value,u):r.$value}if("$valid"in r){r.$invalid=!r.$valid}else if("$invalid"in r){r.$dirty=!r.$invalid}if("$dirty"in r){r.$pristine=!r.$dirty}else if("$pristine"in r){r.$dirty=!r.$pristine}if("$touched"in r){r.$untouched=!r.$touched}else if("$untouched"in r){r.$touched=!r.$untouched}e.$state=v(v({},e.$state),r);return s()}return r}var Ce=function(t){_(n,t);var r=x(n);function n(){var e;S(this,n);for(var t=arguments.length,i=new Array(t),a=0;a with the name '".concat(t,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));u(t,"You should assign a name to , otherwise it will be isolated!");if(r.$$register){r.$$register(t,this.$fieldHandler)}this.$prevState=this.$state;re(this.props.$ref,this.$fieldutil)}},{key:"componentWillUnmount",value:function e(){if(this.$formContext.$$unregister){this.$formContext.$$unregister(this.props.name,this.$fieldHandler,this.props.$reserveOnUnmount)}this.isMounting=false;re(this.props.$ref,null)}},{key:"componentDidUpdate",value:function e(t){var r=this.props.name;if(r!==t.name){if(this.$formContext.$$register){this.$formContext.$$register(r,this.$fieldHandler,t.name)}}re(this.props.$ref,this.$fieldutil);if(this.$state.$value!==this.$prevState.$value){if(!(r in(this.$formContext.$$registers||{}))){this.$registered.$$triggerChange({$newValue:this.$state.$value,$prevValue:this.$prevState.$value})}}this.$prevState=this.$state}},{key:"shouldComponentUpdate",value:function e(t){var r=t.$memo;return!r||!le(this.$registered.$getState(),this.$prevState)||!(Array.isArray(r)?$(r,this.props.$memo):$(this.props,t))}},{key:"_render",value:function e(){var t=this.$fieldutil=v(v(v({$name:this.props.name},this.$registered.$getState()),this.$registered),{},{$$formutil:this.$formContext.$formutil});return Se(t,this.props)}},{key:"render",value:function t(){var r=this;this.shouldRendered=true;return e.createElement(N.Consumer,null,(function(e){var t=!r.$formContext;r.$formContext=e();if(!r.$fieldHandler){r.$fieldHandler=ke(r,r)}r.$registered=(r.$formContext.$$registers||{})[r.$fieldHandler.$name]||r.$fieldHandler;if(t){r.$fieldHandler.$$reset();r.$fieldHandler.$validate()}return r._render()}))}}]);return n}(a);Ce.displayName=Ve;var Oe=["name","$defaultValue","$defaultState","$onFieldChange","$validators","$asyncValidators","$validateLazy","$memo","$reserveOnUnmount","$ref","$parser","$formatter","render","component","children"];function Ee(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var n=function(n){_(a,n);var i=x(a);function a(){var r;S(this,a);for(var n=arguments.length,o=new Array(n),u=0;u.")}},{key:"render",value:function t(){var r=this.props,n=r.$value,i=r.onChange,a=r.onFocus,o=r.onBlur,u=pe(r,["$value","onChange","onFocus","onBlur"]);return e.createElement(Re,null,(function(t){var r=t();var s=r.type,l=r.name;var f=s==="radio"?{checked:r.value===n,onChange:function e(t){r.onChange(n,t);i&&i(t)}}:s==="checkbox"?{checked:r.value.indexOf(n)>-1,onChange:function e(t){r.onChange(t.target.checked?r.value.concat(n):r.value.filter((function(e){return e!==n})),t);i&&i(t)}}:{value:r.value,onChange:function e(t){r.onChange(t);i&&i(t)}};return e.createElement("input",Object.assign({name:l},u,f,{type:s,onFocus:function e(t){r.onFocus(t);a&&a(t)},onBlur:function e(t){r.onBlur(t);o&&o(t)}}))}))}}]);return n}(a);Ne.displayName="React.Formutil.EasyField.Group.Option";var Ue=function(t){_(n,t);var r=x(n);function n(){S(this,n);return r.apply(this,arguments)}C(n,[{key:"componentDidMount",value:function e(){u(false,'The "Field" property in EasyField\'s children-props has been deprecated. Please use "GroupOption" instead.')}},{key:"render",value:function t(){return e.createElement(Ne,this.props)}}]);return n}(a);Ue.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var Te=e.Frament||"div";var Ie=function(t){_(n,t);var r=x(n);function n(e){var t;S(this,n);t=r.call(this,e);t.id=0;t.latestValue=t.props.value;t.$formutil=void 0;t.FieldValidators={required:function e(t){return t!==null}};t.$onFormChange=function(e){e.$onValidates((function(e){var r=e.$invalid,n=e.$params;if(r){if(t.props.value.length){t.props.onChange(t.latestValue=[])}}else if(!$(t.props.value,n.list)){t.props.onChange(t.latestValue=n.list)}}))};t.swap=function(e,r,n){return t.$setState((function(t){var n=t.items;var i=[n[e],n[r]];n[r]=i[0];n[e]=i[1];return n}),n)};t.insert=function(){var e,r,n;for(var i=arguments.length,a=new Array(i),o=0;o=t*1}],["max",function(e,t){return B(e)||e*1<=t*1}],["min",function(e,t){return B(e)||e*1>=t*1}],["pattern",function(e,t){return B(e)||t.test(e)}],["enum",function(e,t){return B(e)||t.indexOf(e)>-1}],["checker",function(e,t,r){return t(e,r)}]].reduce((function(e,t){var r=P(t,2),n=r[0],i=r[1];e[n]=function e(t,r,a){var o=a.validMessage,u=o===void 0?{}:o;return i.apply(void 0,arguments)||u[n]||"Error input: ".concat(n)};return e}),{});var Le="React.Formutil.EasyField";var Be={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(t){return typeof t==="string"?t.trim():t}};function Ye(e,t,r){var n;var i=t.valuePropName,a=t.changePropName,o=t.focusPropName,u=t.blurPropName,s=t.getValueFromEvent,l=t.passUtil;var f=function e(t){return t&&t.target?t.target[i]:t};var $=v(v({},r),{},(n={},c(n,i,e.$viewValue),c(n,a,(function(r){var n;for(var i=arguments.length,o=new Array(i>1?i-1:0),u=1;u=16.8, Please upgrade your reactjs.")}var t=e.useContext;var r=t(N);return r()}function We(t){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!e.useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var n=e.useState,i=e.useLayoutEffect,a=e.useRef;var o;if(t){if(typeof t==="string"){o=t;r.name=o}else{r=t;o=r.name}}var s=Ke();var l=a({}).current;var f=a([]);var $;l.$formContext=s;l.props=r;l.$setState=h;l.shouldRendered=true;var c=n((function(){l.$$FIELD_UUID=Pe();l.$fieldHandler=$=ke(l);l.$fieldHandler.$$reset();l.$fieldHandler.$validate()})),d=P(c,2),p=d[1];if(!$){$=(s.$$registers||{})[l.$fieldHandler.$name]||l.$fieldHandler}i((function(){var e=l.$state;if(l.isMounting){if(!(o in(s.$$registers||{}))){$.$$triggerChange({$newValue:e.$value,$prevValue:l.$prevState.$value})}}l.$prevState=e}),[l.$state.$value]);i((function(){l.isMounting=true;u(!o||s.$formutil,"You should enusre that the useField() with the name '".concat(o,"' must be used underneath a component or withForm() HOC, otherwise it's isolated."));u(o,"You should pass a name argument to useField(), otherwise it will be isolated!");return function(){l.isMounting=false;re(r.$ref,null)}}),[]);i((function(){if(s.$$register){s.$$register(o,l.$fieldHandler)}return function(){if(s.$$unregister){s.$$unregister(o,l.$fieldHandler,!l.isMounting&&r.$reserveOnUnmount)}}}),[o]);i((function(){re(r.$ref,l.$fieldutil)}));i((function(){if(f.current.length>0){var e=b(f.current);f.current.length=0;while(e.length){e.pop()(l.$fieldutil)}}}));function h(e,t){return new Promise((function(r){var n=function e(){return r(K(t,l.$fieldutil))};if(l.isMounting){if(o in(s.$$registers||{})){l.shouldRendered=false;s.$$onChange(o,e,n);if(!l.shouldRendered){p({})}}else{$.$$merge(e);$.$$detectChange(e);p({});f.current.push(n)}}else{$.$$merge(e);n()}}))}return l.$fieldutil=v(v(v({$name:o},$.$getState()),$),{},{$$formutil:s.$formutil})}function Je(){var e=Ke(),t=e.$formutil;return t}function Xe(e){e=v(v(v({},Be),e),{},{children:null});var t=Qe(e),r=t.fieldProps,n=t.childProps;var i=We(r);return Ye(i,r,n)}export{ze as EasyField,Ce as Field,de as Form,qe as connect,N as formContext,We as useField,Je as useForm,Xe as useHandler,_e as withField,ge as withForm}; diff --git a/dist/react-formutil.umd.development.js b/dist/react-formutil.umd.development.js index 1587530..6f4302b 100644 --- a/dist/react-formutil.umd.development.js +++ b/dist/react-formutil.umd.development.js @@ -1194,6 +1194,7 @@ var pathData = pathExist($parsedTree, name); if (force || pathData) { + var $curState = handler.$getState(); var $newState = handler && processer(pathData && pathData.data, handler); if ($newState) { @@ -1209,11 +1210,11 @@ if (findItem) { findItem.$newValue = $newValue; - } else if (name in _this.$formutil.$weakParams) { + } else { _this.$$fieldChangedQueue.push({ name: name, $newValue: $newValue, - $prevValue: _this.$formutil.$weakParams[name] + $prevValue: $curState.$value }); } } diff --git a/dist/react-formutil.umd.production.js b/dist/react-formutil.umd.production.js index 3c2ad1f..3fadc75 100644 --- a/dist/react-formutil.umd.production.js +++ b/dist/react-formutil.umd.production.js @@ -1 +1 @@ -(function(e,r){typeof exports==="object"&&typeof module!=="undefined"?r(exports,require("react"),require("react-dom")):typeof define==="function"&&define.amd?define(["exports","react","react-dom"],r):(e=typeof globalThis!=="undefined"?globalThis:e||self,r(e.ReactFormutil={},e.React,e.ReactDOM))})(this,(function(e,r,t){"use strict";function n(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var a=n(r);function i(e,r,t){if(r in e){Object.defineProperty(e,r,{value:t,enumerable:true,configurable:true,writable:true})}else{e[r]=t}return e}var o=i;function u(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(r)n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}));t.push.apply(t,n)}return t}function s(e){for(var r=1;re.length)r=e.length;for(var t=0,n=new Array(r);t1?t-1:0),a=1;a-1}var mr=function e(r){try{var t=new Function("origin","global","return typeof ".concat(r," === 'number' || (typeof ").concat(r," !== 'undefined' && !(origin in global)) ? ").concat(r," : origin"));return t(r,ar)}catch(e){return r}};function yr(){for(var e=arguments.length,r=new Array(e),t=0;t=0?[]:{}:Array.isArray(s[p])?w(s[p]):f({},s[p]);break;default:s[p]=i;break}}}}catch(e){}if(r.length>2){return n}}function gr(e,r){var t=r.split(nr).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,a=t.length;n0&&arguments[0]!==undefined?arguments[0]:{};var n=arguments.length>1?arguments[1]:undefined;var a=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var o=r.$$deepParseObject(e);var u=[];Pr(r.$$registers,(function(e,t){var a=gr(o,t);if(i||a){var s=e&&n(a&&a.data,e);if(s){var f=e.$$merge(s),l=f.$value;u.push([e,s]);if("$value"in s||"$viewValue"in s){var c=wr(r.$$fieldChangedQueue,(function(e){return e.name===t}));if(c){c.$newValue=l}else if(t in r.$formutil.$weakParams){r.$$fieldChangedQueue.push({name:t,$newValue:l,$prevValue:r.$formutil.$weakParams[t]})}}r.$$formShouldUpdateFields[t]=true}}}));return t.unstable_batchedUpdates((function(){u.forEach((function(e){var r=E(e,2),t=r[0],n=r[1];return t.$$detectChange(n)}));return r.$render(a)}))};r.$render=function(e){return new Promise((function(t){return r.forceUpdate((function(){return t(dr(e,r.$formutil))}))}))};r.$validates=function(){var e;for(var t=arguments.length,n=new Array(t),a=0;a1&&arguments[1]!==undefined?arguments[1]:{};Pr(r,(function(e,r){return yr(t,r,e)}));return t}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){br(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(r){br(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){br(this.props.$ref,null)}},{key:"_render",value:function e(){var t=this.$formutil;var n=this.props,a=n.children,i=n.render,o=n.component;if(o){return r.createElement(o,{$formutil:t})}if(or(i)){return i(t)}if(or(a)){return a(t)}return r.Children.map(a,(function(e){return e&&cr(e.type)?r.cloneElement(e,{$formutil:t}):e}))}},{key:"render",value:function e(){var r=this;var t=this.props.$processer;var n=Object.keys(this.$$registers).map((function(e){return{path:e,$state:r.$$registers[e].$getState()}}));var i=this.$$formShouldUpdateAll;var o=this.$formutil||{};var u=n.some((function(e){var r=e.$state;return r.$invalid}));var s=n.some((function(e){var r=e.$state;return r.$dirty}));var l=n.some((function(e){var r=e.$state;return r.$touched}));var c=n.some((function(e){var r=e.$state;return r.$focused}));var $=this.$$formPending||n.some((function(e){var r=e.$state;return r.$pending}));var d=i?{}:f({},o.$pureParams);var v=i?{}:f({},o.$states);var p=i?{}:f({},o.$errors);var h=i?{}:f({},o.$dirts);var m=i?{}:f({},o.$touches);var y=i?{}:f({},o.$focuses);var g=i?{}:f({},o.$pendings);var b=i?{}:f({},o.$weakStates);var w=i?{}:f({},o.$weakParams);var S=i?{}:f({},o.$weakErrors);var P=i?{}:f({},o.$weakDirts);var C=i?{}:f({},o.$weakFocuses);var F=i?{}:f({},o.$weakTouches);var V=i?{}:f({},o.$weakPendings);for(var k=0,O=n.length;k=0)continue;t[a]=e[a]}return t}var Rr=xr;function Ar(e,r){if(e==null)return{};var t=Rr(e,r);var n,a;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;t[n]=e[n]}}return t}var Dr=Ar;var Nr="function"===typeof Symbol&&Symbol.for,Ur=Nr?Symbol.for("react.element"):60103,Mr=Nr?Symbol.for("react.portal"):60106,Tr=Nr?Symbol.for("react.fragment"):60107,Ir=Nr?Symbol.for("react.strict_mode"):60108,Lr=Nr?Symbol.for("react.profiler"):60114,Hr=Nr?Symbol.for("react.provider"):60109,zr=Nr?Symbol.for("react.context"):60110,Br=Nr?Symbol.for("react.async_mode"):60111,Qr=Nr?Symbol.for("react.concurrent_mode"):60111,qr=Nr?Symbol.for("react.forward_ref"):60112,Gr=Nr?Symbol.for("react.suspense"):60113,Kr=Nr?Symbol.for("react.suspense_list"):60120,Yr=Nr?Symbol.for("react.memo"):60115,Wr=Nr?Symbol.for("react.lazy"):60116,Jr=Nr?Symbol.for("react.block"):60121,Xr=Nr?Symbol.for("react.fundamental"):60117,Zr=Nr?Symbol.for("react.responder"):60118,et=Nr?Symbol.for("react.scope"):60119;function rt(e){if("object"===typeof e&&null!==e){var r=e.$$typeof;switch(r){case Ur:switch(e=e.type,e){case Br:case Qr:case Tr:case Lr:case Ir:case Gr:return e;default:switch(e=e&&e.$$typeof,e){case zr:case qr:case Wr:case Yr:case Hr:return e;default:return r}}case Mr:return r}}}function tt(e){return rt(e)===Qr}var nt=Br;var at=Qr;var it=zr;var ot=Hr;var ut=Ur;var st=qr;var ft=Tr;var lt=Wr;var ct=Yr;var $t=Mr;var dt=Lr;var vt=Ir;var pt=Gr;var ht=function(e){return tt(e)||rt(e)===Br};var mt=tt;var yt=function(e){return rt(e)===zr};var gt=function(e){return rt(e)===Hr};var bt=function(e){return"object"===typeof e&&null!==e&&e.$$typeof===Ur};var wt=function(e){return rt(e)===qr};var St=function(e){return rt(e)===Tr};var Pt=function(e){return rt(e)===Wr};var Ct=function(e){return rt(e)===Yr};var Ft=function(e){return rt(e)===Mr};var Vt=function(e){return rt(e)===Lr};var kt=function(e){return rt(e)===Ir};var Ot=function(e){return rt(e)===Gr};var Et=function(e){return"string"===typeof e||"function"===typeof e||e===Tr||e===Qr||e===Lr||e===Ir||e===Gr||e===Kr||"object"===typeof e&&null!==e&&(e.$$typeof===Wr||e.$$typeof===Yr||e.$$typeof===Hr||e.$$typeof===zr||e.$$typeof===qr||e.$$typeof===Xr||e.$$typeof===Zr||e.$$typeof===et||e.$$typeof===Jr)};var _t=rt;var jt={AsyncMode:nt,ConcurrentMode:at,ContextConsumer:it,ContextProvider:ot,Element:ut,ForwardRef:st,Fragment:ft,Lazy:lt,Memo:ct,Portal:$t,Profiler:dt,StrictMode:vt,Suspense:pt,isAsyncMode:ht,isConcurrentMode:mt,isContextConsumer:yt,isContextProvider:gt,isElement:bt,isForwardRef:wt,isFragment:St,isLazy:Pt,isMemo:Ct,isPortal:Ft,isProfiler:Vt,isStrictMode:kt,isSuspense:Ot,isValidElementType:Et,typeOf:_t};var xt=U((function(e){{e.exports=jt}}));var Rt={childContextTypes:true,contextType:true,contextTypes:true,defaultProps:true,displayName:true,getDefaultProps:true,getDerivedStateFromError:true,getDerivedStateFromProps:true,mixins:true,propTypes:true,type:true};var At={name:true,length:true,prototype:true,caller:true,callee:true,arguments:true,arity:true};var Dt={$$typeof:true,render:true,defaultProps:true,displayName:true,propTypes:true};var Nt={$$typeof:true,compare:true,defaultProps:true,displayName:true,propTypes:true,type:true};var Ut={};Ut[xt.ForwardRef]=Dt;Ut[xt.Memo]=Nt;function Mt(e){if(xt.isMemo(e)){return Nt}return Ut[e["$$typeof"]]||Rt}var Tt=Object.defineProperty;var It=Object.getOwnPropertyNames;var Lt=Object.getOwnPropertySymbols;var Ht=Object.getOwnPropertyDescriptor;var zt=Object.getPrototypeOf;var Bt=Object.prototype;function Qt(e,r,t){if(typeof r!=="string"){if(Bt){var n=zt(r);if(n&&n!==Bt){Qt(e,n,t)}}var a=It(r);if(Lt){a=a.concat(Lt(r))}var i=Mt(e);var o=Mt(r);for(var u=0;u1&&arguments[1]!==undefined?arguments[1]:{};var n=function(r){I(i,r);var n=K(i);function i(){var r;j(this,i);for(var t=arguments.length,o=new Array(t),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var a=Object.assign({},e.$state.$error);if(an(t)){a[r]=t||r;on(t,r,e.props.name)}else{delete a[r]}return h(a,n)}function y(r,t){return e.$setState({$pending:r},t)}function g(){var r=e.$state.$error,t=r===void 0?{}:r;for(var n in t){if(t.hasOwnProperty(n)){return t[n]instanceof Error?t[n].message:t[n]}}}function b(r){var t=Object.assign({},r);if("$error"in t){if(!t.$error){t.$error={}}t.$valid=Object.keys(t.$error).length===0}var n=e.props,a=n.$parser,i=n.$formatter;if("$viewValue"in t&&!("$value"in t)){var o=function e(r){return t.$viewValue=r};t.$value=a?a(t.$viewValue,o):t.$viewValue}else if("$value"in t&&!("$viewValue"in t)){var u=function e(r){return t.$value=r};t.$viewValue=i?i(t.$value,u):t.$value}if("$valid"in t){t.$invalid=!t.$valid}else if("$invalid"in t){t.$dirty=!t.$invalid}if("$dirty"in t){t.$pristine=!t.$dirty}else if("$pristine"in t){t.$dirty=!t.$pristine}if("$touched"in t){t.$untouched=!t.$touched}else if("$untouched"in t){t.$touched=!t.$untouched}e.$state=f(f({},e.$state),t);return s()}return t}var cn=function(e){I(t,e);var r=K(t);function t(){var e;j(this,t);for(var n=arguments.length,a=new Array(n),i=0;i1&&arguments[1]!==undefined?arguments[1]:{};var n=function(r){I(i,r);var n=K(i);function i(){var r;j(this,i);for(var t=arguments.length,o=new Array(t),u=0;u-1,onChange:function e(a){r.onChange(a.target.checked?r.value.concat(t):r.value.filter((function(e){return e!==t})),a);n&&n(a)}}:{value:r.value,onChange:function e(t){r.onChange(t);n&&n(t)}};return a["default"].createElement("input",Object.assign({name:f},u,l,{type:s,onFocus:function e(t){r.onFocus(t);i&&i(t)},onBlur:function e(t){r.onBlur(t);o&&o(t)}}))}))}}]);return t}(r.Component);bn.displayName="React.Formutil.EasyField.Group.Option";var wn=function(e){I(t,e);var r=K(t);function t(){j(this,t);return r.apply(this,arguments)}A(t,[{key:"componentDidMount",value:function e(){}},{key:"render",value:function e(){return a["default"].createElement(bn,this.props)}}]);return t}(r.Component);wn.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var Sn=a["default"].Frament||"div";var Pn=function(e){I(t,e);var r=K(t);function t(e){var n;j(this,t);n=r.call(this,e);n.id=0;n.latestValue=n.props.value;n.$formutil=void 0;n.FieldValidators={required:function e(r){return r!==null}};n.$onFormChange=function(e){e.$onValidates((function(e){var r=e.$invalid,t=e.$params;if(r){if(n.props.value.length){n.props.onChange(n.latestValue=[])}}else if(!rn(n.props.value,t.list)){n.props.onChange(n.latestValue=t.list)}}))};n.swap=function(e,r,t){return n.$setState((function(t){var n=t.items;var a=[n[e],n[r]];n[r]=a[0];n[e]=a[1];return n}),t)};n.insert=function(){var e,r,t;for(var a=arguments.length,i=new Array(a),o=0;o=r*1}],["max",function(e,r){return ur(e)||e*1<=r*1}],["min",function(e,r){return ur(e)||e*1>=r*1}],["pattern",function(e,r){return ur(e)||r.test(e)}],["enum",function(e,r){return ur(e)||r.indexOf(e)>-1}],["checker",function(e,r,t){return r(e,t)}]].reduce((function(e,r){var t=E(r,2),n=t[0],a=t[1];e[n]=function e(r,t,i){var o=i.validMessage,u=o===void 0?{}:o;return a.apply(void 0,arguments)||u[n]||"Error input: ".concat(n)};return e}),{});var Vn="React.Formutil.EasyField";var kn={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(r){return typeof r==="string"?r.trim():r}};function On(e,r,t){var n;var a=r.valuePropName,i=r.changePropName,u=r.focusPropName,s=r.blurPropName,l=r.getValueFromEvent,c=r.passUtil;var $=function e(r){return r&&r.target?r.target[a]:r};var d=f(f({},t),{},(n={},o(n,a,e.$viewValue),o(n,i,(function(t){var n;for(var a=arguments.length,o=new Array(a>1?a-1:0),u=1;u=16.8, Please upgrade your reactjs.")}var e=a["default"].useContext;var r=e(J);return r()}function An(e){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!a["default"].useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var t=a["default"].useState,n=a["default"].useLayoutEffect,i=a["default"].useRef;var o;if(e){if(typeof e==="string"){o=e;r.name=o}else{r=e;o=r.name}}var u=Rn();var s=i({}).current;var l=i([]);var c;s.$formContext=u;s.props=r;s.$setState=p;s.shouldRendered=true;var $=t((function(){s.$$FIELD_UUID=sn();s.$fieldHandler=c=ln(s);s.$fieldHandler.$$reset();s.$fieldHandler.$validate()})),d=E($,2),v=d[1];if(!c){c=(u.$$registers||{})[s.$fieldHandler.$name]||s.$fieldHandler}n((function(){var e=s.$state;if(s.isMounting){if(!(o in(u.$$registers||{}))){c.$$triggerChange({$newValue:e.$value,$prevValue:s.$prevState.$value})}}s.$prevState=e}),[s.$state.$value]);n((function(){s.isMounting=true;W(!o||u.$formutil);return function(){s.isMounting=false;br(r.$ref,null)}}),[]);n((function(){if(u.$$register){u.$$register(o,s.$fieldHandler)}return function(){if(u.$$unregister){u.$$unregister(o,s.$fieldHandler,!s.isMounting&&r.$reserveOnUnmount)}}}),[o]);n((function(){br(r.$ref,s.$fieldutil)}));n((function(){if(l.current.length>0){var e=w(l.current);l.current.length=0;while(e.length){e.pop()(s.$fieldutil)}}}));function p(e,r){return new Promise((function(t){var n=function e(){return t(dr(r,s.$fieldutil))};if(s.isMounting){if(o in(u.$$registers||{})){s.shouldRendered=false;u.$$onChange(o,e,n);if(!s.shouldRendered){v({})}}else{c.$$merge(e);c.$$detectChange(e);v({});l.current.push(n)}}else{c.$$merge(e);n()}}))}return s.$fieldutil=f(f(f({$name:o},c.$getState()),c),{},{$$formutil:u.$formutil})}function Dn(){var e=Rn(),r=e.$formutil;return r}function Nn(e){e=f(f(f({},kn),e),{},{children:null});var r=En(e),t=r.fieldProps,n=r.childProps;var a=An(t);return On(a,t,n)}e.EasyField=jn;e.Field=cn;e.Form=jr;e.connect=xn;e.formContext=J;e.useField=An;e.useForm=Dn;e.useHandler=Nn;e.withField=vn;e.withForm=Yt;Object.defineProperty(e,"__esModule",{value:true})})); +(function(e,r){typeof exports==="object"&&typeof module!=="undefined"?r(exports,require("react"),require("react-dom")):typeof define==="function"&&define.amd?define(["exports","react","react-dom"],r):(e=typeof globalThis!=="undefined"?globalThis:e||self,r(e.ReactFormutil={},e.React,e.ReactDOM))})(this,(function(e,r,t){"use strict";function n(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var a=n(r);function i(e,r,t){if(r in e){Object.defineProperty(e,r,{value:t,enumerable:true,configurable:true,writable:true})}else{e[r]=t}return e}var o=i;function u(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(r)n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}));t.push.apply(t,n)}return t}function s(e){for(var r=1;re.length)r=e.length;for(var t=0,n=new Array(r);t1?t-1:0),a=1;a-1}var mr=function e(r){try{var t=new Function("origin","global","return typeof ".concat(r," === 'number' || (typeof ").concat(r," !== 'undefined' && !(origin in global)) ? ").concat(r," : origin"));return t(r,ar)}catch(e){return r}};function yr(){for(var e=arguments.length,r=new Array(e),t=0;t=0?[]:{}:Array.isArray(s[p])?w(s[p]):f({},s[p]);break;default:s[p]=i;break}}}}catch(e){}if(r.length>2){return n}}function gr(e,r){var t=r.split(nr).map((function(e){return e.trim()})).filter((function(e){return e!==""}));for(var n=0,a=t.length;n0&&arguments[0]!==undefined?arguments[0]:{};var n=arguments.length>1?arguments[1]:undefined;var a=arguments.length>2?arguments[2]:undefined;var i=arguments.length>3?arguments[3]:undefined;var o=r.$$deepParseObject(e);var u=[];Cr(r.$$registers,(function(e,t){var a=gr(o,t);if(i||a){var s=e.$getState();var f=e&&n(a&&a.data,e);if(f){var l=e.$$merge(f),c=l.$value;u.push([e,f]);if("$value"in f||"$viewValue"in f){var $=wr(r.$$fieldChangedQueue,(function(e){return e.name===t}));if($){$.$newValue=c}else{r.$$fieldChangedQueue.push({name:t,$newValue:c,$prevValue:s.$value})}}r.$$formShouldUpdateFields[t]=true}}}));return t.unstable_batchedUpdates((function(){u.forEach((function(e){var r=E(e,2),t=r[0],n=r[1];return t.$$detectChange(n)}));return r.$render(a)}))};r.$render=function(e){return new Promise((function(t){return r.forceUpdate((function(){return t(dr(e,r.$formutil))}))}))};r.$validates=function(){var e;for(var t=arguments.length,n=new Array(t),a=0;a1&&arguments[1]!==undefined?arguments[1]:{};Cr(r,(function(e,r){return yr(t,r,e)}));return t}},{key:"$$resetFormUpdateFields",value:function e(){this.$$formShouldUpdateFields={};this.$$formShouldUpdateAll=false}},{key:"componentDidMount",value:function e(){br(this.props.$ref,this.$formutil)}},{key:"componentDidUpdate",value:function e(r){br(this.props.$ref,this.$formutil);this.$$triggerFormChange()}},{key:"componentWillUnmount",value:function e(){br(this.props.$ref,null)}},{key:"_render",value:function e(){var t=this.$formutil;var n=this.props,a=n.children,i=n.render,o=n.component;if(o){return r.createElement(o,{$formutil:t})}if(or(i)){return i(t)}if(or(a)){return a(t)}return r.Children.map(a,(function(e){return e&&cr(e.type)?r.cloneElement(e,{$formutil:t}):e}))}},{key:"render",value:function e(){var r=this;var t=this.props.$processer;var n=Object.keys(this.$$registers).map((function(e){return{path:e,$state:r.$$registers[e].$getState()}}));var i=this.$$formShouldUpdateAll;var o=this.$formutil||{};var u=n.some((function(e){var r=e.$state;return r.$invalid}));var s=n.some((function(e){var r=e.$state;return r.$dirty}));var l=n.some((function(e){var r=e.$state;return r.$touched}));var c=n.some((function(e){var r=e.$state;return r.$focused}));var $=this.$$formPending||n.some((function(e){var r=e.$state;return r.$pending}));var d=i?{}:f({},o.$pureParams);var v=i?{}:f({},o.$states);var p=i?{}:f({},o.$errors);var h=i?{}:f({},o.$dirts);var m=i?{}:f({},o.$touches);var y=i?{}:f({},o.$focuses);var g=i?{}:f({},o.$pendings);var b=i?{}:f({},o.$weakStates);var w=i?{}:f({},o.$weakParams);var S=i?{}:f({},o.$weakErrors);var C=i?{}:f({},o.$weakDirts);var P=i?{}:f({},o.$weakFocuses);var F=i?{}:f({},o.$weakTouches);var V=i?{}:f({},o.$weakPendings);for(var O=0,k=n.length;O=0)continue;t[a]=e[a]}return t}var Rr=xr;function Ar(e,r){if(e==null)return{};var t=Rr(e,r);var n,a;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;t[n]=e[n]}}return t}var Dr=Ar;var Nr="function"===typeof Symbol&&Symbol.for,Ur=Nr?Symbol.for("react.element"):60103,Mr=Nr?Symbol.for("react.portal"):60106,Tr=Nr?Symbol.for("react.fragment"):60107,Ir=Nr?Symbol.for("react.strict_mode"):60108,Lr=Nr?Symbol.for("react.profiler"):60114,Hr=Nr?Symbol.for("react.provider"):60109,zr=Nr?Symbol.for("react.context"):60110,Br=Nr?Symbol.for("react.async_mode"):60111,Qr=Nr?Symbol.for("react.concurrent_mode"):60111,qr=Nr?Symbol.for("react.forward_ref"):60112,Gr=Nr?Symbol.for("react.suspense"):60113,Kr=Nr?Symbol.for("react.suspense_list"):60120,Yr=Nr?Symbol.for("react.memo"):60115,Wr=Nr?Symbol.for("react.lazy"):60116,Jr=Nr?Symbol.for("react.block"):60121,Xr=Nr?Symbol.for("react.fundamental"):60117,Zr=Nr?Symbol.for("react.responder"):60118,et=Nr?Symbol.for("react.scope"):60119;function rt(e){if("object"===typeof e&&null!==e){var r=e.$$typeof;switch(r){case Ur:switch(e=e.type,e){case Br:case Qr:case Tr:case Lr:case Ir:case Gr:return e;default:switch(e=e&&e.$$typeof,e){case zr:case qr:case Wr:case Yr:case Hr:return e;default:return r}}case Mr:return r}}}function tt(e){return rt(e)===Qr}var nt=Br;var at=Qr;var it=zr;var ot=Hr;var ut=Ur;var st=qr;var ft=Tr;var lt=Wr;var ct=Yr;var $t=Mr;var dt=Lr;var vt=Ir;var pt=Gr;var ht=function(e){return tt(e)||rt(e)===Br};var mt=tt;var yt=function(e){return rt(e)===zr};var gt=function(e){return rt(e)===Hr};var bt=function(e){return"object"===typeof e&&null!==e&&e.$$typeof===Ur};var wt=function(e){return rt(e)===qr};var St=function(e){return rt(e)===Tr};var Ct=function(e){return rt(e)===Wr};var Pt=function(e){return rt(e)===Yr};var Ft=function(e){return rt(e)===Mr};var Vt=function(e){return rt(e)===Lr};var Ot=function(e){return rt(e)===Ir};var kt=function(e){return rt(e)===Gr};var Et=function(e){return"string"===typeof e||"function"===typeof e||e===Tr||e===Qr||e===Lr||e===Ir||e===Gr||e===Kr||"object"===typeof e&&null!==e&&(e.$$typeof===Wr||e.$$typeof===Yr||e.$$typeof===Hr||e.$$typeof===zr||e.$$typeof===qr||e.$$typeof===Xr||e.$$typeof===Zr||e.$$typeof===et||e.$$typeof===Jr)};var _t=rt;var jt={AsyncMode:nt,ConcurrentMode:at,ContextConsumer:it,ContextProvider:ot,Element:ut,ForwardRef:st,Fragment:ft,Lazy:lt,Memo:ct,Portal:$t,Profiler:dt,StrictMode:vt,Suspense:pt,isAsyncMode:ht,isConcurrentMode:mt,isContextConsumer:yt,isContextProvider:gt,isElement:bt,isForwardRef:wt,isFragment:St,isLazy:Ct,isMemo:Pt,isPortal:Ft,isProfiler:Vt,isStrictMode:Ot,isSuspense:kt,isValidElementType:Et,typeOf:_t};var xt=U((function(e){{e.exports=jt}}));var Rt={childContextTypes:true,contextType:true,contextTypes:true,defaultProps:true,displayName:true,getDefaultProps:true,getDerivedStateFromError:true,getDerivedStateFromProps:true,mixins:true,propTypes:true,type:true};var At={name:true,length:true,prototype:true,caller:true,callee:true,arguments:true,arity:true};var Dt={$$typeof:true,render:true,defaultProps:true,displayName:true,propTypes:true};var Nt={$$typeof:true,compare:true,defaultProps:true,displayName:true,propTypes:true,type:true};var Ut={};Ut[xt.ForwardRef]=Dt;Ut[xt.Memo]=Nt;function Mt(e){if(xt.isMemo(e)){return Nt}return Ut[e["$$typeof"]]||Rt}var Tt=Object.defineProperty;var It=Object.getOwnPropertyNames;var Lt=Object.getOwnPropertySymbols;var Ht=Object.getOwnPropertyDescriptor;var zt=Object.getPrototypeOf;var Bt=Object.prototype;function Qt(e,r,t){if(typeof r!=="string"){if(Bt){var n=zt(r);if(n&&n!==Bt){Qt(e,n,t)}}var a=It(r);if(Lt){a=a.concat(Lt(r))}var i=Mt(e);var o=Mt(r);for(var u=0;u1&&arguments[1]!==undefined?arguments[1]:{};var n=function(r){I(i,r);var n=K(i);function i(){var r;j(this,i);for(var t=arguments.length,o=new Array(t),u=0;u1&&arguments[1]!==undefined?arguments[1]:true;var n=arguments.length>2?arguments[2]:undefined;var a=Object.assign({},e.$state.$error);if(an(t)){a[r]=t||r;on(t,r,e.props.name)}else{delete a[r]}return h(a,n)}function y(r,t){return e.$setState({$pending:r},t)}function g(){var r=e.$state.$error,t=r===void 0?{}:r;for(var n in t){if(t.hasOwnProperty(n)){return t[n]instanceof Error?t[n].message:t[n]}}}function b(r){var t=Object.assign({},r);if("$error"in t){if(!t.$error){t.$error={}}t.$valid=Object.keys(t.$error).length===0}var n=e.props,a=n.$parser,i=n.$formatter;if("$viewValue"in t&&!("$value"in t)){var o=function e(r){return t.$viewValue=r};t.$value=a?a(t.$viewValue,o):t.$viewValue}else if("$value"in t&&!("$viewValue"in t)){var u=function e(r){return t.$value=r};t.$viewValue=i?i(t.$value,u):t.$value}if("$valid"in t){t.$invalid=!t.$valid}else if("$invalid"in t){t.$dirty=!t.$invalid}if("$dirty"in t){t.$pristine=!t.$dirty}else if("$pristine"in t){t.$dirty=!t.$pristine}if("$touched"in t){t.$untouched=!t.$touched}else if("$untouched"in t){t.$touched=!t.$untouched}e.$state=f(f({},e.$state),t);return s()}return t}var cn=function(e){I(t,e);var r=K(t);function t(){var e;j(this,t);for(var n=arguments.length,a=new Array(n),i=0;i1&&arguments[1]!==undefined?arguments[1]:{};var n=function(r){I(i,r);var n=K(i);function i(){var r;j(this,i);for(var t=arguments.length,o=new Array(t),u=0;u-1,onChange:function e(a){r.onChange(a.target.checked?r.value.concat(t):r.value.filter((function(e){return e!==t})),a);n&&n(a)}}:{value:r.value,onChange:function e(t){r.onChange(t);n&&n(t)}};return a["default"].createElement("input",Object.assign({name:f},u,l,{type:s,onFocus:function e(t){r.onFocus(t);i&&i(t)},onBlur:function e(t){r.onBlur(t);o&&o(t)}}))}))}}]);return t}(r.Component);bn.displayName="React.Formutil.EasyField.Group.Option";var wn=function(e){I(t,e);var r=K(t);function t(){j(this,t);return r.apply(this,arguments)}A(t,[{key:"componentDidMount",value:function e(){}},{key:"render",value:function e(){return a["default"].createElement(bn,this.props)}}]);return t}(r.Component);wn.displayName="React.Formutil.EasyField.Group.Option.Deprecated";var Sn=a["default"].Frament||"div";var Cn=function(e){I(t,e);var r=K(t);function t(e){var n;j(this,t);n=r.call(this,e);n.id=0;n.latestValue=n.props.value;n.$formutil=void 0;n.FieldValidators={required:function e(r){return r!==null}};n.$onFormChange=function(e){e.$onValidates((function(e){var r=e.$invalid,t=e.$params;if(r){if(n.props.value.length){n.props.onChange(n.latestValue=[])}}else if(!rn(n.props.value,t.list)){n.props.onChange(n.latestValue=t.list)}}))};n.swap=function(e,r,t){return n.$setState((function(t){var n=t.items;var a=[n[e],n[r]];n[r]=a[0];n[e]=a[1];return n}),t)};n.insert=function(){var e,r,t;for(var a=arguments.length,i=new Array(a),o=0;o=r*1}],["max",function(e,r){return ur(e)||e*1<=r*1}],["min",function(e,r){return ur(e)||e*1>=r*1}],["pattern",function(e,r){return ur(e)||r.test(e)}],["enum",function(e,r){return ur(e)||r.indexOf(e)>-1}],["checker",function(e,r,t){return r(e,t)}]].reduce((function(e,r){var t=E(r,2),n=t[0],a=t[1];e[n]=function e(r,t,i){var o=i.validMessage,u=o===void 0?{}:o;return a.apply(void 0,arguments)||u[n]||"Error input: ".concat(n)};return e}),{});var Vn="React.Formutil.EasyField";var On={validMessage:{},valuePropName:"value",changePropName:"onChange",focusPropName:"onFocus",blurPropName:"onBlur",$parser:function e(r){return typeof r==="string"?r.trim():r}};function kn(e,r,t){var n;var a=r.valuePropName,i=r.changePropName,u=r.focusPropName,s=r.blurPropName,l=r.getValueFromEvent,c=r.passUtil;var $=function e(r){return r&&r.target?r.target[a]:r};var d=f(f({},t),{},(n={},o(n,a,e.$viewValue),o(n,i,(function(t){var n;for(var a=arguments.length,o=new Array(a>1?a-1:0),u=1;u=16.8, Please upgrade your reactjs.")}var e=a["default"].useContext;var r=e(J);return r()}function An(e){var r=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(!a["default"].useState){throw new Error("Hooks api need react@>=16.8, Please upgrade your reactjs.")}var t=a["default"].useState,n=a["default"].useLayoutEffect,i=a["default"].useRef;var o;if(e){if(typeof e==="string"){o=e;r.name=o}else{r=e;o=r.name}}var u=Rn();var s=i({}).current;var l=i([]);var c;s.$formContext=u;s.props=r;s.$setState=p;s.shouldRendered=true;var $=t((function(){s.$$FIELD_UUID=sn();s.$fieldHandler=c=ln(s);s.$fieldHandler.$$reset();s.$fieldHandler.$validate()})),d=E($,2),v=d[1];if(!c){c=(u.$$registers||{})[s.$fieldHandler.$name]||s.$fieldHandler}n((function(){var e=s.$state;if(s.isMounting){if(!(o in(u.$$registers||{}))){c.$$triggerChange({$newValue:e.$value,$prevValue:s.$prevState.$value})}}s.$prevState=e}),[s.$state.$value]);n((function(){s.isMounting=true;W(!o||u.$formutil);return function(){s.isMounting=false;br(r.$ref,null)}}),[]);n((function(){if(u.$$register){u.$$register(o,s.$fieldHandler)}return function(){if(u.$$unregister){u.$$unregister(o,s.$fieldHandler,!s.isMounting&&r.$reserveOnUnmount)}}}),[o]);n((function(){br(r.$ref,s.$fieldutil)}));n((function(){if(l.current.length>0){var e=w(l.current);l.current.length=0;while(e.length){e.pop()(s.$fieldutil)}}}));function p(e,r){return new Promise((function(t){var n=function e(){return t(dr(r,s.$fieldutil))};if(s.isMounting){if(o in(u.$$registers||{})){s.shouldRendered=false;u.$$onChange(o,e,n);if(!s.shouldRendered){v({})}}else{c.$$merge(e);c.$$detectChange(e);v({});l.current.push(n)}}else{c.$$merge(e);n()}}))}return s.$fieldutil=f(f(f({$name:o},c.$getState()),c),{},{$$formutil:u.$formutil})}function Dn(){var e=Rn(),r=e.$formutil;return r}function Nn(e){e=f(f(f({},On),e),{},{children:null});var r=En(e),t=r.fieldProps,n=r.childProps;var a=An(t);return kn(a,t,n)}e.EasyField=jn;e.Field=cn;e.Form=jr;e.connect=xn;e.formContext=J;e.useField=An;e.useForm=Dn;e.useHandler=Nn;e.withField=vn;e.withForm=Yt;Object.defineProperty(e,"__esModule",{value:true})})); diff --git a/package.json b/package.json index 401d482..4937c22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-formutil", - "version": "1.1.1", + "version": "1.1.2", "description": "Happy to build the forms in React ^_^", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/src/Form.js b/src/Form.js index 48852d5..34ee23d 100644 --- a/src/Form.js +++ b/src/Form.js @@ -366,6 +366,7 @@ class Form extends Component { const pathData = utils.pathExist($parsedTree, name); if (force || pathData) { + const $curState = handler.$getState(); const $newState = handler && processer(pathData && pathData.data, handler); if ($newState) { @@ -378,11 +379,11 @@ class Form extends Component { if (findItem) { findItem.$newValue = $newValue; - } else if (name in this.$formutil.$weakParams) { + } else { this.$$fieldChangedQueue.push({ name, $newValue, - $prevValue: this.$formutil.$weakParams[name] + $prevValue: $curState.$value }); } }