1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import React, { ReactNode } from 'react';
- import HookModal from './HookModal';
- import { withConfirm, withInfo, withSuccess, withError, withWarning, ConfirmProps } from '../confirm';
- import { ModalReactProps } from '../Modal';
- let uuid = 0;
- function usePatchElement(): ([ReactNode[], (element: ReactNode) => () => void]) {
- const [elements, setElements] = React.useState<ReactNode[]>([]);
- function patchElement(element: ReactNode) {
- setElements(originElements => [...originElements, element]);
- return () => {
- setElements(originElements => originElements.filter(ele => ele !== element));
- };
- }
- return [elements, patchElement];
- }
- export default function useModal() {
- const [elements, patchElement] = usePatchElement();
- // eslint-disable-next-line max-len
- function getConfirmFunc(withFunc: (typeof withConfirm | typeof withInfo | typeof withSuccess | typeof withError | typeof withWarning)) {
- return function hookConfirm(config: ModalReactProps) {
- uuid += 1;
- const modalRef = React.createRef<{
- destroy: () => void;
- update: (newConfig: ConfirmProps) => void;
- }>();
- // eslint-disable-next-line prefer-const
- let closeFunc: () => void;
- const modal = (
- <HookModal
- key={`semi-modal-${uuid}`}
- config={withFunc(config)}
- ref={modalRef}
- afterClose={() => {
- closeFunc();
- }}
- />
- );
- closeFunc = patchElement(modal);
- return {
- destroy: () => {
- if (modalRef.current) {
- modalRef.current.destroy();
- }
- },
- update: (newConfig: ConfirmProps) => {
- if (modalRef.current) {
- modalRef.current.update(newConfig);
- }
- },
- };
- };
- }
- return [
- {
- info: getConfirmFunc(withInfo),
- success: getConfirmFunc(withSuccess),
- error: getConfirmFunc(withError),
- warning: getConfirmFunc(withWarning),
- confirm: getConfirmFunc(withConfirm),
- },
- <>{elements}</>,
- ];
- }
|