diff --git a/snippets/ModalDialog.md b/snippets/ModalDialog.md index cec15cf4f..a6e3114bf 100644 --- a/snippets/ModalDialog.md +++ b/snippets/ModalDialog.md @@ -61,44 +61,71 @@ Use `children` prop to get the content of the dialog passed by Component User.Re ``` ```jsx - class Dialog extends React.Component { - constructor() { - super(); - this.close = this.close.bind(this); - this.modalClick = this.modalClick.bind(this); - this.dialogClick = this.dialogClick.bind(this); - } - render() { - return
-
-
{ this.props.title }+
-
- { - this.props.children - } -
+class Dialog extends React.Component { + constructor() { + super(); + this.modalHandler = (e) => { + this.setState({ + data: e.detail.data, + visible: true + }); + }; + this.state = { + data: { + title: '', + content: '' + }, + visible: false + }; + this.close = this.close.bind(this); + this.modalClick = this.modalClick.bind(this); + } + render() { + return
+
+
{ this.state.data.title }+
+
+ { + this.state.data.content + }
- } - close() { - this.props.close(); - } - modalClick() { - if (this.props.clickModal2Hide) { - this.close(); - } - } - dialogClick(e) { - e.stopPropagation(); - } +
} + componentDidMount() { + document.addEventListener('modal', this.modalHandler); + } + componentWillUnmount() { + document.removeEventListener('modal', this.modalHandler); + } + close() { + this.setState({ + visible: false + }); + } + static show(data) { + document.dispatchEvent(new CustomEvent('modal', { + detail: { + data + } + })); + } + modalClick() { + if (this.props.clickModal2Hide) this.close(); + } +} ``` ```jsx - let ModalDialog = {this.setState({visible: false})}}> - - ; - ReactDOM.render(, Node); +// add to render function + + +// every time you wanna call the dialog +// content is a jsx element +ModalDialog.show({ + title: 'Test', + content: +}); ```