1515 * limitations under the License.
1616 */
1717import { JsonInput , type JsonInputProps } from '@mantine/core' ;
18+ import { omit } from 'rambdax' ;
1819import { useMemo } from 'react' ;
1920import {
2021 type FieldValues ,
@@ -24,19 +25,20 @@ import {
2425
2526import { 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
3334export 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} ;
0 commit comments