Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/renderless/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ export const validate =
}

if (typeof callback === 'function' && ++count === state.fields.length) {
callback(valid, invalidFields, invalidFieldArr)
// 排序
const sortField = sortFields(state.fields, invalidFields)
const sortFieldArr = sortFields(state.fields, invalidFieldArr)
callback(valid, sortField, sortFieldArr)
}
})
})
Expand All @@ -190,6 +193,23 @@ export const validate =
}
}

const sortFields = (fileds, val) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name fileds seems to be a typo. It should be fields to maintain consistency and avoid confusion.

const arrField = fileds.map((item) => item.prop)
if (Object.prototype.toString.call(val) === '[object Object]') {
const keys = Object.keys(val)
const sortKeys = keys.sort((a, b) => arrField.indexOf(a) - arrField.indexOf(b))
const sortedObject = sortKeys.reduce((acc, key) => {
acc[key] = val[key]
return acc
}, {})
return sortedObject
}
if (Array.isArray(val)) {
val.sort((x, y) => arrField.indexOf(x) - arrField.indexOf(y))
return val
}
}

export const validateField =
(state: IFormRenderlessParams['state']) =>
(props, cb): void => {
Expand Down