import { TextField, TextFieldProps } from "@mui/material" import { format, unformat, Replacement, useMask } from "@react-input/mask" import React from "react" export type InputMaskProps = { value?: string mask?: string replacement?: Record onChange?: (data: string) => void } & Omit export const InputMask = React.forwardRef( ( { mask, replacement, value, onChange, ...props }, ref ) => { const parsed: any = {} if (replacement) for (let key of Object.keys(replacement)) parsed[key] = new RegExp(replacement[key]) const maskOptions: { mask: string; replacement: string | Replacement; } | undefined = mask ? { mask, replacement: parsed } : undefined const maskRef = maskOptions ? useMask(maskOptions) : undefined; return <> { if (maskRef) maskRef.current = el if (typeof ref === 'function') ref(el) else if (ref) ref.current = el }} value={ maskOptions ? format(value ?? '', maskOptions) : value ?? '' } onChange={(e) => { onChange?.(maskOptions ? unformat(e.target.value, maskOptions) : e.target.value) }} /> })