-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/expanding core mount fny (#1772)
* expanding core mount fny - so that second and subsequent calls to mount act as a "remount" * Adding mock React app that can mount and unmount the Card component * Comment out console.log
- Loading branch information
Showing
5 changed files
with
164 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Script { | ||
constructor(src, node = 'body', attributes = {}, dataAttributes = {}) { | ||
this.script = document.createElement('script'); | ||
this.src = src; | ||
this.node = node; | ||
this.attributes = attributes; | ||
this.dataAttributes = dataAttributes; | ||
} | ||
|
||
load = () => | ||
new Promise((resolve, reject) => { | ||
Object.assign(this.script, this.attributes); | ||
Object.assign(this.script.dataset, this.dataAttributes); | ||
|
||
this.script.src = this.src; | ||
this.script.async = true; | ||
this.script.onload = event => { | ||
this.script.setAttribute('data-script-loaded', 'true'); | ||
resolve(event); | ||
}; | ||
this.script.onerror = () => { | ||
this.remove(); | ||
reject(new Error(`Unable to load script ${this.src}`)); | ||
}; | ||
|
||
const container = document.querySelector(this.node); | ||
const addedScript = container.querySelector(`script[src="${this.src}"]`); | ||
|
||
if (addedScript) { | ||
const isLoaded = addedScript.getAttribute('data-script-loaded'); | ||
if (isLoaded) resolve(true); | ||
else addedScript.addEventListener('load', resolve); | ||
} else { | ||
container.appendChild(this.script); | ||
} | ||
}); | ||
|
||
remove = () => { | ||
return this.script.parentNode && this.script.parentNode.removeChild(this.script); | ||
}; | ||
} | ||
|
||
export default Script; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Script from '../../Script'; | ||
|
||
function MountButton(props) { | ||
const e = React.createElement; | ||
|
||
const [isMounted, setIsMounted] = React.useState(props.mountAtStart); | ||
return e( | ||
'button', | ||
{ | ||
type: 'button', | ||
onClick: () => { | ||
setIsMounted(!isMounted); | ||
props.onClick(); | ||
}, | ||
className: 'adyen-checkout__button adyen-checkout__button--secondary' | ||
}, | ||
isMounted ? 'Unmount' : 'Mount' | ||
); | ||
} | ||
|
||
function MockReactComp(props) { | ||
const e = React.createElement; | ||
|
||
const ref = React.createRef(); | ||
|
||
const [isMounted, setIsMounted] = React.useState(false); | ||
|
||
const listenerFn = React.useCallback( | ||
e => { | ||
// console.log('### MockReactComp:::: isMounted=', isMounted, 'so...', isMounted ? 'unmount it' : 'mount it'); | ||
if (!isMounted) { | ||
setIsMounted(true); | ||
// New mount/unmount/mount fny | ||
props.docWindow[props.type].mount(ref.current); | ||
|
||
// Old mount/unmount/remount fny | ||
// if (!props.docWindow[props.type]._node) { | ||
// console.log('### MockReactComp:::: first mount'); | ||
// props.docWindow[props.type].mount(ref.current); | ||
// } else { | ||
// console.log('### MockReactComp:::: second & sub remount'); | ||
// props.docWindow[props.type].remount(); | ||
// } | ||
} else { | ||
setIsMounted(false); | ||
props.docWindow[props.type].unmount(); | ||
// props.docWindow[props.type]._node = null; | ||
} | ||
}, | ||
[isMounted] | ||
); | ||
|
||
React.useEffect(() => { | ||
if (props.mountAtStart) { | ||
setIsMounted(true); | ||
// New mount/unmount/mount fny | ||
props.docWindow[props.type].mount(ref.current); // = window.card.mount(ref.current) | ||
} | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
// console.log('### MockReactComp:::: RENDERING isMounted=', isMounted); | ||
}, [isMounted]); | ||
|
||
return [ | ||
e(MountButton, { | ||
key: 'mountBtn', | ||
onClick: listenerFn, | ||
mountAtStart: props.mountAtStart | ||
}), | ||
e('div', { ref, id: 'myReactDiv', key: 'holder', style: { marginTop: '20px' } }) | ||
]; | ||
} | ||
|
||
export async function MockReactApp(docWindow, type, domContainer, mountAtStart = true) { | ||
const script1 = new Script('https://unpkg.com/react@18/umd/react.development.js'); | ||
await script1.load(); | ||
|
||
const script2 = new Script('https://unpkg.com/react-dom@18/umd/react-dom.development.js'); | ||
await script2.load(); | ||
|
||
const e = React.createElement; | ||
const root = ReactDOM.createRoot(domContainer); | ||
root.render(e(MockReactComp, { docWindow, type, mountAtStart })); | ||
} |