我跟vazco /制服,拉多斯拉夫·Miernik的创造者,他说,有解决它的方法有两种:
1)添加的形式,当它被点击了一个按钮,触发功能。
2)创建一个自定义表单。
由于我正在寻找一个解决方案只使用AutoForm,我注意到这是不可能的。然后,我的解决方案是创建一个我需要的自定义表单。所以解决方案是自定义表格(基于AutoForm):
import cloneDeep from 'lodash.clonedeep';
import isEqual from 'lodash.isequal';
import set from 'lodash.set';
import {PropTypes} from 'react';
import ValidatedQuickForm from 'uniforms-bootstrap3/ValidatedQuickForm';
const Auto = parent => class Custom extends parent {
static Auto = Auto;
static displayName = `Auto${parent.displayName}`;
static propTypes = {
...parent.propTypes,
onChangeModel: PropTypes.func
};
constructor() {
super(...arguments);// eslint-disable-line
this.state = {
...this.state,
model: this.props.model,
modelSync: this.props.model
};
}
componentWillReceiveProps({model}) {
super.componentWillReceiveProps(...arguments);// eslint-disable-line
if (!isEqual(this.props.model, model)) {
this.setState({model, modelSync: model});
}
}
func() {
// makes whatever i need
}
getNativeFormProps() {
const {
onChangeModel, // eslint-disable-line no-unused-vars
...props
} = super.getNativeFormProps();
return props;
}
getModel(mode) {
return mode === 'form' ? this.state.modelSync : this.state.model;
}
onChange(key, value) {
this.setState(state => ({modelSync: set(cloneDeep(state.modelSync), key, value)}),() => {
super.onChange(...arguments);// eslint-disable-line
this.setState({model: this.state.modelSync},() => {
if (this.props.onChangeModel) {
this.props.onChangeModel(this.state.model);
}
});
});
}
onReset() {
this.setState(() => {
super.onReset();
return {
model: this.props.model,
modelSync: this.props.model
};
});
}
onSubmit(event) {
if (event) {
event.preventDefault();
event.stopPropagation();
}
this.func(); // func that i wanted to call
return new Promise(resolve =>
this.setState({validate: true},() =>
resolve(this.onValidate().then(
() => super.onSubmit()))
)
);
}
onValidate() {
return this.onValidateModel(this.getChildContextModel());
}
};
export default Auto(ValidatedQuickForm);