@txdfe/at
Version:
一个设计体系组件库
225 lines (192 loc) • 10.7 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _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; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Menu from '../../menu';
import { func, obj, dom } from '../../util';
var Item = Menu.Item,
CheckboxItem = Menu.CheckboxItem;
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;
var getOffset = dom.getOffset;
var TransferItem = /*#__PURE__*/function (_Component) {
_inherits(TransferItem, _Component);
var _super = _createSuper(TransferItem);
function TransferItem(props) {
var _this;
_classCallCheck(this, TransferItem);
_this = _super.call(this, props);
_this.state = {
highlight: false
};
bindCtx(_assertThisInitialized(_this), ['getItemDOM', 'handleClick', 'handleDragStart', 'handleDragOver', 'handleDragEnd', 'handleDrop']);
return _this;
}
_createClass(TransferItem, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
if (this.props.needHighlight) {
this.addHighlightTimer = setTimeout(function () {
_this2.setState({
highlight: true
});
}, 1);
this.removeHighlightTimer = setTimeout(function () {
_this2.setState({
highlight: false
});
}, 201);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearTimeout(this.addHighlightTimer);
clearTimeout(this.removeHighlightTimer);
}
}, {
key: "getItemDOM",
value: function getItemDOM(ref) {
this.item = ref;
}
}, {
key: "handleClick",
value: function handleClick() {
var _this$props = this.props,
onClick = _this$props.onClick,
panelPosition = _this$props.panelPosition,
item = _this$props.item;
onClick(panelPosition === 'left' ? 'right' : 'left', item.value);
}
}, {
key: "handleDragStart",
value: function handleDragStart() {
var _this$props2 = this.props,
onDragStart = _this$props2.onDragStart,
panelPosition = _this$props2.panelPosition,
item = _this$props2.item;
onDragStart(panelPosition, item.value);
}
}, {
key: "getDragGap",
value: function getDragGap(e) {
var referenceTop = getOffset(e.currentTarget).top;
var referenceHeight = e.currentTarget.offsetHeight;
return e.pageY <= referenceTop + referenceHeight / 2 ? 'before' : 'after';
}
}, {
key: "handleDragOver",
value: function handleDragOver(e) {
var _this$props3 = this.props,
panelPosition = _this$props3.panelPosition,
dragPosition = _this$props3.dragPosition,
onDragOver = _this$props3.onDragOver,
item = _this$props3.item;
if (panelPosition === dragPosition) {
e.preventDefault();
var dragGap = this.getDragGap(e);
if (this.dragGap !== dragGap) {
this.dragGap = dragGap;
onDragOver(item.value);
}
}
}
}, {
key: "handleDragEnd",
value: function handleDragEnd() {
var onDragEnd = this.props.onDragEnd;
onDragEnd();
}
}, {
key: "handleDrop",
value: function handleDrop(e) {
e.preventDefault();
var _this$props4 = this.props,
onDrop = _this$props4.onDrop,
item = _this$props4.item,
panelPosition = _this$props4.panelPosition,
dragValue = _this$props4.dragValue;
onDrop(panelPosition, dragValue, item.value, this.dragGap);
}
}, {
key: "render",
value: function render() {
var _cx;
var _this$props5 = this.props,
prefix = _this$props5.prefix,
mode = _this$props5.mode,
checked = _this$props5.checked,
disabled = _this$props5.disabled,
item = _this$props5.item,
onCheck = _this$props5.onCheck,
itemRender = _this$props5.itemRender,
draggable = _this$props5.draggable,
dragOverValue = _this$props5.dragOverValue,
panelPosition = _this$props5.panelPosition,
dragPosition = _this$props5.dragPosition;
var others = pickOthers(Object.keys(TransferItem.propTypes), this.props);
var highlight = this.state.highlight;
var isSimple = mode === 'simple';
var classNames = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "transfer-panel-item"), true), _defineProperty(_cx, "".concat(prefix, "insert-").concat(this.dragGap), dragOverValue === item.value && panelPosition === dragPosition), _defineProperty(_cx, "".concat(prefix, "focused"), highlight), _defineProperty(_cx, "".concat(prefix, "simple"), isSimple), _cx));
var itemProps = _objectSpread({
ref: this.getItemDOM,
className: classNames,
children: itemRender(item),
disabled: disabled,
draggable: draggable && !disabled,
onDragStart: this.handleDragStart,
onDragOver: this.handleDragOver,
onDragEnd: this.handleDragEnd,
onDrop: this.handleDrop
}, others);
if (isSimple) {
if (!itemProps.disabled) {
itemProps.onClick = this.handleClick;
}
return /*#__PURE__*/React.createElement(Item, itemProps);
}
return /*#__PURE__*/React.createElement(CheckboxItem, _extends({
checked: checked,
onChange: onCheck.bind(this, item.value)
}, itemProps));
}
}]);
return TransferItem;
}(Component);
_defineProperty(TransferItem, "menuChildType", CheckboxItem.menuChildType);
_defineProperty(TransferItem, "propTypes", {
prefix: PropTypes.string,
mode: PropTypes.oneOf(['normal', 'simple']),
value: PropTypes.array,
disabled: PropTypes.bool,
item: PropTypes.object,
onCheck: PropTypes.func,
onClick: PropTypes.func,
needHighlight: PropTypes.bool,
itemRender: PropTypes.func,
draggable: PropTypes.bool,
onDragStart: PropTypes.func,
onDragOver: PropTypes.func,
onDragEnd: PropTypes.func,
onDrop: PropTypes.func,
dragPosition: PropTypes.oneOf(['left', 'right']),
dragValue: PropTypes.string,
dragOverValue: PropTypes.string,
panelPosition: PropTypes.oneOf(['left', 'right'])
});
export { TransferItem as default };