|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import Button from '@material-ui/core/Button'; |
| 4 | +import Paper from '@material-ui/core/Paper'; |
| 5 | +import Tabs from '@material-ui/core/Tabs'; |
| 6 | +import Tab from '@material-ui/core/Tab'; |
| 7 | +import Editor from './Editor'; |
| 8 | + |
| 9 | +class TryMe extends Component { |
| 10 | + static propTypes = { |
| 11 | + name: PropTypes.string.isRequired, |
| 12 | + onCodeChange: PropTypes.func.isRequired, |
| 13 | + activeKey: PropTypes.number.isRequired, |
| 14 | + tagKey: PropTypes.number.isRequired, |
| 15 | + samples: PropTypes.arrayOf( |
| 16 | + PropTypes.shape({ |
| 17 | + caption: PropTypes.string.isRequired, |
| 18 | + url: PropTypes.string.isRequired, |
| 19 | + content: PropTypes.string.isRequired, |
| 20 | + defaultUrl: PropTypes.string.isRequired, |
| 21 | + defaultContent: PropTypes.string.isRequired |
| 22 | + }) |
| 23 | + ).isRequired, |
| 24 | + classes: PropTypes.any |
| 25 | + }; |
| 26 | + |
| 27 | + constructor(props) { |
| 28 | + super(props); |
| 29 | + this.setActiveSample = this.setActiveSample.bind(this); |
| 30 | + this.tryIt = this.tryIt.bind(this); |
| 31 | + this.resetSample = this.resetSample.bind(this); |
| 32 | + this.onEditorValueChange = this.onEditorValueChange.bind(this); |
| 33 | + |
| 34 | + const { samples, activeKey } = props; |
| 35 | + this.state = { |
| 36 | + samples, |
| 37 | + activeKey |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + componentWillReceiveProps({ onCodeChange, samples, activeKey }) { |
| 42 | + this.setState({ |
| 43 | + onCodeChange, // eslint-disable-line react/no-unused-state |
| 44 | + samples, |
| 45 | + activeKey |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + onEditorValueChange(text) { |
| 50 | + const { samples, activeKey } = this.state; |
| 51 | + this.setState({ |
| 52 | + samples: samples.map((sample, index) => { |
| 53 | + if (index === activeKey) { |
| 54 | + const newSample = { ...sample }; |
| 55 | + newSample.content = text; |
| 56 | + return newSample; |
| 57 | + } |
| 58 | + return sample; |
| 59 | + }) |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + setActiveSample(activeKey) { |
| 64 | + this.setState({ |
| 65 | + activeKey |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + tryIt() { |
| 70 | + const { onCodeChange } = this.props; |
| 71 | + const { activeKey, samples } = this.state; |
| 72 | + const { content } = samples[activeKey]; |
| 73 | + onCodeChange(activeKey, content); |
| 74 | + } |
| 75 | + |
| 76 | + resetSample() { |
| 77 | + const { activeKey, samples } = this.state; |
| 78 | + this.setState({ |
| 79 | + samples: samples.map((sample, index) => { |
| 80 | + if (index === activeKey) { |
| 81 | + const newSample = { ...sample }; |
| 82 | + newSample.url = sample.defaultUrl; |
| 83 | + newSample.content = sample.defaultContent; |
| 84 | + return newSample; |
| 85 | + } |
| 86 | + return sample; |
| 87 | + }) |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + render() { |
| 92 | + const { name, tagKey } = this.props; |
| 93 | + const { samples, activeKey } = this.state; |
| 94 | + const { url, content } = samples[activeKey]; |
| 95 | + return ( |
| 96 | + <div key={tagKey}> |
| 97 | + <Paper square> |
| 98 | + <Tabs |
| 99 | + id={name} |
| 100 | + value={activeKey} |
| 101 | + onChange={(event, key) => this.setActiveSample(key)} |
| 102 | + aria-label="Samples" |
| 103 | + indicatorColor="primary" |
| 104 | + textColor="primary" |
| 105 | + > |
| 106 | + {samples.map(({ caption }, index) => ( |
| 107 | + <Tab value={index} key={`tab${index}`} label={caption} /> |
| 108 | + ))} |
| 109 | + </Tabs> |
| 110 | + </Paper> |
| 111 | + <Editor content={content} onCodeChange={this.onEditorValueChange} /> |
| 112 | + <div style={{width: "100%", border: "none", padding: "10px"}}> |
| 113 | + <Button variant="contained" color="primary" onClick={() => this.resetSample()}>Reset code value</Button> |
| 114 | + |
| 115 | + <Button variant="contained" color="primary" onClick={() => this.tryIt()}>Try it >></Button> |
| 116 | + </div> |
| 117 | + <Paper square> |
| 118 | + <iframe title="placeholder" src={url} style={{width: "100%", height: "540px", border: "none"}} /> |
| 119 | + </Paper> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export default TryMe; |
0 commit comments