Skip to content

Commit 78d1c32

Browse files
authored
fix(JSONInput): toObject logic (#3117)
* fix(stream_routes): logic for processing data * Revert "fix(stream_routes): logic for processing data" This reverts commit 3495126. * chore
1 parent 20cd07d commit 78d1c32

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/components/form/JsonInput.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import { JsonInput, type JsonInputProps } from '@mantine/core';
18+
import { omit } from 'rambdax';
1819
import { useMemo } from 'react';
1920
import {
2021
type FieldValues,
@@ -24,19 +25,20 @@ import {
2425

2526
import { genControllerProps } from './util';
2627

27-
export type FormItemJsonInputProps<T extends FieldValues> =
28-
UseControllerProps<T> &
29-
JsonInputProps & {
30-
toObject?: boolean;
31-
};
28+
export type FormItemJsonInputProps<T extends FieldValues> = UseControllerProps<T> &
29+
JsonInputProps & {
30+
toObject?: boolean;
31+
objValue?: unknown;
32+
};
3233

3334
export const FormItemJsonInput = <T extends FieldValues>(
3435
props: FormItemJsonInputProps<T>
3536
) => {
37+
const { objValue = {} } = props;
3638
const {
3739
controllerProps,
3840
restProps: { toObject, ...restProps },
39-
} = genControllerProps(props, props.toObject ? {} : '');
41+
} = genControllerProps(props, props.toObject ? objValue : '');
4042
const {
4143
field: { value: rawVal, onChange: fOnChange, ...restField },
4244
fieldState,
@@ -45,9 +47,9 @@ export const FormItemJsonInput = <T extends FieldValues>(
4547
if (!toObject) return rawVal;
4648
if (typeof rawVal === 'string') return rawVal;
4749
const val = JSON.stringify(rawVal, null, 2);
48-
if (val === '{}') return '';
50+
if (val === JSON.stringify(objValue)) return '';
4951
return val;
50-
}, [rawVal, toObject]);
52+
}, [rawVal, toObject, objValue]);
5153

5254
return (
5355
<JsonInput
@@ -59,7 +61,7 @@ export const FormItemJsonInput = <T extends FieldValues>(
5961
try {
6062
res = JSON.parse(val);
6163
} catch {
62-
res = val.length === 0 ? {} : val;
64+
res = val.length === 0 ? objValue : val;
6365
}
6466
}
6567
fOnChange(res);
@@ -69,7 +71,7 @@ export const FormItemJsonInput = <T extends FieldValues>(
6971
autosize
7072
resize="vertical"
7173
{...restField}
72-
{...restProps}
74+
{...omit(['objValue'], restProps)}
7375
/>
7476
);
7577
};

src/types/schema/apisix/stream_routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const StreamRouteProtocolLoggerItem = z.object({
2828
const StreamRouteProtocol = z.object({
2929
name: z.string(),
3030
superior_id: z.string(),
31-
conf: z.object({}),
32-
logger: z.array(StreamRouteProtocolLoggerItem),
31+
conf: z.object({}).optional(),
32+
logger: z.array(StreamRouteProtocolLoggerItem).optional(),
3333
});
3434

3535
const StreamRoute = z

0 commit comments

Comments
 (0)