joywok-material-components
Version:
<h1 align="center"> Joywok Material Components </h1>
185 lines (155 loc) • 26.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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 _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _TextField = require('@material-ui/core/TextField');
var _TextField2 = _interopRequireDefault(_TextField);
var _constants = require('../constants');
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; } /**
*
* @api {} 输入框 / textarea (设置成 multiline 即可)
* @apiName 可以设置最大字符 比如限制 200个字符
* @apiGroup 组件使用
* @apiParam {String} className 自定义样式名,用于自定义样式时使用
* @apiParam {String} placeholder 输入框占位文字,默认为空
* @apiParam {String} value 输入框内容,编辑时,可以把原有值传入
* @apiParam multiline 多行属性,如果设置后,会是textare模式, 不设置,即为 单行输入框
* @apiParam {String} limitDict 默认为 "字符限制 { nums }"
* @apiParam {Int} limit 限制最多输入的字符数量,不传 就不限制
* @apiParam {function } onChange 输入内容发生变化之后的回调事件,回传event function(event: object)
* @apiParam 小技巧:使用 compositionstart 和 compositionend 兼容 中文输入(避免中文输入过程被截断),中文输入之后截取限制的字符数
*
* @apiSuccessExample {json} 使用案例:
import { TextField as JwInput } from 'joywok-material-components';
onChange(e){
console.log('input change:',e.target.value)
}
// 输入框
<JwInput
className: '',
limit: 200,
onChange={(e)=>this.onChange(e)}
/>
// textarea
<JwInput
placeholder={"请输入"}
multiline
limit={300}
onChange={(e)=>this.onChange(e)}
/>
*
*
*/
require('./style/index.css');
console.log("COMPONENT_DICT:::::", (0, _constants.COMPONENT_DICT)('label.input.text.limit'));
var JwTextField = function (_React$Component) {
_inherits(JwTextField, _React$Component);
function JwTextField(props) {
_classCallCheck(this, JwTextField);
var _this = _possibleConstructorReturn(this, (JwTextField.__proto__ || Object.getPrototypeOf(JwTextField)).call(this, props));
var value = props.value || '';
if (props.limit && props.limit > 0) {
value = (0, _constants.jwSubStr)(value, props.limit);
}
_this.state = {
className: props.className || '',
limitDict: props.limitDict || (0, _constants.COMPONENT_DICT)('label.input.text.limit') + " { nums }",
limit: props.limit || null,
value: value
};
_this.id = (0, _constants.randomString)(16);
return _this;
}
_createClass(JwTextField, [{
key: 'onChange',
value: function onChange(e) {
var value = e.target.value;
if (value == this.state.value) return;
// 如果限制输入字符数
if (this.props.limit && this.props.limit > 0) {
// 如果正在输入中文,不限制
if ($('#' + this.id).find('input').prop('comStart')) {
this.setState({
value: value
});
typeof this.props.onChange == 'function' && this.props.onChange(e);
// 判断有没有超限
} else {
if ((0, _constants.DataLength)(value) <= this.props.limit) {
this.setState({
value: value
});
typeof this.props.onChange == 'function' && this.props.onChange(e);
}
}
// 不限制字符数
} else {
this.setState({
value: value
});
typeof this.props.onChange == 'function' && this.props.onChange(e);
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps, nextState) {
if (nextProps.value != undefined && nextProps.value != null) {
this.setState({
value: nextProps.value
});
} else if ($.trim(nextProps.value) == '') {
this.setState({
value: ''
});
}
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var self = this;
if (this.props.limit && this.props.limit > 0) {
$('#' + this.id).find('input').on('compositionstart', function () {
$(this).prop('comStart', true);
// console.log('中文输入:开始');
}).on('compositionend', function (e) {
$(this).prop('comStart', false);
// console.log('中文输入:结束',$(this));
if ((0, _constants.DataLength)($(this).val()) > self.props.limit) {
self.setState({
value: (0, _constants.jwSubStr)($(this).val(), self.props.limit)
});
typeof self.props.onEnd == 'function' && self.props.onEnd(e);
}
});
}
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
return _react2.default.createElement(
'div',
{ id: this.id, className: "jw-input " + this.state.className },
_react2.default.createElement(_TextField2.default, _extends({}, this.props, { value: this.state.value, onChange: function onChange(e) {
return _this2.onChange(e);
} })),
this.state.limit != null && typeof this.state.limit == 'number' ? _react2.default.createElement(
'div',
{ className: 'jw-input-limit' },
this.state.limitDict.replace('{ nums }', (0, _constants.DataLength)(this.state.value) + "/" + this.state.limit)
) : ''
);
}
}]);
return JwTextField;
}(_react2.default.Component);
exports.default = JwTextField;