@iimm/formily-mui
Version:
form field components based on @mui/material and @formily/react
352 lines • 16.7 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 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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useCreation, useControllableValue, useSafeState, useMemoizedFn } from "ahooks";
import { Box, IconButton, Skeleton, Tooltip } from "@mui/material";
import { IconArrowBigLeft, IconArrowBigRight, IconRefresh } from "@tabler/icons-react";
import { isEqual, isInArray, intersection, differenceSet as not, union } from "@iimm/shared";
import { useOverflow } from "@iimm/react-shared";
import { useFetchOptions } from "../../hooks";
import { FormItemBase } from "../../layout";
import { ListCard } from "./ListCard";
import "../../styles/refresh.scss";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var defaultTitles = ["可选项", "已选项"];
export var TransferBase = function TransferBase(props) {
var _ref, _ref2, _ref3;
var labelPosition = props.labelPosition,
labelWidth = props.labelWidth,
labelAlign = props.labelAlign,
labelWrap = props.labelWrap,
wrapperAlign = props.wrapperAlign,
wrapperWrap = props.wrapperWrap,
wrapperWidth = props.wrapperWidth,
fullWidth = props.fullWidth,
colon = props.colon,
tooltipIcon = props.tooltipIcon,
tooltipLayout = props.tooltipLayout,
showFeedback = props.showFeedback,
feedbackLayout = props.feedbackLayout,
noLabel = props.noLabel,
label = props.label,
labelStyle = props.labelStyle,
wrapperStyle = props.wrapperStyle,
tooltip = props.tooltip,
required = props.required,
feedbackStatus = props.feedbackStatus,
feedbackText = props.feedbackText,
feedbackIcon = props.feedbackIcon,
extra = props.extra,
addonBefore = props.addonBefore,
addonAfter = props.addonAfter,
formItemCls = props.formItemCls,
formItemStyle = props.formItemStyle,
formItemPrefixCls = props.formItemPrefixCls,
error = props.error,
feedbackCls = props.feedbackCls,
extraCls = props.extraCls,
keepTopSpace = props.keepTopSpace,
valueProp = props.value,
onChangeProp = props.onChange,
defaultValue = props.defaultValue,
noField = props.noField,
noFormLayout = props.noFormLayout,
withFormItem = props.withFormItem,
optionsProp = props.options,
listSxProp = props.listSx,
cardSxProp = props.cardSx,
cardHeaderSx = props.cardHeaderSx,
searchProps = props.searchProps,
itemCheckboxProps = props.itemCheckboxProps,
listItemTextProps = props.listItemTextProps,
iconButtonProps = props.iconButtonProps,
_props$keepExtraItems = props.keepExtraItems,
keepExtraItems = _props$keepExtraItems === void 0 ? true : _props$keepExtraItems,
width = props.width,
_props$minWidth = props.minWidth,
minWidth = _props$minWidth === void 0 ? 200 : _props$minWidth,
maxWidth = props.maxWidth,
height = props.height,
_props$minHeight = props.minHeight,
minHeight = _props$minHeight === void 0 ? 250 : _props$minHeight,
_props$maxHeight = props.maxHeight,
maxHeight = _props$maxHeight === void 0 ? "85vh" : _props$maxHeight,
containerBoxProps = props.containerBoxProps,
_props$overflowThresh = props.overflowThreshold,
overflowThreshold = _props$overflowThresh === void 0 ? 40 : _props$overflowThresh,
_props$overflowRatio = props.overflowRatio,
overflowRatio = _props$overflowRatio === void 0 ? 1.5 : _props$overflowRatio,
readOnly = props.readOnly,
disabled = props.disabled,
showSearch = props.showSearch,
_props$showSelectAll = props.showSelectAll,
showSelectAll = _props$showSelectAll === void 0 ? true : _props$showSelectAll,
_props$titles = props.titles,
titles = _props$titles === void 0 ? defaultTitles : _props$titles,
showRefresh = props.showRefresh,
refreshProp = props.refresh,
onRefreshChangeProp = props.onRefreshChange,
_props$refreshText = props.refreshText,
refreshText = _props$refreshText === void 0 ? "刷新选项" : _props$refreshText,
_props$refreshIcon = props.refreshIcon,
refreshIcon = _props$refreshIcon === void 0 ? /*#__PURE__*/_jsx(IconRefresh, {
color: "#eb2f96"
}) : _props$refreshIcon,
keepFeedbackSpace = props.keepFeedbackSpace;
/** value是右侧的值(可能存在不显示的不在列表里的值) */
var _useControllableValue = useControllableValue(props, {
defaultValue: []
}),
_useControllableValue2 = _slicedToArray(_useControllableValue, 2),
value = _useControllableValue2[0],
setValue = _useControllableValue2[1];
var _useControllableValue3 = useControllableValue(props, {
trigger: "onRefreshChange",
valuePropName: "refresh"
}),
_useControllableValue4 = _slicedToArray(_useControllableValue3, 2),
refresh = _useControllableValue4[0],
onRefreshChange = _useControllableValue4[1];
var _useSafeState = useSafeState([]),
_useSafeState2 = _slicedToArray(_useSafeState, 2),
checked = _useSafeState2[0],
setChecked = _useSafeState2[1];
var _useSafeState3 = useSafeState(false),
_useSafeState4 = _slicedToArray(_useSafeState3, 2),
loading = _useSafeState4[0],
setLoading = _useSafeState4[1];
var _useSafeState5 = useSafeState([]),
_useSafeState6 = _slicedToArray(_useSafeState5, 2),
optionsValues = _useSafeState6[0],
setOptionsValues = _useSafeState6[1];
var options = useFetchOptions(optionsProp, {
onLoading: setLoading,
callback: function callback(items) {
return setOptionsValues(items.map(function (ele) {
return ele.value;
}));
},
deps: refresh
});
var doRefresh = useMemoizedFn(function () {
onRefreshChange(refresh + 1);
});
var postValue = useMemoizedFn(function (values) {
var v = Array.isArray(values) ? _toConsumableArray(values) : [];
if (!v.length) {
return [];
}
var list = optionsValues || [];
if (!keepExtraItems) {
v = v.filter(function (item) {
return isInArray(item, list);
});
}
return v;
});
var handleToggle = useMemoizedFn(function (value) {
var currentIndex = checked.findIndex(function (ele) {
return isEqual(value, ele);
});
var newChecked = _toConsumableArray(checked);
if (currentIndex === -1) {
newChecked.push(value);
} else {
newChecked.splice(currentIndex, 1);
}
setChecked(newChecked);
});
var handleToggleAll = useMemoizedFn(function (items) {
var newChecked = [];
var enabledItems = items.filter(function (item) {
var _options$find;
return !((_options$find = options.find(function (ele) {
return isEqual(item, ele.value);
})) !== null && _options$find !== void 0 && _options$find.disabled);
});
if (intersection(checked, enabledItems).length === enabledItems.length) {
newChecked = not(checked, enabledItems);
} else {
newChecked = union(checked, enabledItems);
}
newChecked = intersection(newChecked, optionsValues);
setChecked(newChecked);
});
var left = useCreation(function () {
return not(optionsValues, value);
}, [optionsValues, value]);
var leftChecked = intersection(checked, left);
var rightChecked = intersection(checked, value);
var onClickToLeft = useMemoizedFn(function () {
if (readOnly || disabled) return;
var newValue = postValue(not(value, rightChecked));
setValue(newValue);
setChecked(not(checked, rightChecked));
});
var onClickToRight = useMemoizedFn(function () {
if (readOnly || disabled) return;
var newValue = postValue(union(value, leftChecked));
setValue(newValue);
setChecked(not(checked, leftChecked));
});
var _useOverflow = useOverflow(overflowThreshold, overflowRatio),
overflow = _useOverflow.overflow,
containerRef = _useOverflow.containerRef,
contentRef = _useOverflow.contentRef,
containerWidth = _useOverflow.containerWidth;
var cardSx = useCreation(function () {
var sx = _objectSpread({}, cardSxProp || {});
if (width) sx.width = width;
if (minWidth) sx.minWidth = minWidth;
if (maxWidth || containerWidth) {
sx.maxWidth = maxWidth || containerWidth;
}
return sx;
}, [cardSxProp, width, minWidth, maxWidth, containerWidth]);
var listSx = useCreation(function () {
var sx = _objectSpread({}, listSxProp || {});
if (height) sx.height = height;
if (minHeight) sx.minHeight = minHeight;
if (maxHeight) sx.maxHeight = maxHeight;
return sx;
}, [listSxProp, height, minHeight, maxHeight]);
var dom = /*#__PURE__*/_jsxs(Box, _objectSpread(_objectSpread({
ref: containerRef,
display: "flex",
gap: "4px",
alignItems: "center",
justifyContent: "center",
flexDirection: overflow ? "column" : "row"
}, containerBoxProps || {}), {}, {
children: [loading ? /*#__PURE__*/_jsx(Skeleton, {
variant: "rectangular",
width: (_ref = width !== null && width !== void 0 ? width : minWidth) !== null && _ref !== void 0 ? _ref : maxWidth,
height: (_ref2 = height !== null && height !== void 0 ? height : minHeight) !== null && _ref2 !== void 0 ? _ref2 : maxHeight,
animation: "wave"
}) : /*#__PURE__*/_jsx(ListCard, {
ref: contentRef,
error: error,
readOnly: readOnly,
disabled: disabled,
items: left,
options: options,
handleToggle: handleToggle,
handleToggleAll: handleToggleAll,
checked: checked,
setChecked: setChecked,
showSearch: showSearch,
showSelectAll: showSelectAll,
title: Array.isArray(titles) ? titles[0] : titles,
listSx: listSx,
cardSx: cardSx,
cardHeaderSx: cardHeaderSx,
searchProps: searchProps,
listItemTextProps: listItemTextProps,
itemCheckboxProps: itemCheckboxProps
}), /*#__PURE__*/_jsxs(Box, {
display: "flex",
flexDirection: "column",
sx: {
transform: overflow ? "rotate(90deg)" : undefined
},
children: [/*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
color: "primary"
}, iconButtonProps || {}), {}, {
onClick: onClickToRight,
disabled: disabled || readOnly || !(leftChecked !== null && leftChecked !== void 0 && leftChecked.length),
children: /*#__PURE__*/_jsx(IconArrowBigRight, {})
})), /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
color: "primary",
tabIndex: -1
}, iconButtonProps || {}), {}, {
onClick: onClickToLeft,
disabled: disabled || readOnly || !(rightChecked !== null && rightChecked !== void 0 && rightChecked.length),
children: /*#__PURE__*/_jsx(IconArrowBigLeft, {})
})), showRefresh && !readOnly && !disabled && /*#__PURE__*/_jsx(Tooltip, {
arrow: true,
placement: "top",
title: refreshText,
children: /*#__PURE__*/_jsx(IconButton, {
onClick: doRefresh,
className: "refresh-icon-i",
tabIndex: -1,
children: refreshIcon
})
})]
}), loading ? /*#__PURE__*/_jsx(Skeleton, {
variant: "rectangular",
width: width,
height: (_ref3 = height !== null && height !== void 0 ? height : minHeight) !== null && _ref3 !== void 0 ? _ref3 : maxHeight,
animation: "wave"
}) : /*#__PURE__*/_jsx(ListCard, {
error: error,
readOnly: readOnly,
disabled: disabled,
items: intersection(value, optionsValues),
options: options,
handleToggle: handleToggle,
handleToggleAll: handleToggleAll,
checked: checked,
setChecked: setChecked,
showSearch: showSearch,
showSelectAll: showSelectAll,
title: Array.isArray(titles) ? titles[1] : titles,
listSx: listSx,
cardSx: cardSx,
cardHeaderSx: cardHeaderSx,
searchProps: searchProps,
listItemTextProps: listItemTextProps,
itemCheckboxProps: itemCheckboxProps
})]
}));
return withFormItem ? /*#__PURE__*/_jsx(FormItemBase, {
keepFeedbackSpace: keepFeedbackSpace,
className: formItemCls,
style: formItemStyle,
prefixCls: formItemPrefixCls,
extra: extra,
extraCls: extraCls,
error: error,
noLabel: noLabel,
keepTopSpace: keepTopSpace,
label: label,
labelStyle: labelStyle,
labelPosition: labelPosition,
labelWidth: labelWidth,
labelAlign: labelAlign,
labelWrap: labelWrap,
wrapperAlign: wrapperAlign,
wrapperWrap: wrapperWrap,
wrapperWidth: wrapperWidth,
wrapperStyle: wrapperStyle,
fullWidth: fullWidth,
colon: colon,
required: required,
tooltip: tooltip,
tooltipIcon: tooltipIcon,
tooltipLayout: tooltipLayout,
showFeedback: showFeedback,
feedbackLayout: feedbackLayout,
feedbackCls: feedbackCls,
feedbackIcon: feedbackIcon,
feedbackStatus: feedbackStatus,
feedbackText: feedbackText,
addonBefore: addonBefore,
addonAfter: addonAfter,
children: dom
}) : dom;
};