Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Rename Confirm to ConfirmDialog, add ConfirmDialogProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Jun 20, 2018
1 parent 91926b5 commit 86d86e2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import PropTypes from 'prop-types';
import {Component} from 'react';

/**
* Confirm wraps window.confirm
* ConfirmDialog wraps window.confirm
*/
export default class Confirm extends Component {
export default class ConfirmDialog extends Component {

constructor(props) {
super(props);
Expand All @@ -30,18 +30,14 @@ export default class Confirm extends Component {
}
}

Confirm.defaultProps = {
result: {
timestamp: -1
},
call_on_cancel: false,
ConfirmDialog.defaultProps = {
n_clicks: 0,
n_clicks_timestamp: -1,
submit_n_clicks: 0,
cancel_n_clicks: 0,
};

Confirm.propTypes = {
ConfirmDialog.propTypes = {
id: PropTypes.string,

/**
Expand Down
81 changes: 81 additions & 0 deletions src/components/ConfirmDialogProvider.react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import PropTypes from 'prop-types';

import ConfirmDialog from './ConfirmDialog.react'



/**
* Wrap children onClick to send a confirmation dialog.
*/
export default class ConfirmDialogProvider extends React.Component {
render() {
const { id, setProps, children } = this.props;

// Will lose the previous onClick of the child
const wrapClick = (child) => React.cloneElement(child, {onClick: () =>
{
setProps({
send_confirm: true
});
}
});

const realChild = children.props ? children.props.children : children;

return (
<div id={id}>
{realChild && realChild.length ? realChild.map(wrapClick) :
wrapClick(children.props.children)}
<ConfirmDialog ref={this._dialog} {...this.props}/>
</div>
)
}
};

ConfirmDialogProvider.defaultProps = {
n_clicks: 0,
n_clicks_timestamp: -1,
submit_n_clicks: 0,
cancel_n_clicks: 0,
};

ConfirmDialogProvider.propTypes = {
id: PropTypes.string,

/**
* Message to show in the popup.
*/
message: PropTypes.string,

/**
* Number of times the modal was submited or canceled.
*/
n_clicks: PropTypes.number,
/**
* Last timestamp the popup was clicked.
*/
n_clicks_timestamp: PropTypes.number,
/**
* Number of times the submit was clicked
*/
submit_n_clicks: PropTypes.number,
/**
* Number of times the popup was canceled.
*/
cancel_n_clicks: PropTypes.number,
/**
* Set to true to send the popup.
*/
send_confirm: PropTypes.bool,
/**
* Is the modal currently displayed.
*/
displayed: PropTypes.bool,

/**
* Dash-assigned callback that gets fired when the value changes.
*/
setProps: PropTypes.func,
children: PropTypes.any,
};
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import Textarea from './components/Textarea.react';
import DatePickerSingle from './components/DatePickerSingle.react';
import DatePickerRange from './components/DatePickerRange.react';
import Upload from './components/Upload.react';
import Confirm from './components/Confirm.react';
import ConfirmDialog from './components/ConfirmDialog.react';
import ConfirmDialogProvider from './components/ConfirmDialogProvider.react'

export {
Checklist,
Confirm,
ConfirmDialog,
ConfirmDialogProvider,
Dropdown,
Graph,
Input,
Expand Down
3 changes: 1 addition & 2 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def test_confirm(self):

app.layout = html.Div([
html.Button(id='button', children='Send confirm', n_clicks=0),
dcc.Confirm(id='confirm', message='Please confirm.'),
dcc.ConfirmDialog(id='confirm', message='Please confirm.'),
html.Div(id='confirmed')
])

Expand All @@ -553,7 +553,6 @@ def on_confirmed(n_clicks, submit_n_clicks, cancel_n_clicks):
elif n_clicks == 2:
return 'canceled'


self.startServer(app)
button = self.wait_for_element_by_css_selector('#button')
self.snapshot('confirmation -> initial')
Expand Down

0 comments on commit 86d86e2

Please sign in to comment.