This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
57 lines (45 loc) · 1.66 KB
/
index.jsx
File metadata and controls
57 lines (45 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const React = require('react')
const ReactDOM = require('react-dom')
module.exports = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
elm: React.PropTypes.object,
className: React.PropTypes.string,
id: React.PropTypes.string
},
getInitialState() {
return { app: null }
},
componentDidMount() {
let Elmy = typeof Elm === 'object' ? Elm : this.props.elm
let props = Object.keys(module.exports.propTypes).join("|")
let ports = Object
.keys(this.props)
.filter(x => !(new RegExp(`^(children|ref|key|on[A-Z]|${props})`)).test(x))
.reduce((o, x) => Object.assign(o, { [x]: this.props[x] }), {})
let app = Elmy.embed(Elmy[this.props.name], ReactDOM.findDOMNode(this), ports)
Object
.keys(app.ports)
.forEach(x => {
if (typeof app.ports[x].subscribe !== "function") { return }
let handler = this.props[`on${x.substring(0, 1).toUpperCase()+x.substring(1)}`]
if (handler == null) { return }
app.ports[x].subscribe(handler)
})
this.setState({ app })
},
shouldComponentUpdate(nextProps) {
if (this.state.app == null) { return false }
let ports = this.state.app.ports
Object
.keys(ports)
.forEach(x => {
if (typeof ports[x].send !== "function") { return }
ports[x].send(nextProps[x])
})
return false
},
render() {
return (<div className={this.props.className} id={this.props.id}></div>)
}
})