Skip to content

Commit 4402f71

Browse files
committed
Squashed commit of the following:
commit 07f051f Author: Carl Smith <5456533+CarlosNZ@users.noreply.github.com> Date: Tue Jul 23 14:39:30 2024 +1200 Improve keyboard handler for Boolean and Null
1 parent 653785e commit 4402f71

File tree

2 files changed

+16
-52
lines changed

2 files changed

+16
-52
lines changed

src/ValueNodeWrapper.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ const getInputComponent = (dataType: DataType, inputProps: InputProps) => {
358358
return <BooleanValue {...inputProps} value={value as boolean} />
359359
case 'null':
360360
return <NullValue {...inputProps} />
361-
case 'object':
362-
return <ObjectValue {...inputProps} value={value} />
363-
case 'array':
364-
return <ArrayValue {...inputProps} />
365361
default:
366362
return <InvalidValue {...inputProps} />
367363
}

src/ValueNodes.tsx

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
139139
}) => {
140140
const { getStyles } = useTheme()
141141

142-
useKeyboardListener(isEditing, handleEdit, handleCancel)
142+
const handleKeyPress = (e: React.KeyboardEvent) => {
143+
if (e.key === 'Enter') handleEdit()
144+
else if (e.key === 'Escape') handleCancel()
145+
}
143146

144147
return isEditing ? (
145148
<input
@@ -148,6 +151,8 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
148151
name={toPathString(path)}
149152
checked={value}
150153
onChange={() => setValue(!value)}
154+
onKeyDown={handleKeyPress}
155+
autoFocus
151156
/>
152157
) : (
153158
<span
@@ -170,53 +175,6 @@ export const NullValue: React.FC<InputProps> = ({
170175
}) => {
171176
const { getStyles } = useTheme()
172177

173-
useKeyboardListener(isEditing, handleEdit, handleCancel)
174-
175-
return (
176-
<div
177-
onDoubleClick={() => setIsEditing(true)}
178-
className="jer-value-null"
179-
style={getStyles('null', nodeData)}
180-
>
181-
{String(value)}
182-
</div>
183-
)
184-
}
185-
186-
export const ObjectValue: React.FC<InputProps> = ({
187-
value,
188-
translate,
189-
isEditing,
190-
handleEdit,
191-
handleCancel,
192-
nodeData,
193-
}) => {
194-
useKeyboardListener(isEditing, handleEdit, handleCancel)
195-
196-
return (
197-
<span className="jer-value-object">{`{${translate('DEFAULT_NEW_KEY', nodeData)}: "${String(
198-
value
199-
)}" }`}</span>
200-
)
201-
}
202-
203-
export const ArrayValue: React.FC<InputProps> = ({
204-
value,
205-
isEditing,
206-
handleEdit,
207-
handleCancel,
208-
}) => {
209-
useKeyboardListener(isEditing, handleEdit, handleCancel)
210-
211-
return <span className="jer-value-array">{`[${value === null ? '' : String(value)}]`}</span>
212-
}
213-
214-
// Used for inputs that don't naturally register keyboard events
215-
const useKeyboardListener = (
216-
isEditing: boolean,
217-
handleEdit: () => void,
218-
handleCancel: () => void
219-
) => {
220178
useEffect(() => {
221179
if (isEditing) document.addEventListener('keydown', listenForSubmit)
222180
return () => document.removeEventListener('keydown', listenForSubmit)
@@ -227,6 +185,16 @@ const useKeyboardListener = (
227185
handleEdit()
228186
} else if (event.key === 'Escape') handleCancel()
229187
}
188+
189+
return (
190+
<div
191+
onDoubleClick={() => setIsEditing(true)}
192+
className="jer-value-null"
193+
style={getStyles('null', nodeData)}
194+
>
195+
{String(value)}
196+
</div>
197+
)
230198
}
231199

232200
export const InvalidValue: React.FC<InputProps> = ({ value }) => {

0 commit comments

Comments
 (0)