Coder Social home page Coder Social logo

johnnynia / formik-material-ui Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stackworx/formik-mui

0.0 0.0 0.0 7.31 MB

Bindings for using Formik with Material-UI

Home Page: https://stackworx.github.io/formik-material-ui

License: MIT License

JavaScript 8.60% TypeScript 91.40%

formik-material-ui's Introduction

Build 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

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

In this case we will use <TextField />.

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

Next, in your Formik form component, pass this <TextField /> component as the component prop of the Formik <Field /> component:

<Field
  name="email"
  label="Email"
  type="email"
+ component={TextField}
/>

Now you can add any of the Material UI props to the <Field /> component and <TextField /> will be able to accept them.

<Field
  name="email"
  label="Email"
  type="email"
  component={TextField}
+ margin="normal"
+ fullWidth
/>

Goals

  • Convenience: This project containis 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 Behaviour 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 {fieldToTextField, TextField, TextFieldProps} from 'formik-material-ui';
...
const UppercasingTextField = (props: TextFieldProps) => (
  <MuiTextField
    {...fieldToTextField(props)}
    onChange={event => {
      const {value} = event.target;
      props.form.setFieldValue(
        props.field.name,
        value ? value.toUpperCase() : ''
      );
    }}
  />
);
...

Composing mappers

import {fieldToTextField} from 'formik-material-ui';

<Field name="name" render={(props) => (
  <div>
    <Comp1 {...fieldToTextField(props)} />
    <Comp2 {...fieldToTextField(props)} />
    ...
  </div>
)} />

Development

yarn install
yarn storybook

Running tests

yarn prepublishOnly

formik-material-ui's People

Contributors

cliedeman avatar cherniavskii avatar bfirsh avatar yardz avatar jhoffmcd avatar lironer avatar ajdaniel avatar vegtelenseg avatar jeffvandyke avatar johnraz avatar jhenderson219 avatar joaopaulomoraes avatar 9renpoto avatar oliviertassinari avatar wunsz 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.