Created
August 31, 2020 12:51
-
-
Save Verthon/00ff393e15b1aed8a4e377438310e660 to your computer and use it in GitHub Desktop.
This file contains 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
export const InputWithCounter = ({ maxLength, control, placeholder, name, rules }: Props) => { | |
const [counter, setCounter] = useState(0); | |
const handleInputCounter = (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => { | |
const value = e.target.value.length; | |
setCounter(value); | |
}; | |
return ( | |
<Controller | |
render={() => ( | |
<Styled.InputWithCounter | |
name={name} | |
placeholder={placeholder} | |
onChange={e => handleInputCounter(e)} | |
maxLength={maxLength} | |
defaultValue="" | |
suffix={ | |
<Styled.WordCounter> | |
{counter} / {maxLength} | |
</Styled.WordCounter> | |
} | |
/> | |
)} | |
control={control} | |
type="text" | |
name={name} | |
rules={rules} | |
/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment