2

Input in formik Error in type of TextField

        <InputMask
          mask="99/99/9999"
          value={formik.values.phone}
          onChange={formik.handleChange}
          onBlur={formik.handleBlur}
        >
          {(inputProps: Props): ReactElement => (
            <TextField
              {...inputProps}
              type="tel"
              label={t("addDriverModal.phone")}
            />
          )}
        </InputMask>

Does not work. Props - type declaration from @types/react-input-mask

3 Answers 3

3

import { TextField, TextFieldProps } from "@material-ui/core";
import InputMask, {Props} from 'react-input-mask';

    <InputMask
      mask="99/99/9999"
      value={formik.values.phone}
      onChange={formik.handleChange}
      onBlur={formik.handleBlur}
    >
      {/*
          'props' supplied to TextField weren't matching the required type. 
          Hence we use '&' operator which is for intersecting two types
       */} 
      {(inputProps: Props & TextFieldProps)=>
        <TextField
          {...inputProps}
          type="tel"
          label={t("addDriverModal.phone")}
        />
      }
    </InputMask>

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure how it works in TypeScript but you can do this.

/*  Create Your Mask Component Like This */

const phoneMaskComponent = arg => {
	const { inputRef, ...other } = arg;
	const digitRegExp = /[0-9٠-٩۰-۹]/;

	return (
		<MaskedInput
			ref={ref => {
				inputRef(ref ? ref.inputElement : null);
			}}
			guide={false}
			mask={strNumber => {
				return strNumber.split('').map(() => {
					return digitRegExp;
				});
			}}
			pipe={str => {
				const newStr = str
					.replace(/[٠١٢٣٤٥٦٧٨٩]/g, d => d.charCodeAt(0) - 1632)
					.replace(/[۰۱۲۳۴۵۶۷۸۹]/g, d => d.charCodeAt(0) - 1776)
					.replace(/[^0-9]+/g, '');

				return {
					value: newStr,
					indexesOfPipedChars: range(newStr.length * 2),
				};
			}}
			{...other}
		/>
	);
};

/* Set The Mask in Your Input Component */
<InputBase
    fullWidth
    isLtr
    name="phone"
    label="Enter Phone Number"
    type="tel"
    value={formik.values.phone}
    /* Use The Mask like This */
    inputComponent={phoneMaskComponent}
  />

this mask dose not let user to enter En numbers.

Comments

0

i done that way and it works for me:

const MYInput = ({...props}) => (
    <Field
        name={props.name}
        render={({field}) => {
          return (
            <InputMask
              {...field}
              mask={props.mask}
              > 
                {(innerProps) => (
                    <TextField
                      {...innerProps}
                      variant={props.variant ? props.variant : 'filled'}
                      label={props.label ? props.label : ''}
                      />

                )}
              </InputMask>

          )
        }}
      />


);

<MYInput name="Phone" label="Phone" mask="(99) 99999-9999" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.