UNPKG

tsp-component

Version:

提供多端和react版本的UI组件

81 lines (80 loc) 3.23 kB
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, validatePattern } from '../util/validate'; var prefix = 'tsp-component-Input'; var Input = (function (_super) { __extends(Input, _super); function Input(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); return _this; } Input.prototype.componentDidMount = function () { this.formCore = new FormCore({ elem: this.refs.elem, required: this.props.required, patternType: this.props.patternType, pattern: this.props.pattern, patternTrigger: this.props.patternTrigger, patternMsg: this.props.patternMsg, defaultValue: this.props.defaultValue }); }; Input.prototype.componentWillReceiveProps = function (nextProps) { if (this.props.defaultValue !== nextProps.defaultValue) { this.setState({ value: nextProps.defaultValue }); this.formCore.setValue(nextProps.defaultValue); } }; Input.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; this.setState({ value: value }); }; Input.prototype.onBlur = function (e) { validatePattern(this.formCore.elem); if (this.props.onBlur) { this.props.onBlur(this.state.value, e); } }; Input.prototype.render = function () { return (React.createElement("input", { id: this.props.id, className: classNames((_a = {}, _a[prefix] = true, _a[this.props.className] = this.props.className, _a[prefix + "-disabled"] = this.props.disabled, _a)), ref: "elem", type: this.props.type, 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 })); var _a; }; Input.defaultProps = { id: '', type: 'text', patternTrigger: true, defaultValue: '', emoji: true }; return Input; }(React.Component)); export default Input;