From 19addf497e13cccb574c90d1b4ec45d187e287d8 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Sun, 14 Aug 2022 23:28:42 +0900 Subject: [PATCH 1/3] add test for onLoad --- test/Frame.spec.jsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Frame.spec.jsx b/test/Frame.spec.jsx index c90242b..47915ae 100644 --- a/test/Frame.spec.jsx +++ b/test/Frame.spec.jsx @@ -62,6 +62,16 @@ describe('The Frame Component', () => { expect(node.getAttribute('width')).to.equal('80%'); }); + it('should call onLoad given as props', done => { + div = document.body.appendChild(document.createElement('div')); + + const onLoad = () => { + done(); + }; + + ReactDOM.render(, div); + }); + it('should create an iFrame with a tag inside', done => { div = document.body.appendChild(document.createElement('div')); const frame = ReactDOM.render( From 761fa5ce2356cb3aa9c41eb8bede5ee66ff35180 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Sun, 14 Aug 2022 23:28:46 +0900 Subject: [PATCH 2/3] call onLoad given as Props --- src/Frame.jsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Frame.jsx b/src/Frame.jsx index 767362a..0dfc348 100644 --- a/src/Frame.jsx +++ b/src/Frame.jsx @@ -11,6 +11,7 @@ export class Frame extends Component { // element that we render react into. static propTypes = { style: PropTypes.object, // eslint-disable-line + onLoad: PropTypes.func, head: PropTypes.node, initialContent: PropTypes.string, mountTarget: PropTypes.string, @@ -24,6 +25,7 @@ export class Frame extends Component { static defaultProps = { style: {}, + onLoad: undefined, head: null, children: undefined, mountTarget: undefined, @@ -82,6 +84,9 @@ export class Frame extends Component { handleLoad = () => { this.setState({ iframeLoaded: true }); + if (this.props.onLoad) { + this.props.onLoad(); + } }; renderFrameContents() { From 85b924e9abbd574a2adb2df10ba06c636bcba565 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Fri, 19 Aug 2022 10:51:29 +0900 Subject: [PATCH 3/3] handle iframe load event separately --- src/Frame.jsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Frame.jsx b/src/Frame.jsx index 0dfc348..d6f1655 100644 --- a/src/Frame.jsx +++ b/src/Frame.jsx @@ -59,6 +59,13 @@ export class Frame extends Component { this.nodeRef.current.removeEventListener('load', this.handleLoad); } + onIframeLoad = () => { + this.handleLoad(); + if (this.props.onLoad) { + this.props.onLoad(); + } + }; + getDoc() { return this.nodeRef.current ? this.nodeRef.current.contentDocument : null; // eslint-disable-line } @@ -84,9 +91,6 @@ export class Frame extends Component { handleLoad = () => { this.setState({ iframeLoaded: true }); - if (this.props.onLoad) { - this.props.onLoad(); - } }; renderFrameContents() { @@ -136,7 +140,7 @@ export class Frame extends Component { delete props.contentDidUpdate; delete props.forwardedRef; return ( - );