UNPKG

drip-table

Version:

A tiny and powerful enterprise-class solution for building tables.

429 lines (374 loc) 14.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep")); var _react = _interopRequireDefault(require("react")); var _select = _interopRequireDefault(require("../../react-components/select")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var DTCSelect = /*#__PURE__*/function (_React$PureComponent) { _inherits(DTCSelect, _React$PureComponent); var _super = _createSuper(DTCSelect); function DTCSelect(props) { var _this; _classCallCheck(this, DTCSelect); _this = _super.call(this, props); _this.state = {}; return _this; } _createClass(DTCSelect, [{ key: "disabled", get: function get() { var options = this.props.schema.options; if (typeof options.disabled === 'string') { return !!this.props.safeEvaluate("return ".concat(options.disabled), { props: { value: this.props.value, record: this.props.record, ext: this.props.ext }, rec: this.props.record }, false); } return !!options.disabled; } }, { key: "options", get: function get() { var _this2 = this; var options = this.props.schema.options; if (options.options) { return options.options.map(function (item) { return _objectSpread(_objectSpread({}, item), {}, { disabled: _this2.finalizeOptionDisabled(item.disabled, item.value) }); }); } if (options.url) { return this.state.options || []; } return []; } }, { key: "value", get: function get() { return this.props.value; // 类型不明啊 } }, { key: "finalizeOptionDisabled", value: function finalizeOptionDisabled(disabled, value) { if (typeof disabled === 'string') { return !!this.props.safeEvaluate("return ".concat(disabled), { props: { value: value, record: this.props.record, recordIndex: this.props.recordIndex, ext: this.props.ext }, value: value, rec: this.props.record }, false); } return !!disabled; } }, { key: "finalizeRequestBody", value: function finalizeRequestBody(body) { if (typeof body === 'string') { var finalBodyString = void 0; try { finalBodyString = this.props.evaluate("return ".concat(body), { props: { record: this.props.record, recordIndex: this.props.recordIndex, ext: this.props.ext }, rec: this.props.record }); if (typeof finalBodyString !== 'string') { return void 0; } } catch (_unused) {} return finalBodyString; } if (_typeof(body) === 'object') { return JSON.stringify(body); } return body; } }, { key: "finalizeOptionsResponse", value: function finalizeOptionsResponse(response) { var _options$response, _options$response2; var options = this.props.schema.options; if ((_options$response = options.response) !== null && _options$response !== void 0 && _options$response.dataIndex) { if (typeof options.response.dataIndex === 'string' || typeof options.response.dataIndex === 'number') { return response[options.response.dataIndex]; } if (Array.isArray(options.response.dataIndex)) { var finalOptions = (0, _cloneDeep.default)(response); options.response.dataIndex.forEach(function (key) { finalOptions = finalOptions[key]; }); return finalOptions; } return response; } if ((_options$response2 = options.response) !== null && _options$response2 !== void 0 && _options$response2.mapper) { var _options$response3; return this.props.safeEvaluate("return ".concat(((_options$response3 = options.response) === null || _options$response3 === void 0 ? void 0 : _options$response3.mapper) || 'response'), { props: { response: response, ext: this.props.ext }, response: response }, response); } return response; } }, { key: "componentDidMount", value: function componentDidMount() { var _this3 = this; var options = this.props.schema.options; if (options.url && !options.options) { this.setState({ loading: true }); fetch(this.props.finalizeString('pattern', options.url, this.props.record, this.props.recordIndex, this.props.ext), options.request ? { method: options.request.method, headers: options.request.headers, credentials: options.request.credentials, body: this.finalizeRequestBody(options.request.body) } : void 0).then(function (res) { return res.json(); }).then(function (json) { _this3.setState({ loading: false, options: _this3.finalizeOptionsResponse(json) }); // 类型一塌糊涂,不懂 return json; }).catch(function (error) { _this3.setState({ loading: false }); console.error(error); }); } } }, { key: "render", value: function render() { var _this4 = this; var options = this.props.schema.options; return /*#__PURE__*/_react.default.createElement(_select.default, { style: options.style, allowClear: options.allowClear, mode: options.mode, bordered: options.bordered, placeholder: options.placeholder, placement: options.placement, showSearch: options.showSearch, showArrow: options.showArrow, disabled: this.disabled, loading: this.state.loading, defaultValue: options.defaultValue, value: options.bindValue === false ? void 0 : this.value, getPopupContainer: function getPopupContainer(triggerNode) { return triggerNode; }, open: this.props.preview ? false : void 0, onChange: function onChange(value) { if (_this4.props.preview) { return; } if (options.event) { _this4.props.fireEvent({ type: 'drip-select-change', payload: { name: options.event, value: value } }); } } }, this.options.map(function (option, index) { return /*#__PURE__*/_react.default.createElement(_select.default.Option, { key: index, disabled: option.disabled, value: option.value }, option.label); })); } }]); return DTCSelect; }(_react.default.PureComponent); exports.default = DTCSelect; _defineProperty(DTCSelect, "componentName", 'select'); _defineProperty(DTCSelect, "schema", { type: 'object', properties: { allowClear: { type: 'boolean' }, bordered: { type: 'boolean' }, disabled: { anyOf: [{ type: 'string' }, { type: 'boolean' }] }, event: { type: 'string' }, mode: { enum: ['multiple', 'tags'] }, options: { type: 'array', items: { type: 'object', properties: { label: { type: 'string' }, value: { anyOf: [{ type: 'string' }, { type: 'number' }] }, disabled: { anyOf: [{ type: 'string' }, { type: 'boolean' }] } }, required: ['label', 'value'] } }, placeholder: { type: 'string' }, placement: { enum: ['bottomLeft', 'bottomRight', 'topLeft', 'topRight'] }, showArrow: { type: 'boolean' }, showSearch: { type: 'boolean' }, bindValue: { type: 'boolean' }, size: { enum: ['large', 'middle', 'small'] }, style: { type: 'object', patternProperties: { '^.*$': { anyOf: [{ type: 'string' }, { type: 'number' }] } } }, defaultValue: { anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'array', items: { anyOf: [{ type: 'string' }, { type: 'number' }] } }] }, url: { type: 'string' }, request: { type: 'object', properties: { method: { enum: ['GET', 'POST'] }, headers: { type: 'object', patternProperties: { '^.*$': { type: 'string' } } }, body: { anyOf: [{ type: 'string' }, { type: 'object', patternProperties: { '^.*$': {} } }] }, credentials: { enum: ['include', 'same-origin', 'omit'] } } }, response: { type: 'object', properties: { dataIndex: { anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'array', items: { anyOf: [{ type: 'string' }, { type: 'number' }] } }] }, mapper: { type: 'string' } } } } });