joywok-material-components
Version:
<h1 align="center"> Joywok Material Components </h1>
229 lines (198 loc) • 10.6 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 _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _async = require('async');
var _async2 = _interopRequireDefault(_async);
var _core = require('@material-ui/core');
var _input2 = require('./../input');
var _input3 = _interopRequireDefault(_input2);
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; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /**
*
* @api {} 自添加input输入框
* @apiName 输入组件
* @apiGroup 组件使用
*
* @apiParam {Array } defaultValue 多少个input以及每个input的内容
* @apiParam {function } onChange 返回所有的input数据
*
* @apiSuccessExample {json} 使用案例:
import AutoEditor from 'joywok-material-components/lib/editor/autoEditor';
<AutoEditor defaultValue={[]} onChange={(e)=>{console.log(e)}}></AutoEditor>
*
*
*/
window.htmlencode = function (s) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(s));
var html = div.innerHTML;
return html;
};
window.htmldecode = function (s) {
var div = document.createElement('div');
div.innerHTML = s;
var content = div.innerText || div.textContent;
return content;
};
var styles = function styles(theme) {
return {
input: _defineProperty({
'&:before': {
borderBottom: '1px solid #E7E7E7!important'
},
'&:after': {
borderBottom: '1px solid #E7E7E7!important'
},
'&:hover:after': {
borderBottom: '1px solid #3297FC!important'
},
'&:hover:before': {
borderBottom: '1px solid #3297FC!important'
},
'&:focus:after': {
borderBottom: '1px solid #3297FC!important'
}
}, '&:focus:after', {
borderBottom: '1px solid #3297FC!important'
})
};
};
require('./style/autoEditor.css');
var JwEditor = function (_React$Component) {
_inherits(JwEditor, _React$Component);
function JwEditor(props) {
_classCallCheck(this, JwEditor);
// let beforData = props.defaultValue.slice(0,(props.defaultValue.length==0?0:props.defaultValue.length-1))
var _this = _possibleConstructorReturn(this, (JwEditor.__proto__ || Object.getPrototypeOf(JwEditor)).call(this, props));
_this.state = {
defaultValue: props.defaultValue.length != 0 ? props.defaultValue : ['']
};
return _this;
}
_createClass(JwEditor, [{
key: 'onChange',
value: function onChange(e, targetIndex) {
var value = e.target.value;
var list = JSON.parse(JSON.stringify(this.state.defaultValue));
// if(list.length>1){
// list[list.length-1] = value
// }else{
// list[0] = value
// }
_.each(list, function (i, index) {
if (index == targetIndex) {
list[index] = value;
}
});
this.setState({
// inputValue:value,
defaultValue: list
});
this.props.onChange && this.props.onChange(list);
}
}, {
key: 'onKeyUp',
value: function onKeyUp(e, targetIndex) {
var self = this;
var keyCode = e.keyCode;
var value = e.target.value;
if (keyCode == 13 && value.length != 0) {
var list = JSON.parse(JSON.stringify(this.state.defaultValue));
var newList = [];
_.each(list, function (i, index) {
if (index == targetIndex) {
newList.push(i);
newList.push('');
} else {
newList.push(i);
}
});
self.setState({
defaultValue: newList
});
this.props.onChange && this.props.onChange(list);
setTimeout(function () {
$('.jw-auto-editor-input').eq(targetIndex + 1).find('input').focus();
}, 0);
}
if (keyCode == 8 && value.length == 0) {
var _list = JSON.parse(JSON.stringify(this.state.defaultValue));
var _newList = [];
_.each(_list, function (i, index) {
if (index == targetIndex) {} else {
_newList.push(i);
}
});
if (_newList.length == 0) {
_newList.push('');
}
this.props.onChange && this.props.onChange(_newList);
self.setState({
defaultValue: _newList
});
}
}
}, {
key: 'onPaste',
value: function onPaste(e) {
var value = e.target.value;
var list = JSON.parse(JSON.stringify(this.state.defaultValue));
_.each(list, function (i, index) {
if (index == targetIndex) {
list[index] = value;
}
});
this.setState({
// inputValue:value,
defaultValue: list
});
this.props.onChange && this.props.onChange(list);
}
}, {
key: 'render',
value: function render() {
var self = this;
var classes = this.props.classes;
var list = this.state.defaultValue;
return _react2.default.createElement(
'div',
{ className: 'jw-auto-editor' },
_.map(list, function (i, index) {
return _react2.default.createElement(
'div',
{ className: 'jw-auto-editor-input' },
_react2.default.createElement(_input3.default, { onChange: function onChange(e) {
return self.onChange(e, index);
}, value: i, InputProps: {
className: classes.input
}, onKeyUp: function onKeyUp(e) {
return self.onKeyUp(e, index);
} })
);
})
);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var self = this;
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {}
}]);
return JwEditor;
}(_react2.default.Component);
exports.default = (0, _core.withStyles)(styles)(JwEditor);