Skip to content

feat: add the ability to link existing documents in the join field #12029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions packages/ui/src/elements/ListDrawer/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@import '../../scss/styles.scss';

@layer payload-default {
.list-drawer {
&__toggler {
background: transparent;
border: 0;
margin: 0;
padding: 0;
cursor: pointer;
color: inherit;

&:focus,
&:focus-within {
outline: none;
}

&:disabled {
pointer-events: none;
}
}
}
}
1 change: 1 addition & 0 deletions packages/ui/src/elements/ListDrawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useConfig } from '../../providers/Config/index.js'
import { useEditDepth } from '../../providers/EditDepth/index.js'
import { Drawer, DrawerToggler } from '../Drawer/index.js'
import { ListDrawerContent } from './DrawerContent.js'
import './index.scss'

export const baseClass = 'list-drawer'
export const formatListDrawerSlug = ({
Expand Down
48 changes: 40 additions & 8 deletions packages/ui/src/elements/RelationshipTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type Where,
} from 'payload'
import { hoistQueryParamsToAnd, transformColumnsToPreferences } from 'payload/shared'
import React, { Fragment, useCallback, useEffect, useState } from 'react'
import React, { Fragment, useCallback, useEffect, useMemo, useState } from 'react'

import type { DocumentDrawerProps } from '../DocumentDrawer/types.js'

Expand All @@ -27,6 +27,7 @@ import { useTranslation } from '../../providers/Translation/index.js'
import { AnimateHeight } from '../AnimateHeight/index.js'
import { ColumnSelector } from '../ColumnSelector/index.js'
import { useDocumentDrawer } from '../DocumentDrawer/index.js'
import { type ListDrawerProps, useListDrawer } from '../ListDrawer/index.js'
import { Popup, PopupList } from '../Popup/index.js'
import { RelationshipProvider } from '../Table/RelationshipProvider/index.js'
import { DrawerLink } from './cells/DrawerLink/index.js'
Expand Down Expand Up @@ -186,7 +187,33 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
collectionSlug: selectedCollection,
})

const onDrawerSave = useCallback<DocumentDrawerProps['onSave']>(
const filter = useMemo(
() =>
data?.docs !== undefined
? {
[selectedCollection]: {
id: { not_in: data.docs.map((doc) => doc.id) },
},
}
: undefined,
[selectedCollection, data?.docs],
)

const [ListDrawer, ListDrawerToggler, { closeDrawer: closeListDrawer }] = useListDrawer({
collectionSlugs: [selectedCollection],
filterOptions: filter,
})

const onListDrawerSelect = useCallback<ListDrawerProps['onSelect']>(
(args) => {
closeListDrawer()

void renderTable([args.doc, ...data.docs])
},
[closeListDrawer, renderTable, data?.docs],
)

const onDocumentDrawerSave = useCallback<DocumentDrawerProps['onSave']>(
(args) => {
const foundDocIndex = data?.docs?.findIndex((doc) => doc.id === args.doc.id)
let withNewOrUpdatedDoc: PaginatedDocs['docs'] = undefined
Expand All @@ -204,16 +231,16 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
[data?.docs, renderTable],
)

const onDrawerCreate = useCallback<DocumentDrawerProps['onSave']>(
const onDocumentDrawerCreate = useCallback<DocumentDrawerProps['onSave']>(
(args) => {
closeDrawer()

void onDrawerSave(args)
void onDocumentDrawerSave(args)
},
[closeDrawer, onDrawerSave],
[closeDrawer, onDocumentDrawerSave],
)

const onDrawerDelete = useCallback<DocumentDrawerProps['onDelete']>(
const onDocumentDrawerDelete = useCallback<DocumentDrawerProps['onDelete']>(
(args) => {
const newDocs = data.docs.filter((doc) => doc.id !== args.id)
void renderTable(newDocs)
Expand Down Expand Up @@ -243,6 +270,7 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
<div className={`${baseClass}__header`}>
{Label}
<div className={`${baseClass}__actions`}>
<ListDrawerToggler>{i18n.t('fields:chooseFromExisting')}</ListDrawerToggler>
{!Array.isArray(relationTo) && canCreate && (
<DocumentDrawerToggler className={`${baseClass}__add-new`}>
{i18n.t('fields:addNew')}
Expand Down Expand Up @@ -347,7 +375,10 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
collectionSlug={Array.isArray(relationTo) ? relationTo[0] : relationTo}
columnState={columnState}
LinkedCellOverride={
<DrawerLink onDrawerDelete={onDrawerDelete} onDrawerSave={onDrawerSave} />
<DrawerLink
onDrawerDelete={onDocumentDrawerDelete}
onDrawerSave={onDocumentDrawerSave}
/>
}
>
<AnimateHeight
Expand All @@ -370,7 +401,8 @@ export const RelationshipTable: React.FC<RelationshipTableComponentProps> = (pro
</Fragment>
)}
{AfterInput}
<DocumentDrawer initialData={initialDrawerData} onSave={onDrawerCreate} />
<DocumentDrawer initialData={initialDrawerData} onSave={onDocumentDrawerCreate} />
<ListDrawer allowCreate={false} onSelect={onListDrawerSelect} />
</div>
)
}