UNPKG

zent

Version:

一套前端设计语言和基于React的实现

193 lines (157 loc) 5.93 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactcss = require('../helpers/reactcss'); var _reactcss2 = _interopRequireDefault(_reactcss); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var EditableInput = function (_Component) { (0, _inherits3['default'])(EditableInput, _Component); function EditableInput(props) { (0, _classCallCheck3['default'])(this, EditableInput); var _this = (0, _possibleConstructorReturn3['default'])(this, (EditableInput.__proto__ || Object.getPrototypeOf(EditableInput)).call(this)); _this.handleBlur = function () { if (_this.state.blurValue) { _this.setState({ value: _this.state.blurValue, blurValue: null }); } }; _this.handleChange = function (e) { var label = !!_this.props.label; if (label) { _this.props.onChange((0, _defineProperty3['default'])({}, _this.props.label, e.target.value), e); } else { _this.props.onChange(e.target.value, e); } _this.setState({ value: e.target.value }); }; _this.handleKeyDown = function (e) { var number = Number(e.target.value); if (!isNaN(number)) { var amount = _this.props.arrowOffset || 1; // Up if (e.keyCode === 38) { if (_this.props.label !== null) { _this.props.onChange((0, _defineProperty3['default'])({}, _this.props.label, number + amount), e); } else { _this.props.onChange(number + amount, e); } _this.setState({ value: number + amount }); } // Down if (e.keyCode === 40) { if (_this.props.label !== null) { _this.props.onChange((0, _defineProperty3['default'])({}, _this.props.label, number - amount), e); } else { _this.props.onChange(number - amount, e); } _this.setState({ value: number - amount }); } } }; _this.handleDrag = function (e) { if (_this.props.dragLabel) { var newValue = Math.round(_this.props.value + e.movementX); if (newValue >= 0 && newValue <= _this.props.dragMax) { _this.props.onChange((0, _defineProperty3['default'])({}, _this.props.label, newValue), e); } } }; _this.handleMouseDown = function (e) { if (_this.props.dragLabel) { e.preventDefault(); _this.handleDrag(e); window.addEventListener('mousemove', _this.handleDrag); window.addEventListener('mouseup', _this.handleMouseUp); } }; _this.handleMouseUp = function () { _this.unbindEventListeners(); }; _this.unbindEventListeners = function () { window.removeEventListener('mousemove', _this.handleDrag); window.removeEventListener('mouseup', _this.handleMouseUp); }; _this.state = { value: String(props.value).toUpperCase(), blurValue: String(props.value).toUpperCase() }; return _this; } (0, _createClass3['default'])(EditableInput, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var InputRefs = this.refs; if (nextProps.value !== this.state.value) { if (InputRefs.input === document.activeElement) { this.setState({ blurValue: String(nextProps.value).toUpperCase() }); } else { this.setState({ value: String(nextProps.value).toUpperCase() }); } } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.unbindEventListeners(); } }, { key: 'render', value: function render() { var _this2 = this; var styles = (0, _reactcss2['default'])({ 'default': { wrap: { position: 'relative' } }, 'user-override': { wrap: this.props.style && this.props.style.wrap ? this.props.style.wrap : {}, input: this.props.style && this.props.style.input ? this.props.style.input : {}, label: this.props.style && this.props.style.label ? this.props.style.label : {} }, 'dragLabel-true': { label: { cursor: 'ew-resize' } } }, { 'user-override': true }, this.props); return _react2['default'].createElement( 'div', { style: styles.wrap }, _react2['default'].createElement('input', { prefix: 'colorpicker-rgb', style: styles.input, ref: function ref(_ref) { return _this2.refs = _ref; }, value: this.state.value, onKeyDown: this.handleKeyDown, onChange: this.handleChange, onBlur: this.handleBlur, placeholder: this.props.placeholder }), this.props.label ? _react2['default'].createElement( 'span', { style: styles.label, onMouseDown: this.handleMouseDown }, this.props.label ) : null ); } }]); return EditableInput; }(_react.Component); exports['default'] = EditableInput;