Update and rename LimitedWordTextArea.md to LimitedWordTextarea.md
This commit is contained in:
@ -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`<div>` to wrap both the`<textarea>` and the `<p>` element that displays the character count and bind the `onChange` event of the `<textarea>` to the `handleChange` method.
|
||||
|
||||
```jsx
|
||||
class LimitedWordTextArea extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
content: props.value,
|
||||
wordCount: props.value.split(' ').filter(Boolean).length,
|
||||
limit: props.limit
|
||||
};
|
||||
this.handleChange = this.handleChange.bind(this);
|
||||
}
|
||||
|
||||
handleChange(event) {
|
||||
let newContent = event.target.value;
|
||||
|
||||
if (newContent.split(' ').filter(Boolean).length > this.state.limit) {
|
||||
newContent = newContent
|
||||
.split(' ')
|
||||
.splice(0, this.state.limit)
|
||||
.join(' ');
|
||||
}
|
||||
this.setState(state => ({
|
||||
content: newContent,
|
||||
wordCount: newContent.split(' ').filter(Boolean).length
|
||||
}));
|
||||
}
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<textarea
|
||||
rows={this.props.rows}
|
||||
cols={this.props.cols}
|
||||
onChange={this.handleChange}
|
||||
value={this.state.content}
|
||||
>
|
||||
</textarea>
|
||||
<p>{this.state.wordCount} / {this.props.limit}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```jsx
|
||||
ReactDOM.render(
|
||||
<LimitedWordTextArea limit={5} value='Hello there!' />,
|
||||
document.getElementById('root')
|
||||
);
|
||||
```
|
||||
|
||||
< !--tags: input,state,class -- >
|
||||
|
||||
< !--expertise: 0 -- >
|
||||
|
||||
61
snippets/LimitedWordTextarea.md
Normal file
61
snippets/LimitedWordTextarea.md
Normal file
@ -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`<div>` to wrap both the`<textarea>` and the `<p>` element that displays the character count and bind the `onChange` event of the `<textarea>` to call `setFormattedContent` with the value of `event.target.value`.
|
||||
|
||||
```jsx
|
||||
function LimitedWordTextarea({ rows, cols, value, limit }) {
|
||||
const [content, setContent] = React.useState(value);
|
||||
const [wordCount, setWordCount] = React.useState(0);
|
||||
|
||||
const setFormattedContent = text => {
|
||||
let words = text.split(" ");
|
||||
if (words.filter(Boolean).length > limit) {
|
||||
setContent(
|
||||
text
|
||||
.split(" ")
|
||||
.slice(0, limit)
|
||||
.join(" ")
|
||||
);
|
||||
setWordCount(limit);
|
||||
} else {
|
||||
setContent(text);
|
||||
setWordCount(words.filter(Boolean).length);
|
||||
}
|
||||
};
|
||||
|
||||
React.useEffect(() => {
|
||||
setFormattedContent(content);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<textarea
|
||||
rows={rows}
|
||||
cols={cols}
|
||||
onChange={event => setFormattedContent(event.target.value)}
|
||||
value={content}
|
||||
/>
|
||||
<p>
|
||||
{wordCount}/{limit}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
```jsx
|
||||
ReactDOM.render(
|
||||
<LimitedWordTextArea limit={5} value='Hello there!' />,
|
||||
document.getElementById('root')
|
||||
);
|
||||
```
|
||||
<!-- tags: input,state,effect -->
|
||||
|
||||
<!-- expertise: 0 -->
|
||||
|
||||
Reference in New Issue
Block a user