-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextarea.js
73 lines (65 loc) · 1.66 KB
/
textarea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { Component, PropTypes } from 'react';
import toCSS from 'style-to-css';
import uniqueId from 'mini-unique-id';
const PLACEHOLDER_PREFIXES = [
'::-webkit-input-placeholder',
'::-moz-placeholder',
':-ms-input-placeholder',
':placeholder-shown'
];
export default class Textarea extends Component {
constructor(...args) {
super(...args);
this.id = `Textarea-${uniqueId()}`;
}
render() {
const { id } = this;
const {
_ref, stylePlaceholder, styleHover, styleFocus, styleDisabled, text, ...rest
} = this.props;
const inlineStyle = [];
if (stylePlaceholder) {
PLACEHOLDER_PREFIXES.forEach(prefix => {
inlineStyle.push(`#${id}${prefix} {${toCSS(stylePlaceholder)}}`);
});
}
if (styleHover) {
inlineStyle.push(`${inlineStyle} #${id}:hover {${toCSS(styleHover)}}`);
}
if (styleFocus) {
inlineStyle.push(`#${id}:focus {${toCSS(styleFocus)}}`);
}
if (styleDisabled) {
inlineStyle.push(`#${id}:disabled {${toCSS(styleDisabled)}}`);
}
return (
<div>
<style>{inlineStyle.join('\n')}</style>
<textarea
{...rest}
defaultValue={text}
id={id}
ref={_ref}
/>
</div>
);
}
}
Textarea.defaultProps = {
style: {},
styleDisabled: {},
styleFocus: {},
stylePlaceholder: {},
text: ''
};
Textarea.propTypes = {
disabled: PropTypes.bool,
maxlength: PropTypes.number,
placeholder: PropTypes.string,
rows: PropTypes.number,
style: PropTypes.object,
styleDisabled: PropTypes.object,
styleFocus: PropTypes.object,
stylePlaceholder: PropTypes.object,
text: PropTypes.string.isRequired
};