@iimm/formily-mui
Version:
form field components based on @mui/material and @formily/react
237 lines • 11.5 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 { useRef } from "react";
import { useControllableValue, useMemoizedFn, useSafeState, useKeyPress } from "ahooks";
import { Box, Chip, Divider, IconButton, InputBase, Paper, Stack } from "@mui/material";
import ClearIcon from "@mui/icons-material/Clear";
import TelegramIcon from "@mui/icons-material/Telegram";
import { FormItemBase } from "../../layout";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var defaultAddIcon = /*#__PURE__*/_jsx(TelegramIcon, {});
export var KeyWordsBase = function KeyWordsBase(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,
valueProp = props.value,
onChangeProp = props.onChange,
defaultValue = props.defaultValue,
noField = props.noField,
noFormLayout = props.noFormLayout,
withFormItem = props.withFormItem,
readOnly = props.readOnly,
disabled = props.disabled,
_props$showClear = props.showClear,
showClear = _props$showClear === void 0 ? true : _props$showClear,
_props$autoClear = props.autoClear,
autoClear = _props$autoClear === void 0 ? true : _props$autoClear,
textConvert = props.textConvert,
chipsBoxProps = props.chipsBoxProps,
containerStackProps = props.containerStackProps,
InputBaseProps = props.InputBaseProps,
_props$AddIcon = props.AddIcon,
AddIcon = _props$AddIcon === void 0 ? defaultAddIcon : _props$AddIcon,
AddButtonProps = props.AddButtonProps,
chipProps = props.chipProps,
renderChip = props.renderChip,
InputBasePaperProps = props.InputBasePaperProps,
keepFeedbackSpace = props.keepFeedbackSpace;
var ref = useRef();
var _useSafeState = useSafeState(""),
_useSafeState2 = _slicedToArray(_useSafeState, 2),
text = _useSafeState2[0],
setText = _useSafeState2[1];
var _useControllableValue = useControllableValue(props, {
defaultValue: []
}),
_useControllableValue2 = _slicedToArray(_useControllableValue, 2),
value = _useControllableValue2[0],
onChange = _useControllableValue2[1];
var onTextChange = useMemoizedFn(function (e) {
var v = (e.target.value || "").trim();
setText(v);
});
var handleAddKeyWord = useMemoizedFn(function () {
var txt = typeof textConvert === "function" ? textConvert(text) : text;
if (!txt) {
return;
}
if (!(value !== null && value !== void 0 && value.includes(txt))) {
var newValue = _toConsumableArray(value || []);
newValue.push(txt);
onChange(newValue);
}
if (autoClear) {
setText("");
}
});
var handleRemoveKeyWord = useMemoizedFn(function (kw) {
var index = (value || []).findIndex(function (ele) {
return ele === kw;
});
if (index !== -1) {
var newValue = _toConsumableArray(value);
newValue.splice(index, 1);
onChange(newValue);
}
});
useKeyPress(["enter"], function () {
return handleAddKeyWord();
}, {
events: ["keyup"],
target: ref
});
var dom = /*#__PURE__*/_jsxs(Stack, _objectSpread(_objectSpread({
spacing: 1
}, containerStackProps || {}), {}, {
direction: "column",
children: [!readOnly && !disabled ? /*#__PURE__*/_jsxs(Paper, _objectSpread(_objectSpread({}, InputBasePaperProps || {}), {}, {
sx: _objectSpread({
p: "0px 4px",
display: "flex",
alignItems: "center",
borderRadius: "5px",
boxShadow: "0px 0px 1px 0px grey"
}, (InputBasePaperProps === null || InputBasePaperProps === void 0 ? void 0 : InputBasePaperProps.sx) || {}),
children: [/*#__PURE__*/_jsx(InputBase, _objectSpread(_objectSpread({
sx: {
ml: 1,
flex: 1
},
placeholder: "\u8F93\u5165\u5173\u952E\u5B57\u5E76\u6DFB\u52A0"
}, InputBaseProps || {}), {}, {
value: text,
onChange: onTextChange,
ref: ref
})), showClear && !!text ? /*#__PURE__*/_jsx(IconButton, {
size: "small",
sx: {
color: "InactiveCaptionText"
},
onClick: function onClick() {
return setText("");
},
children: /*#__PURE__*/_jsx(ClearIcon, {})
}) : null, /*#__PURE__*/_jsx(Divider, {
orientation: "vertical",
sx: {
height: 28
}
}), /*#__PURE__*/_jsx(IconButton, _objectSpread(_objectSpread({
color: "primary",
disabled: !text || disabled
}, AddButtonProps || {}), {}, {
onClick: handleAddKeyWord,
children: AddIcon
}))]
})) : null, /*#__PURE__*/_jsx(Box, _objectSpread(_objectSpread({
display: "flex",
flexWrap: "wrap",
width: "100%",
alignItems: "center",
gap: 0.5
}, chipsBoxProps || {}), {}, {
children: (value || []).map(function (ele, index) {
if (typeof renderChip === "function") {
var Node = renderChip({
item: ele,
index: index,
items: value,
disabled: disabled || readOnly,
handleRemoveKeyWord: handleRemoveKeyWord
});
return Node || null;
}
return /*#__PURE__*/_jsx(Chip, _objectSpread(_objectSpread({
label: ele,
variant: "outlined",
disabled: disabled,
color: error ? "error" : index % 2 ? "secondary" : "info"
}, chipProps || {}), {}, {
onDelete: readOnly || disabled ? undefined : function () {
return handleRemoveKeyWord(ele);
}
}), ele);
})
}))]
}));
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;
};