@@ -6,10 +6,14 @@ import { EditIcon } from './icon/edit';
66import { DeleteIcon } from './icon/delete' ;
77import type { JsonViewEditorProps } from './' ;
88
9- const Quotes : FC < PropsWithChildren < React . HTMLAttributes < HTMLSpanElement > & { quotes ?: JsonViewEditorProps < object > [ 'quotes' ] ; show ?: boolean ; } > > = ( { show, style, quotes } ) => {
9+ const Quotes : FC <
10+ PropsWithChildren <
11+ React . HTMLAttributes < HTMLSpanElement > & { quotes ?: JsonViewEditorProps < object > [ 'quotes' ] ; show ?: boolean }
12+ >
13+ > = ( { show, style, quotes } ) => {
1014 if ( ! quotes || ! show ) return ;
1115 return < span style = { style } > { quotes } </ span > ;
12- }
16+ } ;
1317
1418export interface ReValueProps < T extends object > extends React . HTMLAttributes < HTMLSpanElement > {
1519 onEdit ?: JsonViewEditorProps < T > [ 'onEdit' ] ;
@@ -19,14 +23,31 @@ export interface ReValueProps<T extends object> extends React.HTMLAttributes<HTM
1923 value ?: unknown ;
2024 data ?: T ;
2125 visible ?: boolean ;
26+ namespace ?: Array < string | number > ;
2227 editableValue ?: boolean ;
2328 displayDataTypes ?: boolean ;
2429 setValue ?: React . Dispatch < React . SetStateAction < T > > ;
2530 onDelete ?: JsonViewEditorProps < T > [ 'onDelete' ] ;
2631}
2732
2833export function ReValue < T extends object > ( props : ReValueProps < T > ) {
29- const { type, value, setValue, data, keyName, visible, quotes, style, children, displayDataTypes, editableValue, onDelete, onEdit, ...reset } = props ;
34+ const {
35+ type,
36+ value,
37+ setValue,
38+ data,
39+ keyName,
40+ visible,
41+ quotes,
42+ style,
43+ children,
44+ namespace,
45+ displayDataTypes,
46+ editableValue,
47+ onDelete,
48+ onEdit,
49+ ...reset
50+ } = props ;
3051 const [ editable , setEditable ] = useState ( false ) ;
3152 const $edit = useRef < HTMLSpanElement > ( null ) ;
3253 const [ curentType , setCurentType ] = useState ( type ) ;
@@ -40,7 +61,7 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
4061 $edit . current . setAttribute ( 'contentEditable' , 'true' ) ;
4162 $edit . current ?. focus ( ) ;
4263 }
43- }
64+ } ;
4465 const keyDown = ( evn : React . KeyboardEvent < HTMLSpanElement > ) => {
4566 if ( ! editableValue ) return ;
4667 if ( evn . key === 'Enter' ) {
@@ -51,7 +72,7 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
5172 $edit . current . setAttribute ( 'contentEditable' , 'false' ) ;
5273 }
5374 }
54- }
75+ } ;
5576 const blur = async ( ) => {
5677 if ( ! editableValue ) return ;
5778 setEditable ( false ) ;
@@ -70,29 +91,29 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
7091 if ( Number . isNaN ( text ) ) {
7192 typeStr = 'number' ;
7293 }
73- if ( typeof text === 'string' && / ^ ( t r u e | f a l s e ) $ / ig . test ( text ) ) {
74- text = / ^ ( t r u e ) $ / ig . test ( text ) ? true : false ;
94+ if ( typeof text === 'string' && / ^ ( t r u e | f a l s e ) $ / gi . test ( text ) ) {
95+ text = / ^ ( t r u e ) $ / gi . test ( text ) ? true : false ;
7596 typeStr = 'boolean' ;
76- } else if ( typeof text === 'string' && / ^ [ \d ] + n $ / ig . test ( text ) ) {
77- text = BigInt ( text . replace ( / n $ / ig , '' ) ) ;
97+ } else if ( typeof text === 'string' && / ^ [ \d ] + n $ / gi . test ( text ) ) {
98+ text = BigInt ( text . replace ( / n $ / gi , '' ) ) ;
7899 typeStr = 'bigint' ;
79- } else if ( typeof text === 'string' && / ^ ( n u l l ) $ / ig . test ( text ) ) {
100+ } else if ( typeof text === 'string' && / ^ ( n u l l ) $ / gi . test ( text ) ) {
80101 text = null ;
81102 typeStr = 'null' ;
82- } else if ( typeof text === 'string' && / ^ ( u n d e f i n e d ) $ / ig . test ( text ) ) {
103+ } else if ( typeof text === 'string' && / ^ ( u n d e f i n e d ) $ / gi . test ( text ) ) {
83104 text = undefined ;
84105 typeStr = 'undefined' ;
85106 } else if ( typeof text === 'string' ) {
86107 try {
87- if ( text && text . length > 10 && ! isNaN ( Date . parse ( text ) ) ) {
108+ if ( text && text . length > 10 && ! isNaN ( Date . parse ( text ) ) ) {
88109 const dt = new Date ( text ) ;
89110 text = dt ;
90111 typeStr = 'date' ;
91112 }
92113 } catch ( error ) { }
93114 }
94115 if ( onEdit ) {
95- const result = await onEdit ( { type : 'value' , value : text , oldValue : curentChild } ) ;
116+ const result = await onEdit ( { type : 'value' , value : text , oldValue : curentChild , namespace } ) ;
96117 if ( result ) {
97118 setCurentType ( typeStr ) ;
98119 setCurentChild ( text ) ;
@@ -102,17 +123,22 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
102123 }
103124 }
104125 }
105- }
106- const defaultStyle = { minWidth : 34 , minHeight : 18 , paddingInline : 3 , display : 'inline-block' } as React . CSSProperties ;
126+ } ;
127+ const defaultStyle = {
128+ minWidth : 34 ,
129+ minHeight : 18 ,
130+ paddingInline : 3 ,
131+ display : 'inline-block' ,
132+ } as React . CSSProperties ;
107133 const { type : typeStr , content : childStr } = getValueString ( curentChild ) ;
108134 const color = typeMap [ typeStr ] ?. color || '' ;
109135 const spanProps : React . HTMLAttributes < HTMLSpanElement > = {
110136 ...reset ,
111137 onBlur : blur ,
112138 onKeyDown : keyDown ,
113139 spellCheck : false ,
114- style : editable ? { ...style , ...defaultStyle , color } : { ...style , color} ,
115- }
140+ style : editable ? { ...style , ...defaultStyle , color } : { ...style , color } ,
141+ } ;
116142 let typeView = < Type type = { typeStr } /> ;
117143 if ( typeStr === 'null' || typeStr === 'undefined' ) {
118144 typeView = < Fragment /> ;
@@ -123,16 +149,17 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
123149 delete ( data as Record < string , any > ) [ keyName as string ] ;
124150 setValue ( { ...data } as T ) ;
125151 }
126- }
152+ } ;
127153 return (
128154 < Fragment >
129155 { displayDataTypes && typeView }
130156 < Quotes style = { style } quotes = { quotes } show = { typeStr === 'string' } />
131- < span { ...spanProps } ref = { $edit } data-value = { childStr } > { typeof curentChild === 'string' ? curentChild : childStr } </ span >
157+ < span { ...spanProps } ref = { $edit } data-value = { childStr } >
158+ { typeof curentChild === 'string' ? curentChild : childStr }
159+ </ span >
132160 < Quotes style = { style } quotes = { quotes } show = { typeStr === 'string' } />
133161 { visible && editableValue && onEdit && < EditIcon onClick = { click } /> }
134162 { visible && editableValue && onDelete && < DeleteIcon onClick = { deleteHandle } /> }
135163 </ Fragment >
136164 ) ;
137-
138- }
165+ }
0 commit comments