Skip to content

Commit 3ca203e

Browse files
authored
fix(ui): json fields can now take a maxHeight in admin props and there's a mininum height of 3 lines (#9018)
JSON fields are now 3 lines minimum in height like so: ![image](https://github.com/user-attachments/assets/0b2ad47e-6929-4836-ac9d-022ffcdc6f27) This helps fix an issue where long content is wrapped: ![image](https://github.com/user-attachments/assets/40fc2426-11d7-4ca5-a716-3347bb0d5a4b) Previously it would show like this: ![image](https://github.com/user-attachments/assets/7f321220-ffa2-40ff-bc4b-2b26d21d4911)
1 parent 50f3ca9 commit 3ca203e

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ export type JSONField = {
10111011
Label?: CustomComponent<JSONFieldLabelClientComponent | JSONFieldLabelServerComponent>
10121012
} & Admin['components']
10131013
editorOptions?: EditorProps['options']
1014+
maxHeight?: number
10141015
} & Admin
10151016

10161017
jsonSchema?: {
@@ -1030,6 +1031,7 @@ export type JSONFieldClient = {
10301031
Error?: MappedComponent
10311032
Label?: MappedComponent
10321033
} & AdminClient['components']
1034+
maxHeight?: number
10331035
} & AdminClient &
10341036
Pick<JSONField['admin'], 'editorOptions'>
10351037
} & Omit<FieldBaseClient, 'admin'> &

packages/ui/src/elements/CodeEditor/CodeEditor.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ const Editor = (EditorImport.default || EditorImport) as unknown as typeof Edito
1313
const baseClass = 'code-editor'
1414

1515
const CodeEditor: React.FC<Props> = (props) => {
16-
const { className, options, readOnly, ...rest } = props
16+
const { className, maxHeight, options, readOnly, ...rest } = props
1717
const [dynamicHeight, setDynamicHeight] = useState(20)
1818
const { theme } = useTheme()
1919

20+
const MIN_HEIGHT = 56 // equivalent to 3 lines
21+
2022
const classes = [
2123
baseClass,
2224
className,
@@ -52,14 +54,14 @@ const CodeEditor: React.FC<Props> = (props) => {
5254
// can already have scrolling, we want the height of the
5355
// editor to fit its content.
5456
// See: https://github.com/microsoft/monaco-editor/discussions/3677
55-
height={dynamicHeight}
57+
height={maxHeight ? Math.min(dynamicHeight, maxHeight) : dynamicHeight}
5658
onChange={(value, ev) => {
5759
rest.onChange?.(value, ev)
58-
setDynamicHeight(value.split('\n').length * 18 + 2)
60+
setDynamicHeight(Math.max(MIN_HEIGHT, value.split('\n').length * 18 + 2))
5961
}}
6062
onMount={(editor, monaco) => {
6163
rest.onMount?.(editor, monaco)
62-
setDynamicHeight(editor.getValue().split('\n').length * 18 + 2)
64+
setDynamicHeight(Math.max(MIN_HEIGHT, editor.getValue().split('\n').length * 18 + 2))
6365
}}
6466
/>
6567
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EditorProps } from '@monaco-editor/react'
22

33
export type Props = {
4+
maxHeight?: number
45
readOnly?: boolean
56
} & EditorProps

packages/ui/src/fields/JSON/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const JSONFieldComponent: JSONFieldClientComponent = (props) => {
2929
className,
3030
description,
3131
editorOptions,
32+
maxHeight,
3233
readOnly: readOnlyFromAdmin,
3334
style,
3435
width,
@@ -144,6 +145,7 @@ const JSONFieldComponent: JSONFieldClientComponent = (props) => {
144145
<RenderComponent mappedComponent={field?.admin?.components?.beforeInput} />
145146
<CodeEditor
146147
defaultLanguage="json"
148+
maxHeight={maxHeight}
147149
onChange={handleChange}
148150
onMount={handleMount}
149151
options={editorOptions}

test/fields/collections/JSON/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const JSON: CollectionConfig = {
1111
{
1212
name: 'json',
1313
type: 'json',
14+
admin: {
15+
maxHeight: 542,
16+
},
1417
jsonSchema: {
1518
fileMatch: ['a://b/foo.json'],
1619
schema: {

0 commit comments

Comments
 (0)