Skip to content

Commit 835c11e

Browse files
mdaj06bvaughn
andauthored
Add checkbox toggle for boolean values (#19714)
* added a checkbox which appears to the right of a value when value is boolean * checkbox with toggle capability created for boolean props Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
1 parent 99cae88 commit 835c11e

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

packages/react-devtools-shared/src/devtools/views/Components/EditableValue.css

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
.CheckboxLabel {
2-
flex: 1 1 100%;
3-
display: flex;
4-
}
5-
.CheckboxLabel:focus-within {
6-
background-color: var(--color-button-background-focus);
7-
}
8-
9-
.Checkbox:focus {
10-
outline: none;
1+
.Checkbox {
2+
flex: 0 0 auto;
3+
align-self: center;
4+
margin: 0 0.25rem;
115
}
126

137
.Input {

packages/react-devtools-shared/src/devtools/views/Components/EditableValue.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import * as React from 'react';
11-
import {Fragment, useRef} from 'react';
11+
import {Fragment} from 'react';
1212
import styles from './EditableValue.css';
1313
import {useEditableValue} from '../hooks';
1414

@@ -27,7 +27,6 @@ export default function EditableValue({
2727
path,
2828
value,
2929
}: EditableValueProps) {
30-
const inputRef = useRef<HTMLInputElement | null>(null);
3130
const [state, dispatch] = useEditableValue(value);
3231
const {editableValue, hasPendingChanges, isValid, parsedValue} = state;
3332

@@ -44,6 +43,21 @@ export default function EditableValue({
4443
externalValue: value,
4544
});
4645

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+
4761
const handleKeyDown = event => {
4862
// Prevent keydown events from e.g. change selected element in the tree
4963
event.stopPropagation();
@@ -73,6 +87,8 @@ export default function EditableValue({
7387
placeholder = 'Enter valid JSON';
7488
}
7589

90+
const isBool = parsedValue === true || parsedValue === false;
91+
7692
return (
7793
<Fragment>
7894
<input
@@ -82,10 +98,17 @@ export default function EditableValue({
8298
onChange={handleChange}
8399
onKeyDown={handleKeyDown}
84100
placeholder={placeholder}
85-
ref={inputRef}
86101
type="text"
87102
value={editableValue}
88103
/>
104+
{isBool && (
105+
<input
106+
className={styles.Checkbox}
107+
checked={parsedValue}
108+
type="checkbox"
109+
onChange={handleCheckBoxToggle}
110+
/>
111+
)}
89112
</Fragment>
90113
);
91114
}

0 commit comments

Comments
 (0)