change limited text area component to hooks implementation
This commit is contained in:
@ -2,42 +2,42 @@
|
|||||||
|
|
||||||
Renders a textarea component with a character limit.
|
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`.
|
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 uses `Component.prototype.setState` to update `state.content` and `state.characterCount`, and bind it to the component's context.
|
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`<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.
|
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
|
```jsx
|
||||||
class LimitedTextarea extends React.Component {
|
function LimitedTextArea(props) {
|
||||||
constructor(props) {
|
const { rows, cols, value, limit } = props;
|
||||||
super(props);
|
|
||||||
this.state = {
|
const setFormattedContent = text => {
|
||||||
content: props.value,
|
text.length > limit ? setContent(text.slice(0, limit)) : setContent(text);
|
||||||
characterCount: props.value.length,
|
};
|
||||||
limit: props.limit
|
|
||||||
};
|
const [content, setContent] = useState(value);
|
||||||
this.handleChange = this.handleChange.bind(this);
|
// Run once to test if the initial value is greater than the limit
|
||||||
}
|
useEffect(() => {
|
||||||
|
setFormattedContent(content);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleChange = event => {
|
||||||
|
setFormattedContent(event.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
handleChange(event) {
|
|
||||||
let newContent = event.target.value;
|
|
||||||
if(newContent.length >= this.state.limit) newContent = newContent.slice(0, this.state.limit);
|
|
||||||
this.setState(state => ({ content: newContent, characterCount: newContent.length }));
|
|
||||||
}
|
|
||||||
render() {
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<textarea
|
<textarea
|
||||||
rows={this.props.rows}
|
rows={rows}
|
||||||
cols={this.props.cols}
|
cols={cols}
|
||||||
onChange={this.handleChange}
|
onChange={handleChange}
|
||||||
value={this.state.content}
|
value={content}
|
||||||
>
|
/>
|
||||||
</textarea>
|
<p>
|
||||||
<p>{this.state.characterCount}/{this.props.limit}</p>
|
{content.length}/{limit}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
@ -46,7 +46,6 @@ ReactDOM.render(
|
|||||||
document.getElementById('root')
|
document.getElementById('root')
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- tags: input,state,class -->
|
<!-- tags: input,state,class -->
|
||||||
|
|
||||||
<!-- expertise: 0 -->
|
<!-- expertise: 0 -->
|
||||||
|
|||||||
Reference in New Issue
Block a user