Skip to content

Commit 5230bb6

Browse files
committed
fix: fix formItem validating status did not match component life circle
1 parent e2ed091 commit 5230bb6

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

packages/rcre/src/core/Form/FormItem.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ export interface FormItemProps {
3131

3232
export type RCREFormItemProps = FormItemProps & BasicProps;
3333

34-
export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
34+
export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {
35+
validating: boolean;
36+
}> {
3537
static contextType = FormContext as any;
3638
context: FormContextType;
37-
private isApiValidate: boolean;
3839
private isUnMounted?: boolean;
3940
public readonly controlElements: {
4041
[name: string]: ElementsInfo;
@@ -49,7 +50,9 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
4950
constructor(props: RCREFormItemProps) {
5051
super(props);
5152

52-
this.isApiValidate = false;
53+
this.state = {
54+
validating: false
55+
};
5356
this.isUnMounted = false;
5457
this.controlElements = {};
5558
}
@@ -192,7 +195,7 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
192195
}
193196

194197
private apiValidate = async (apiRule: ApiRule, formItemName: string, value: any): Promise<boolean> => {
195-
if (this.isApiValidate) {
198+
if (this.state.validating) {
196199
return true;
197200
}
198201

@@ -233,7 +236,9 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
233236

234237
this.props.rcreContext.dataProviderEvent.addToList(url);
235238

236-
this.isApiValidate = true;
239+
this.setState({
240+
validating: true
241+
});
237242

238243
let ret = await request(url, {
239244
url: url,
@@ -242,7 +247,9 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
242247
formSubmit: apiRule.formSubmit
243248
}, runTime.$global.proxy);
244249

245-
this.isApiValidate = false;
250+
this.setState({
251+
validating: false
252+
});
246253

247254
// 如果这个时候直接unmount,会有异常
248255
if (this.isUnMounted) {
@@ -447,7 +454,6 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
447454
try {
448455
return await this.apiValidate(apiRule, formItemName, data);
449456
} catch (err) {
450-
this.isApiValidate = false;
451457
let apiFailedMsg = apiRule!.errmsg || err.message;
452458

453459
if (isExpression(apiFailedMsg)) {
@@ -465,6 +471,10 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
465471
required: required,
466472
errorMsg: apiFailedMsg || '验证接口调用失败'
467473
});
474+
475+
this.setState({
476+
validating: false
477+
});
468478
return false;
469479
}
470480
}
@@ -486,7 +496,8 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
486496

487497
private handleChange = (name: string, data: any) => {
488498
// 输入值改变,需要重置API请求锁
489-
this.isApiValidate = false;
499+
console.log('validate');
500+
// this.isApiValidate = false;
490501
this.props.containerContext.$setData(name, data);
491502
this.validateFormItem(name, data);
492503
}
@@ -560,7 +571,7 @@ export class RCREFormItem extends React.PureComponent<RCREFormItemProps, {}> {
560571
$formItem: {
561572
valid: formItemStatus.valid,
562573
errmsg: formItemStatus.errmsg,
563-
validating: this.isApiValidate
574+
validating: this.state.validating
564575
},
565576
isUnderFormItem: true
566577
};

0 commit comments

Comments
 (0)