tsp-component
Version:
提供多端和react版本的UI组件
92 lines (91 loc) • 3.8 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import * as React from 'react';
import classNames from 'classnames';
import FormCore from '../form/core';
import { regexp } from '../util/validate';
var prefix = 'tsp-component-Textarea';
var Textarea = (function (_super) {
__extends(Textarea, _super);
function Textarea(props, state) {
var _this = _super.call(this, props, state) || this;
_this.state = {
value: _this.props.defaultValue
};
_this.onChange = _this.onChange.bind(_this);
_this.onBlur = _this.onBlur.bind(_this);
_this.onKeyUp = _this.onKeyUp.bind(_this);
return _this;
}
Textarea.prototype.componentDidMount = function () {
this.preElem = this.refs.pre;
this.formCore = new FormCore({
elem: this.refs.elem,
required: this.props.required,
defaultValue: this.props.defaultValue
});
this.preElem.textContent = this.props.defaultValue.toString();
};
Textarea.prototype.componentDidUpdate = function (prevProps) {
if (this.props.defaultValue !== prevProps.defaultValue) {
this.preElem.textContent = this.props.defaultValue.toString();
}
};
Textarea.prototype.onKeyUp = function (e) {
if (this.props.auto) {
if (e.keyCode === 13) {
this.preElem.textContent += ' ';
}
}
};
Textarea.prototype.onChange = function (e) {
var value = e.target.value;
if (this.props.emoji) {
value = value.replace(regexp.emoji, '');
}
if (this.props.onChange) {
this.props.onChange(value, e);
}
this.formCore.elem.dataset.value = value;
if (this.props.auto) {
this.preElem.textContent = value;
}
this.setState({ value: value });
};
Textarea.prototype.onBlur = function (e) {
if (this.props.onBlur) {
this.props.onBlur(this.state.value, e);
}
};
Textarea.prototype.render = function () {
return (React.createElement("div", { className: prefix + "-wrap" },
React.createElement("pre", { className: classNames((_a = {},
_a[prefix + "-hidecode"] = this.props.auto,
_a[prefix + "-hidecode-hidden"] = !this.props.auto,
_a)), ref: "pre" }),
React.createElement("textarea", { id: this.props.id, className: classNames((_b = {},
_b[prefix] = true,
_b[this.props.className] = this.props.className,
_b[prefix + "-disabled"] = this.props.disabled,
_b[prefix + "-auto"] = this.props.auto,
_b)), ref: "elem", placeholder: this.props.placeholder, value: this.state.value, maxLength: this.props.maxLength, disabled: this.props.disabled, readOnly: this.props.readOnly, onChange: this.onChange, onBlur: this.onBlur, onKeyUp: this.onKeyUp, rows: this.props.rows })));
var _a, _b;
};
Textarea.defaultProps = {
id: '',
defaultValue: '',
auto: false,
rows: 5,
emoji: true
};
return Textarea;
}(React.Component));
export default Textarea;