Skip to content

Commit 11b1207

Browse files
committed
Link field: allow target blank
1 parent f112e57 commit 11b1207

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

admin/pages/general.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default () => (
1313
}}
1414
>
1515
<TextField field="text" label="Text" />
16-
<LinkField field="privacyPolicyPage" label="Link to another" />
16+
<LinkField field="homePage" label="Link to home" allowDisconnect />
17+
<LinkField field="privacyPolicyPage" label="Link to another" allowTargetBlank allowDisconnect />
1718

1819
<CollapsibleBox heading="Images">
1920
<ImageField field="singleImage" label="Single image" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"formatVersion": 3,
3+
"modifications": [
4+
{
5+
"modification": "createRelation",
6+
"entityName": "General",
7+
"owningSide": {
8+
"type": "OneHasOne",
9+
"name": "homePage",
10+
"target": "Link",
11+
"joiningColumn": {
12+
"columnName": "home_page_id",
13+
"onDelete": "set null"
14+
},
15+
"nullable": true
16+
}
17+
},
18+
{
19+
"modification": "patchAclSchema",
20+
"patch": [
21+
{
22+
"op": "add",
23+
"path": "/roles/admin/entities/General/operations/read/homePage",
24+
"value": true
25+
},
26+
{
27+
"op": "add",
28+
"path": "/roles/admin/entities/General/operations/create/homePage",
29+
"value": true
30+
},
31+
{
32+
"op": "add",
33+
"path": "/roles/admin/entities/General/operations/update/homePage",
34+
"value": true
35+
}
36+
]
37+
}
38+
]
39+
}

api/model/General.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class General {
1212
singleImage = d.oneHasOne(Image).setNullOnDelete()
1313
imageList = d.oneHasOne(ImageList).setNullOnDelete()
1414

15+
homePage = d.oneHasOne(Link).setNullOnDelete()
1516
privacyPolicyPage = d.oneHasOne(Link).setNullOnDelete()
1617

1718
singleYoutubeVideo = d.oneHasOne(YoutubeVideo).setNullOnDelete()

src/link/admin/LinkField.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ActionableBox,
33
Block,
44
Box,
5+
CheckboxField,
56
Component,
67
DiscriminatedBlocks,
78
Field,
@@ -16,10 +17,11 @@ export interface LinkFieldProps {
1617
label?: string
1718
titleField?: boolean
1819
allowDisconnect?: boolean
20+
allowTargetBlank?: boolean
1921
}
2022

2123
export const LinkField = Component<LinkFieldProps>(
22-
({ field, label, titleField = true, allowDisconnect = false }) => {
24+
({ field, label, titleField = true, allowDisconnect = false, allowTargetBlank = false }) => {
2325
const rootEntity = useEntity()
2426

2527
const insideContent = (
@@ -33,6 +35,25 @@ export const LinkField = Component<LinkFieldProps>(
3335
<TextField field="externalLink" label="URL" />
3436
</Block>
3537
</DiscriminatedBlocks>
38+
{allowTargetBlank && (
39+
<CheckboxField
40+
field="isTargetBlank"
41+
label="Target blank"
42+
defaultValue={false}
43+
labelDescription={
44+
<>
45+
Force link to open in new tab.{' '}
46+
<span style={{ color: 'red' }}>
47+
This feature is highly discouraged.{' '}
48+
<a href="https://css-tricks.com/use-target_blank/" target="_blank" rel="noreferrer">
49+
Read more
50+
</a>
51+
.
52+
</span>
53+
</>
54+
}
55+
/>
56+
)}
3657
</HasOne>
3758
)
3859

@@ -44,10 +65,11 @@ export const LinkField = Component<LinkFieldProps>(
4465

4566
return content
4667
},
47-
({ field, titleField = true }) => (
68+
({ field }) => (
4869
<HasOne field={field}>
4970
<Field field="type" />
50-
{titleField && <Field field="title" />}
71+
<Field field="title" />
72+
<Field field="isTargetBlank" />
5173
<SelectField field="internalLink" label={undefined} options="Linkable.url" />
5274
<Field field="externalLink" />
5375
</HasOne>

0 commit comments

Comments
 (0)