From fd813aa738f0961aeee003cc2b5bafe3fdbc0f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Thu, 13 Dec 2018 10:33:36 +0100 Subject: [PATCH 1/2] add limited word text area --- snippets/LimitedWordTextArea.md | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 snippets/LimitedWordTextArea.md diff --git a/snippets/LimitedWordTextArea.md b/snippets/LimitedWordTextArea.md new file mode 100644 index 000000000..662ec93dd --- /dev/null +++ b/snippets/LimitedWordTextArea.md @@ -0,0 +1,62 @@ +### LimitedWordTextArea + +Renders a textarea component with a word limit. + +Use the value of the `value` prop to determine the initial `state.content` and `state.wordCount` 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.wordCount`, and bind it to the component's context. +In the`render()` method, use a`
` to wrap both the` +

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

+
+ ); + } +} +``` + +```jsx +ReactDOM.render( + , + document.getElementById('root') +); +``` + +< !--tags: input,state,class -- > + +< !--expertise: 0 -- > + From 66878b1a79f2b8d4f5d875f7aa029ee579da1fc4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 20 Feb 2019 18:22:22 +0200 Subject: [PATCH 2/2] Update and rename LimitedWordTextArea.md to LimitedWordTextarea.md --- snippets/LimitedWordTextArea.md | 62 --------------------------------- snippets/LimitedWordTextarea.md | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 62 deletions(-) delete mode 100644 snippets/LimitedWordTextArea.md create mode 100644 snippets/LimitedWordTextarea.md diff --git a/snippets/LimitedWordTextArea.md b/snippets/LimitedWordTextArea.md deleted file mode 100644 index 662ec93dd..000000000 --- a/snippets/LimitedWordTextArea.md +++ /dev/null @@ -1,62 +0,0 @@ -### LimitedWordTextArea - -Renders a textarea component with a word limit. - -Use the value of the `value` prop to determine the initial `state.content` and `state.wordCount` 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.wordCount`, and bind it to the component's context. -In the`render()` method, use a`
` to wrap both the` -

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

-
- ); - } -} -``` - -```jsx -ReactDOM.render( - , - document.getElementById('root') -); -``` - -< !--tags: input,state,class -- > - -< !--expertise: 0 -- > - diff --git a/snippets/LimitedWordTextarea.md b/snippets/LimitedWordTextarea.md new file mode 100644 index 000000000..189b54ff1 --- /dev/null +++ b/snippets/LimitedWordTextarea.md @@ -0,0 +1,61 @@ +### LimitedWordTextarea + +Renders a textarea component with a word limit. + +Use the `React.useState()` hook to create the `content` and `wordCount` state variables and set their values to `value` and `0` respectively. +Create a method `setFormattedContent`, which uses `String.prototype.split(' ')` to turn the input into an array of words and check if the result of applying `Array.prototype.filter(Boolean)` has a `length` longer than `limit`. +If the afforementioned `length` exceeds the `limit`, trim the input, otherwise return the raw input, updating `content` and `wordCount` accordingly in both cases. +Use the `React.useEffect()` hook to call the `setFormattedContent` method on the value of the `content` state variable. +Use a`
` to wrap both the`