Download react-hook-form/app/src/crossFrameForm.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/app/src/crossFrameForm.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/app/src/crossFrameForm.tsx
-
curl -L -o crossFrameForm.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/app/src/crossFrameForm.tsx
1.67 kB
| 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; | |