drip-table
Version:
A tiny and powerful enterprise-class solution for building tables.
532 lines (470 loc) • 18.8 kB
JavaScript
"use strict";
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _ajv = require("../../../utils/ajv");
var _dom = require("../../../utils/dom");
var _hooks = require("../hooks");
var _button = _interopRequireDefault(require("../../react-components/button"));
var _tooltip = _interopRequireDefault(require("../../react-components/tooltip"));
var _utils = require("../utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 DTCButton = /*#__PURE__*/function (_React$PureComponent) {
_inherits(DTCButton, _React$PureComponent);
var _super = _createSuper(DTCButton);
function DTCButton() {
var _this;
_classCallCheck(this, DTCButton);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
showPopconfirm: false
});
return _this;
}
_createClass(DTCButton, [{
key: "configured",
get: function get() {
var options = this.props.schema.options;
if (options.mode === 'multiple') {
if (options.buttons) {
return true;
}
return false;
}
return true;
}
}, {
key: "label",
get: function get() {
var options = this.props.schema.options;
return this.props.finalizeString('pattern', options.label || '', this.props.record, this.props.recordIndex, this.props.ext);
}
}, {
key: "getIcon",
value: function getIcon(iconName) {
var Icons = this.props.icons;
var Icon = Icons === null || Icons === void 0 ? void 0 : Icons[iconName];
if (Icon) {
return /*#__PURE__*/_react.default.createElement(Icon, null);
}
return null;
}
}, {
key: "getVisible",
value: function getVisible(visibleFunc) {
var _this$props = this.props,
schema = _this$props.schema,
record = _this$props.record;
var dataIndex = schema.dataIndex;
if (!visibleFunc) {
return true;
}
return !!(0, _utils.dataProcessValue)(this.props.evaluate, record, dataIndex, visibleFunc);
}
}, {
key: "getDisabled",
value: function getDisabled(disableFunc) {
var _this$props2 = this.props,
schema = _this$props2.schema,
record = _this$props2.record;
var dataIndex = schema.dataIndex;
if (!disableFunc) {
var _this$props$disable;
return (_this$props$disable = this.props.disable) !== null && _this$props$disable !== void 0 ? _this$props$disable : false;
}
return !!(0, _utils.dataProcessValue)(this.props.evaluate, record, dataIndex, disableFunc);
}
}, {
key: "parseReactCSS",
value: function parseReactCSS(style) {
var _this$props3 = this.props,
record = _this$props3.record,
recordIndex = _this$props3.recordIndex,
ext = _this$props3.ext;
var styleObject = typeof style === 'string' ? this.props.safeEvaluate(style, {
props: {
record: record,
recordIndex: recordIndex,
ext: ext
}
}) : style;
return (0, _dom.parseReactCSS)(styleObject);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var options = this.props.schema.options;
if (!this.configured) {
return /*#__PURE__*/_react.default.createElement("div", {
style: {
color: 'red'
}
}, "\u5C5E\u6027\u914D\u7F6E\u9519\u8BEF");
}
if (options.mode === 'single') {
if (!this.getVisible(options.visibleFunc)) {
return null;
}
var wrapperEl = /*#__PURE__*/_react.default.createElement(_hooks.DripTableComponentContext.Consumer, null, function (context) {
return /*#__PURE__*/_react.default.createElement(_button.default, {
style: _this2.parseReactCSS(options.style),
type: options.buttonType,
size: options.size,
shape: options.shape,
danger: options.danger,
ghost: options.ghost,
disabled: _this2.getDisabled(options.disableFunc),
icon: options.icon ? _this2.getIcon(options.icon) : void 0,
iconPosition: options.iconPosition,
onClick: function onClick() {
if (options.popconfirm) {
_this2.setState({
showPopconfirm: true
});
return;
}
if (_this2.props.preview) {
return;
}
if (options.event) {
_this2.props.fireEvent({
type: 'drip-button-click',
payload: options.event
});
}
if (options.closePopover) {
context.setState({
closePopover: options.closePopover
});
}
}
}, _this2.label);
});
if (options.popconfirm) {
var _this$props$icons, _this$props$icons2;
var popconfirm = options.popconfirm;
var TitleIcon = popconfirm.titleIcon ? (_this$props$icons = this.props.icons) === null || _this$props$icons === void 0 ? void 0 : _this$props$icons[popconfirm.titleIcon] : null;
var ContentIcon = popconfirm.contentIcon ? (_this$props$icons2 = this.props.icons) === null || _this$props$icons2 === void 0 ? void 0 : _this$props$icons2[popconfirm.contentIcon] : null;
return /*#__PURE__*/_react.default.createElement(_hooks.DripTableComponentContext.Consumer, null, function (context) {
return /*#__PURE__*/_react.default.createElement(_tooltip.default, {
overlayStyle: _objectSpread(_objectSpread({}, _this2.parseReactCSS(popconfirm.overlayStyle)), (0, _dom.parseThemeCSS)(context.info.schema.theme)),
overlayInnerStyle: _this2.parseReactCSS(popconfirm.overlayInnerStyle),
title: /*#__PURE__*/_react.default.createElement("div", {
style: {
fontSize: '14px',
fontWeight: '600',
lineHeight: '22px'
}
}, TitleIcon ? /*#__PURE__*/_react.default.createElement(TitleIcon, {
style: _this2.parseReactCSS(popconfirm.titleIconStyle)
}) : null, _this2.props.finalizeString('pattern', popconfirm.title, _this2.props.record, _this2.props.recordIndex, _this2.props.ext)),
overlay: /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
style: {
fontSize: '14px',
fontWeight: '400',
lineHeight: '22px',
marginTop: '4px'
}
}, ContentIcon ? /*#__PURE__*/_react.default.createElement(ContentIcon, {
style: _this2.parseReactCSS(popconfirm.contentIconStyle)
}) : null, _this2.props.finalizeString('pattern', popconfirm.content, _this2.props.record, _this2.props.recordIndex, _this2.props.ext)), /*#__PURE__*/_react.default.createElement("div", {
style: {
display: 'flex',
justifyContent: 'flex-end',
marginTop: '8px'
}
}, popconfirm.cancelText ? /*#__PURE__*/_react.default.createElement(_button.default, {
style: _this2.parseReactCSS(popconfirm.cancelStyle),
size: "small",
onClick: function onClick() {
_this2.setState({
showPopconfirm: false
});
}
}, popconfirm.cancelText) : null, /*#__PURE__*/_react.default.createElement(_button.default, {
style: _objectSpread({
marginLeft: '10px'
}, _this2.parseReactCSS(popconfirm.confirmStyle)),
type: "primary",
size: "small",
shape: options.shape,
danger: options.danger,
ghost: options.ghost,
onClick: function onClick() {
if (options.event) {
_this2.props.fireEvent({
type: 'drip-button-click',
payload: options.event
});
}
_this2.setState({
showPopconfirm: false
});
}
}, popconfirm.confirmText || 'Yes'))),
visible: _this2.state.showPopconfirm,
onVisibleChange: function onVisibleChange(visible) {
_this2.setState({
showPopconfirm: visible
});
},
trigger: "click",
placement: popconfirm.placement
}, wrapperEl);
});
}
return wrapperEl;
}
if (options.mode === 'multiple') {
var _options$buttons;
return (_options$buttons = options.buttons) === null || _options$buttons === void 0 ? void 0 : _options$buttons.map(function (config, index) {
return _this2.getVisible(config.visibleFunc) && /*#__PURE__*/_react.default.createElement(_button.default, {
key: index,
style: {
marginLeft: options.margin,
marginRight: options.margin
},
type: config.buttonType,
size: config.size,
shape: config.shape,
danger: config.danger,
disabled: _this2.getDisabled(config.disableFunc),
ghost: config.ghost,
icon: config.icon ? _this2.getIcon(config.icon) : void 0,
onClick: function onClick() {
if (_this2.props.preview) {
return;
}
if (config.event) {
_this2.props.fireEvent({
type: 'drip-button-click',
payload: config.event
});
}
}
}, _this2.props.finalizeString('pattern', config.label || '', _this2.props.record, _this2.props.recordIndex, _this2.props.ext));
});
}
return null;
}
}]);
return DTCButton;
}(_react.default.PureComponent);
exports.default = DTCButton;
_defineProperty(DTCButton, "componentName", 'button');
_defineProperty(DTCButton, "schema", {
type: 'object',
properties: {
style: {
anyOf: [{
type: 'string'
}, {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
}]
},
mode: {
enum: ['single', 'multiple']
},
label: {
type: 'string'
},
buttonType: {
enum: ['primary', 'dashed', 'text', 'link']
},
shape: {
enum: ['circle', 'round']
},
size: {
enum: ['large', 'middle', 'small']
},
danger: {
type: 'boolean'
},
ghost: {
type: 'boolean'
},
icon: {
type: 'string'
},
iconPosition: {
enum: ['left', 'right']
},
event: {
type: 'string'
},
closePopover: {
type: 'string'
},
margin: {
type: 'number'
},
popconfirm: {
type: 'object',
properties: {
overlayStyle: {
anyOf: [{
type: 'string'
}, {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
}]
},
overlayInnerStyle: {
anyOf: [{
type: 'string'
}, {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
}]
},
title: {
type: 'string'
},
titleIcon: {
type: 'string'
},
titleIconStyle: _ajv.DRIP_TABLE_GENERIC_CSS_SCHEMA,
content: {
type: 'string'
},
contentIcon: {
type: 'string'
},
contentIconStyle: _ajv.DRIP_TABLE_GENERIC_CSS_SCHEMA,
placement: {
type: 'string'
},
cancelText: {
type: 'string'
},
cancelStyle: {
anyOf: [{
type: 'string'
}, {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
}]
},
confirmText: {
type: 'string'
},
confirmStyle: {
anyOf: [{
type: 'string'
}, {
type: 'object',
patternProperties: {
'^.*$': {
anyOf: [{
type: 'string'
}, {
type: 'number'
}]
}
}
}]
}
}
},
disableFunc: {
type: 'string'
},
visibleFunc: {
type: 'string'
},
buttons: {
type: 'array',
items: {
type: 'object',
properties: {
label: {
type: 'string'
},
event: {
type: 'string'
},
closePopover: {
type: 'string'
},
buttonType: {
enum: ['primary', 'dashed', 'text', 'link']
},
shape: {
enum: ['circle', 'round']
},
size: {
enum: ['large', 'middle', 'small']
},
danger: {
type: 'boolean'
},
ghost: {
type: 'boolean'
},
icon: {
type: 'string'
},
disableFunc: {
type: 'string'
},
visibleFunc: {
type: 'string'
}
}
}
}
}
});