Last active
June 4, 2016 07:09
-
-
Save davidgilbertson/924e64e04384670bf15aa4a10d84495f to your computer and use it in GitHub Desktop.
Allowing state to be set by child
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| const {Component, PropTypes} = React; | |
| import SignInModalBody from './SignInModalBody.jsx'; | |
| import EditScreenModalBody from './EditScreenModalBody.jsx'; | |
| import FeedbackModalBody from './FeedbackModalBody.jsx'; | |
| import HelpPanelModalBody from './HelpPanelModalBody.jsx'; | |
| import styles from './styles.js'; | |
| class Modal extends Component { | |
| constructor(props) { | |
| super(props); | |
| // state can be passed UP from the modal's content | |
| // this is the equivalent of defaultProps | |
| this.state = { | |
| title: '', | |
| showOk: true, | |
| okText: 'OK', | |
| okDisabled: false, | |
| width: 400, | |
| onOk: () => {} | |
| }; | |
| } | |
| render() { | |
| let ModalBody; | |
| switch (this.props.currentModal) { | |
| case 'signIn' : | |
| ModalBody = SignInModalBody; | |
| break; | |
| case 'editScreen' : | |
| ModalBody = EditScreenModalBody; | |
| break; | |
| case 'feedback' : | |
| ModalBody = FeedbackModalBody; | |
| break; | |
| case 'helpPanel' : | |
| ModalBody = HelpPanelModalBody; | |
| break; | |
| default : | |
| return null; // no known modal, don't render anything | |
| } | |
| return ( | |
| <div style={styles.back}> | |
| <div style={styles.panel}> | |
| <div style={styles.header}> | |
| <h1 style={styles.title}>{this.state.title}</h1> | |
| <button style={styles.close}>Close</button> | |
| </div> | |
| <div style={styles.body}> | |
| <ModalBody | |
| {...this.props} | |
| setModalState={state => this.setState(state)} | |
| /> | |
| </div> | |
| <div style={styles.actions}> | |
| <button | |
| onClick={this.state.onOk} | |
| >{this.state.okText}</button> | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| } | |
| Modal.propTypes = { | |
| currentModal: PropTypes.string.isRequired, | |
| hideModal: PropTypes.func.isRequired, | |
| }; | |
| export default Modal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment