UNPKG

react-antd-formutil

Version:

Happy to use react-formutil in the project based on ant-design ^_^

2 lines (1 loc) 7.9 kB
import{EasyField as e}from"react-formutil";export*from"react-formutil";import t,{Children as r,cloneElement as n,Component as o,createContext as i}from"react";import{Form as a,Switch as u,Checkbox as c,Radio as l,Transfer as s,Pagination as f,Upload as p,Select as d}from"antd";import v from"react-is";import m from"react-fast-compare";function y(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 h(e){"@babel/helpers - typeof";if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){h=function e(t){return typeof t}}else{h=function e(t){return t&&typeof Symbol==="function"&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t}}return h(e)}function b(e,t){if(h(e)!=="object"||e===null)return e;var r=e[Symbol.toPrimitive];if(r!==undefined){var n=r.call(e,t||"default");if(h(n)!=="object")return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return(t==="string"?String:Number)(e)}function g(e){var t=b(e,"string");return h(t)==="symbol"?t:String(t)}function P(e,t){if(e==null)return{};var r={};var n=Object.keys(e);var o,i;for(i=0;i<n.length;i++){o=n[i];if(t.indexOf(o)>=0)continue;r[o]=e[o]}return r}function k(e,t){if(e==null)return{};var r=P(e,t);var n,o;if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(o=0;o<i.length;o++){n=i[o];if(t.indexOf(n)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(e,n))continue;r[n]=e[n]}}return r}function O(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}function _(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||false;n.configurable=true;if("value"in n)n.writable=true;Object.defineProperty(e,n.key,n)}}function j(e,t,r){if(t)_(e.prototype,t);if(r)_(e,r);return e}function $(e,t){$=Object.setPrototypeOf||function e(t,r){t.__proto__=r;return t};return $(e,t)}function S(e,t){if(typeof t!=="function"&&t!==null){throw new TypeError("Super expression must either be null or a function")}e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:true,configurable:true}});if(t)$(e,t)}function w(e){w=Object.setPrototypeOf?Object.getPrototypeOf:function e(t){return t.__proto__||Object.getPrototypeOf(t)};return w(e)}function C(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{Date.prototype.toString.call(Reflect.construct(Date,[],(function(){})));return true}catch(e){return false}}function E(e){if(e===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return e}function V(e,t){if(t&&(h(t)==="object"||typeof t==="function")){return t}return E(e)}function F(e){var t=C();return function r(){var n=w(e),o;if(t){var i=w(this).constructor;o=Reflect.construct(n,arguments,i)}else{o=n.apply(this,arguments)}return V(this,o)}}var T=v.isValidElementType;var N=i(),x=N.Consumer,A=N.Provider;var I=1;var R=function e(t){I=t};var L=a.Item.name!=="FormItem";var U=L?u:"Switch";var q=L?c:"Checkbox";var D=L?l:"Radio";var Y=L?s:"Transfer";var B=L?f:"Pagination";var H=L?p:"Upload";var K=L?d:"Select";function M(e){if(e){var t=e.type;if(T(t)){if(t.formutilType){return t.formutilType}if(typeof t==="string"&&e.props.type){return e.props.type}if(!L){var r;var n=t.displayName||t.name;if(n){return n}if(((r=t.render)===null||r===void 0?void 0:r.name)==="InternalSelect"){return"Select"}return t}}return t||e}}var W=function(o){S(u,o);var i=F(u);function u(){var e;O(this,u);for(var t=arguments.length,r=new Array(t),n=0;n<t;n++){r[n]=arguments[n]}e=i.call.apply(i,[this].concat(r));e.fields={};e.registerField=function(t,r){return r?e.fields[t]=r:delete e.fields[t]};e.latestValidationProps=null;e.checkHasError=function(e,t,r,n,o){switch(e){case 0:return t&&r&&n;case 1:return t&&r;case 2:return t;default:return false}};e.fetchCurrentValidationProps=function(t){var r=Object.keys(e.fields).map((function(t){return e.fields[t].$new()}));var n=r.filter((function(e){return e.$invalid}));var o=n.length>0;var i=r.some((function(e){return e.$dirty}));var a=r.some((function(e){return e.$touched}));var u=r.some((function(e){return e.$focused}));var c=n.map((function(e){return e.$getFirstError()}));return e.getValidationProps(t,o,i,a,u,c)};e.getValidationProps=function(t,r,n,o,i,a){var u=e.checkHasError(t,r,n,o,i);var c={className:[e.props.className,u&&"has-error",r?"is-invalid":"is-valid",n?"is-dirty":"is-pristine",o?"is-touched":"is-untouched",i?"is-focused":"is-unfocused"].filter(Boolean).join(" ")};if(u){Object.assign(c,{validateStatus:"error",help:a})}return c};return e}j(u,[{key:"componentDidMount",value:function e(){var t;(t=this.registerAncestorField)===null||t===void 0?void 0:t.call(this,this.props.name,this.$fieldutil)}},{key:"componentWillUnmount",value:function e(){var t;(t=this.registerAncestorField)===null||t===void 0?void 0:t.call(this,this.props.name,null)}},{key:"render",value:function o(){var i=this;var u=this.props;var c=u.children,l=u.itemProps,s=u.errorLevel,f=s===void 0?I:s,p=u.noStyle,d=k(u,["children","itemProps","errorLevel","noStyle"]);if(!u.name){var v=this.latestValidationProps=this.fetchCurrentValidationProps(f);Promise.resolve().then((function(){if(!m(i.latestValidationProps,i.fetchCurrentValidationProps(f))){i.forceUpdate()}}));return t.createElement(A,{value:this.registerField},t.createElement(a.Item,Object.assign({},d,v),typeof c==="function"?c():c))}if(d.$memo===true){d.__DIFF__={childList:c,compositionValue:this.compositionValue}}else if(Array.isArray(d.$memo)){d.$memo=d.$memo.concat(this.compositionValue)}var h=typeof c==="function"?c:r.only(c);var b=M(h);switch(b){case U:case q:case D:d.__TYPE__="checked";break;case B:if(!("$defaultValue"in d)){d.$defaultValue=1}break;case"checked":case"array":case"object":case"number":case"empty":d.__TYPE__=b;break;default:d.__TYPE__="empty";break}return t.createElement(e,Object.assign({},d,{passUtil:"$fieldutil",render:function e(r){var o,c,s;var v=u.valuePropName,m=v===void 0?"value":v,P=u.changePropName,O=P===void 0?"onChange":P,_=u.focusPropName,j=_===void 0?"onFocus":_,$=u.blurPropName,S=$===void 0?"onBlur":$;var w=r.$fieldutil,C=r[O],E=r[j],V=r[S],F=r[m],T=k(r,["$fieldutil",O,j,S,m].map(g));var N=w.$invalid,A=w.$dirty,I=w.$touched,R=w.$focused,L=w.$getFirstError;var M;switch(b){case U:case q:case D:case"checked":var W=u.checked,z=W===void 0?true:W,G=u.unchecked,J=G===void 0?false:G;M={checked:F===z,onChange:function e(t){var r=t&&t.target?t.target.checked:t;C(r?z:J,t)}};break;case Y:M={targetKeys:F,onChange:C};break;case B:M={current:F,onChange:C};break;case H:M={fileList:(o=F===null||F===void 0?void 0:F.fileList)!==null&&o!==void 0?o:F,onChange:C};break;default:M=(c={onCompositionEnd:function e(t){i.isComposing=false;delete i.compositionValue;C(t)},onCompositionStart:function e(){return i.isComposing=true}},y(c,O,(function(e){if(i.isComposing){var t,r;i.compositionValue=(t=(r=e.target)===null||r===void 0?void 0:r[m])!==null&&t!==void 0?t:e;i.forceUpdate()}else{for(var n=arguments.length,o=new Array(n>1?n-1:0),a=1;a<n;a++){o[a-1]=arguments[a]}C.apply(void 0,[e].concat(o))}})),y(c,m,"compositionValue"in i?i.compositionValue:F),y(c,S,(function(){if(i.isComposing){i.isComposing=false;delete i.compositionValue;C.apply(void 0,arguments)}return V.apply(void 0,arguments)})),c);break}if(b===K){delete M.onCompositionStart;delete M.onCompositionEnd}M=Object.assign((s={},y(s,j,E),y(s,S,V),s),M);if(!T.required&&d.required&&(!l||!("required"in l))){T.required=true}var Q=typeof h==="function"?h(M):n(h,M);return t.createElement(x,null,(function(e){if(p){i.$fieldutil=w;i.registerAncestorField=e;return Q}var r=i.getValidationProps(f,N,A,I,R,L());return t.createElement(a.Item,Object.assign({},T,l,r),Q)}))}}))}}]);return u}(o);export{W as FormItem,R as setErrorLevel};