Skip to content

Instantly share code, notes, and snippets.

@Verthon
Created August 31, 2020 12:51
Show Gist options
  • Save Verthon/00ff393e15b1aed8a4e377438310e660 to your computer and use it in GitHub Desktop.
Save Verthon/00ff393e15b1aed8a4e377438310e660 to your computer and use it in GitHub Desktop.
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