Last active
October 18, 2018 19:40
-
-
Save jmserrano-dev/44df06156bcfec3925914a2792fb20d9 to your computer and use it in GitHub Desktop.
RenderProps
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 { render } from "react-dom"; | |
import Styles from "./Styles"; | |
import { Form, Field } from "react-final-form"; | |
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
const onSubmit = async values => { | |
await sleep(300); | |
window.alert(JSON.stringify(values, 0, 2)); | |
}; | |
const MyForm = props => { | |
return ( | |
<Form | |
onSubmit={onSubmit} | |
initialValues={{ stooge: "larry", employed: false }} | |
render={({ handleSubmit, form, submitting, pristine, values }) => ( | |
<form onSubmit={handleSubmit}> | |
{props.render({ formOptions: { form, submitting, values } })} | |
</form> | |
)} | |
/> | |
); | |
}; | |
const App = () => ( | |
<MyForm | |
render={({ formOptions }) => ( | |
<div> | |
<div> | |
<label>First Name</label> | |
<Field | |
name="firstName" | |
component="input" | |
type="text" | |
placeholder="First Name" | |
/> | |
</div> | |
<div> | |
<label>Last Name</label> | |
<Field | |
name="lastName" | |
component="input" | |
type="text" | |
placeholder="Last Name" | |
/> | |
</div> | |
<div> | |
<label>Employed</label> | |
<Field name="employed" component="input" type="checkbox" /> | |
</div> | |
<div> | |
<label>Favorite Color</label> | |
<Field name="favoriteColor" component="select"> | |
<option /> | |
<option value="#ff0000">β€οΈ Red</option> | |
<option value="#00ff00">π Green</option> | |
<option value="#0000ff">π Blue</option> | |
</Field> | |
</div> | |
<div> | |
<label>Toppings</label> | |
<Field name="toppings" component="select" multiple> | |
<option value="chicken">π Chicken</option> | |
<option value="ham">π· Ham</option> | |
<option value="mushrooms">π Mushrooms</option> | |
<option value="cheese">π§ Cheese</option> | |
<option value="tuna">π Tuna</option> | |
<option value="pineapple">π Pineapple</option> | |
</Field> | |
</div> | |
<div> | |
<label>Sauces</label> | |
<div> | |
<label> | |
<Field | |
name="sauces" | |
component="input" | |
type="checkbox" | |
value="ketchup" | |
/>{" "} | |
Ketchup | |
</label> | |
<label> | |
<Field | |
name="sauces" | |
component="input" | |
type="checkbox" | |
value="mustard" | |
/>{" "} | |
Mustard | |
</label> | |
<label> | |
<Field | |
name="sauces" | |
component="input" | |
type="checkbox" | |
value="mayonnaise" | |
/>{" "} | |
Mayonnaise | |
</label> | |
<label> | |
<Field | |
name="sauces" | |
component="input" | |
type="checkbox" | |
value="guacamole" | |
/>{" "} | |
Guacamole π₯ | |
</label> | |
</div> | |
</div> | |
<div> | |
<label>Best Stooge</label> | |
<div> | |
<label> | |
<Field | |
name="stooge" | |
component="input" | |
type="radio" | |
value="larry" | |
/>{" "} | |
Larry | |
</label> | |
<label> | |
<Field | |
name="stooge" | |
component="input" | |
type="radio" | |
value="moe" | |
/>{" "} | |
Moe | |
</label> | |
<label> | |
<Field | |
name="stooge" | |
component="input" | |
type="radio" | |
value="curly" | |
/>{" "} | |
Curly | |
</label> | |
</div> | |
</div> | |
<div> | |
<label>Notes</label> | |
<Field name="notes" component="textarea" placeholder="Notes" /> | |
</div> | |
<div className="buttons"> | |
<button | |
type="submit" | |
disabled={formOptions.submitting || formOptions.pristine} | |
> | |
Submit | |
</button> | |
<button | |
type="button" | |
onClick={formOptions.form.reset} | |
disabled={formOptions.submitting || formOptions.pristine} | |
> | |
Reset | |
</button> | |
</div> | |
<pre>{JSON.stringify(formOptions.values, 0, 2)}</pre> | |
</div> | |
)} | |
/> | |
); | |
render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment