The Dialog component is a flexible component that allows you to easily create a wide variety of modals, from simple alerts and confirms, to multiple choice responses, input responses, or even something completely custom.
The Dialog component is triggered by a function (see index.js) which you pass a props object to and it returns a Promise while rendering a modal on the screen above your content based on the props you passed. Dialog.js is not meant to be modified, as every customization is handled by the props (see below).
If the user confirms the dialog, the Promise resolves and returns the results of the user's action (it can be as simple as undefined or based on a choice, etc.). If the user cancels the dialog, the Promise rejects.
The Dialog component is rendered using Bulma Modal. The dialog has a built-in style, which you can override (see below), has a close button in the top-right corner, can be canceled by clicking outside or by pressing ESCAPE, and can be submitted by pressing ENTER.
Because the Dialog Component is built to be flexible, there are many optional props that can be passed. Here's how you use each one.
Required Props
title
- This is the only required prop and it is the title of the dialog.
Optional Props
icon
- This is a FontAwesome icon string, such as "fas,exclamation". It appears to the left of the title.cancel
- If you want a cancel button, this is the string value you want the cancel button to show. You can opt to pass just a cancel prop and no submit prop if you don't care about the user's response, such as an alert, set cancel to "OK" or something along those lines and do not pass submit. When the user clicks the cancel button, the Promise rejects.submit
- If you want a submit button, this is the string value you want the submit button to show. When the user clicks the submit button, the Promise resolves. For a simple dialog that is a yes/no interaction, the resolve is yes and the reject is no (undefined is passed to the resolve in this case).isForced
- If you require the user to submit something, set this to true. The cancel button (and close button) are not displayed and the dialog will not close using any method other than the user submitting.message
- If you want to display a message on the dialog, set this string value. This value is rendered as HTML, so escape any entities such as < and >, etc.choices
- If you want to ask the user to make a choice, pass an array of objects (label, value) and they will be rendered as radio buttons below the (optional) message. Whichever value the user selects will be returned in the Promise's resolve.styles
- You can pass two css style names. The first isdialog
and is the most common one you will use. The css class is put on the root div of the modal. The other isbody
and will be put on themodal-card-body
tag (see Bulma Modal above).type
- As per the Bulma Modal usage, this can beprimary
,accent
,info
,warning
,danger
, orsuccess
and determines the color of the submit button, as well as being available for your css use through your own css style.subtree
- This is specifically and exclusively for use when you need the React context to be available, such as with Redux or React-Apollo. You always set subtree tothis
wherethis
is a reference to a React.Component class that is in the main "tree" as it were, or from the React.Component calling the Dialog function.
Outside of these typical types of dialogs, you have the ability to make something custom. The Dialog component has an API for doing this, which works like this.
The custom
prop is an object that has the following props:
View
- This is required for a custom component and expects a React component. Note the capitalV
inView
.props
- This object is optional and will be passed as props to the View component.selected
- This can be any value of any type and will be returned on submit. This can be blank, or you can use it to preselect a value in the View component, if that's how you custom Dialog works.mustEqualToSubmit
- This allows you to create a custom Dialog in which only one selected value will be submittable. One example is a Dialog that requires you type something (or select checkboxes/dropdown) to submit, such as submitting to do something destructive that cannot be undone (type XXX to delete something).
The View component you pass automatically receives the following props for your usage (if you need them).
reject
- This is the reject of the Promise. It allows you to cancel the Dialog from within your custom View. One use for this is to not pass cancel or submit props to the Dialog and no buttons will be rendered so your custom View is the only thing rendered.resolve
- This is the resolve of the Promise. Similar toreject
, it allows you to resolve the Dialog from within your custom ViewonLoading
- If your View component needs to load something asynchronously in order to render itself and you need the user to wait, this prop is a function which accepts a boolean. When you callthis.props.onLoading(true)
, the user will not be able to cancel or submit until you callthis.props.onLoading(false)
.onSubmitDisabled
- If you want to disable the Dialog's submit until your View is in a specific state, you can call this function with a boolean to disable (true) and enable (false) the Dialog's submit from within your View.
These next two are very specific functionality that give you even more flexibility in your custom dialogs. You can use them in a variety of ways.
onUpdate
- When the value in your View changes, you pass it viathis.props.onUpdate(value)
and it gets stored as the selected state of the Dialog and the value is returned in the Promise resolve (submit). This can be used on its own, or in concert withmustEqualToSubmit
. In that case, when the value matches, the submit action enables (click or enter). This has one other special use. If you don't pass asubmit
value to the Dialog component, callingonUpdate
will resolve with that value automatically. This allows you to have your own custom submit button or happen in other situation of your choosing which will cause the dialog to submit with that value (assumingmustEqualToSubmit
is not set or is satisfied by the value you pass).onRegister
- This one is a bit tricky to explain, but the way it works is you call this function with a callback function as an argument and you usually callthis.props.onRegister(callback)
in the constructor orcomponentDidMount
. Your function will be called when the user clicks submit, but will not allow the Dialog's Promise to resolve, leaving you to callthis.props.resolve(foo)
to submit the dialog on your own. Why would you want this? A multi-step dialog, a dialog where you would validate on submit or make an asynchronous call on submit, etc. There are many uses for this.
This is super old and has been deprecated by React Portal.