@txdfe/at
Version:
一个设计体系组件库
191 lines • 10 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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) {
function TransferItem(props) {
var _this;
_classCallCheck(this, TransferItem);
_this = _callSuper(this, TransferItem, [props]);
_this.state = {
highlight: false
};
bindCtx(_this, ['getItemDOM', 'handleClick', 'handleDragStart', 'handleDragOver', 'handleDragEnd', 'handleDrop']);
return _this;
}
_inherits(TransferItem, _Component);
return _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 _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(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "transfer-panel-item"), true), "".concat(prefix, "insert-").concat(this.dragGap), dragOverValue === item.value && panelPosition === dragPosition), "".concat(prefix, "focused"), highlight), "".concat(prefix, "simple"), isSimple));
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));
}
}]);
}(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 };