Skip to content

Fix null switch bug #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ This component is heavily inspired by [react-json-view](https://github.com/mac-s

## Changelog

- **1.26.1**: Fix bug when submitting with keyboard after switching to `null` type ([#194](https://github.com/CarlosNZ/json-edit-react/pull/194))
- **1.26.0**:
- Handle non-standard data types (e.g. `undefined`, `BigInt`) when stringifying/parsing JSON
- More custom components (See [library ReadMe](https://github.com/CarlosNZ/json-edit-react/blob/main/custom-component-library/README.md))
Expand Down
43 changes: 31 additions & 12 deletions src/ValueNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
}) => {
const { getStyles } = useTheme()

if (typeof value !== 'boolean') return null

return isEditing ? (
<input
className="jer-input-boolean"
Expand Down Expand Up @@ -286,27 +288,44 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
)
}

// A custom hook to add a keyboard listener to a component that does't have
// A custom hook to add a keyboard listener to a component that doesn't have
// standard DOM keyboard behaviour (like inputs). Only used for the `null`
// component here, but is exported for re-use with Custom Components if required
export const useKeyboardListener = (isEditing: boolean, listener: (e: unknown) => void) => {
const timer = useRef<number | undefined>(undefined)
const currentListener = useRef(listener)

// Always update the ref to point to the latest listener
useEffect(() => {
if (!isEditing) {
// The listener messes with other elements when switching rapidly (e.g.
// when "getNext" is called repeatedly on inaccessible elements), so we
// cancel the listener load before it even happens if this node gets
// switched from isEditing to not in less than 100ms
window.clearTimeout(timer.current)
return
}
currentListener.current = listener
}, [listener])

// Define our stable event handler function
const eventHandler = (e: unknown) => {
currentListener.current(e)
}

useEffect(() => {
// The listener messes with other elements when switching rapidly (e.g. when
// "getNext" is called repeatedly on inaccessible elements), so we cancel
// the listener load before it even happens if this node gets switched from
// isEditing to not in less than 100ms
window.clearTimeout(timer.current)

if (!isEditing) return

// Small delay to prevent registering keyboard input from previous element
// if switched using "Tab"
timer.current = window.setTimeout(() => window.addEventListener('keydown', listener), 100)
timer.current = window.setTimeout(() => {
window.addEventListener('keydown', eventHandler)
}, 100)

return () => window.removeEventListener('keydown', listener)
}, [isEditing, listener])
// Cleanup function
return () => {
window.clearTimeout(timer.current)
window.removeEventListener('keydown', eventHandler)
}
}, [isEditing])
}

export const NullValue: React.FC<InputProps> = ({
Expand Down