Skip to content

Instantly share code, notes, and snippets.

@jmserrano-dev
Last active October 18, 2018 19:40
Show Gist options
  • Save jmserrano-dev/44df06156bcfec3925914a2792fb20d9 to your computer and use it in GitHub Desktop.
Save jmserrano-dev/44df06156bcfec3925914a2792fb20d9 to your computer and use it in GitHub Desktop.
RenderProps
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