Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hideModal() from within Dialog component #48

Open
barrettg opened this issue Oct 13, 2021 · 6 comments
Open

hideModal() from within Dialog component #48

barrettg opened this issue Oct 13, 2021 · 6 comments

Comments

@barrettg
Copy link

barrettg commented Oct 13, 2021

I'm wanting to create a self contained Dialog that has some logic in it as well. When the OK button is pressed, the business logic executes and the modal is closed. Is it possible to call hideModal from within the dialog itself?

const ConfirmationDialog: React.FC<Props> = ({ title, description, ...props }) => {
    const { hideModal } = useModal();
    const handleClose = () => {
        // Do stuff here
        ...
        hideModal(); // What is the modal id?
    };
    return (
        <Dialog {...props}>
            <DialogTitle>{title}</DialogTitle>
            <DialogContent>
                <DialogContentText>{description}</DialogContentText>
            </DialogContent>
            <DialogActions>
                <Button onClick={handleClose} color="primary">
                    Ok
                </Button>
            </DialogActions>
        </Dialog>
    );
};

Meanwhile somewhere else in my app:

showModal(ConfirmationDialog, {
  title: 'Hello World',
  description: 'description text',
});

Thanks for a great library!

@Quernest
Copy link
Owner

Hi @barrettg, you can close the Dialog after performing the necessary logic using the callback function, for example onConfirm;

const ConfirmationDialog: React.FC<Props> = ({ title, description, onConfirm, ...props }) => {
    const handleConfirm = () => {
        // Do stuff here
        
       if (onConfirm) {
         onConfirm();
       }
    };

    return (
        <Dialog {...props}>
            <DialogTitle>{title}</DialogTitle>
            <DialogContent>
                <DialogContentText>{description}</DialogContentText>
            </DialogContent>
            <DialogActions>
                <Button onClick={handleConfirm} color="primary">
                    Ok
                </Button>
            </DialogActions>
        </Dialog>
    );
};
const modal = showModal(ConfirmationDialog, {
  title: 'Hello World',
  description: 'description text',
  onConfirm: () => {
    modal.hide();
  }
});

I have an example of such ConfirmationDialog 🙂

const modal = showModal(ConfirmationDialog, {

@mysticfall
Copy link

Hi, I'm using this module from Kotlin and unfortunately, it's impossible to access the modal variable from within the closure in that language.

I think it'd be better if the module can provide a way to reference the current id and/or a method to close/hide the dialog, even when we can ignore such an issue regarding non-JS languages, like when you want to make your dialog self-contained, so that the invoker doesn't have to concern about it implementation details.

@storrdev
Copy link
Contributor

storrdev commented Dec 1, 2021

I'm also attempting to use this library using nextjs (without typescript) and I'm running into the same issue as @mysticfall.

@storrdev
Copy link
Contributor

storrdev commented Dec 2, 2021

I was able to work around this issue when using nextjs (and it should work for @mysticfall as well).

Turns out the {...props} that are passed to the Dialog component contain the onClose handler you are looking for. See my example below.

const ConfirmationDialog: React.FC<Props> = ({ title, description, onConfirm, ...props }) => {
    const handleConfirm = () => {
        // Do stuff here
        
       if (onConfirm) {
         onConfirm();
       }
       props.onClose(); // Add this
    };

    return (
        <Dialog {...props}>
            <DialogTitle>{title}</DialogTitle>
            <DialogContent>
                <DialogContentText>{description}</DialogContentText>
            </DialogContent>
            <DialogActions>
                <Button onClick={handleConfirm} color="primary">
                    Ok
                </Button>
            </DialogActions>
        </Dialog>
    );
};

@rafaelfaria
Copy link

rafaelfaria commented Dec 7, 2021

How do you do the same thing using a Modal (instead of Dialog)?

@Quernest
Copy link
Owner

Quernest commented Dec 7, 2021

How do you do the same thing using a Modal (instead of Dialog)?

Modal has almost the same API as Dialog.
https://mui.com/api/modal/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants