-
-
Save semoal/9ff2ac000040062d71ee6add04141dc1 to your computer and use it in GitHub Desktop.
Zoid HOC
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
import React from 'react'; | |
import Head from 'next/head'; | |
const withZoid = (Page, wantedWidget) => { | |
return class extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = { | |
zoidProps: null | |
} | |
} | |
componentDidMount() { | |
if (window.xprops) { | |
this.setState({ zoidProps: window.xprops }); | |
window.xprops.onProps(props => this.setState({ zoidProps: props })); | |
} | |
} | |
static async getInitialProps(ctx) { | |
if(Page.getInitialProps) return Page.getInitialProps(ctx); | |
return {}; | |
} | |
render() { | |
const { zoidProps } = this.state; | |
return ( | |
<> | |
<Head> | |
<script src="/static/zoid.min.js"></script> | |
<script src={`/static/widgets/${wantedWidget}-widget.js`}></script> | |
</Head> | |
<Page zoidProps={zoidProps} {...this.props} /> | |
</> | |
) | |
} | |
} | |
} | |
export default withZoid; |
Parent widget:
import * as zoid from 'zoid/dist/zoid.frameworks';
const BASE_URL = 'http://localhost:2000';
const ContractsWidget = zoid.create({
tag: 'contracts-widget',
attributes: {
iframe: {
scrolling: 'yes'
}
},
url: () => `${BASE_URL}/contracts`,
props: {
configuration: {
type: 'object',
required: false
}
},
});
export default ContractsWidget;
Parent page app:
import React from 'react';
import ReactDOM from 'react-dom';
import { ContractsWidget } from './Widgets';
const ContractsPreview = ContractsWidget.driver('react',{
React,
ReactDOM
});
function App() {
const [value, setValue] = React.useState('hola');
return (
<>
<input type="text" onChange={(e) => setValue(e.target.value)} value={value} />
<ContractsPreview configuration={{ metadata: { items: ['hola', 'hola2']}}} />
</>
);
}
export default App;
Common widgets:
/static/widgets/contracts-widget.jszoid.create({ tag: 'contracts-widget', url: 'http://localhost:2000/contracts' });
Example of /pages/contracts/index.js (NEXTJS):
import React from 'react';
import withZoid from '../../components/withZoid';
class ContractsPage extends React.Component {
render() {
return (
<>
<h1>Showing from iframe from contracts-page: </h1>
<div style={{ color: "yellow", background: "red" }}>{JSON.stringify(this.props.zoidProps)}</div>
</>
);
}
}
export default withZoid(ContractsPage, 'contracts');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Common widgets:
/static/widgets/contracts-widget.js