Skip to content

Commit 78d1bb3

Browse files
authoredJul 15, 2021
fix(field): disable revert changes on read-only fields and documents and hide fields from review changes if hidden: true (#2619)
* fix(field): disable revert changes buttons if fields are `readOnly` * fix(field): hide fields if they are `hidden`
1 parent 5a55971 commit 78d1bb3

File tree

8 files changed

+45
-9
lines changed

8 files changed

+45
-9
lines changed
 

‎examples/test-studio/schemas/objects.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const myObject = {
55
name: 'myObject',
66
title: 'My object',
77
icon,
8+
// readOnly: true,
89
fields: [
910
{
1011
name: 'first',
@@ -23,6 +24,7 @@ export default {
2324
name: 'objectsTest',
2425
type: 'document',
2526
title: 'Objects test',
27+
// readOnly: true,
2628
preview: {
2729
select: {
2830
title: 'myObject.first',
@@ -42,13 +44,30 @@ export default {
4244
type: 'object',
4345
description:
4446
'This is a field of (anonymous, inline) object type. Values here should never get a `_type` property',
47+
// readOnly: true,
48+
// hidden: true,
4549
fields: [
46-
{name: 'field1', type: 'string', description: 'This is a string field'},
50+
{
51+
name: 'field1',
52+
type: 'string',
53+
description: 'This is a string field',
54+
// readOnly: true,
55+
hidden: true,
56+
},
4757
{
4858
name: 'field2',
4959
type: 'myObject',
50-
title: 'A field of myObject',
60+
title: 'A field of myObject 1',
61+
description: 'This is another field of "myObject"',
62+
readOnly: true,
63+
},
64+
{
65+
name: 'field3',
66+
type: 'myObject',
67+
title: 'A field of myObject 2',
5168
description: 'This is another field of "myObject"',
69+
// readOnly: true,
70+
hidden: true,
5271
},
5372
],
5473
},

‎packages/@sanity/field/src/diff/changes/buildChangeList.ts

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function buildObjectChangeList(
8080
path,
8181
titlePath,
8282
changes: reduceTitlePaths(changes, titlePath.length),
83+
schemaType,
8384
},
8485
]
8586
}
@@ -222,6 +223,7 @@ export function buildArrayChangeList(
222223
path,
223224
titlePath,
224225
changes: reduceTitlePaths(changes, titlePath.length),
226+
schemaType,
225227
},
226228
]
227229
}
@@ -280,6 +282,7 @@ function getFieldChange(
280282
showIndex: true,
281283
key: pathToString(path) || 'root',
282284
diffComponent: error ? undefined : component,
285+
parentSchema,
283286
},
284287
]
285288
}

‎packages/@sanity/field/src/diff/components/ChangeList.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function ChangeList({diff, fields, schemaType}: Props): React.ReactElemen
9393
change={change}
9494
key={change.key}
9595
data-revert-all-changes-hover={confirmRevertAllHover ? '' : undefined}
96+
readOnly={schemaType.readOnly}
9697
/>
9798
))}
9899
</Stack>
@@ -107,6 +108,7 @@ export function ChangeList({diff, fields, schemaType}: Props): React.ReactElemen
107108
onClick={handleRevertAllChangesClick}
108109
onMouseEnter={handleRevertAllChangesMouseEnter}
109110
onMouseLeave={handleRevertAllChangesMouseLeave}
111+
disabled={schemaType?.readOnly}
110112
/>
111113
</Stack>
112114
)}

‎packages/@sanity/field/src/diff/components/ChangeResolver.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {GroupChange} from './GroupChange'
66
export function ChangeResolver({
77
change,
88
...restProps
9-
}: {change: ChangeNode} & React.HTMLAttributes<HTMLDivElement>) {
9+
}: {change: ChangeNode; readOnly?: boolean} & React.HTMLAttributes<HTMLDivElement>) {
1010
if (change.type === 'field') {
1111
return <FieldChange change={change} {...restProps} />
1212
}

‎packages/@sanity/field/src/diff/components/FieldChange.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ const DiffBorder = styled.div`
5151

5252
export function FieldChange({
5353
change,
54+
readOnly,
5455
...restProps
55-
}: {change: FieldChangeNode} & React.HTMLAttributes<HTMLDivElement>) {
56+
}: {change: FieldChangeNode; readOnly?: boolean} & React.HTMLAttributes<HTMLDivElement>) {
5657
const DiffComponent = change.diffComponent || FallbackDiff
5758
const {
5859
documentId,
@@ -92,7 +93,7 @@ export function FieldChange({
9293
setRevertHovered(false)
9394
}, [])
9495

95-
return (
96+
return change.schemaType.hidden ? null : (
9697
<Stack space={1} as={FieldChangeContainer} {...restProps}>
9798
{change.showHeader && <ChangeBreadcrumb change={change} titlePath={change.titlePath} />}
9899
<FieldWrapper path={change.path} hasHover={revertHovered}>
@@ -120,6 +121,7 @@ export function FieldChange({
120121
onMouseLeave={handleRevertButtonMouseLeave}
121122
ref={setRevertButtonElement}
122123
selected={confirmRevertOpen}
124+
disabled={readOnly || change?.parentSchema?.readOnly || change.schemaType.readOnly}
123125
/>
124126
</Box>
125127
)}

‎packages/@sanity/field/src/diff/components/GroupChange.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ const GroupChangeContainer = styled.div`
5050

5151
export function GroupChange({
5252
change: group,
53+
readOnly,
5354
...restProps
54-
}: {change: GroupChangeNode} & React.HTMLAttributes<HTMLDivElement>): React.ReactElement {
55+
}: {change: GroupChangeNode; readOnly?: boolean} & React.HTMLAttributes<
56+
HTMLDivElement
57+
>): React.ReactElement | null {
5558
const {titlePath, changes, path: groupPath} = group
5659
const {path: diffPath} = useContext(DiffContext)
5760
const {documentId, schemaType, FieldWrapper, rootDiff, isComparingCurrent} = useContext(
@@ -107,23 +110,27 @@ export function GroupChange({
107110
>
108111
<Stack as={ChangeListWrapper} space={5}>
109112
{changes.map((change) => (
110-
<ChangeResolver key={change.key} change={change} />
113+
<ChangeResolver
114+
key={change.key}
115+
change={change}
116+
readOnly={readOnly || group?.schemaType?.readOnly}
117+
/>
111118
))}
112119
</Stack>
113-
114120
{isComparingCurrent && updatePermission.granted && (
115121
<Box>
116122
<RevertChangesButton
117123
onClick={handleRevertChangesConfirm}
118124
ref={setRevertButtonRef}
119125
selected={confirmRevertOpen}
126+
disabled={group?.schemaType?.readOnly || readOnly}
120127
/>
121128
</Box>
122129
)}
123130
</Stack>
124131
)
125132

126-
return (
133+
return group?.schemaType?.hidden ? null : (
127134
<Stack space={1} {...restProps}>
128135
<ChangeBreadcrumb titlePath={titlePath} />
129136
{isNestedInDiff || !FieldWrapper ? (

‎packages/@sanity/field/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export interface GroupChangeNode {
194194
key: string
195195
path: Path
196196
titlePath: ChangeTitlePath
197+
schemaType?: SchemaType
197198
}
198199

199200
export interface FieldChangeNode {
@@ -209,6 +210,7 @@ export interface FieldChangeNode {
209210
showHeader: boolean
210211
showIndex: boolean
211212
diffComponent?: DiffComponent
213+
parentSchema?: ArraySchemaType | ObjectSchemaType
212214
}
213215

214216
export type ChangeNode = GroupChangeNode | FieldChangeNode

‎packages/@sanity/types/src/schema/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface BaseSchemaType {
4545
icon?: React.ComponentType
4646
initialValue?: InitialValueProperty
4747
options?: unknown
48+
hidden?: boolean
4849

4950
preview?: {
5051
select?: PreviewValue

2 commit comments

Comments
 (2)

vercel[bot] commented on Jul 15, 2021

@vercel[bot]

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

vercel[bot] commented on Jul 15, 2021

@vercel[bot]

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

Please sign in to comment.