Created
January 14, 2020 02:21
-
-
Save ErikGMatos/1880dde3b7d6424ac11be2f4b9a0496d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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