Skip to content

Commit 88769c8

Browse files
feat(ui): extracts relationship input for external use (#12339)
1 parent bd6ee31 commit 88769c8

File tree

15 files changed

+1197
-849
lines changed

15 files changed

+1197
-849
lines changed

packages/create-payload-app/src/lib/manage-env-files.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const updateEnvExampleVariables = (
2222

2323
const [key] = line.split('=')
2424

25-
if (!key) {return}
25+
if (!key) {
26+
return
27+
}
2628

2729
if (key === 'DATABASE_URI' || key === 'POSTGRES_URL' || key === 'MONGODB_URI') {
2830
const dbChoice = databaseType ? dbChoiceRecord[databaseType] : null

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ export type PolymorphicRelationshipField = {
12081208

12091209
export type PolymorphicRelationshipFieldClient = {
12101210
admin?: {
1211-
sortOptions?: Pick<PolymorphicRelationshipField['admin'], 'sortOptions'>
1211+
sortOptions?: PolymorphicRelationshipField['admin']['sortOptions']
12121212
} & RelationshipAdminClient
12131213
} & Pick<PolymorphicRelationshipField, 'relationTo'> &
12141214
SharedRelationshipPropertiesClient

packages/ui/src/elements/AddNewRelation/index.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { ClientCollectionConfig } from 'payload'
44
import { getTranslation } from '@payloadcms/translations'
55
import React, { Fragment, useCallback, useEffect, useState } from 'react'
66

7-
import type { Value } from '../../fields/Relationship/types.js'
87
import type { DocumentDrawerContextType } from '../DocumentDrawer/Provider.js'
98
import type { Props } from './types.js'
109

@@ -24,9 +23,9 @@ const baseClass = 'relationship-add-new'
2423
export const AddNewRelation: React.FC<Props> = ({
2524
Button: ButtonFromProps,
2625
hasMany,
26+
onChange,
2727
path,
2828
relationTo,
29-
setValue,
3029
unstyled,
3130
value,
3231
}) => {
@@ -54,18 +53,15 @@ export const AddNewRelation: React.FC<Props> = ({
5453
const onSave: DocumentDrawerContextType['onSave'] = useCallback(
5554
({ doc, operation }) => {
5655
if (operation === 'create') {
57-
const newValue: Value = Array.isArray(relationTo)
58-
? {
59-
relationTo: collectionConfig?.slug,
60-
value: doc.id,
61-
}
62-
: doc.id
63-
6456
// ensure the value is not already in the array
65-
const isNewValue =
66-
Array.isArray(relationTo) && Array.isArray(value)
67-
? !value.some((v) => v && typeof v === 'object' && v.value === doc.id)
68-
: value !== doc.id
57+
let isNewValue = false
58+
if (!value) {
59+
isNewValue = true
60+
} else {
61+
isNewValue = Array.isArray(value)
62+
? !value.some((v) => v && v.value === doc.id)
63+
: value.value !== doc.id
64+
}
6965

7066
if (isNewValue) {
7167
// dispatchOptions({
@@ -79,17 +75,26 @@ export const AddNewRelation: React.FC<Props> = ({
7975
// sort: true,
8076
// })
8177

82-
if (hasMany) {
83-
setValue([...(Array.isArray(value) ? value : []), newValue])
78+
if (hasMany === true) {
79+
onChange([
80+
...(Array.isArray(value) ? value : []),
81+
{
82+
relationTo: collectionConfig?.slug,
83+
value: doc.id,
84+
},
85+
])
8486
} else {
85-
setValue(newValue)
87+
onChange({
88+
relationTo: relatedCollections[0].slug,
89+
value: doc.id,
90+
})
8691
}
8792
}
8893

8994
setSelectedCollection(undefined)
9095
}
9196
},
92-
[relationTo, collectionConfig, hasMany, setValue, value],
97+
[collectionConfig, hasMany, onChange, value, relatedCollections],
9398
)
9499

95100
const onPopupToggle = useCallback((state) => {
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import type { Value } from '../../fields/Relationship/types.js'
1+
import type { ValueWithRelation } from 'payload'
22

33
export type Props = {
44
readonly Button?: React.ReactNode
5-
readonly hasMany: boolean
65
readonly path: string
76
readonly relationTo: string | string[]
8-
readonly setValue: (value: unknown) => void
97
readonly unstyled?: boolean
10-
readonly value: Value | Value[]
11-
}
8+
} & SharedRelationshipInputProps
9+
10+
type SharedRelationshipInputProps =
11+
| {
12+
readonly hasMany: false
13+
readonly onChange: (value: ValueWithRelation, modifyForm?: boolean) => void
14+
readonly value?: null | ValueWithRelation
15+
}
16+
| {
17+
readonly hasMany: true
18+
readonly onChange: (value: ValueWithRelation[]) => void
19+
readonly value?: null | ValueWithRelation[]
20+
}

packages/ui/src/exports/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export { NumberField } from '../../fields/Number/index.js'
176176
export { PasswordField } from '../../fields/Password/index.js'
177177
export { PointField } from '../../fields/Point/index.js'
178178
export { RadioGroupField } from '../../fields/RadioGroup/index.js'
179-
export { RelationshipField } from '../../fields/Relationship/index.js'
179+
export { RelationshipField, RelationshipInput } from '../../fields/Relationship/index.js'
180180
export { RichTextField } from '../../fields/RichText/index.js'
181181
export { RowField } from '../../fields/Row/index.js'
182182
export { SelectField, SelectInput } from '../../fields/Select/index.js'

0 commit comments

Comments
 (0)