Skip to content
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
3 changes: 2 additions & 1 deletion src/Demo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import IframeComm from "react-iframe-comm"; // loads build file

const Demo = ({}) => {
const Demo = ({ }) => {
// the html attributes to create the iframe with
// make sure you use camelCase attribute names
const attributes = {
src: "https://pbojinov.github.io/iframe-communication/iframe.html",
// name: 'Demo',
width: "100%",
height: "175"
};
Expand Down
10 changes: 8 additions & 2 deletions src/IframeComm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from "react";
import PropTypes from 'prop-types';
import { atLeastOneRequired } from './custom.proptypes'

class IframeComm extends Component {
constructor() {
Expand Down Expand Up @@ -93,6 +94,11 @@ IframeComm.defaultProps = {
postMessageData: ""
};

const atLeastOne = atLeastOneRequired({
name: PropTypes.string,
src: PropTypes.string
})

IframeComm.propTypes = {
/*
Iframe Attributes
Expand All @@ -108,12 +114,12 @@ IframeComm.propTypes = {
]),
frameBorder: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
name: PropTypes.string,
name: atLeastOne,
scrolling: PropTypes.string,
// https://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/
sandbox: PropTypes.string,
srcDoc: PropTypes.string,
src: PropTypes.string.isRequired,
src: atLeastOne,
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
}),

Expand Down
19 changes: 19 additions & 0 deletions src/custom.proptypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function atLeastOneRequired(checkProps) {
return function(props, propName, compName) {
const requiredPropNames = Object.keys(checkProps);

const found = requiredPropNames.find((requiredProp) => props[requiredProp]);
// console.log(requiredPropNames);
try {
if (!found) {
throw new Error(
`One of ${requiredPropNames.join(',')} is required by '${compName}' component.`,
);
}
PropTypes.checkPropTypes(checkProps, props, propName, compName);
} catch (e) {
return e;
}
return null;
};
}