react-form-ui-y
Version:
248 lines (208 loc) • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactRedux = require('react-redux');
var _reactUtilsY = require('react-utils-y');
var _themeY = require('theme-y');
var _index = require('../../actions/index');
var _textareaStyle = require('./textareaStyle');
var _subpage = require('./subpage');
var _publicField = require('../publicField');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// import {Theme} from '../../../theme/index';
var propTypes = {
designationTheme: _propTypes2.default.string,
formType: _propTypes2.default.string,
inputName: _propTypes2.default.string.isRequired,
formText: _propTypes2.default.string,
disabled: _propTypes2.default.bool,
required: _propTypes2.default.bool,
hintText: _propTypes2.default.string,
textWidth: _propTypes2.default.string,
valueWidth: _propTypes2.default.string,
changeValue: _propTypes2.default.func,
errorText: _propTypes2.default.string,
dataType: _propTypes2.default.string,
// remarks 无规则 自定义传参
textlength: _propTypes2.default.number, // textarea 特有
maxHeight: _propTypes2.default.string //textarea 特有
};
var extra = void 0,
isFirefox = void 0,
isOpera = void 0,
getStyle = void 0,
minHeight = void 0,
addEvent = void 0;
var TextareaField = function (_Theme) {
_inherits(TextareaField, _Theme);
function TextareaField(props) {
_classCallCheck(this, TextareaField);
var _this = _possibleConstructorReturn(this, (TextareaField.__proto__ || Object.getPrototypeOf(TextareaField)).call(this, props));
_this.autoTextarea = function (elem, extr, maxHeight) {
extra = extr || 0;
isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window;
isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera');
getStyle = elem.currentStyle ? function (name) {
var val = elem.currentStyle[name];
if (name === 'height' && val.search(/px/i) !== 1) {
var rect = elem.getBoundingClientRect();
return rect.bottom - rect.top - parseFloat(getStyle('paddingTop')) - parseFloat(getStyle('paddingBottom')) + 'px';
}
return val;
} : function (name) {
return getComputedStyle(elem, null)[name];
};
minHeight = parseFloat(getStyle('height'));
addEvent = function addEvent(type, callback) {
elem.addEventListener ? elem.addEventListener(type, callback, false) : elem.attachEvent('on' + type, callback);
};
elem.style.resize = 'none';
};
_this.change = function (e) {
var value = e.target.value;
var elem = document.getElementById(_this.props.inputName + "TA"),
maxHeight = _this.props.maxHeight || "600px",
scrollTop = void 0,
height = void 0,
padding = 0,
style = elem.style;
maxHeight = maxHeight && maxHeight.indexOf('rem') >= 0 ? parseFloat(maxHeight) * 16 : parseFloat(maxHeight);
//let value = elem.value;
// if (elem._length === elem.value.length) return;
// elem._length = elem.value.length;
if (!isFirefox && !isOpera) {
padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
}
scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
elem.style.height = minHeight / 16 + 'rem';
if (elem.scrollHeight > minHeight) {
if (maxHeight && elem.scrollHeight > maxHeight) {
height = maxHeight - padding;
style.overflowY = 'auto';
} else {
height = elem.scrollHeight - padding;
style.overflowY = 'hidden';
}
style.height = (height + extra) / 16 + 'rem';
scrollTop += parseInt(style.height) - elem.currHeight;
//document.body.scrollTop = scrollTop;
document.documentElement.scrollTop = scrollTop;
elem.currHeight = parseInt(style.height);
}
_this.setState({
focusState: true,
length: value.length
});
var obj = [{
type: "textarea", //type 类型
name: _this.props.inputName, //name input名称
text: value, //text input值
value: value,
obj: {},
remarks: _this.props.remarks, //备注
other: {
required: _this.props.required,
length: value.length
}
}];
_this.props.setFormData(obj, _this.props.dataType);
_this.props.changeValue && _this.props.changeValue(obj[0]);
};
_this.textareaFocus = function (e) {
_this.setState({
focusState: true
});
};
_this.textareaBlur = function (e) {
_this.setState({
focusState: false,
initFocusState: 1
});
};
var _state = {
focusState: false, //处理点击的样式
initFocusState: 0, //是否触发过 0 未触发 1 已经触发
errorState: false //是否显示报错提示框, true 显示报错 false 不显示
//styles: _Object.merge({}, textareaSty(this.state))
};
_this.state = _reactUtilsY._Object.merge(_this.state || {}, _state);
return _this;
}
_createClass(TextareaField, [{
key: 'componentWillMount',
value: function componentWillMount() {
var props = this.props;
this.setState({
rows: props.rows || "5"
});
var flag = "set";
try {
//已经存在数据 就不 重新赋值
flag = props.formData[props.dataType || "default"][props.inputName] ? "none" : "set";
} catch (ex) {
flag = "set";
} finally {
if (props.inputName && flag == "set") {
var obj = [{
type: "textarea", //type 类型
name: this.props.inputName, //name input名称
text: "", // textarea输入值
value: "", // textarea输入值
obj: {}, //无
remarks: this.props.remarks, //备注
other: {
required: this.props.required
}
}];
props.setFormData(obj, props.dataType);
}
}
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var textarea = document.getElementById(this.props.inputName + "TA");
textarea && this.autoTextarea(textarea);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.props.delFormData(this.props.inputName || "", this.props.dataType);
}
}, {
key: 'render',
value: function render() {
return (0, _publicField.renderBox)(this, _textareaStyle.textareaSty, _subpage.formTypeDom, _subpage.verifyFn);
}
}]);
return TextareaField;
}(_themeY.Theme);
TextareaField.propTypes = propTypes;
var mapStateToProps = function mapStateToProps(state) {
return {
formData: state.formData,
subVerifyRes: state.subVerifyRes,
theme: state.theme
};
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
return {
setFormData: function setFormData(data, dataType) {
return dispatch((0, _index.setFormData)(data, dataType));
},
delFormData: function delFormData(inputName, dataType) {
return dispatch((0, _index.delFormData)(inputName, dataType));
}
};
};
exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(TextareaField);
//# sourceMappingURL=textareaField.js.map