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 { type FormField, type FormState } from 'payload'
|
||||||
import { deepCopyObjectComplex } from 'payload/shared'
|
|
||||||
|
|
||||||
type BlacklistedKeys = 'customComponents' | 'validate'
|
type BlacklistedKeys = 'customComponents' | 'validate'
|
||||||
const blacklistedKeys: BlacklistedKeys[] = ['validate', 'customComponents']
|
const blacklistedKeys: BlacklistedKeys[] = ['validate', 'customComponents']
|
||||||
|
|
||||||
const sanitizeField = (incomingField: FormField): FormField => {
|
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]
|
delete field[key]
|
||||||
})
|
}
|
||||||
|
|
||||||
return field
|
return field
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Takes in FormState and removes fields that are not serializable.
|
* Takes in FormState and removes fields that are not serializable.
|
||||||
Returns FormState without blacklisted keys.
|
* Returns FormState without blacklisted keys.
|
||||||
**/
|
*/
|
||||||
export const reduceToSerializableFields = (
|
export const reduceToSerializableFields = (
|
||||||
fields: FormState,
|
fields: FormState,
|
||||||
): {
|
): {
|
||||||
[key: string]: Omit<FormField, BlacklistedKeys>
|
[key: string]: Omit<FormField, BlacklistedKeys>
|
||||||
} => {
|
} => {
|
||||||
return Object.keys(fields).reduce(
|
const result: Record<string, Omit<FormField, BlacklistedKeys>> = {}
|
||||||
(acc, key) => {
|
|
||||||
acc[key] = sanitizeField(fields[key])
|
for (const key in fields) {
|
||||||
return acc
|
result[key] = sanitizeField(fields[key])
|
||||||
},
|
}
|
||||||
{} as {
|
|
||||||
[key: string]: Omit<FormField, BlacklistedKeys>
|
return result
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user