material-table-core-formik
Version:
A @material-table/core wrapper to display a dialog for add, edit and delete. It also allows Formik/YUP validation.
3 lines (2 loc) • 11.4 kB
JavaScript
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),r=e(t),n=require("@material-table/core"),o=e(n),i=e(require("@material-ui/core/Dialog")),a=e(require("@material-ui/core/DialogTitle")),l=e(require("@material-ui/core/DialogContent")),c=e(require("@material-ui/core/DialogActions")),u=e(require("@material-ui/core/Button")),d=require("formik"),s=require("@material-ui/core");function f(e,t,r,n,o,i,a){try{var l=e[i](a),c=l.value}catch(e){return void r(e)}l.done?t(c):Promise.resolve(c).then(n,o)}function h(){return(h=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var p,v=(function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function c(e,t,r,n){var o=Object.create((t&&t.prototype instanceof s?t:s).prototype),i=new L(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var l=b(a,r);if(l){if(l===d)continue;return l}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=u(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===d)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(e,r,i),o}function u(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=c;var d={};function s(){}function f(){}function h(){}var p={};p[o]=function(){return this};var v=Object.getPrototypeOf,m=v&&v(v(T([])));m&&m!==t&&r.call(m,o)&&(p=m);var y=h.prototype=s.prototype=Object.create(p);function g(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function w(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,l){var c=u(e[o],e,i);if("throw"!==c.type){var d=c.arg,s=d.value;return s&&"object"==typeof s&&r.call(s,"__await")?t.resolve(s.__await).then((function(e){n("next",e,a,l)}),(function(e){n("throw",e,a,l)})):t.resolve(s).then((function(e){d.value=e,a(d)}),(function(e){return n("throw",e,a,l)}))}l(c.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return d;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var n=u(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,d;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,d):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,d)}function E(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function x(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function L(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(E,this),this.reset(!0)}function T(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return f.prototype=y.constructor=h,h.constructor=f,f.displayName=l(h,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===f||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,l(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},g(w.prototype),w.prototype[i]=function(){return this},e.AsyncIterator=w,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new w(c(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},g(y),l(y,a,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=T,L.prototype={constructor:L,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(x),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var l=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(l&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(l){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,d):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),d},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),x(r),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;x(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:T(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},e}(e.exports);try{regeneratorRuntime=t}catch(e){Function("r","regeneratorRuntime = r")(t)}}(p={exports:{}}),p.exports),m=s.makeStyles({field:{padding:8}});function y(e){var o=e.children,p=e.onEditingCanceled,y=e.validate,w=e.onEditingApproved,b=e.validationSchema,E=e.mode,x=e.editField,L=e.dialogLocalization,T=e.dateTimePickerLocalization,O=function(e,t){if(null==e)return{};var r,n,o={},i=Object.keys(e);for(n=0;n<i.length;n++)t.indexOf(r=i[n])>=0||(o[r]=e[r]);return o}(e,["children","onEditingCanceled","validate","onEditingApproved","validationSchema","mode","editField","displayField","dialogLocalization","dateTimePickerLocalization"]),k=O.localization,j=O.data,F=O.columns,S=m(),P=t.useRef(!1);t.useEffect((function(){return P.current=!0,function(){P.current=!1}}),[]);var _,C,D,z=r.useMemo((function(){return j?h({},j):{}}),[j]),A=function(){p(E,j)};switch(E){case"add":_=L.addTooltip;break;case"update":_=L.editTooltip;break;case"delete":_=L.deleteHeader}return r.createElement(r.Fragment,null,r.createElement(i,{onClose:A,open:!0,fullWidth:!0},r.createElement(a,{id:"simple-dialog-title"},_),r.createElement(d.Formik,{validationSchema:b,initialValues:z,validate:y,onSubmit:(C=v.mark((function e(t,r){var n;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return(n=r.setSubmitting)(!0),delete t.tableData,e.next=5,w(E,t,j);case 5:P&&P.current&&n(!1);case 6:case"end":return e.stop()}}),e)})),D=function(){var e=this,t=arguments;return new Promise((function(r,n){var o=C.apply(e,t);function i(e){f(o,r,n,i,a,"next",e)}function a(e){f(o,r,n,i,a,"throw",e)}i(void 0)}))},function(e,t){return D.apply(this,arguments)})},(function(e){var t=e.isSubmitting,n=e.setValues;return r.createElement("form",{onSubmit:e.handleSubmit},r.createElement(l,null,r.createElement(s.Grid,{container:!0},"delete"!==E&&F.filter((function(e){return function(e,t){return"add"===t?!0!==e.hidden&&g(e,t):!0!==e.hidden}(e,E)})).map((function(e){return r.createElement(d.Field,{key:e.field,name:e.field},(function(t){var o=t.field,i=t.meta;return r.createElement(s.Grid,Object.assign({className:S.field,item:!0,xs:12},e.gridProps),r.createElement("label",{htmlFor:e.field},e.title),r.createElement("br",null),function(e,t,n,o){if(!g(e,E,j))return e.render&&j?e.render(j,"row"):r.createElement("div",null,t.value);var i=function(e){return t.onChange({target:{value:e,checked:e,name:t.name}})},a={helperText:n.touched?n.error:"",error:Boolean(n.touched&&void 0!==n.error)};return e.editComponent?e.editComponent(h({rowData:j||{},value:t.value,onChange:i,onRowDataChange:function(e){j&&o(h({},j,e))},columnDef:e},a)):r.createElement(x,Object.assign({},t,a,{locale:T,fullWidth:!0,id:e.field,columnDef:e,onChange:i,rowData:j}))}(e,o,i,n))}))}))),"delete"===E&&r.createElement(s.DialogContentText,null,k.deleteText),r.createElement(c,null,t?r.createElement(s.CircularProgress,{size:25}):r.createElement(r.Fragment,null,r.createElement(u,{onClick:A,color:"primary"},k.cancelTooltip),r.createElement(u,{color:"primary",autoFocus:!0,type:"submit",disabled:t},"delete"!==E?k.saveTooltip:L.deleteAction)))))}))),j&&r.createElement(n.MTableBodyRow,Object.assign({},O,{onToggleDetailPanel:function(){}}),o))}function g(e,t,r){if("string"==typeof e.editable)switch(t){case"add":return"always"===e.editable||"onAdd"===e.editable;case"update":return"always"===e.editable||"onUpdate"===e.editable}else if(e.editable&&r)return e.editable(e,r);return!0}exports.default=function(e){var t=e.localization,i=e.components,a=e.validate,l=e.validationSchema,c={addTooltip:t&&t.body&&t.body.addTooltip||"Add Row",editTooltip:t&&t.body&&t.body.editTooltip||"Edit Row",deleteHeader:t&&t.deleteHeader||"Delete Row",deleteAction:t&&t.deleteAction||"Delete"},u=(null==i?void 0:i.EditField)||n.MTableEditField,d=(null==i?void 0:i.Cell)||n.MTableCell;return r.createElement(o,Object.assign({},e,{components:h({},i,{EditRow:function(e){var n;return r.createElement(y,Object.assign({},e,{dateTimePickerLocalization:null==t||null==(n=t.body)?void 0:n.dateTimePickerLocalization,editField:u,displayField:d,dialogLocalization:c,validate:a,validationSchema:l}))}})}))};
//# sourceMappingURL=material-table-core-formik.cjs.production.min.js.map