plot-plan-designer
Version:
Design Editor Tools with React.js + ant.design + fabric.js
203 lines (202 loc) • 9.23 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const prop_types_1 = __importDefault(require("prop-types"));
const antd_1 = require("antd");
const i18next_1 = __importDefault(require("i18next"));
const Icon_1 = __importDefault(require("../icon/Icon"));
const flex_1 = require("../flex");
let EditTable = /** @class */ (() => {
class EditTable extends react_1.Component {
constructor() {
super(...arguments);
this.state = {
userProperty: this.props.userProperty,
tempKey: '',
originKey: '',
tempValue: '',
visible: false,
current: 'add',
validateStatus: '',
help: '',
};
this.getDataSource = (userProperty) => {
return Object.keys(userProperty).map((key) => {
return {
key,
value: userProperty[key],
};
});
};
this.handlers = {
onOk: () => {
const { onChange } = this.props;
const { tempKey, originKey, tempValue, current, validateStatus } = this.state;
if (validateStatus === 'error') {
return;
}
if (current === 'modify') {
delete this.state.userProperty[originKey];
}
const userProperty = Object.assign({}, this.state.userProperty, { [tempKey]: tempValue });
if (onChange) {
onChange(userProperty);
}
this.setState({
visible: false,
userProperty,
});
},
onCancel: () => {
this.modalHandlers.onHide();
},
onChangeKey: (value) => {
let validateStatus = 'success';
let help = '';
if ((this.state.current === 'add' && Object.keys(this.state.userProperty).some((p) => p === value)) ||
(this.state.current === 'modify' && this.state.originKey !== value && this.state.userProperty[value])) {
validateStatus = 'error';
help = i18next_1.default.t('validation.already-property', { arg: i18next_1.default.t('common.key') });
}
else if (!value.length) {
validateStatus = 'error';
help = i18next_1.default.t('validation.enter-property', { arg: i18next_1.default.t('common.key') });
}
else {
validateStatus = 'success';
help = '';
}
this.setState({
tempKey: value,
validateStatus,
help,
});
},
};
this.modalHandlers = {
onShow: () => {
this.setState({
visible: true,
});
},
onHide: () => {
this.setState({
visible: false,
});
},
};
this.handleAdd = () => {
this.setState({
visible: true,
tempKey: '',
tempValue: '',
current: 'add',
validateStatus: '',
help: '',
});
};
this.handleEdit = (key) => {
this.setState({
visible: true,
tempKey: key,
originKey: key,
tempValue: this.state.userProperty[key],
current: 'modify',
validateStatus: '',
help: '',
});
};
this.handleDelete = (key) => {
delete this.state.userProperty[key];
this.setState({ userProperty: this.state.userProperty });
};
this.handleClear = () => {
this.setState({ userProperty: {} });
};
}
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({
userProperty: nextProps.userProperty || {},
});
}
render() {
const { userProperty, tempKey, tempValue, visible, validateStatus, help } = this.state;
const { onOk, onCancel, onChangeKey } = this.handlers;
const columns = [
{
title: i18next_1.default.t('common.key'),
dataIndex: 'key',
},
{
title: i18next_1.default.t('common.value'),
dataIndex: 'value',
},
{
title: '',
dataIndex: 'action',
render: (text, record) => {
return (react_1.default.createElement("div", null,
react_1.default.createElement(antd_1.Button, { className: "rde-action-btn", shape: "circle", onClick: () => {
this.handleEdit(record.key);
} },
react_1.default.createElement(Icon_1.default, { name: "edit" })),
react_1.default.createElement(antd_1.Button, { className: "rde-action-btn", shape: "circle", onClick: () => {
this.handleDelete(record.key);
} },
react_1.default.createElement(Icon_1.default, { name: "times" }))));
},
},
];
return (react_1.default.createElement(flex_1.Flex, { flexDirection: "column" },
react_1.default.createElement(flex_1.Flex, { justifyContent: "flex-end" },
react_1.default.createElement(antd_1.Button, { className: "rde-action-btn", shape: "circle", onClick: this.handleAdd },
react_1.default.createElement(Icon_1.default, { name: "plus" })),
react_1.default.createElement(antd_1.Button, { className: "rde-action-btn", shape: "circle", onClick: this.handleClear },
react_1.default.createElement(Icon_1.default, { name: "times" }))),
react_1.default.createElement(antd_1.Table, { size: "small", pagination: {
pageSize: 5,
}, columns: columns, dataSource: this.getDataSource(userProperty) }),
react_1.default.createElement(antd_1.Modal, { onCancel: onCancel, onOk: onOk, visible: visible },
react_1.default.createElement(antd_1.Form.Item, { required: true, label: i18next_1.default.t('common.key'), colon: false, hasFeedback: true, validateStatus: validateStatus, help: help },
react_1.default.createElement(antd_1.Input, { defaultValue: tempKey, value: tempKey, onChange: (e) => {
onChangeKey(e.target.value);
} })),
react_1.default.createElement(antd_1.Form.Item, { label: i18next_1.default.t('common.value'), colon: false },
react_1.default.createElement(antd_1.Input, { defaultValue: tempValue, value: tempValue, onChange: (e) => {
this.setState({ tempValue: e.target.value });
} })))));
}
}
EditTable.propTypes = {
userProperty: prop_types_1.default.object,
form: prop_types_1.default.any,
onChange: prop_types_1.default.func,
};
EditTable.defaultProps = {
userProperty: {},
};
return EditTable;
})();
exports.default = EditTable;