@@ -6,10 +6,14 @@ import { EditIcon } from './icon/edit';
6
6
import { DeleteIcon } from './icon/delete' ;
7
7
import type { JsonViewEditorProps } from './' ;
8
8
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 } ) => {
10
14
if ( ! quotes || ! show ) return ;
11
15
return < span style = { style } > { quotes } </ span > ;
12
- }
16
+ } ;
13
17
14
18
export interface ReValueProps < T extends object > extends React . HTMLAttributes < HTMLSpanElement > {
15
19
onEdit ?: JsonViewEditorProps < T > [ 'onEdit' ] ;
@@ -19,14 +23,31 @@ export interface ReValueProps<T extends object> extends React.HTMLAttributes<HTM
19
23
value ?: unknown ;
20
24
data ?: T ;
21
25
visible ?: boolean ;
26
+ namespace ?: Array < string | number > ;
22
27
editableValue ?: boolean ;
23
28
displayDataTypes ?: boolean ;
24
29
setValue ?: React . Dispatch < React . SetStateAction < T > > ;
25
30
onDelete ?: JsonViewEditorProps < T > [ 'onDelete' ] ;
26
31
}
27
32
28
33
export 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 ;
30
51
const [ editable , setEditable ] = useState ( false ) ;
31
52
const $edit = useRef < HTMLSpanElement > ( null ) ;
32
53
const [ curentType , setCurentType ] = useState ( type ) ;
@@ -40,7 +61,7 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
40
61
$edit . current . setAttribute ( 'contentEditable' , 'true' ) ;
41
62
$edit . current ?. focus ( ) ;
42
63
}
43
- }
64
+ } ;
44
65
const keyDown = ( evn : React . KeyboardEvent < HTMLSpanElement > ) => {
45
66
if ( ! editableValue ) return ;
46
67
if ( evn . key === 'Enter' ) {
@@ -51,7 +72,7 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
51
72
$edit . current . setAttribute ( 'contentEditable' , 'false' ) ;
52
73
}
53
74
}
54
- }
75
+ } ;
55
76
const blur = async ( ) => {
56
77
if ( ! editableValue ) return ;
57
78
setEditable ( false ) ;
@@ -70,29 +91,29 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
70
91
if ( Number . isNaN ( text ) ) {
71
92
typeStr = 'number' ;
72
93
}
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 ;
75
96
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 , '' ) ) ;
78
99
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 ) ) {
80
101
text = null ;
81
102
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 ) ) {
83
104
text = undefined ;
84
105
typeStr = 'undefined' ;
85
106
} else if ( typeof text === 'string' ) {
86
107
try {
87
- if ( text && text . length > 10 && ! isNaN ( Date . parse ( text ) ) ) {
108
+ if ( text && text . length > 10 && ! isNaN ( Date . parse ( text ) ) ) {
88
109
const dt = new Date ( text ) ;
89
110
text = dt ;
90
111
typeStr = 'date' ;
91
112
}
92
113
} catch ( error ) { }
93
114
}
94
115
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 } ) ;
96
117
if ( result ) {
97
118
setCurentType ( typeStr ) ;
98
119
setCurentChild ( text ) ;
@@ -102,17 +123,22 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
102
123
}
103
124
}
104
125
}
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 ;
107
133
const { type : typeStr , content : childStr } = getValueString ( curentChild ) ;
108
134
const color = typeMap [ typeStr ] ?. color || '' ;
109
135
const spanProps : React . HTMLAttributes < HTMLSpanElement > = {
110
136
...reset ,
111
137
onBlur : blur ,
112
138
onKeyDown : keyDown ,
113
139
spellCheck : false ,
114
- style : editable ? { ...style , ...defaultStyle , color } : { ...style , color} ,
115
- }
140
+ style : editable ? { ...style , ...defaultStyle , color } : { ...style , color } ,
141
+ } ;
116
142
let typeView = < Type type = { typeStr } /> ;
117
143
if ( typeStr === 'null' || typeStr === 'undefined' ) {
118
144
typeView = < Fragment /> ;
@@ -123,16 +149,17 @@ export function ReValue<T extends object>(props: ReValueProps<T>) {
123
149
delete ( data as Record < string , any > ) [ keyName as string ] ;
124
150
setValue ( { ...data } as T ) ;
125
151
}
126
- }
152
+ } ;
127
153
return (
128
154
< Fragment >
129
155
{ displayDataTypes && typeView }
130
156
< 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 >
132
160
< Quotes style = { style } quotes = { quotes } show = { typeStr === 'string' } />
133
161
{ visible && editableValue && onEdit && < EditIcon onClick = { click } /> }
134
162
{ visible && editableValue && onDelete && < DeleteIcon onClick = { deleteHandle } /> }
135
163
</ Fragment >
136
164
) ;
137
-
138
- }
165
+ }
0 commit comments