Skip to content

Commit 155f9f8

Browse files
authored
feat: add siblingFields arg to field hooks (#11117)
This PR adds a new `siblingFields` argument to field hooks. This allows us to dramatically simplify the `lexicalHTML` field, which previously had to use a complex `findFieldPathAndSiblingFields` function that deeply traverses the entire `CollectionConfig` just to find the sibling fields.
1 parent 2056e9b commit 155f9f8

File tree

13 files changed

+34
-65
lines changed

13 files changed

+34
-65
lines changed

docs/hooks/fields.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ The following arguments are provided to all Field Hooks:
7171
| **`schemaPath`** | The path of the [Field](../fields/overview) in the schema. |
7272
| **`siblingData`** | The data of sibling fields adjacent to the field that the Hook is running against. |
7373
| **`siblingDocWithLocales`** | The sibling data of the Document with all [Locales](../configuration/localization). |
74+
| **`siblingFields`** | The sibling fields of the field which the hook is running against.
75+
|
7476
| **`value`** | The value of the [Field](../fields/overview). |
7577

7678
<Banner type="success">

packages/payload/src/fields/config/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ export type FieldHookArgs<TData extends TypeWithID = any, TValue = any, TSibling
164164
/**
165165
* Only available in `afterRead` hooks
166166
*/
167-
currentDepth?: number /**
167+
currentDepth?: number
168+
/**
168169
* Only available in `afterRead` hooks
169170
*/
170171
/** The data passed to update the document within create and update operations, and the full document itself in the afterRead hook. */
@@ -212,6 +213,10 @@ export type FieldHookArgs<TData extends TypeWithID = any, TValue = any, TSibling
212213
* The original siblingData with locales (not modified by any hooks). Only available in `beforeChange` and `beforeDuplicate` field hooks.
213214
*/
214215
siblingDocWithLocales?: Record<string, unknown>
216+
/**
217+
* The sibling fields of the field which the hook is running against.
218+
*/
219+
siblingFields: (Field | TabAsField)[]
215220
/** The value of the field. */
216221
value?: TValue
217222
}

packages/payload/src/fields/hooks/afterChange/promise.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Args = {
3131
req: PayloadRequest
3232
siblingData: JsonObject
3333
siblingDoc: JsonObject
34+
siblingFields?: (Field | TabAsField)[]
3435
}
3536

3637
// This function is responsible for the following actions, in order:
@@ -54,6 +55,7 @@ export const promise = async ({
5455
req,
5556
siblingData,
5657
siblingDoc,
58+
siblingFields,
5759
}: Args): Promise<void> => {
5860
const { indexPath, path, schemaPath } = getFieldPaths({
5961
field,
@@ -90,6 +92,7 @@ export const promise = async ({
9092
req,
9193
schemaPath: schemaPathSegments,
9294
siblingData,
95+
siblingFields,
9396
value: siblingDoc[field.name],
9497
})
9598

packages/payload/src/fields/hooks/afterChange/traverseFields.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Args = {
2626
req: PayloadRequest
2727
siblingData: JsonObject
2828
siblingDoc: JsonObject
29+
siblingFields?: (Field | TabAsField)[]
2930
}
3031

3132
export const traverseFields = async ({
@@ -45,6 +46,7 @@ export const traverseFields = async ({
4546
req,
4647
siblingData,
4748
siblingDoc,
49+
siblingFields,
4850
}: Args): Promise<void> => {
4951
const promises = []
5052

@@ -68,6 +70,7 @@ export const traverseFields = async ({
6870
req,
6971
siblingData,
7072
siblingDoc,
73+
siblingFields,
7174
}),
7275
)
7376
})

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Args = {
5151
selectMode?: SelectMode
5252
showHiddenFields: boolean
5353
siblingDoc: JsonObject
54+
siblingFields?: (Field | TabAsField)[]
5455
triggerAccessControl?: boolean
5556
triggerHooks?: boolean
5657
}
@@ -90,6 +91,7 @@ export const promise = async ({
9091
selectMode,
9192
showHiddenFields,
9293
siblingDoc,
94+
siblingFields,
9395
triggerAccessControl = true,
9496
triggerHooks = true,
9597
}: Args): Promise<void> => {
@@ -260,6 +262,7 @@ export const promise = async ({
260262
schemaPath: schemaPathSegments,
261263
showHiddenFields,
262264
siblingData: siblingDoc,
265+
siblingFields,
263266
value,
264267
})
265268

@@ -291,6 +294,7 @@ export const promise = async ({
291294
schemaPath: schemaPathSegments,
292295
showHiddenFields,
293296
siblingData: siblingDoc,
297+
siblingFields,
294298
value: siblingDoc[field.name],
295299
})
296300

packages/payload/src/fields/hooks/afterRead/traverseFields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const traverseFields = ({
106106
selectMode,
107107
showHiddenFields,
108108
siblingDoc,
109+
siblingFields: fields,
109110
triggerAccessControl,
110111
triggerHooks,
111112
}),

packages/payload/src/fields/hooks/beforeChange/promise.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Args = {
3939
siblingData: JsonObject
4040
siblingDoc: JsonObject
4141
siblingDocWithLocales?: JsonObject
42+
siblingFields?: (Field | TabAsField)[]
4243
skipValidation: boolean
4344
}
4445

@@ -71,6 +72,7 @@ export const promise = async ({
7172
siblingData,
7273
siblingDoc,
7374
siblingDocWithLocales,
75+
siblingFields,
7476
skipValidation,
7577
}: Args): Promise<void> => {
7678
const { indexPath, path, schemaPath } = getFieldPaths({
@@ -123,6 +125,7 @@ export const promise = async ({
123125
schemaPath: schemaPathSegments,
124126
siblingData,
125127
siblingDocWithLocales,
128+
siblingFields,
126129
value: siblingData[field.name],
127130
})
128131

packages/payload/src/fields/hooks/beforeChange/traverseFields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const traverseFields = async ({
100100
siblingData,
101101
siblingDoc,
102102
siblingDocWithLocales,
103+
siblingFields: fields,
103104
skipValidation,
104105
}),
105106
)

packages/payload/src/fields/hooks/beforeDuplicate/promise.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Args<T> = {
2525
parentSchemaPath: string
2626
req: PayloadRequest
2727
siblingDoc: JsonObject
28+
siblingFields?: (Field | TabAsField)[]
2829
}
2930

3031
export const promise = async <T>({
@@ -41,6 +42,7 @@ export const promise = async <T>({
4142
parentSchemaPath,
4243
req,
4344
siblingDoc,
45+
siblingFields,
4446
}: Args<T>): Promise<void> => {
4547
const { indexPath, path, schemaPath } = getFieldPaths({
4648
field,
@@ -82,6 +84,7 @@ export const promise = async <T>({
8284
schemaPath: schemaPathSegments,
8385
siblingData: siblingDoc,
8486
siblingDocWithLocales: siblingDoc,
87+
siblingFields,
8588
value: siblingDoc[field.name]?.[locale],
8689
}
8790

@@ -116,6 +119,7 @@ export const promise = async <T>({
116119
schemaPath: schemaPathSegments,
117120
siblingData: siblingDoc,
118121
siblingDocWithLocales: siblingDoc,
122+
siblingFields,
119123
value: siblingDoc[field.name],
120124
}
121125

packages/payload/src/fields/hooks/beforeDuplicate/traverseFields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const traverseFields = async <T>({
5555
parentSchemaPath,
5656
req,
5757
siblingDoc,
58+
siblingFields: fields,
5859
}),
5960
)
6061
})

0 commit comments

Comments
 (0)