Skip to content

Commit 710981e

Browse files
authored
Merge pull request #3 from jepser/handle-dynamic-component
fix: dynamic registering
2 parents 1f2d454 + 2d12a1e commit 710981e

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

demo/demo.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
import React from 'react'
1+
import React, { Component } from 'react'
22
import ReactDOM from 'react-dom'
33
import Cui from '../src/ReactCui'
44

5-
const App = ({}) => {
6-
return <div>
7-
<p>Let's add avatar and theme</p>
8-
<Cui uid="Sb1WbK" theme="red" avatar="http://i.imgur.com/6jr3M0j.png" height="400" />
9-
</div>
5+
class App extends Component {
6+
7+
constructor() {
8+
super()
9+
this.state = {
10+
show: false
11+
}
12+
13+
this.handleOnClick = this.handleOnClick.bind(this)
14+
}
15+
16+
handleOnClick() {
17+
this.setState(prevState => ({
18+
show: !prevState.show
19+
}))
20+
}
21+
render() {
22+
return <div>
23+
<p>Let's add avatar and theme</p>
24+
<Cui uid="d6wXaD" theme="blue" avatar="https://i.imgur.com/6jr3M0j.png" height="400" />
25+
{this.state.show &&
26+
<Cui uid="Sb1WbK" theme="red" avatar="https://i.imgur.com/6jr3M0j.png" height="400" />}
27+
<span onClick={this.handleOnClick}>Click to {this.state.show ? 'hide' : 'show'}</span>
28+
</div>
29+
}
1030
}
1131

1232

src/ReactCui.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import React, { Component, Fragment } from 'react'
1+
import React, { Component, Fragment, createRef } from 'react'
22
import PropTypes from 'prop-types'
33

44
export const EMBED_URL = 'https://labs-assets.typeform.com/cui/cui-embed.js'
55

66
class Cui extends Component {
7+
constructor () {
8+
super()
9+
this.ref = createRef()
10+
}
711
componentDidMount () {
812
if (!scriptTagExists({ document })) {
913
const embedJs = document.createElement('script')
1014
embedJs.type = 'text/javascript'
1115
embedJs.id = 'cui-embed-script'
1216
embedJs.src = EMBED_URL
1317
embedJs.async = true
14-
document.getElementsByTagName('head')[0].appendChild(embedJs)
15-
}
16-
}
17-
18-
componentWillUnmount () {
19-
if (scriptTagExists({ document })) {
20-
const scripts = getScriptTags({ document })
21-
scripts.forEach(script => script.remove())
18+
document.head.appendChild(embedJs)
19+
} else {
20+
window.__CUI && window.__CUI.register(this.ref.current)
2221
}
2322
}
2423

@@ -34,6 +33,7 @@ class Cui extends Component {
3433
data-cui-height={height}
3534
data-cui-avatar={avatar}
3635
data-cui-theme={theme}
36+
ref={this.ref}
3737
/>
3838
) : null}
3939
</Fragment>

src/ReactCui.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,4 @@ describe('<Cui />', () => {
5252

5353
expect(scriptTags.length).toBe(1)
5454
})
55-
56-
test('Unmount the component will remove the script tag created', () => {
57-
const Component = mount(<Cui uid={'123'} />)
58-
Component.unmount()
59-
60-
const scriptTags = getEmbedScriptTag({ document })
61-
62-
expect(scriptTags.length).toBe(0)
63-
})
6455
})

0 commit comments

Comments
 (0)