@handie/squirtle
Version:
Widgets for Handie-React
76 lines (75 loc) • 3.78 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { isBoolean, isFunction, capitalize, getControl, normalizeClassName, } from 'handie-react';
import { FormViewStructuralWidget } from 'handie-react/dist/widgets/class';
import defaultBehaviors from './behavior';
import style from './style.scss';
export default class FormViewWidget extends FormViewStructuralWidget {
isActionBarOutside() {
if (isBoolean(this.config.actionBarOutside)) {
return this.config.actionBarOutside;
}
return isBoolean(this.getBehavior('actionBarOutside'))
? this.getBehavior('actionBarOutside')
: this.getCommonBehavior('view.objectViewActionBarOutside', false);
}
resolveActionBarAlignment() {
if (this.config.actionBarAlignment) {
return this.config.actionBarAlignment;
}
return (this.getBehavior('actionBarAlignment') ||
this.getCommonBehavior('view.objectViewActionBarAlignment', 'left'));
}
initFormViewContextEvents() {
if (this.config.readonly === true) {
return;
}
const newOne = this.isNewOne();
const { gotoListView, listViewRouteName = `${this.$$module.getModuleName()}List`, successMessage = `${newOne ? '添加' : '修改'}成功`, } = this.config;
const callback = gotoListView !== false
? () => this.$$history.push({ name: listViewRouteName, params: this.$$route.params })
: () => this.$$app.success(isFunction(successMessage)
? successMessage(this.state.value)
: successMessage);
this.on('submit', () => {
this.$$view.setBusy(true);
if (newOne) {
this.$$view
.insert(this.state.value, callback, message => this.$$app.error(message))
.finally(() => this.$$view.setBusy(false));
}
else {
this.$$view
.update(this.state.value, callback, message => this.$$app.error(message))
.finally(() => this.$$view.setBusy(false));
}
});
}
render() {
const outside = this.isActionBarOutside();
const readonly = this.config.readonly === true;
const actionBar = this.renderActionBar({
className: style['FormViewWidget-actionBar'],
executors: {
[this.getCommonBehavior('action.cancelAction')]: () => this.$$history.push({
name: this.config.listViewRouteName || `${this.$$module.getModuleName()}List`,
params: this.$$route.params,
}),
},
readonly,
});
const Wait = getControl('Wait');
return Wait ? (_jsx(Wait, Object.assign({ className: normalizeClassName(style.FormViewWidget, style[`FormViewWidget--actionBar${capitalize(this.resolveActionBarAlignment())}`], { [style['FormViewWidget--actionBarOutside']]: outside }, this.config.className), nativeStyle: this.getStyle(), busy: this.state.loading }, { children: outside ? (_jsxs(_Fragment, { children: [_jsx("div", Object.assign({ className: style['FormViewWidget-formContainer'] }, { children: this.renderForm({ className: style['FormViewWidget-form'], readonly }) }), void 0), actionBar] }, void 0)) : (this.renderForm({
className: style['FormViewWidget-form'],
readonly,
children: actionBar,
})) }), void 0)) : null;
}
componentWillMount() {
super.componentWillMount();
this.setBehaviors('view.form', defaultBehaviors);
this.initFormViewContextEvents();
}
componentDidMount() {
this.fetchData();
}
}