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

Feaure request: return promise from dialogs #1953

Closed
Christilut opened this issue Nov 4, 2019 · 11 comments
Closed

Feaure request: return promise from dialogs #1953

Christilut opened this issue Nov 4, 2019 · 11 comments
Assignees
Projects

Comments

@Christilut
Copy link
Contributor

Description

As a heavy Buefy user, I would really like it when the confirm dialog would return a Promise.

My workaround now is simple but not very pretty:

    const confirmed = await new Promise((resolve, reject) => {
      this.$buefy.dialog.confirm({
        message: `Are you sure?`,
        onCancel: () => reject(false),
        onConfirm: () => resolve(true)
      })
    })

Perhaps for a non-API breaking method we could do it like this (AWS SDK added promises to their library in the same way):

    const confirmed = await this.$buefy.dialog.confirm({
        message: `Are you sure?`
      }).promise()

Why Buefy need this feature

This is quite common nowadays. SWAL uses this method and it's very nice to work with. I ditched SWAL when I found out Buefy now has similar programmatic dialog/modal functionality.

@jtommy
Copy link
Member

jtommy commented Nov 4, 2019

@Christilut sure, it's a good idea

@travisfisher
Copy link

Just ran into this issue today. I agree that returning a Promise would be a nice addition.

@jtommy
Copy link
Member

jtommy commented May 24, 2020

#2539

@jtommy jtommy closed this as completed May 24, 2020
@Christilut
Copy link
Contributor Author

@jtommy I think this issue is not related to #2539

This one is specifically about this.$buefy.dialog returning a Promise so that you can await it. You can achieve the same behavior with closeOnConfirm but that was already possible by wrapping it in a Promise as in the first post.

This issue is more for a convenience so that you can do simple stuff like

const isConfirmed = await this.$buefy.dialog.confirm({
        message: `Are you sure?`
})
// isConfirmed = true if OK pressed. False if canceled by button or Escape key

without all the extra boilerplate code in onConfirm

@jtommy
Copy link
Member

jtommy commented May 25, 2020

@Christilut i'm working on a similar way for the next version 0.9.0 , for example:

const dialog = this.$buefy.dialog.confirm({
        message: `Are you sure?`,
        closeOnConfirm: false
});
dialog.result.then(({value, dialog}) => ...).catch(...)

https://github.com/buefy/buefy/blob/promise-programmatic/src/components/dialog/index.js#L16

what do you think ?

@jtommy jtommy reopened this May 25, 2020
@Christilut
Copy link
Contributor Author

I think the following will be possible with 0.9.0 then?

const isConfirmed = (await this.$buefy.dialog.confirm({
        message: `Are you sure?`,
        closeOnConfirm: false
}).result).value

Is closeOnConfirm: false needed for this? Doesn't it always return the promise then? I'd like it as simple and intuitive as possible. The above is okay but would be even better if the promise only returned a boolean but I understand that might not be ideal for other situations.

Personally I wouldn't mind extending the API with something like this (same as in the first post):

    const confirmed = await this.$buefy.dialog.confirm({
        message: `Are you sure?`
      }).promise()

Because it's just super simple and understandable for new users and isn't in the way for older users who are used to how it is now.

@jtommy
Copy link
Member

jtommy commented May 25, 2020

You can write my previous code in this way:

const { confirmed, dialog } = await this.$buefy.dialog.confirm({
        message: 'Are you sure?',
        closeOnConfirm: false
}).result;

closeOnConfirm is only for disable autoclose :)

@Christilut
Copy link
Contributor Author

Ah okay, looks good then! Put the last snippet (without closeOnConfirm) in an example, I think that will help users a lot :)

@jtommy
Copy link
Member

jtommy commented May 25, 2020

@Christilut sure!

@jtommy jtommy closed this as completed in 9d45c6a Jul 31, 2020
@jtommy
Copy link
Member

jtommy commented Jul 31, 2020

@Christilut i don't want to add an other breaking so it will be available by defaultProgrammaticPromise in the constructor options (default false); setting it to true value you can use a syntax like:

const { result, dialog } = await this.$buefy.dialog.confirm({
        message: 'Are you sure?',
        closeOnConfirm: false
});

@jtommy jtommy moved this from In progress to Done in v1 Jul 31, 2020
@Christilut
Copy link
Contributor Author

Very cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
v1
  
Done
Development

No branches or pull requests

3 participants