diff --git a/snippets/LimitedTextarea.md b/snippets/LimitedTextarea.md index 995e73630..3fb7111a2 100644 --- a/snippets/LimitedTextarea.md +++ b/snippets/LimitedTextarea.md @@ -2,41 +2,41 @@ Renders a textarea component with a character limit. -Use the value of the `value` prop to determine the initial `state.content` and `state.characterCount` and the value of the `limit` props to determine the value of `state.limit`. -Create a method, `handleChange`, which trims the `event.target.value` data if necessary and uses `Component.prototype.setState` to update `state.content` and `state.characterCount`, and bind it to the component's context. +Use the `value` and `limit` props to pass in the initial `content` and the `limit` values for the LimitedTextArea component. +Create a method, `handleChange`, which trims the `event.target.value` data if necessary and updates `content` with the new entered content. In the`render()` method, use a`
` to wrap both the` -

{this.state.characterCount}/{this.props.limit}

-
- ); - } +function LimitedTextArea(props) { + const { rows, cols, value, limit } = props; + + const setFormattedContent = text => { + text.length > limit ? setContent(text.slice(0, limit)) : setContent(text); + }; + + const [content, setContent] = useState(value); + // Run once to test if the initial value is greater than the limit + useEffect(() => { + setFormattedContent(content); + }, []); + + const handleChange = event => { + setFormattedContent(event.target.value); + }; + + return ( +
+