question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component `Modal ` provider a API `Model.open` to create and open a new instance of `Modal `

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

i have a list or table. every list item (row) have a action list. there are some feature in action list:

  • open a modal to edit this row
  • open a modal to show details of this row
  • do some thing need to input password or key word to confirm …

if there are too many action need to open a Modal, i need to maintain stateA, stateB, stateC… And some time i need to clear this state after close Modal instance.

it is too heavy…

This API is not recommended, but it is still necessary for implementation.

What does the proposed API look like?

Modal.open(props). like Modal.warn

Model.open({
    title: 'title',
    onConfirm: () => {},
    onCancel: () => {},
    children: () => <div></div>
    ......
})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
qiqiboycommented, Sep 3, 2018
import Modal from './MyModal';

const modal = Modal.open({
    compoennt: MyPage,
    //component: <Mypage otherProp="..." />
    //component: ({ close, dismiss }) => <div />
});

modal.result.then(resolveCallback, rejectCallback);
modal.close(); // close with resolved
modal.dismiss(); // close with rejected

MyModal.js

import React, { Children, cloneElement } from 'react';
import { render as reactRender, unmountComponentAtNode } from 'react-dom';
import { Modal } from 'antd';

export default Modal;

const defaultSettings = {
    destroyOnClose: true,
    footer: null,
    maskClosable: false,
    closable: false
};

export const open = (Modal.open = (props = {}) => {
    let destroyed, withResolve, withReject;

    const settings = { ...defaultSettings, ...props };

    if (settings.footer === true) {
        delete settings.footer;
    }

    if (props.onOk) {
        settings.onOk = ev => props.onOk(ev, { close, dismiss });
    }

    if (props.onCancel) {
        settings.onCancel = ev => props.onCancel(ev, { close, dismiss });
    }

    const div = document.createElement('div');
    document.body.appendChild(div);

    function destroy() {
        if (!destroyed) {
            destroyed = true;

            unmountComponentAtNode(div);

            document.body.removeChild(div);
        }
    }

    function close(data) {
        render(false, () => withResolve(data));
    }

    function dismiss(reason) {
        render(false, () => withReject(reason));
    }

    function render(visible, callback) {
        const { component: TheComponent, ...props } = settings;
        const childProps = {
            close,
            dismiss
        };

        let children;
        if (typeof TheComponent === 'function') {
            children = <TheComponent />;
        } else {
            children = TheComponent;
        }

        reactRender(
            <Modal
                onCancel={dismiss}
                onOk={close}
                {...props}
                visible={visible}
                afterClose={() => {
                    if (!callback) {
                        callback = withReject;
                    }

                    callback();
                    destroy();
                }}>
                {Children.map(children, child => cloneElement(child, childProps))}
            </Modal>,
            div
        );
    }

    render(true);

    return {
        close,
        dismiss,
        result: new Promise((resolve, reject) => {
            withResolve = resolve;
            withReject = reject;
        })
    };
});

1reaction
tinybullcommented, Nov 12, 2019

angular version of antd provides an open method, while the imperative call is not recommended in React, it is not means that React prohibit using it. And also why are you provide Modal.info, Modal.success, Modal.error,Modal.warning and Modal.confirm methods? These methods do not violate the recommendation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Implement a Modal Component in React - DigitalOcean
Build a modal component in your React project using props and state to control displaying and closing.
Read more >
Your next React Modal with your own "useModal" Hook ...
Hi there everyone, this is a quick review about how to use Modals Components in your React project combining Hooks, Context, and Portals....
Read more >
Build a simple Modal Component with React - Bits and Pieces
These are the steps for this tutorial: What we'll learn. Create a simple file structure; Use react toggle state to build a simple...
Read more >
4 steps to set up global modals in React - Opensource.com
1. Create a global modal component · 2. Create modal dialog components · 3. Integrate GlobalModal into the top-level component in your application....
Read more >
ion-modal: Ionic Mobile App Custom Modal API Component
ion-modal is a dialog that appears on top of mobile app content, and must be dismissed before interaction resumes. Learn more about custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found