Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-mui-formik

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-mui-formik

TextFields Material UI with Formik and masks

  • 1.5.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26
increased by550%
Maintainers
1
Weekly downloads
 
Created
Source

js-mui-formik

material ui mui formik mask

How to use


import { useFormik } from 'formik';
import { FormikMuiTextField} from 'js-mui-formik'
import { useState } from 'react';
import * as yup from 'yup';


export default function App() {

    const validationSchema = yup.object({
        name: yup.string().required('Please enter name').min(2, "Name too short")
    });

    const [name, setName] = useState<string>("");

    const formik = useFormik({
        initialValues: {name},
        enableReinitialize: true,
        validationSchema: validationSchema,
        onSubmit: (values, formikHelpers) => {
            setName(() => values.name);
            formikHelpers.resetForm();
        },
    });

  return (
    <div>
        <h1>Hello {name} </h1>
        <form onSubmit={formik.handleSubmit}>
            <FormikMuiTextField name="name" label="Name" formik={formik}/>
            <button type="submit">Submit</button>
        </form>
    </div>
  )
} 


Use input with mask

import { useFormik } from 'formik';
import { FormikMuiMaskedTextField, FormikMuiTextField } from 'js-mui-formik'
import { BR_MOBILE_MASK, BR_PHONE_MASK } from 'js-mui-formik'
import { useState } from 'react';
import styled from 'styled-components';
import * as yup from 'yup';

const InputWrapper = styled.div`margin: 1rem 0`;

class User {
  name: string;
  phone: string;
  mobile: string;

  constructor() {
    this.name = "";
    this.phone = "";
    this.mobile = "";
  }
}

export default function ShowInputs() {

  const validationSchema = yup.object({
    name: yup.string().required('Please enter name').min(2, "Name too short"),
    phone: yup.string().required('Please enter phone').min(11, "Phone too short"),
    mobile: yup.string().required('Please enter mobile').min(12, "Phone too short")

  });

  const [user, setUser] = useState<User>(new User);

  const formik = useFormik({
    initialValues: { ...user },
    enableReinitialize: true,
    validationSchema: validationSchema,
    validateOnMount: true,
    onSubmit: (values, formikHelpers) => {
      setUser(() => values);
      formikHelpers.resetForm();
    },
  });

  return (
    <div>
      <h1>
        Hello {user?.name ? user?.name : '______'},&nbsp;
        your phone is {user?.phone ? user?.phone : '_____'}&nbsp;
        and your mobile is {user?.mobile ? user?.mobile : '_____'}.
      </h1>
      <form onSubmit={formik.handleSubmit}>
        <InputWrapper>
          <FormikMuiTextField
            name="name"
            label="Name"
            placeholder='Enter with your name'
            formik={formik} />
        </InputWrapper>
        <InputWrapper>
          <FormikMuiMaskedTextField
            id="phone"
            name="phone"
            mask={BR_PHONE_MASK}
            alternativeMask={BR_MOBILE_MASK}
            testAlternativeMask={(value: string) => value.replace(/\D/g, '')[2] === '9'}
            label="Phone"
            formik={formik} />
        </InputWrapper>
        <InputWrapper>
          <FormikMuiMaskedTextField
            id="mobile"
            name="mobile"
            mask={BR_MOBILE_MASK}
            label="Mobile"
            formik={formik} />
        </InputWrapper>
        <button type="submit">Submit</button>
      </form>
    </div>
  )
}

Keywords

FAQs

Package last updated on 28 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc