@vlzh/react-modal-manager
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3
@@ -9,3 +9,3 @@ import { Manager } from "./Manager"; | ||
*/ | ||
export declare const useModalManager: (name?: string) => ReturnType<typeof _useModalManager>; | ||
export declare const useModalManager: (name?: string | undefined) => ReturnType<typeof _useModalManager>; | ||
/** | ||
@@ -15,2 +15,2 @@ * Create new HOC with predefined manager | ||
*/ | ||
export declare const withModalManager: (name?: string) => ReturnType<typeof _withModalManager>; | ||
export declare const withModalManager: (name?: string | undefined) => ReturnType<typeof _withModalManager>; |
@@ -23,7 +23,1 @@ import { CallbackParams } from "./CallbackParams"; | ||
}; | ||
export interface HookReturns { | ||
openModal: (modal_name?: string, close_other?: boolean) => void; | ||
closeModal: (modal_name?: string) => void; | ||
isOpen: (modal_name?: string) => boolean; | ||
getParams: (modal_name?: string) => ModalParams | null; | ||
} |
@@ -20,3 +20,3 @@ import { ModalsList, Modal, ModalParams, OpenModalsList, Event, Callbacks, Subscribers, CallbackFunction } from "./interfaces"; | ||
*/ | ||
getModalByName(modal_name: string): Modal; | ||
getModalByName(modal_name: string): Modal | undefined; | ||
/** | ||
@@ -23,0 +23,0 @@ * Open modal with specific name and close another modals if <close_other> is true |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var React = require('react'); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -249,2 +269,5 @@ if (!o) return; | ||
var createUndefinedModalError = function createUndefinedModalError(method_name) { | ||
return new Error("You must provide modal name in 'useModalManage' otherwise in method '" + method_name + "'"); | ||
}; | ||
/** | ||
@@ -259,2 +282,3 @@ * Attach element to modal in modal manager | ||
var useModalManager = function useModalManager(modalManager, modal_name) { | ||
@@ -283,3 +307,3 @@ var _modal_name = modal_name; | ||
return { | ||
openModal: function openModal(modal_name, close_other) { | ||
openModal: function openModal(modal_name, close_other, params) { | ||
if (modal_name === void 0) { | ||
@@ -289,3 +313,4 @@ modal_name = _modal_name; | ||
modalManager.openModal(modal_name, close_other); | ||
if (!modal_name) throw createUndefinedModalError("openModal"); | ||
return modalManager.openModal(modal_name, close_other, params); | ||
}, | ||
@@ -297,2 +322,3 @@ closeModal: function closeModal(modal_name) { | ||
if (!modal_name) throw createUndefinedModalError("closeModal"); | ||
return modalManager.closeModal(modal_name); | ||
@@ -305,2 +331,3 @@ }, | ||
if (!modal_name) throw createUndefinedModalError("isOpen"); | ||
return modalManager.isOpen(modal_name); | ||
@@ -313,2 +340,3 @@ }, | ||
if (!modal_name) throw createUndefinedModalError("getParams"); | ||
return modalManager.getParams(modal_name); | ||
@@ -329,3 +357,3 @@ } | ||
var methods = useModalManager(modalManager, name); | ||
return /*#__PURE__*/React.createElement(ComposedComponent, Object.assign({}, methods, props)); | ||
return /*#__PURE__*/React.createElement(ComposedComponent, _extends({}, methods, props)); | ||
}; | ||
@@ -332,0 +360,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("react");function n(e,n){(null==n||n>e.length)&&(n=e.length);for(var r=0,t=new Array(n);r<n;r++)t[r]=e[r];return t}function r(e,r){var t;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return n(e,void 0);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,void 0):void 0}}(e))||r&&e&&"number"==typeof e.length){t&&(e=t);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=e[Symbol.iterator]()).next.bind(t)}var t=function(e){this.modal_name=e},o=["afterOpen","beforeOpen","afterClose","beforeClose"],i=function(n,r){var t=r,o=e.useState(0)[1];return e.useEffect((function(){var e=function(){o(Math.random())};return r&&(n.addModal(r),n.addSubscriber(r,e)),function(){r&&n.removeSubscriber(r,e)}}),[r]),{openModal:function(e,r){void 0===e&&(e=t),n.openModal(e,r)},closeModal:function(e){return void 0===e&&(e=t),n.closeModal(e)},isOpen:function(e){return void 0===e&&(e=t),n.isOpen(e)},getParams:function(e){return void 0===e&&(e=t),n.getParams(e)}}},a=new(function(){function e(){this.modals=[],this.open=[],this.subscribers={},this.callbacks={afterOpen:[],beforeOpen:[],afterClose:[],beforeClose:[]},this.scroll_point=0}var n=e.prototype;return n.addModal=function(e,n){void 0===n&&(n={}),this.getModalByName(e)||(this.modals.push({name:e,default_params:n}),this.subscribers[e]=[])},n.delModal=function(e){this.modals=this.modals.filter((function(n){return n.name!==e})),this.open=this.open.filter((function(n){return n.name!==e})),delete this.subscribers[e]},n.getModalByName=function(e){return this.modals.find((function(n){return n.name===e}))},n.openModal=function(e,n,o){void 0===n&&(n=!0);var i=this.getModalByName(e);if(!i)throw new Error("manager do not have modal '"+e+"'");if(this.callbacks.beforeOpen.forEach((function(n){return n(new t(e))})),n)for(var a,s=r(this.open);!(a=s()).done;)this.closeModal(a.value.name);this.open.push({name:i.name,params:o||i.default_params}),this.callSubscribers(e),this.callbacks.afterOpen.forEach((function(n){return n(new t(e))}))},n.closeModal=function(e){if(!this.getModalByName(e))throw new Error("manager do not have modal with name '"+e+"'");this.callbacks.beforeClose.forEach((function(n){return n(new t(e))})),this.open=this.open.filter((function(n){return n.name!==e})),this.callSubscribers(e),this.callbacks.afterClose.forEach((function(n){return n(new t(e))}))},n.isOpen=function(e){return!!this.open.find((function(n){return n.name===e}))},n.getParams=function(e){var n=this.open.find((function(n){return n.name===e}));return(null==n?void 0:n.params)||null},n.addSubscriber=function(e,n){this.subscribers[e].push(n)},n.removeSubscriber=function(e,n){this.subscribers[e]=this.subscribers[e].filter((function(e){return e!==n})),0===this.subscribers[e].length&&this.delModal(e)},n.callSubscribers=function(e){if(!Object.keys(this.subscribers).includes(e))throw new Error("Subscribers for '"+e+" not defined'");for(var n,t=r(this.subscribers[e]);!(n=t()).done;)(0,n.value)()},n.on=function(e,n){if(!o.includes(e))throw new Error("Unknown event "+e);this.callbacks[e].push(n)},e}());exports.manager=a,exports.useModalManager=function(e){return i(a,e)},exports.withModalManager=function(n){return function(n,r){return function(t){var o=function(o){var a=i(n,r);return e.createElement(t,Object.assign({},a,o))};return o.displayName="withModalManager("+(t.displayName||t.name||"Unknown")+")",o}}(a,n)}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}return e}).apply(this,arguments)}function n(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function t(e,r){var t;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return n(e,void 0);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,void 0):void 0}}(e))||r&&e&&"number"==typeof e.length){t&&(e=t);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=e[Symbol.iterator]()).next.bind(t)}var o=function(e){this.modal_name=e},a=["afterOpen","beforeOpen","afterClose","beforeClose"],i=function(e){return new Error("You must provide modal name in 'useModalManage' otherwise in method '"+e+"'")},s=function(r,n){var t=n,o=e.useState(0)[1];return e.useEffect((function(){var e=function(){o(Math.random())};return n&&(r.addModal(n),r.addSubscriber(n,e)),function(){n&&r.removeSubscriber(n,e)}}),[n]),{openModal:function(e,n,o){if(void 0===e&&(e=t),!e)throw i("openModal");return r.openModal(e,n,o)},closeModal:function(e){if(void 0===e&&(e=t),!e)throw i("closeModal");return r.closeModal(e)},isOpen:function(e){if(void 0===e&&(e=t),!e)throw i("isOpen");return r.isOpen(e)},getParams:function(e){if(void 0===e&&(e=t),!e)throw i("getParams");return r.getParams(e)}}},u=new(function(){function e(){this.modals=[],this.open=[],this.subscribers={},this.callbacks={afterOpen:[],beforeOpen:[],afterClose:[],beforeClose:[]},this.scroll_point=0}var r=e.prototype;return r.addModal=function(e,r){void 0===r&&(r={}),this.getModalByName(e)||(this.modals.push({name:e,default_params:r}),this.subscribers[e]=[])},r.delModal=function(e){this.modals=this.modals.filter((function(r){return r.name!==e})),this.open=this.open.filter((function(r){return r.name!==e})),delete this.subscribers[e]},r.getModalByName=function(e){return this.modals.find((function(r){return r.name===e}))},r.openModal=function(e,r,n){void 0===r&&(r=!0);var a=this.getModalByName(e);if(!a)throw new Error("manager do not have modal '"+e+"'");if(this.callbacks.beforeOpen.forEach((function(r){return r(new o(e))})),r)for(var i,s=t(this.open);!(i=s()).done;)this.closeModal(i.value.name);this.open.push({name:a.name,params:n||a.default_params}),this.callSubscribers(e),this.callbacks.afterOpen.forEach((function(r){return r(new o(e))}))},r.closeModal=function(e){if(!this.getModalByName(e))throw new Error("manager do not have modal with name '"+e+"'");this.callbacks.beforeClose.forEach((function(r){return r(new o(e))})),this.open=this.open.filter((function(r){return r.name!==e})),this.callSubscribers(e),this.callbacks.afterClose.forEach((function(r){return r(new o(e))}))},r.isOpen=function(e){return!!this.open.find((function(r){return r.name===e}))},r.getParams=function(e){var r=this.open.find((function(r){return r.name===e}));return(null==r?void 0:r.params)||null},r.addSubscriber=function(e,r){this.subscribers[e].push(r)},r.removeSubscriber=function(e,r){this.subscribers[e]=this.subscribers[e].filter((function(e){return e!==r})),0===this.subscribers[e].length&&this.delModal(e)},r.callSubscribers=function(e){if(!Object.keys(this.subscribers).includes(e))throw new Error("Subscribers for '"+e+" not defined'");for(var r,n=t(this.subscribers[e]);!(r=n()).done;)(0,r.value)()},r.on=function(e,r){if(!a.includes(e))throw new Error("Unknown event "+e);this.callbacks[e].push(r)},e}());exports.manager=u,exports.useModalManager=function(e){return s(u,e)},exports.withModalManager=function(n){return function(n,t){return function(o){var a=function(a){var i=s(n,t);return e.createElement(o,r({},i,a))};return a.displayName="withModalManager("+(o.displayName||o.name||"Unknown")+")",a}}(u,n)}; | ||
//# sourceMappingURL=react-modal-manager.cjs.production.min.js.map |
import { useState, useEffect, createElement } from 'react'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -247,2 +265,5 @@ if (!o) return; | ||
var createUndefinedModalError = function createUndefinedModalError(method_name) { | ||
return new Error("You must provide modal name in 'useModalManage' otherwise in method '" + method_name + "'"); | ||
}; | ||
/** | ||
@@ -257,2 +278,3 @@ * Attach element to modal in modal manager | ||
var useModalManager = function useModalManager(modalManager, modal_name) { | ||
@@ -281,3 +303,3 @@ var _modal_name = modal_name; | ||
return { | ||
openModal: function openModal(modal_name, close_other) { | ||
openModal: function openModal(modal_name, close_other, params) { | ||
if (modal_name === void 0) { | ||
@@ -287,3 +309,4 @@ modal_name = _modal_name; | ||
modalManager.openModal(modal_name, close_other); | ||
if (!modal_name) throw createUndefinedModalError("openModal"); | ||
return modalManager.openModal(modal_name, close_other, params); | ||
}, | ||
@@ -295,2 +318,3 @@ closeModal: function closeModal(modal_name) { | ||
if (!modal_name) throw createUndefinedModalError("closeModal"); | ||
return modalManager.closeModal(modal_name); | ||
@@ -303,2 +327,3 @@ }, | ||
if (!modal_name) throw createUndefinedModalError("isOpen"); | ||
return modalManager.isOpen(modal_name); | ||
@@ -311,2 +336,3 @@ }, | ||
if (!modal_name) throw createUndefinedModalError("getParams"); | ||
return modalManager.getParams(modal_name); | ||
@@ -327,3 +353,3 @@ } | ||
var methods = useModalManager(modalManager, name); | ||
return /*#__PURE__*/createElement(ComposedComponent, Object.assign({}, methods, props)); | ||
return /*#__PURE__*/createElement(ComposedComponent, _extends({}, methods, props)); | ||
}; | ||
@@ -330,0 +356,0 @@ |
@@ -0,3 +1,3 @@ | ||
import { ModalParams } from "./interfaces"; | ||
import { Manager } from "./Manager"; | ||
import { HookReturns } from "./interfaces"; | ||
/** | ||
@@ -11,2 +11,7 @@ * Attach element to modal in modal manager | ||
*/ | ||
export declare const useModalManager: (modalManager: Manager, modal_name?: string) => HookReturns; | ||
export declare const useModalManager: (modalManager: Manager, modal_name?: string | undefined) => { | ||
openModal: (modal_name?: string | undefined, close_other?: boolean | undefined, params?: ModalParams | undefined) => void; | ||
closeModal: (modal_name?: string | undefined) => void; | ||
isOpen: (modal_name?: string | undefined) => boolean; | ||
getParams: (modal_name?: string | undefined) => ModalParams | null; | ||
}; |
@@ -7,2 +7,2 @@ import { Manager } from "./Manager"; | ||
*/ | ||
export declare const withModalManager: (modalManager: Manager, name: string) => any; | ||
export declare const withModalManager: (modalManager: Manager, name: string | undefined) => any; |
{ | ||
"name": "@vlzh/react-modal-manager", | ||
"private": false, | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-alpha.3", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/react-modal-manager.esm.js", |
@@ -33,7 +33,1 @@ import { CallbackParams } from "./CallbackParams"; | ||
export interface HookReturns { | ||
openModal: (modal_name?: string, close_other?: boolean) => void; | ||
closeModal: (modal_name?: string) => void; | ||
isOpen: (modal_name?: string) => boolean; | ||
getParams: (modal_name?: string) => ModalParams | null; | ||
} |
@@ -61,3 +61,3 @@ import { | ||
*/ | ||
getModalByName(modal_name: string): Modal { | ||
getModalByName(modal_name: string): Modal | undefined { | ||
return this.modals.find((m) => m.name === modal_name); | ||
@@ -64,0 +64,0 @@ } |
import { useEffect, useState } from "react"; | ||
import { ModalParams } from "./interfaces"; | ||
import { Manager } from "./Manager"; | ||
import { HookReturns } from "./interfaces"; | ||
const createUndefinedModalError = (method_name: string) => | ||
new Error( | ||
`You must provide modal name in 'useModalManage' otherwise in method '${method_name}'` | ||
); | ||
/** | ||
@@ -13,6 +18,3 @@ * Attach element to modal in modal manager | ||
*/ | ||
export const useModalManager = ( | ||
modalManager: Manager, | ||
modal_name?: string | ||
): HookReturns => { | ||
export const useModalManager = (modalManager: Manager, modal_name?: string) => { | ||
const _modal_name = modal_name; | ||
@@ -36,14 +38,22 @@ const [, update] = useState(0); | ||
openModal: ( | ||
modal_name: string = _modal_name, | ||
close_other?: boolean | ||
modal_name: string | undefined = _modal_name, | ||
close_other?: boolean, | ||
params?: ModalParams | ||
) => { | ||
modalManager.openModal(modal_name, close_other); | ||
if (!modal_name) throw createUndefinedModalError("openModal"); | ||
return modalManager.openModal(modal_name, close_other, params); | ||
}, | ||
closeModal: (modal_name: string = _modal_name) => | ||
modalManager.closeModal(modal_name), | ||
isOpen: (modal_name: string = _modal_name) => | ||
modalManager.isOpen(modal_name), | ||
getParams: (modal_name: string = _modal_name) => | ||
modalManager.getParams(modal_name), | ||
closeModal: (modal_name: string | undefined = _modal_name) => { | ||
if (!modal_name) throw createUndefinedModalError("closeModal"); | ||
return modalManager.closeModal(modal_name); | ||
}, | ||
isOpen: (modal_name: string | undefined = _modal_name) => { | ||
if (!modal_name) throw createUndefinedModalError("isOpen"); | ||
return modalManager.isOpen(modal_name); | ||
}, | ||
getParams: (modal_name: string | undefined = _modal_name) => { | ||
if (!modal_name) throw createUndefinedModalError("getParams"); | ||
return modalManager.getParams(modal_name); | ||
}, | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
97557
24
1122
0