Skip to content

Commit a9a34df

Browse files
committed
fix: JSON formatting not working after keepValues change
1 parent ff6afbb commit a9a34df

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/components/databrowser/components/display/display-simple.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ const EditorDisplayForm = ({
4545
const form = useForm({
4646
defaultValues: { value: data },
4747
})
48-
const { editor, selector } = useField({ name: "value", form })
49-
50-
const { mutateAsync: setKey, isPending: isSettingKey } = useSetSimpleKey(dataKey, type)
5148

5249
// Updates default values after submit
5350
useEffect(() => {
@@ -59,6 +56,10 @@ const EditorDisplayForm = ({
5956
)
6057
}, [data])
6158

59+
const { editor, selector } = useField({ name: "value", form, data })
60+
61+
const { mutateAsync: setKey, isPending: isSettingKey } = useSetSimpleKey(dataKey, type)
62+
6263
const handleCancel = () => {
6364
form.reset()
6465
}

src/components/databrowser/components/display/input/use-field.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ export const useField = ({
1010
height,
1111
showCopyButton,
1212
readOnly,
13+
data,
1314
}: {
1415
name: string
1516
form: UseFormReturn<any>
1617
height?: number
1718
showCopyButton?: boolean
1819
readOnly?: boolean
20+
data?: string
1921
}) => {
2022
const { field, fieldState } = useController<Record<string, string>>({
2123
name,
@@ -26,7 +28,7 @@ export const useField = ({
2628
checkIsValidJSON(field.value) ? "JSON" : "Text"
2729
)
2830

29-
// Attempt to format JSON on initial load
31+
// Attempt to format JSON everytime the underlying data changes
3032
useEffect(() => {
3133
if (!checkIsValidJSON(field.value)) {
3234
return
@@ -35,7 +37,7 @@ export const useField = ({
3537
form.setValue(name, formatJSON(field.value), {
3638
shouldDirty: false,
3739
})
38-
}, [])
40+
}, [data])
3941

4042
const handleTypeChange = (type: ContentType) => {
4143
setContentType(type)

0 commit comments

Comments
 (0)