perf(ui): remove unnecessary deepCopy in reduceToSerializableFields (#10667)
This commit is contained in:
@@ -1,35 +1,32 @@
|
||||
import { type FormField, type FormState } from 'payload'
|
||||
import { deepCopyObjectComplex } from 'payload/shared'
|
||||
|
||||
type BlacklistedKeys = 'customComponents' | 'validate'
|
||||
const blacklistedKeys: BlacklistedKeys[] = ['validate', 'customComponents']
|
||||
|
||||
const sanitizeField = (incomingField: FormField): FormField => {
|
||||
const field = deepCopyObjectComplex(incomingField)
|
||||
const field = { ...incomingField } // shallow copy, as we only need to remove top-level keys
|
||||
|
||||
blacklistedKeys.forEach((key) => {
|
||||
for (const key of blacklistedKeys) {
|
||||
delete field[key]
|
||||
})
|
||||
}
|
||||
|
||||
return field
|
||||
}
|
||||
|
||||
/*
|
||||
Takes in FormState and removes fields that are not serializable.
|
||||
Returns FormState without blacklisted keys.
|
||||
**/
|
||||
/**
|
||||
* Takes in FormState and removes fields that are not serializable.
|
||||
* Returns FormState without blacklisted keys.
|
||||
*/
|
||||
export const reduceToSerializableFields = (
|
||||
fields: FormState,
|
||||
): {
|
||||
[key: string]: Omit<FormField, BlacklistedKeys>
|
||||
} => {
|
||||
return Object.keys(fields).reduce(
|
||||
(acc, key) => {
|
||||
acc[key] = sanitizeField(fields[key])
|
||||
return acc
|
||||
},
|
||||
{} as {
|
||||
[key: string]: Omit<FormField, BlacklistedKeys>
|
||||
},
|
||||
)
|
||||
const result: Record<string, Omit<FormField, BlacklistedKeys>> = {}
|
||||
|
||||
for (const key in fields) {
|
||||
result[key] = sanitizeField(fields[key])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user