Skip to content

Commit bbf97e6

Browse files
committed
allow opening edit view via context menu
1 parent 77c6661 commit bbf97e6

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

Diff for: packages/ui/src/elements/Link/index.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ type Props = {
3030
* @default true
3131
*/
3232
preventDefault?: boolean
33+
/**
34+
* Skip the route transition if true
35+
*
36+
* @default false
37+
*/
38+
skipRouteTransition?: boolean
3339
} & Parameters<typeof NextLink>[0]
3440

3541
export const Link: React.FC<Props> = ({
@@ -40,6 +46,7 @@ export const Link: React.FC<Props> = ({
4046
ref,
4147
replace,
4248
scroll,
49+
skipRouteTransition = false,
4350
...rest
4451
}) => {
4552
const router = useRouter()
@@ -63,6 +70,10 @@ export const Link: React.FC<Props> = ({
6370
e.preventDefault()
6471
}
6572

73+
if (skipRouteTransition) {
74+
return
75+
}
76+
6677
startRouteTransition(() => {
6778
const url = typeof href === 'string' ? href : formatUrl(href)
6879

Diff for: packages/ui/src/elements/ReactSelect/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CommonProps, GroupBase, Props as ReactSelectStateManagerProps } fr
33
import type { DocumentDrawerProps, UseDocumentDrawer } from '../DocumentDrawer/types.js'
44

55
type CustomSelectProps = {
6+
adminRoute: string
67
disableKeyDown?: boolean
78
disableMouseDown?: boolean
89
DocumentDrawerToggler?: ReturnType<UseDocumentDrawer>[1]

Diff for: packages/ui/src/fields/Relationship/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ const RelationshipFieldComponent: RelationshipFieldClientComponent = (props) =>
697697
...(appearance !== 'select' && { DropdownIndicator: null }),
698698
}}
699699
customProps={{
700+
adminRoute: config.routes.admin,
700701
disableKeyDown: isDrawerOpen || isListDrawerOpen,
701702
disableMouseDown: isDrawerOpen || isListDrawerOpen,
702703
onDocumentOpen,

Diff for: packages/ui/src/fields/Relationship/select-components/MultiValueLabel/index.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use client'
22
import type { MultiValueProps } from 'react-select'
33

4+
import { formatAdminURL } from 'payload/shared'
45
import React, { Fragment, useState } from 'react'
56
import { components } from 'react-select'
67

78
import type { ReactSelectAdapterProps } from '../../../../elements/ReactSelect/types.js'
89
import type { Option } from '../../types.js'
910

11+
import { Link } from '../../../../elements/Link/index.js'
1012
import { Tooltip } from '../../../../elements/Tooltip/index.js'
1113
import { EditIcon } from '../../../../icons/Edit/index.js'
1214
import { useAuth } from '../../../../providers/Auth/index.js'
@@ -25,13 +27,17 @@ export const MultiValueLabel: React.FC<
2527
> = (props) => {
2628
const {
2729
data: { allowEdit, label, relationTo, value },
28-
selectProps: { customProps: { draggableProps, onDocumentOpen } = {} } = {},
30+
selectProps: { customProps: { adminRoute, draggableProps, onDocumentOpen } = {} } = {},
2931
} = props
3032

3133
const { permissions } = useAuth()
3234
const [showTooltip, setShowTooltip] = useState(false)
3335
const { t } = useTranslation()
3436
const hasReadPermission = Boolean(permissions?.collections?.[relationTo]?.read)
37+
const docUrl = formatAdminURL({
38+
adminRoute,
39+
path: `/collections/${relationTo}/${value}`,
40+
})
3541

3642
return (
3743
<div className={baseClass}>
@@ -46,9 +52,10 @@ export const MultiValueLabel: React.FC<
4652
</div>
4753
{relationTo && hasReadPermission && allowEdit !== false && (
4854
<Fragment>
49-
<button
55+
<Link
5056
aria-label={`Edit ${label}`}
5157
className={`${baseClass}__drawer-toggler`}
58+
href={docUrl}
5259
onClick={(event) => {
5360
setShowTooltip(false)
5461
onDocumentOpen({
@@ -67,13 +74,13 @@ export const MultiValueLabel: React.FC<
6774
onMouseEnter={() => setShowTooltip(true)}
6875
onMouseLeave={() => setShowTooltip(false)}
6976
onTouchEnd={(e) => e.stopPropagation()} // prevents react-select dropdown from opening
70-
type="button"
77+
skipRouteTransition
7178
>
7279
<Tooltip className={`${baseClass}__tooltip`} show={showTooltip}>
7380
{t('general:editLabel', { label: '' })}
7481
</Tooltip>
7582
<EditIcon className={`${baseClass}__icon`} />
76-
</button>
83+
</Link>
7784
</Fragment>
7885
)}
7986
</div>

Diff for: packages/ui/src/fields/Relationship/select-components/SingleValue/index.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use client'
22
import type { SingleValueProps } from 'react-select'
33

4+
import { formatAdminURL } from 'payload/shared'
45
import React, { Fragment, useState } from 'react'
56
import { components as SelectComponents } from 'react-select'
67

78
import type { ReactSelectAdapterProps } from '../../../../elements/ReactSelect/types.js'
89
import type { Option } from '../../types.js'
910

11+
import { Link } from '../../../../elements/Link/index.js'
1012
import { Tooltip } from '../../../../elements/Tooltip/index.js'
1113
import { EditIcon } from '../../../../icons/Edit/index.js'
1214
import { useAuth } from '../../../../providers/Auth/index.js'
@@ -26,13 +28,17 @@ export const SingleValue: React.FC<
2628
const {
2729
children,
2830
data: { allowEdit, label, relationTo, value },
29-
selectProps: { customProps: { onDocumentOpen } = {} } = {},
31+
selectProps: { customProps: { adminRoute, onDocumentOpen } = {} } = {},
3032
} = props
3133

3234
const [showTooltip, setShowTooltip] = useState(false)
3335
const { t } = useTranslation()
3436
const { permissions } = useAuth()
3537
const hasReadPermission = Boolean(permissions?.collections?.[relationTo]?.read)
38+
const docUrl = formatAdminURL({
39+
adminRoute,
40+
path: `/collections/${relationTo}/${value}`,
41+
})
3642

3743
return (
3844
<SelectComponents.SingleValue {...props} className={baseClass}>
@@ -41,9 +47,10 @@ export const SingleValue: React.FC<
4147
<div className={`${baseClass}__text`}>{children}</div>
4248
{relationTo && hasReadPermission && allowEdit !== false && (
4349
<Fragment>
44-
<button
50+
<Link
4551
aria-label={t('general:editLabel', { label })}
4652
className={`${baseClass}__drawer-toggler`}
53+
href={docUrl}
4754
onClick={(event) => {
4855
setShowTooltip(false)
4956
onDocumentOpen({
@@ -62,13 +69,13 @@ export const SingleValue: React.FC<
6269
onMouseEnter={() => setShowTooltip(true)}
6370
onMouseLeave={() => setShowTooltip(false)}
6471
onTouchEnd={(e) => e.stopPropagation()} // prevents react-select dropdown from openingtype="button"
65-
type="button"
72+
skipRouteTransition
6673
>
6774
<Tooltip className={`${baseClass}__tooltip`} show={showTooltip}>
6875
{t('general:edit')}
6976
</Tooltip>
7077
<EditIcon />
71-
</button>
78+
</Link>
7279
</Fragment>
7380
)}
7481
</div>

0 commit comments

Comments
 (0)