Use Input mask with Material UI

Issue

This Content is from Stack Overflow. Question asked by Wang Ming

I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.

I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.I want to use InputMask (react-imask) with material-ui (version 5).
Also, I am using yup and react-hook-form to validate the fields.
Here is what I am doing.

Problem: when I submit the form, the cpf number gets cleared.v

import { InputHTMLAttributes, forwardRef } from "react";
    import * as Yup from "yup";
    import { useForm } from "react-hook-form";
    import { yupResolver } from "@hookform/resolvers/yup";
    import { IMaskInput } from "react-imask";
    import { Button } from "@mui/material";
    import Input from "./components/Input";
    import "./styles.css";

    const TextMaskCustom = forwardRef<any, InputHTMLAttributes<HTMLInputElement>>(
      function TextMaskCustom(props, ref) {
        const { onChange, name, ...other } = props;

        const handleAccept = (value: string) => {
          if (onChange && name) {
            onChange({ target: { name: name, value } });
          }
        };

        return (
          <IMaskInput
            mask="000-000-000.00"
            definitions={{
              "#": /[1-9]/
            }}
            inputRef={ref}
            onAccept={handleAccept}
            overwrite
            {...other}
          />
        );
      }
    );

    export default function App() {
      const schema = Yup.object().shape({
        cpf: Yup.string().required("Please Input Valid cpf number!")
      });

      const {
        register,
        formState: { errors },
        handleSubmit
      } = useForm({
        resolver: yupResolver(schema)
      });

      const onSubmit = (field: any) => {
        console.log(field);
      };

      return (
        <div className="App">
          <h1>
            <span role="img" aria-label="applause">
              👋
            </span>
            Use Input Mask with Material UI
          </h1>

          <Input
            name="cpf"
            required={true}
            register={register}
            error={errors?.cpf?.message}
            inputComponent={TextMaskCustom}
          />

          <Button
            sx={{ mt: 4 }}
            variant="contained"
            onClick={handleSubmit(onSubmit)}
          >
            Submit
          </Button>
        </div>
      );
    }



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?