UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

157 lines (156 loc) 7.01 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import classnames from 'classnames'; import PropTypes from 'prop-types'; import { isNil, assign } from 'lodash'; import { QuestionCircleOutlined } from '@ant-design/icons'; import { Tooltip, Form } from 'antd'; import { observer } from 'mobx-react'; import { formatString } from '../../../../services/language'; import BasicComponent from '../../../Base/BasicComponent'; import bind from '../../../../utils/bind'; import * as style from './style.mless'; /** * Form Item 封装 */ var Item = /** @class */ (function (_super) { __extends(Item, _super); function Item() { return _super !== null && _super.apply(this, arguments) || this; } Item.prototype.handleStyleServiceFieldChange = function (e) { var styleService = this.context.styleService; var field = this.props.field; if (!styleService || !field) { return; } var isEvent = !isNil(e) && e.nativeEvent instanceof Event; var value = isEvent ? e.target.value : e; styleService.set(field, value); }; Item.prototype.calcExtraProps = function (field) { var _a = this.context, mobxForm = _a.mobxForm, styleService = _a.styleService; if (mobxForm) { // 根据mobxForm和field的值更新 rootProps 和 子元素 var formField = mobxForm.$(field); var rootProps = { label: formField.label, // Error字符串为mobxReactForm自动设置,在此情况下不显示help help: formField.error === 'Error' ? undefined : formField.error, validateStatus: formField.error ? 'error' : undefined }; var itemProps = formField.bind(); rootProps.label = formatString(rootProps.label); rootProps.help = formatString(rootProps.help); return { rootProps: rootProps, itemProps: itemProps }; } if (styleService) { var itemProps = { value: styleService[field], onChange: this.handleStyleServiceFieldChange }; return { itemProps: itemProps }; } return {}; }; Item.prototype.render = function () { var _a; var _b = this.props, className = _b.className, bindIndex = _b.bindIndex, field = _b.field, mask = _b.mask, hide = _b.hide, explain = _b.explain, children = _b.children, getPopupContainer = _b.getPopupContainer, otherProps = __rest(_b, ["className", "bindIndex", "field", "mask", "hide", "explain", "children", "getPopupContainer"]); // root组件的props var rootProps = { className: classnames(style.item, className, (_a = {}, _a[style.hide] = hide, _a)) }; var bindItemProps = null; if (field && isNil(mask)) { // 根据context计算额外属性 var extraProps = this.calcExtraProps(field); assign(rootProps, extraProps.rootProps); bindItemProps = extraProps.itemProps; } var content = bindItemProps ? React.Children.map(children, function (child, index) { if (index === bindIndex) { var props = assign({}, bindItemProps, child.props); props['placeholder'] = formatString(props['placeholder']); return React.createElement(child.type, props); } return child; }) : children; // 合并otherProps,同时保证otherProps拥有最高优先级 assign(rootProps, otherProps); rootProps.label = formatString(rootProps.label); rootProps.help = formatString(rootProps.help); if (explain) { rootProps.label = (React.createElement("span", null, formatString(rootProps.label), React.createElement(Tooltip, { title: formatString(explain), getPopupContainer: getPopupContainer || this.getRootDOM }, React.createElement(QuestionCircleOutlined, { className: style.explainIcon })))); } return React.createElement(Form.Item, __assign({}, rootProps), isNil(mask) ? content : mask); }; Item.defaultProps = { bindIndex: 0, hide: false }; Item.contextTypes = { mobxForm: PropTypes.object, styleService: PropTypes.object }; __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], Item.prototype, "handleStyleServiceFieldChange", null); Item = __decorate([ observer ], Item); return Item; }(BasicComponent)); export default Item;