@iimm/formily-mui
Version:
form field components based on @mui/material and @formily/react
118 lines (117 loc) • 11.4 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); }
import { useField } from "@formily/react";
import { isVoidField } from "@formily/core";
import { useFormLayout } from "../../layout/FormLayout";
import { UseFormilyFieldPropsFormFieldBaseConfig, UseFormilyFieldPropsFormItemConfig } from "../../utils";
/** 用来处理FormItem及字段项 props的hooks可以从FormLayout或Field 获取布局或label,error等信息*/
export function useFormilyFieldProps(props) {
var _props$withFormItem, _config19, _config20, _config21, _config22, _config23, _config24, _config25, _config26;
var extraConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var formatProps = _objectSpread({}, props);
var layout = useFormLayout();
var withFormItem = (_props$withFormItem = props === null || props === void 0 ? void 0 : props.withFormItem) !== null && _props$withFormItem !== void 0 ? _props$withFormItem : props !== null && props !== void 0 && props.noFormLayout ? undefined : layout === null || layout === void 0 ? void 0 : layout.withFormItem;
formatProps.withFormItem = withFormItem;
var field = useField();
if ((props !== null && props !== void 0 && props.noField || !field) && (!layout || props !== null && props !== void 0 && props.noFormLayout)) return formatProps;
var config = {};
if (withFormItem) {
config = _objectSpread(_objectSpread(_objectSpread({}, UseFormilyFieldPropsFormItemConfig), UseFormilyFieldPropsFormFieldBaseConfig), extraConfig || {});
} else {
config = _objectSpread(_objectSpread({}, UseFormilyFieldPropsFormFieldBaseConfig), extraConfig || {});
}
if (layout && !(props !== null && props !== void 0 && props.noFormLayout)) {
var _props$noField, _config, _props$fullWidth, _config2, _props$labelPosition, _config3, _props$labelAlign, _config4, _props$labelWidth, _config5, _props$labelWrap, _config6, _props$wrapperAlign, _config7, _props$wrapperWidth, _config8, _props$wrapperWrap, _config9, _props$colon, _config10, _props$tooltipIcon, _config11, _props$tooltipLayout, _config12, _props$showFeedback, _config13, _props$feedbackLayout, _config14, _props$showInnerLabel, _config15, _props$keepFeedbackSp;
formatProps.noField = (_props$noField = props.noField) !== null && _props$noField !== void 0 ? _props$noField : layout.noField;
if ((_config = config) !== null && _config !== void 0 && _config.fullWidth) formatProps.fullWidth = (_props$fullWidth = props.fullWidth) !== null && _props$fullWidth !== void 0 ? _props$fullWidth : layout === null || layout === void 0 ? void 0 : layout.fullWidth;
if ((_config2 = config) !== null && _config2 !== void 0 && _config2.labelPosition) formatProps.labelPosition = (_props$labelPosition = props.labelPosition) !== null && _props$labelPosition !== void 0 ? _props$labelPosition : layout.labelPosition;
if ((_config3 = config) !== null && _config3 !== void 0 && _config3.labelAlign) formatProps.labelAlign = (_props$labelAlign = props.labelAlign) !== null && _props$labelAlign !== void 0 ? _props$labelAlign : layout.labelAlign;
if ((_config4 = config) !== null && _config4 !== void 0 && _config4.labelWidth) formatProps.labelWidth = (_props$labelWidth = props.labelWidth) !== null && _props$labelWidth !== void 0 ? _props$labelWidth : layout.labelWidth;
if ((_config5 = config) !== null && _config5 !== void 0 && _config5.labelWrap) formatProps.labelWrap = (_props$labelWrap = props.labelWrap) !== null && _props$labelWrap !== void 0 ? _props$labelWrap : layout.labelWrap;
if ((_config6 = config) !== null && _config6 !== void 0 && _config6.wrapperAlign) formatProps.wrapperAlign = (_props$wrapperAlign = props.wrapperAlign) !== null && _props$wrapperAlign !== void 0 ? _props$wrapperAlign : layout.wrapperAlign;
if ((_config7 = config) !== null && _config7 !== void 0 && _config7.wrapperWidth) formatProps.wrapperWidth = (_props$wrapperWidth = props.wrapperWidth) !== null && _props$wrapperWidth !== void 0 ? _props$wrapperWidth : layout.wrapperWidth;
if ((_config8 = config) !== null && _config8 !== void 0 && _config8.wrapperWrap) formatProps.wrapperWrap = (_props$wrapperWrap = props.wrapperWrap) !== null && _props$wrapperWrap !== void 0 ? _props$wrapperWrap : layout.wrapperWrap;
if ((_config9 = config) !== null && _config9 !== void 0 && _config9.colon) formatProps.colon = (_props$colon = props.colon) !== null && _props$colon !== void 0 ? _props$colon : layout.colon;
if ((_config10 = config) !== null && _config10 !== void 0 && _config10.tooltipIcon) formatProps.tooltipIcon = (_props$tooltipIcon = props.tooltipIcon) !== null && _props$tooltipIcon !== void 0 ? _props$tooltipIcon : layout.tooltipIcon;
if ((_config11 = config) !== null && _config11 !== void 0 && _config11.tooltipLayout) formatProps.tooltipLayout = (_props$tooltipLayout = props.tooltipLayout) !== null && _props$tooltipLayout !== void 0 ? _props$tooltipLayout : layout.tooltipLayout;
if ((_config12 = config) !== null && _config12 !== void 0 && _config12.showFeedback) formatProps.showFeedback = (_props$showFeedback = props.showFeedback) !== null && _props$showFeedback !== void 0 ? _props$showFeedback : layout.showFeedback;
if ((_config13 = config) !== null && _config13 !== void 0 && _config13.feedbackLayout) formatProps.feedbackLayout = (_props$feedbackLayout = props.feedbackLayout) !== null && _props$feedbackLayout !== void 0 ? _props$feedbackLayout : layout.feedbackLayout;
if ((_config14 = config) !== null && _config14 !== void 0 && _config14.showInnerLabel) formatProps.showInnerLabel = (_props$showInnerLabel = props.showInnerLabel) !== null && _props$showInnerLabel !== void 0 ? _props$showInnerLabel : layout.showInnerLabel;
if ((_config15 = config) !== null && _config15 !== void 0 && _config15.keepFeedbackSpace) formatProps.keepFeedbackSpace = (_props$keepFeedbackSp = props.keepFeedbackSpace) !== null && _props$keepFeedbackSp !== void 0 ? _props$keepFeedbackSp : layout.keepFeedbackSpace;
}
if (field && !props.noField) {
var _config16, _config17, _config18;
if ((_config16 = config) !== null && _config16 !== void 0 && _config16.display) {
var _props$display;
formatProps.display = (_props$display = props.display) !== null && _props$display !== void 0 ? _props$display : field.display;
}
if ((_config17 = config) !== null && _config17 !== void 0 && _config17.label) {
var _props$label;
formatProps.label = (_props$label = props.label) !== null && _props$label !== void 0 ? _props$label : field.title;
}
if ((_config18 = config) !== null && _config18 !== void 0 && _config18.tooltip) {
var _props$tooltip;
formatProps.tooltip = (_props$tooltip = props === null || props === void 0 ? void 0 : props.tooltip) !== null && _props$tooltip !== void 0 ? _props$tooltip : field.description;
}
}
if (formatProps.noField || !field || isVoidField(field)) {
return formatProps;
}
if ((_config19 = config) !== null && _config19 !== void 0 && _config19.defaultValue) {
var _field$initialValue;
formatProps.defaultValue = (_field$initialValue = field.initialValue) !== null && _field$initialValue !== void 0 ? _field$initialValue : props.defaultValue;
}
if ((_config20 = config) !== null && _config20 !== void 0 && _config20.disabled) {
var _props$disabled;
formatProps.disabled = (_props$disabled = props.disabled) !== null && _props$disabled !== void 0 ? _props$disabled : field.disabled;
}
if ((_config21 = config) !== null && _config21 !== void 0 && _config21.readOnly) {
var _props$readOnly;
formatProps.readOnly = (_props$readOnly = props.readOnly) !== null && _props$readOnly !== void 0 ? _props$readOnly : field.readOnly;
}
if ((_config22 = config) !== null && _config22 !== void 0 && _config22.error) {
var _props$error;
formatProps.error = (_props$error = props.error) !== null && _props$error !== void 0 ? _props$error : field.selfInvalid;
}
if ((_config23 = config) !== null && _config23 !== void 0 && _config23.required) {
var takeRequired = function takeRequired() {
if (field.required && field.pattern !== "readPretty") {
return true;
}
if ("required" in props) {
return props.required;
}
return false;
};
formatProps.required = takeRequired();
}
if ((_config24 = config) !== null && _config24 !== void 0 && _config24.options) {
var _field$dataSource;
formatProps.options = (_field$dataSource = field.dataSource) !== null && _field$dataSource !== void 0 ? _field$dataSource : props.options;
}
if ((_config25 = config) !== null && _config25 !== void 0 && _config25.feedbackStatus) {
var _props$feedbackStatus;
formatProps.feedbackStatus = (_props$feedbackStatus = props.feedbackStatus) !== null && _props$feedbackStatus !== void 0 ? _props$feedbackStatus : field.validating ? "pending" : field.decoratorProps.feedbackStatus || field.validateStatus;
}
if ((_config26 = config) !== null && _config26 !== void 0 && _config26.feedbackText) {
var takeMessage = function takeMessage() {
if (field.validating) return;
if (props.feedbackText) return props.feedbackText;
var split = function split(messages) {
return messages.reduce(function (buf, text, index) {
if (!text) return buf;
return index < messages.length - 1 ? buf.concat([text, ", "]) : buf.concat([text]);
}, []);
};
if (field.selfErrors.length) return split(field.selfErrors);
if (field.selfWarnings.length) return split(field.selfWarnings);
if (field.selfSuccesses.length) return split(field.selfSuccesses);
};
formatProps.feedbackText = takeMessage();
}
return formatProps;
}