UNPKG

@iimm/formily-mui

Version:

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

83 lines 5.65 kB
var _excluded = ["onSubmit", "onSubmitFailed", "onSubmitSuccess", "resetOnSuccess", "enterKeySubmit", "loading", "onClick", "children", "keyPressEvents", "keyPressTarget", "keyPressExactMatch"]; 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 _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 _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 { useCreation, useMemoizedFn, useKeyPress } from "ahooks"; import { useParentForm, observer } from "@formily/react"; import { LoadingButton } from "mui-component"; import { jsx as _jsx } from "react/jsx-runtime"; var defaultEvents = { onSubmitSuccess: function onSubmitSuccess() {}, onSubmitFailed: function onSubmitFailed(error) { console.log("submitFailed", error); } }; export var Submit = observer(function (props) { var onSubmit = props.onSubmit, _props$onSubmitFailed = props.onSubmitFailed, onSubmitFailed = _props$onSubmitFailed === void 0 ? defaultEvents.onSubmitFailed : _props$onSubmitFailed, _props$onSubmitSucces = props.onSubmitSuccess, onSubmitSuccess = _props$onSubmitSucces === void 0 ? defaultEvents.onSubmitSuccess : _props$onSubmitSucces, resetOnSuccess = props.resetOnSuccess, enterKeySubmit = props.enterKeySubmit, loading = props.loading, onClick = props.onClick, _props$children = props.children, children = _props$children === void 0 ? "提交" : _props$children, keyPressEvents = props.keyPressEvents, keyPressTarget = props.keyPressTarget, keyPressExactMatch = props.keyPressExactMatch, restProps = _objectWithoutProperties(props, _excluded); var form = useParentForm(); var submit = useMemoizedFn(function (e) { if (!form) return; if (onClick) { if ((onClick === null || onClick === void 0 ? void 0 : onClick(e)) === false) return; } if (onSubmit) { form.submit(onSubmit).then(function (res) { onSubmitSuccess === null || onSubmitSuccess === void 0 || onSubmitSuccess(res); if (!!resetOnSuccess && res !== false) { form === null || form === void 0 || form.reset("*"); } }).catch(onSubmitFailed); } }); var autoSubmit = useMemoizedFn(function () { if (!enterKeySubmit) { return; } submit(); }); var options = useCreation(function () { if (!keyPressEvents && !keyPressTarget && typeof keyPressExactMatch !== "boolean") return undefined; var Op = {}; if (keyPressEvents) Op.events = keyPressEvents; if (typeof keyPressExactMatch === "boolean") Op.exactMatch = keyPressExactMatch; if (["number", "string"].includes(_typeof(keyPressTarget))) { Op.target = function () { return document.getElementById(keyPressTarget); }; } else if (["object", "function"].includes(_typeof(keyPressTarget))) { Op.target = keyPressTarget; } return Op; }, [keyPressEvents, keyPressTarget, keyPressExactMatch]); useKeyPress("enter", function () { return autoSubmit(); }, options); return /*#__PURE__*/_jsx(LoadingButton, _objectSpread(_objectSpread({ variant: "contained" }, restProps), {}, { loading: loading !== null && loading !== void 0 ? loading : form === null || form === void 0 ? void 0 : form.submitting, onClick: submit, children: children })); }, { displayName: "iimm.Mui.Formily.Submit" });