chowa
Version:
UI component library based on React
83 lines (82 loc) • 2.74 kB
JavaScript
/**
* @license chowa v1.1.3
*
* Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn).
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const React = require("react");
const PropTypes = require("prop-types");
const classnames_1 = require("classnames");
const utils_1 = require("../utils");
class Textarea extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
result: props.defaultValue || props.value || ''
};
[
'onChangeHandler',
'onKeyDownHandler'
].forEach((fn) => {
this[fn] = this[fn].bind(this);
});
}
componentDidUpdate(preProps) {
if (preProps.value !== this.props.value && this.state.result !== this.props.value) {
this.setState({ result: this.props.value || '' });
}
}
onKeyDownHandler(e) {
if (e.keyCode === 13 && this.props.onPressEnter) {
this.props.onPressEnter(e);
}
}
onChangeHandler(e) {
if (this.props.onChange) {
this.props.onChange(e);
}
this.setState({
result: e.target.value
});
}
focus() {
this.htmlEle.focus();
}
blur() {
this.htmlEle.blur();
}
render() {
const { className, disabled, placeholder, resizeable, tabIndex } = this.props;
const { result } = this.state;
const componentClass = classnames_1.default({
[utils_1.preClass('textarea')]: true,
[utils_1.preClass('textarea-can-resize')]: resizeable,
[className]: utils_1.isExist(className)
});
return (React.createElement("textarea", Object.assign({}, utils_1.otherProps(Textarea.propTypes, this.props), { tabIndex: disabled ? -1 : tabIndex, className: componentClass, disabled: disabled, placeholder: placeholder, value: result, onChange: this.onChangeHandler, onKeyDown: this.onKeyDownHandler, ref: (ele) => {
this.htmlEle = ele;
} })));
}
}
Textarea.propTypes = {
className: PropTypes.string,
tabIndex: PropTypes.number,
disabled: PropTypes.bool,
placeholder: PropTypes.string,
resizeable: PropTypes.bool,
onPressEnter: PropTypes.func,
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};
Textarea.defaultProps = {
tabIndex: 0,
disabled: false,
resizeable: false,
value: undefined
};
exports.default = Textarea;