Skip to content

Commit 989072d

Browse files
authored
fix(form): [form] modifying form validation sorting issues in asynchronous situations (#2982)
* fix: modifying form validation sorting issues in asynchronous situations * fix: revise inspection comments * fix: revise inspection comments
1 parent a210f5a commit 989072d

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

packages/renderless/src/form/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ export const validate =
180180
}
181181

182182
if (typeof callback === 'function' && ++count === state.fields.length) {
183-
callback(valid, invalidFields, invalidFieldArr)
183+
// 排序
184+
const sortField = sortFields(state.fields, invalidFields)
185+
const sortFieldArr = sortFields(state.fields, invalidFieldArr)
186+
callback(valid, sortField, sortFieldArr)
184187
}
185188
})
186189
})
@@ -190,6 +193,23 @@ export const validate =
190193
}
191194
}
192195

196+
const sortFields = (fileds, val) => {
197+
const arrField = fileds.map((item) => item.prop)
198+
if (Object.prototype.toString.call(val) === '[object Object]') {
199+
const keys = Object.keys(val)
200+
const sortKeys = keys.sort((a, b) => arrField.indexOf(a) - arrField.indexOf(b))
201+
const sortedObject = sortKeys.reduce((acc, key) => {
202+
acc[key] = val[key]
203+
return acc
204+
}, {})
205+
return sortedObject
206+
}
207+
if (Array.isArray(val)) {
208+
val.sort((x, y) => arrField.indexOf(x) - arrField.indexOf(y))
209+
return val
210+
}
211+
}
212+
193213
export const validateField =
194214
(state: IFormRenderlessParams['state']) =>
195215
(props, cb): void => {

0 commit comments

Comments
 (0)