react-antd-admin-panel
Version:
Easy prototyping admin panel using React and Antd
85 lines • 4.91 kB
JavaScript
import React, { useEffect, useState } from "react";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Button as ButtonAnt } from "antd";
const Button = (props) => {
var _a, _b, _c;
const model = props.model;
const [state, setState] = useState({
loading: false,
disabled: (_a = model._disabled) !== null && _a !== void 0 ? _a : false, type: model._primary ? 'primary' : model._link ? 'link' : undefined,
style: model._style
});
let addProps = {};
if (model._shape)
addProps['shape'] = model._shape;
if (model._danger)
addProps['danger'] = model._danger;
if (model._block)
addProps['block'] = model._block;
const onClick = () => {
var _a, _b, _c;
if (model._loadable)
setState(Object.assign(Object.assign({}, state), { loading: true }));
if (!((_a = model._action) === null || _a === void 0 ? void 0 : _a._onError)) {
model._action.onError(() => setState(Object.assign(Object.assign({}, state), { loading: false })));
}
if (!((_b = model._action) === null || _b === void 0 ? void 0 : _b._onComplete)) {
model._action.onComplete(() => setState(Object.assign(Object.assign({}, state), { loading: false })));
}
(_c = model._action) === null || _c === void 0 ? void 0 : _c.click();
};
// Remove onClear from onComplete. Form reset should probably not be here.
const onClear = () => {
var _a, _b;
(_a = props.form) === null || _a === void 0 ? void 0 : _a.resetFields();
(_b = model._formula) === null || _b === void 0 ? void 0 : _b.valuesOnReset();
};
const Empty = () => {
return (React.createElement(React.Fragment, null));
};
model.setLoading = (v) => setState(Object.assign(Object.assign({}, state), { loading: v }));
model.tsxSetLoading = (v) => setState(Object.assign(Object.assign({}, state), { loading: v }));
model.tsxSetDisabled = (v) => setState(Object.assign(Object.assign({}, state), { disabled: v }));
model.tsxSetType = (v) => setState(Object.assign(Object.assign({}, state), { type: v }));
model.tsxSetStyle = (v) => setState(Object.assign(Object.assign({}, state), { style: v }));
const Button = () => {
var _a;
let access = props.main.$access(model._access);
let hide = access.hidden;
const Icon = (model, marginRight = 8) => {
var _a, _b;
if ((_a = model._action) === null || _a === void 0 ? void 0 : _a._fontawesome)
return (React.createElement("span", { style: { width: 20 } },
React.createElement(FontAwesomeIcon, { style: { opacity: .6, marginRight: marginRight }, icon: model._action._fontawesome })));
if (model._fontawesome)
return (React.createElement("span", { style: { width: 20 } },
React.createElement(FontAwesomeIcon, { style: { opacity: .6, marginRight: marginRight }, icon: model._fontawesome })));
if ((_b = model._action) === null || _b === void 0 ? void 0 : _b._icon)
return React.createElement(model._action._icon, null);
if (model._icon)
return React.createElement(model._icon, null);
return React.createElement(Empty, null);
};
let Inner = () => { var _a; return React.createElement(React.Fragment, null, (_a = model._action) === null || _a === void 0 ? void 0 : _a._label); };
if (!!model._component) {
Inner = model._component;
addProps['args'] = model._componentArgs;
}
return hide ? null : React.createElement(ButtonAnt, Object.assign({ type: state.type, onClick: () => onClick(), icon: Icon(model), size: (_a = model._sizeString) !== null && _a !== void 0 ? _a : 'large', loading: state.loading, disabled: state.disabled || !access.access, style: state.style }, addProps),
React.createElement(Inner, Object.assign({}, addProps)));
};
const Clear = () => {
var _a;
return (React.createElement(React.Fragment, null,
React.createElement(ButtonAnt, { style: { marginRight: 12 }, onClick: () => onClear(), size: (_a = model._sizeString) !== null && _a !== void 0 ? _a : 'large' }, "Nulstil"),
React.createElement(Button, null)));
};
useEffect(() => {
/** Debug purposes */
if (model._trigger)
setTimeout(() => onClick(), 250);
}, []);
return (((_b = model._action) === null || _b === void 0 ? void 0 : _b._type) === 'submit' && !((_c = model._action) === null || _c === void 0 ? void 0 : _c._hideClear)) ? React.createElement(Clear, null) : React.createElement(Button, null);
};
export default Button;
//# sourceMappingURL=Button.js.map