Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/components/form/JsonInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/
import { JsonInput, type JsonInputProps } from '@mantine/core';
import { omit } from 'rambdax';
import { useMemo } from 'react';
import {
type FieldValues,
Expand All @@ -24,19 +25,20 @@ import {

import { genControllerProps } from './util';

export type FormItemJsonInputProps<T extends FieldValues> =
UseControllerProps<T> &
JsonInputProps & {
toObject?: boolean;
};
export type FormItemJsonInputProps<T extends FieldValues> = UseControllerProps<T> &
JsonInputProps & {
toObject?: boolean;
objValue?: unknown;
};

export const FormItemJsonInput = <T extends FieldValues>(
props: FormItemJsonInputProps<T>
) => {
const { objValue = {} } = props;
const {
controllerProps,
restProps: { toObject, ...restProps },
} = genControllerProps(props, props.toObject ? {} : '');
} = genControllerProps(props, props.toObject ? objValue : '');
const {
field: { value: rawVal, onChange: fOnChange, ...restField },
fieldState,
Expand All @@ -45,9 +47,9 @@ export const FormItemJsonInput = <T extends FieldValues>(
if (!toObject) return rawVal;
if (typeof rawVal === 'string') return rawVal;
const val = JSON.stringify(rawVal, null, 2);
if (val === '{}') return '';
if (val === JSON.stringify(objValue)) return '';
return val;
}, [rawVal, toObject]);
}, [rawVal, toObject, objValue]);

return (
<JsonInput
Expand All @@ -59,7 +61,7 @@ export const FormItemJsonInput = <T extends FieldValues>(
try {
res = JSON.parse(val);
} catch {
res = val.length === 0 ? {} : val;
res = val.length === 0 ? objValue : val;
}
}
fOnChange(res);
Expand All @@ -69,7 +71,7 @@ export const FormItemJsonInput = <T extends FieldValues>(
autosize
resize="vertical"
{...restField}
{...restProps}
{...omit(['objValue'], restProps)}
/>
);
};
4 changes: 2 additions & 2 deletions src/types/schema/apisix/stream_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const StreamRouteProtocolLoggerItem = z.object({
const StreamRouteProtocol = z.object({
name: z.string(),
superior_id: z.string(),
conf: z.object({}),
logger: z.array(StreamRouteProtocolLoggerItem),
conf: z.object({}).optional(),
logger: z.array(StreamRouteProtocolLoggerItem).optional(),
});

const StreamRoute = z
Expand Down
Loading