UNPKG

@iimm/formily-mui

Version:

form field components based on @mui/material and @formily/react

255 lines 14.3 kB
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); } var _excluded = ["labelPosition", "labelWidth", "labelAlign", "labelWrap", "wrapperAlign", "wrapperWrap", "wrapperWidth", "fullWidth", "colon", "tooltipIcon", "tooltipLayout", "showFeedback", "feedbackLayout", "noLabel", "label", "labelStyle", "wrapperStyle", "tooltip", "required", "feedbackStatus", "feedbackText", "feedbackIcon", "extra", "addonBefore", "addonAfter", "formItemCls", "formItemStyle", "formItemPrefixCls", "error", "feedbackCls", "extraCls", "keepTopSpace", "options", "value", "onChange", "defaultValue", "noField", "noFormLayout", "withFormItem", "minCount", "maxCount", "exclusive", "orientation", "layout", "size", "color", "disabled", "readOnly", "itemSx", "itemWidth", "itemMinWidth", "itemMaxWidth", "itemFullWidth", "showRefresh", "refresh", "onRefreshChange", "refreshText", "refreshIcon", "keepFeedbackSpace"]; 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 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 _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; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { useSafeState, useMemoizedFn, useControllableValue, useCreation } from "ahooks"; import { Checkbox, Skeleton, ToggleButtonGroup as MuiToggleButtonGroup, ToggleButton as MuiToggleButton, Tooltip, IconButton } from "@mui/material"; import { Refresh } from "@mui/icons-material"; import { isEqual, isInArray } from "@iimm/shared"; import { FormItemBase } from "../../layout"; import { COLORS } from "../../utils"; import { useFetchOptions } from "../../hooks"; import "../../styles/refresh.scss"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var defaultItemSx = { fontWeight: "bold" }; export var ToggleButtonGroupBase = function ToggleButtonGroupBase(props) { 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, optionsProp = props.options, valueProp = props.value, onChangeProp = props.onChange, defaultValue = props.defaultValue, noField = props.noField, noFormLayout = props.noFormLayout, withFormItem = props.withFormItem, minCount = props.minCount, maxCount = props.maxCount, exclusive = props.exclusive, orientation = props.orientation, layout = props.layout, _props$size = props.size, size = _props$size === void 0 ? "small" : _props$size, _props$color = props.color, color = _props$color === void 0 ? "primary" : _props$color, disabled = props.disabled, readOnly = props.readOnly, _props$itemSx = props.itemSx, itemSxProp = _props$itemSx === void 0 ? defaultItemSx : _props$itemSx, itemWidth = props.itemWidth, _props$itemMinWidth = props.itemMinWidth, itemMinWidth = _props$itemMinWidth === void 0 ? 50 : _props$itemMinWidth, itemMaxWidth = props.itemMaxWidth, itemFullWidth = props.itemFullWidth, 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(Refresh, { sx: { color: "#eb2f96" } }) : _props$refreshIcon, keepFeedbackSpace = props.keepFeedbackSpace, restProps = _objectWithoutProperties(props, _excluded); var _useControllableValue = useControllableValue(props, { trigger: "onRefreshChange", valuePropName: "refresh", defaultValue: 0 }), _useControllableValue2 = _slicedToArray(_useControllableValue, 2), refresh = _useControllableValue2[0], onRefreshChange = _useControllableValue2[1]; var _useSafeState = useSafeState(false), _useSafeState2 = _slicedToArray(_useSafeState, 2), loading = _useSafeState2[0], setLoading = _useSafeState2[1]; var _useControllableValue3 = useControllableValue(props), _useControllableValue4 = _slicedToArray(_useControllableValue3, 2), value = _useControllableValue4[0], onChange = _useControllableValue4[1]; var _useSafeState3 = useSafeState([]), _useSafeState4 = _slicedToArray(_useSafeState3, 2), optionsValues = _useSafeState4[0], setOptionsValues = _useSafeState4[1]; var doRefresh = useMemoizedFn(function () { onRefreshChange((+refresh || 0) + 1); }); var options = useFetchOptions(optionsProp, { onLoading: setLoading, callback: function callback(opts) { return setOptionsValues(opts.map(function (ele) { return ele.value; })); }, deps: refresh }); var itemSx = useCreation(function () { if (!itemWidth && !itemMaxWidth && !itemMinWidth) return itemSxProp; var sx = _objectSpread({}, itemSxProp || {}); if (!itemFullWidth && itemWidth) { sx.width = itemWidth; } if (itemMinWidth) sx.minWidth = itemMinWidth; if (itemMaxWidth) sx.maxWidth = itemMaxWidth; return sx; }, [itemSxProp, itemWidth, itemFullWidth, itemMinWidth, itemMaxWidth]); var handleChange = useMemoizedFn(function (v) { if (readOnly) { return; } if (exclusive) { if (!(minCount === 1 && v !== undefined)) { onChange(v); } } else { var newValue = _toConsumableArray(value || []); if (isInArray(v, newValue)) { var index = newValue.findIndex(function (ele) { return isEqual(v, ele); }); newValue.splice(index, 1); } else { newValue.push(v); } if (!(minCount !== undefined && newValue.length < minCount || maxCount !== undefined && newValue.length > maxCount)) { newValue.sort(function (a, b) { var indexA = options.findIndex(function (opt) { return isEqual(a, opt.value); }); var indexB = options.findIndex(function (opt) { return isEqual(b, opt.value); }); return indexA - indexB; }); onChange(newValue.filter(function (item) { return isInArray(item, optionsValues); })); } } }); var dom = loading ? /*#__PURE__*/_jsx(Skeleton, { variant: "rectangular", animation: "wave", width: "100%", children: /*#__PURE__*/_jsx(Checkbox, {}) }) : /*#__PURE__*/_jsxs(MuiToggleButtonGroup, _objectSpread(_objectSpread({ orientation: orientation !== null && orientation !== void 0 ? orientation : layout, disabled: disabled, exclusive: exclusive, fullWidth: itemFullWidth, size: size, color: color }, restProps), {}, { children: [options.map(function (item, index) { var _item$size, _item$disabled, _item$sx; return /*#__PURE__*/_jsx(MuiToggleButton, { size: (_item$size = item === null || item === void 0 ? void 0 : item.size) !== null && _item$size !== void 0 ? _item$size : size, color: item.color && COLORS.includes(item.color) ? item.color : color, disabled: (_item$disabled = item.disabled) !== null && _item$disabled !== void 0 ? _item$disabled : disabled, value: item.value, onClick: function onClick() { return handleChange(item.value); }, selected: exclusive ? isEqual(item.value, value) : isInArray(item.value, value), sx: (_item$sx = item === null || item === void 0 ? void 0 : item.sx) !== null && _item$sx !== void 0 ? _item$sx : itemSx, children: item.label }, index); }), showRefresh && !readOnly && !disabled && /*#__PURE__*/_jsx(Tooltip, { arrow: true, placement: "top", title: refreshText, children: /*#__PURE__*/_jsx(IconButton, { size: size, onClick: doRefresh, className: "refresh-icon-i", children: refreshIcon }) })] })); 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; };