UNPKG

1.8 kBJavaScriptView Raw
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3
4import { Button, TextAreaStyled } from './index';
5
6export default class TextArea extends Component {
7 constructor(props) {
8 super(props);
9 this.state = { textareaVal: '' };
10 }
11 render() {
12 return (
13 <TextAreaStyled
14 isValid={this.props.isValid}
15 isInvalid={this.props.isInvalid}
16 textSize={this.props.textSize}
17 >
18 <Button
19 buttonLink
20 success
21 inline
22 small
23 onClick={() => {
24 const textVal = this.textArea;
25 const cursorStart = textVal.selectionStart;
26 const cursorEnd = textVal.selectionEnd;
27 const text = this.state.textareaVal;
28 this.setState({
29 textareaVal: `${text.substr(0, cursorStart)}***${text.substr(
30 cursorStart,
31 cursorEnd - cursorStart,
32 )}***${text.substr(cursorEnd)}`,
33 });
34 }}
35 >
36 Bold
37 </Button>
38 <label htmlFor={this.props.name}>
39 {this.props.children}
40 </label>
41 <textarea
42 {...this.props}
43 name={this.props.name}
44 ref={(c) => {
45 this.textArea = c;
46 }}
47 value={this.state.textareaVal}
48 onChange={(event) => {
49 this.setState({
50 textareaVal: event.target.value,
51 });
52 }}
53 />
54 </TextAreaStyled>
55 );
56 }
57}
58
59TextArea.defaultProps = {
60 children: '',
61 name: '',
62 isValid: false,
63 isInvalid: false,
64 textSize: '16px',
65};
66
67TextArea.propTypes = {
68 children: PropTypes.node.isRequired,
69 name: PropTypes.string,
70 isValid: PropTypes.bool,
71 isInvalid: PropTypes.bool,
72 textSize: PropTypes.string,
73};