Skip to content

call onLoad given as Props #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Frame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,6 +25,7 @@ export class Frame extends Component {

static defaultProps = {
style: {},
onLoad: undefined,
head: null,
children: undefined,
mountTarget: undefined,
Expand Down Expand Up @@ -57,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
}
Expand Down Expand Up @@ -131,7 +140,7 @@ export class Frame extends Component {
delete props.contentDidUpdate;
delete props.forwardedRef;
return (
<iframe {...props} ref={this.setRef} onLoad={this.handleLoad}>
<iframe {...props} ref={this.setRef} onLoad={this.onIframeLoad}>
{this.state.iframeLoaded && this.renderFrameContents()}
</iframe>
);
Expand Down
10 changes: 10 additions & 0 deletions test/Frame.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Frame id="foo" onLoad={onLoad} />, div);
});

it('should create an iFrame with a <link> tag inside', done => {
div = document.body.appendChild(document.createElement('div'));
const frame = ReactDOM.render(
Expand Down