Giter Club home page Giter Club logo

formik-material-ui's Introduction

Actions Status license Coverage Status

Formik Material-UI

Bindings for using Formik with Material-UI.

Getting Started

Install the package as a dependency to your project (yarn or npm).

yarn add formik-material-ui
yarn add formik-material-ui-pickers

Then, import the components you need to use with Formik in the file you have your form component set up. The components avaliable are:

  • Checkbox & CheckboxWithLabel
  • RadioGroup
  • Select
  • SimpleFileUpload
  • Switch
  • TextField

Available with @material-ui/pickers peer dep

  • DatePicker
  • KeyboardDatePicker
  • TimePicker
  • KeyboardTimePicker
  • DateTimePicker
  • KeyboardDateTimePicker

In this case we will use <TextField />.

import { Formik, Form } from 'formik';
+ import { TextField } from 'formik-material-ui';

Next, in your Formik form component, render the <TextField /> component:

<TextField
  name="email"
  label="Email"
  type="email"
/>

Now you can add any of the Material UI props to the <TextField /> proprs that are managed by Formik are excluded. E.g. (name, value, onChange etc).

<TextField
  name="email"
  label="Email"
  type="email"
+ margin="normal"
+ fullWidth
/>

Goals

  • Convenience: This project contains mostly the tedious wrapper code needed to convert the formik field props to Material UI shapes. Which is why certain assumptions are made (See below). The mapping functions are exported so you can build on top of them for more specialized behavior

Form Behavior Assumptions

  • Errors are only displayed on touched/dirty fields
  • Fields are disabled during submission
  • All form state is managed to Formik, so props such as name, onChange and value are excluded from the typescript types. Note: if you are using js this does not prevent you from providing these props but here be dragons.

Non Goals

  • Negate the need to import Material UI directly or create custom components

FAQ

Custom onChange

import MuiTextField from '@material-ui/core/TextField';
import {useFieldToTextField, TextField, TextFieldProps} from 'formik-material-ui';
...
const UppercasingTextField = (props: TextFieldProps) => {
  const fieldProps = useFieldToTextField(props);
  return (<MuiTextField
    {...fieldProps}
    onChange={event => {
      const {value} = event.target;
      props.form.setFieldValue(
        props.field.name,
        value ? value.toUpperCase() : ''
      );
    }}
  />
);
...

Development

yarn install
yarn storybook

formik-material-ui's People

Contributors

9renpoto avatar bfirsh avatar cherniavskii avatar cliedeman avatar jeffvandyke avatar jhenderson219 avatar jhoffmcd avatar joaopaulomoraes avatar johnraz avatar komachi avatar lironer avatar mosoc avatar oliviertassinari avatar vegtelenseg avatar wunsz avatar yardz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.