Skip to content

Commit 4ee00b8

Browse files
authored
Customise boolean toggle key (#152)
1 parent 5aaa83f commit 4ee00b8

File tree

5 files changed

+11
-2
lines changed

5 files changed

+11
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ It's pretty self explanatory (click the "edit" icon to edit, etc.), but there ar
105105
- When clicking the "clipboard" icon, holding down `Cmd/Ctrl` will copy the *path* to the selected node rather than its value
106106
- When opening/closing a node, hold down "Alt/Option" to open/close *all* child nodes at once
107107
- For Number inputs, arrow-up and down keys will increment/decrement the value
108+
- For Boolean inputs, space bar will toggle the value
108109
- Drag and drop items to change the structure or modify display order
109110
- JSON text input can accept "looser" input, if an additional JSON parsing method is provided (e.g. [JSON5](https://json5.org/)). See `jsonParse` prop.
110111

@@ -699,6 +700,7 @@ The default keyboard controls are [outlined above](#usage), but it's possible to
699700
numberUp: 'ArrowUp',
700701
numberDown: 'ArrowDown',
701702
booleanConfirm: 'Enter',
703+
booleanToggle: ' ', // Space bar
702704
clipboardModifier: ['Meta', 'Control'],
703705
collapseModifier: 'Alt',
704706
}

demo/src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ function App() {
411411
// clipboardModifier: ['Alt', 'Shift'],
412412
// collapseModifier: 'Control',
413413
// booleanConfirm: 'Enter',
414+
// booleanToggle: 'r',
414415
// }}
415416
// insertAtBeginning="object"
416417
/>

src/ValueNodes.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
141141
name={toPathString(path)}
142142
checked={value}
143143
onChange={() => setValue(!value)}
144-
onKeyDown={(e) =>
144+
onKeyDown={(e) => {
145+
// If we don't explicitly suppress normal checkbox keyboard behaviour,
146+
// the default key (Space) will continue to work even if re-defined
147+
if (e.key === ' ') e.preventDefault()
145148
handleKeyboard(e, {
146149
booleanConfirm: handleEdit,
147150
cancel: handleCancel,
151+
booleanToggle: () => setValue(!value),
148152
})
149-
}
153+
}}
150154
autoFocus
151155
/>
152156
) : (

src/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const defaultKeyboardControls: KeyboardControlsFull = {
203203
numberUp: { key: 'ArrowUp' },
204204
numberDown: { key: 'ArrowDown' },
205205
booleanConfirm: ENTER,
206+
booleanToggle: { key: ' ' },
206207
clipboardModifier: ['Meta', 'Control'],
207208
collapseModifier: ['Alt'],
208209
}

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export interface KeyboardControls {
166166
stringConfirm?: KeyEvent | string
167167
stringLineBreak?: KeyEvent | string // for Value nodes
168168
booleanConfirm?: KeyEvent | string
169+
booleanToggle?: KeyEvent | string
169170
numberConfirm?: KeyEvent | string
170171
numberUp?: KeyEvent | string
171172
numberDown?: KeyEvent | string

0 commit comments

Comments
 (0)