Skip to content

Instantly share code, notes, and snippets.

@nathanforce
Created May 2, 2018 18:32
Show Gist options
  • Save nathanforce/ecdfe363a9bc59ce848fba6ffb37eae7 to your computer and use it in GitHub Desktop.
Save nathanforce/ecdfe363a9bc59ce848fba6ffb37eae7 to your computer and use it in GitHub Desktop.
Formik Config Based Fields
const fieldsConfig = {
title: {
label: 'Title',
},
postal: { label: 'Postal Code' },
type: { label: 'Job Type' },
experience: { label: 'Experience Level' },
description: { label: 'Description', component: 'textarea' },
url: { label: 'URL', helper: `You don't need to include http://` },
};
const MyForm = () => (
<Formik>
{({ isSubmitting }) => (
<Form>
{Object.keys(fieldsConfig).map(field => {
const { label, component, helper } = fieldsConfig[field];
return (
<Fieldset
key={field}
className="text-input xs-col-12 md-col-6"
name={field}
label={label}
helpText={helper}
component={component}
/>
);
})}
<button
className="button button--purple"
type="submit"
disabled={isSubmitting}
>
Post Job
</button>
</Form>
)}
</Formik>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment