8
8
*/
9
9
10
10
import * as React from 'react' ;
11
- import { Fragment , useRef } from 'react' ;
11
+ import { Fragment } from 'react' ;
12
12
import styles from './EditableValue.css' ;
13
13
import { useEditableValue } from '../hooks' ;
14
14
@@ -27,7 +27,6 @@ export default function EditableValue({
27
27
path,
28
28
value,
29
29
} : EditableValueProps ) {
30
- const inputRef = useRef < HTMLInputElement | null > ( null ) ;
31
30
const [ state , dispatch ] = useEditableValue ( value ) ;
32
31
const { editableValue, hasPendingChanges, isValid, parsedValue} = state ;
33
32
@@ -44,6 +43,21 @@ export default function EditableValue({
44
43
externalValue : value ,
45
44
} ) ;
46
45
46
+ const handleCheckBoxToggle = ( { target} ) => {
47
+ dispatch ( {
48
+ type : 'UPDATE' ,
49
+ editableValue : target . checked ,
50
+ externalValue : value ,
51
+ } ) ;
52
+
53
+ // Unlike <input type="text"> which has both an onChange and an onBlur,
54
+ // <input type="checkbox"> updates state *and* applies changes in a single event.
55
+ // So we read from target.checked rather than parsedValue (which has not yet updated).
56
+ // We also don't check isValid (because that hasn't changed yet either);
57
+ // we don't need to check it anyway, since target.checked is always a boolean.
58
+ overrideValueFn ( path , target . checked ) ;
59
+ } ;
60
+
47
61
const handleKeyDown = event => {
48
62
// Prevent keydown events from e.g. change selected element in the tree
49
63
event . stopPropagation ( ) ;
@@ -73,6 +87,8 @@ export default function EditableValue({
73
87
placeholder = 'Enter valid JSON' ;
74
88
}
75
89
90
+ const isBool = parsedValue === true || parsedValue === false ;
91
+
76
92
return (
77
93
< Fragment >
78
94
< input
@@ -82,10 +98,17 @@ export default function EditableValue({
82
98
onChange = { handleChange }
83
99
onKeyDown = { handleKeyDown }
84
100
placeholder = { placeholder }
85
- ref = { inputRef }
86
101
type = "text"
87
102
value = { editableValue }
88
103
/>
104
+ { isBool && (
105
+ < input
106
+ className = { styles . Checkbox }
107
+ checked = { parsedValue }
108
+ type = "checkbox"
109
+ onChange = { handleCheckBoxToggle }
110
+ />
111
+ ) }
89
112
</ Fragment >
90
113
) ;
91
114
}
0 commit comments