@@ -5,11 +5,11 @@ import {
5
5
Edit as EditIcon
6
6
} from '@mui/icons-material'
7
7
import { Box , styled } from '@mui/material'
8
- import copy from 'copy-to-clipboard'
9
8
import type React from 'react'
10
9
import { useCallback , useMemo , useState } from 'react'
11
10
12
11
import { useTextColor } from '../hooks/useColor'
12
+ import { useClipboard } from '../hooks/useCopyToClipboard'
13
13
import { useInspect } from '../hooks/useInspect'
14
14
import { useJsonViewerStore } from '../stores/JsonViewerStore'
15
15
import { useTypeComponents } from '../stores/typeRegistry'
@@ -22,7 +22,7 @@ export type DataKeyPairProps = {
22
22
path : ( string | number ) [ ]
23
23
}
24
24
25
- const IconBox = styled ( props => < Box { ...props } component = 'span' /> ) `
25
+ const IconBox = styled ( props => < Box { ...props } component = 'span' /> ) `
26
26
cursor: pointer;
27
27
padding-left: 0.7rem;
28
28
` as typeof Box
@@ -56,6 +56,8 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
56
56
setInspect,
57
57
value
58
58
} ) , [ inspect , path , setInspect , value ] )
59
+ const { copy, copied } = useClipboard ( )
60
+
59
61
const actionIcons = useMemo ( ( ) => {
60
62
if ( editing ) {
61
63
return (
@@ -101,11 +103,23 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
101
103
event . preventDefault ( )
102
104
} }
103
105
>
104
- < ContentCopyIcon
105
- sx = { {
106
- fontSize : '.8rem'
107
- } }
108
- />
106
+ {
107
+ copied
108
+ ? (
109
+ < CheckIcon
110
+ sx = { {
111
+ fontSize : '.8rem'
112
+ } }
113
+ />
114
+ )
115
+ : (
116
+ < ContentCopyIcon
117
+ sx = { {
118
+ fontSize : '.8rem'
119
+ } }
120
+ />
121
+ )
122
+ }
109
123
</ IconBox >
110
124
{ /* todo: support edit object */ }
111
125
{ Editor &&
@@ -126,15 +140,15 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
126
140
}
127
141
</ >
128
142
)
129
- } , [ Editor , editing , onChange , path , tempValue , value ] )
143
+ } , [ Editor , copied , copy , editing , onChange , path , tempValue , value ] )
130
144
131
145
const expandable = PreComponent && PostComponent
132
146
const KeyRenderer = useJsonViewerStore ( store => store . keyRenderer )
133
147
return (
134
148
< Box className = 'data-key-pair'
135
- onMouseEnter = {
136
- useCallback ( ( ) => setHover ( path ) , [ setHover , path ] )
137
- }
149
+ onMouseEnter = {
150
+ useCallback ( ( ) => setHover ( path ) , [ setHover , path ] )
151
+ }
138
152
>
139
153
< DataBox
140
154
component = 'span'
@@ -156,11 +170,11 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
156
170
>
157
171
{
158
172
KeyRenderer . when ( downstreamProps )
159
- ? < KeyRenderer { ...downstreamProps } />
173
+ ? < KeyRenderer { ...downstreamProps } />
160
174
: ! props . nested && (
161
175
isNumberKey
162
176
? < Box component = 'span'
163
- style = { { color : numberKeyColor } } > { displayKey } </ Box >
177
+ style = { { color : numberKeyColor } } > { displayKey } </ Box >
164
178
: < > "{ displayKey } "</ >
165
179
)
166
180
}
@@ -169,18 +183,18 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
169
183
< DataBox sx = { { mx : 0.5 } } > :</ DataBox >
170
184
)
171
185
}
172
- { PreComponent && < PreComponent { ...downstreamProps } /> }
186
+ { PreComponent && < PreComponent { ...downstreamProps } /> }
173
187
{ ( isHover && expandable && inspect ) && actionIcons }
174
188
</ DataBox >
175
189
{
176
190
editing
177
- ? ( Editor && < Editor value = { tempValue } setValue = { setTempValue } /> )
191
+ ? ( Editor && < Editor value = { tempValue } setValue = { setTempValue } /> )
178
192
: ( Component )
179
- ? < Component { ...downstreamProps } />
193
+ ? < Component { ...downstreamProps } />
180
194
: < Box component = 'span'
181
- className = 'data-value-fallback' > { `fallback: ${ value } ` } </ Box >
195
+ className = 'data-value-fallback' > { `fallback: ${ value } ` } </ Box >
182
196
}
183
- { PostComponent && < PostComponent { ...downstreamProps } /> }
197
+ { PostComponent && < PostComponent { ...downstreamProps } /> }
184
198
{ ( isHover && expandable && ! inspect ) && actionIcons }
185
199
{ ( isHover && ! expandable ) && actionIcons }
186
200
</ Box >
0 commit comments