Skip to content

Instantly share code, notes, and snippets.

@ErikGMatos
Created January 14, 2020 02:21
Show Gist options
  • Select an option

  • Save ErikGMatos/1880dde3b7d6424ac11be2f4b9a0496d to your computer and use it in GitHub Desktop.

Select an option

Save ErikGMatos/1880dde3b7d6424ac11be2f4b9a0496d to your computer and use it in GitHub Desktop.
import React, { useState, useRef, useEffect } from 'react';
import InputMask from 'react-input-mask';
import { useField } from '@rocketseat/unform';
export default function Mask({ name, inputMask }) {
const ref = useRef(null);
const { fieldName, registerField, defaultValue, error } = useField(name);
const [mask, setMask] = useState(defaultValue);
debugger
useEffect(() => {
registerField({
name: fieldName,
ref: ref.current,
path: 'props.value',
clearValue: pickerRef => {
pickerRef.setInputValue(null);
},
});
}, [ref.current, fieldName]); // eslint-disable-line
function handleMask(e) {
const { value } = e.target;
return setMask(value);
}
return (
<>
<InputMask
name={fieldName}
mask={inputMask}
value={mask}
onChange={e => handleMask(e)}
ref={ref}
/>
{error && <span>{error}</span>}
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment