File size: 1,669 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import React from 'react';
import ReactDOM from 'react-dom/client';
import { useForm } from 'react-hook-form';
import App from './app';
const FRAME_CONTENT = `
<style>
label {display: block; margin-bottom: .5em}
form {margin-bottom: 2em}
input {margin: 0 1em}
</style>
<div id='inner-root'>
Loading content...
</div>
`;
const FRAME_STYLE = {
width: '640px',
height: '480px',
background: 'white',
};
const CrossFrameForm: React.FC = () => {
const ref = React.useRef<HTMLIFrameElement>(null);
function renderFormInFrame() {
const rootElement =
ref.current!.contentDocument!.getElementById('inner-root');
const root = ReactDOM.createRoot(rootElement);
root.render(<FrameForm />);
}
return (
<iframe
ref={ref}
style={FRAME_STYLE}
srcDoc={FRAME_CONTENT}
onLoad={renderFormInFrame}
/>
);
};
const FrameForm: React.FC = () => {
const { register, watch } = useForm();
const value = watch();
return (
<>
<form>
<label>
Free text
<input type="text" {...register('input', { required: true })} />
</label>
<label>
<input
type="radio"
value="a"
{...register('radio', { required: true })}
/>
Choice A
</label>
<label>
<input
type="radio"
value="b"
{...register('radio', { required: true })}
/>
Choice B
</label>
</form>
<label>
Form value
<pre>{JSON.stringify(value)}</pre>
</label>
</>
);
};
export default CrossFrameForm;
|