File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import React from 'react'
1+ import React , { Component } from 'react'
22import ReactDOM from 'react-dom'
33import 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
Original file line number Diff line number Diff line change 1- import React , { Component , Fragment } from 'react'
1+ import React , { Component , Fragment , createRef } from 'react'
22import PropTypes from 'prop-types'
33
44export const EMBED_URL = 'https://labs-assets.typeform.com/cui/cui-embed.js'
55
66class 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 >
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments