@iimm/formily-mui
Version:
form field components based on @mui/material and @formily/react
244 lines • 13.9 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); }
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", "children", "noField", "noFormLayout", "withFormItem", "readOnly", "disabled", "minCount", "maxCount", "layout", "itemSx", "labelPlacement", "row", "icon", "checkedIcon", "size", "color", "showRefresh", "refresh", "onRefreshChange", "refreshText", "refreshIcon", "keepFeedbackSpace"];
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; }
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 { useControllableValue, useMemoizedFn, useSafeState } from "ahooks";
import { Checkbox, FormControlLabel, FormGroup, IconButton, Skeleton, Tooltip } from "@mui/material";
import { Refresh } from "@mui/icons-material";
import { isEqual, isInArray } from "@iimm/shared";
import { useFetchOptions } from "../../hooks";
import { FormItemBase } from "../../layout/FormItem/FormItemBase";
import "../../styles/refresh.scss";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export var CheckboxGroupBase = function CheckboxGroupBase(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,
children = props.children,
noField = props.noField,
noFormLayout = props.noFormLayout,
_props$withFormItem = props.withFormItem,
withFormItem = _props$withFormItem === void 0 ? true : _props$withFormItem,
readOnly = props.readOnly,
disabled = props.disabled,
minCount = props.minCount,
maxCount = props.maxCount,
_props$layout = props.layout,
layout = _props$layout === void 0 ? "horizontal" : _props$layout,
itemSx = props.itemSx,
labelPlacement = props.labelPlacement,
row = props.row,
icon = props.icon,
checkedIcon = props.checkedIcon,
_props$size = props.size,
size = _props$size === void 0 ? "small" : _props$size,
color = props.color,
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 _useSafeState3 = useSafeState([]),
_useSafeState4 = _slicedToArray(_useSafeState3, 2),
optionsValues = _useSafeState4[0],
setOptionsValues = _useSafeState4[1];
var _useControllableValue3 = useControllableValue(props),
_useControllableValue4 = _slicedToArray(_useControllableValue3, 2),
value = _useControllableValue4[0],
onChange = _useControllableValue4[1];
var options = useFetchOptions(optionsProp, {
onLoading: setLoading,
callback: function callback(opts) {
return setOptionsValues(opts.map(function (ele) {
return ele.value;
}));
},
deps: refresh
});
var doRefresh = useMemoizedFn(function () {
onRefreshChange((+refresh || 0) + 1);
});
var handleChange = useMemoizedFn(function (e, optionValue) {
if (readOnly) {
return;
}
var checked = e.target.checked;
var newValue = _toConsumableArray(value || []);
var optionIndex = newValue.findIndex(function (ele) {
return isEqual(ele, optionValue);
});
if (optionIndex === -1) {
if (checked) {
newValue.push(optionValue);
}
} else if (!checked) {
newValue.splice(optionIndex, 1);
}
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;
});
newValue = newValue.filter(function (item) {
return isInArray(item, optionsValues);
});
var shouldUpdate = true;
if (minCount !== undefined && newValue.length < minCount || maxCount !== undefined && newValue.length > maxCount) {
shouldUpdate = false;
}
if (shouldUpdate) {
onChange(newValue);
}
});
var dom = loading ? /*#__PURE__*/_jsx(Skeleton, {
variant: "rectangular",
animation: "wave",
width: "100%",
children: /*#__PURE__*/_jsx(Checkbox, {})
}) : /*#__PURE__*/_jsxs(FormGroup, _objectSpread(_objectSpread({
row: row !== null && row !== void 0 ? row : layout === "horizontal"
}, restProps), {}, {
children: [options.map(function (item, index) {
var _item$label, _item$size, _ref, _item$disabled, _item$icon, _item$checkedIcon, _item$sx;
return /*#__PURE__*/_jsx(FormControlLabel, {
label: (_item$label = item.label) !== null && _item$label !== void 0 ? _item$label : "",
labelPlacement: labelPlacement,
control: /*#__PURE__*/_jsx(Checkbox, {
size: (_item$size = item.size) !== null && _item$size !== void 0 ? _item$size : size,
color: (_ref = item.color) !== null && _ref !== void 0 ? _ref : color,
disabled: (_item$disabled = item.disabled) !== null && _item$disabled !== void 0 ? _item$disabled : disabled,
checked: isInArray(item.value, value),
onChange: function onChange(e) {
return handleChange(e, item.value);
},
icon: (_item$icon = item.icon) !== null && _item$icon !== void 0 ? _item$icon : icon,
checkedIcon: (_item$checkedIcon = item.checkedIcon) !== null && _item$checkedIcon !== void 0 ? _item$checkedIcon : checkedIcon,
required: item.required,
sx: (_item$sx = item.sx) !== null && _item$sx !== void 0 ? _item$sx : itemSx
})
}, index);
}), showRefresh && !readOnly && !disabled && /*#__PURE__*/_jsx(FormControlLabel, {
className: "refresh-icon-i",
label: refreshText,
control: /*#__PURE__*/_jsx(Tooltip, {
arrow: true,
placement: "top",
title: refreshText,
children: /*#__PURE__*/_jsx(IconButton, {
size: size,
onClick: doRefresh,
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;
};