@@ -256,6 +256,8 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
256256} ) => {
257257 const { getStyles } = useTheme ( )
258258
259+ if ( typeof value !== 'boolean' ) return null
260+
259261 return isEditing ? (
260262 < input
261263 className = "jer-input-boolean"
@@ -286,27 +288,44 @@ export const BooleanValue: React.FC<InputProps & { value: boolean }> = ({
286288 )
287289}
288290
289- // A custom hook to add a keyboard listener to a component that does 't have
291+ // A custom hook to add a keyboard listener to a component that doesn 't have
290292// standard DOM keyboard behaviour (like inputs). Only used for the `null`
291293// component here, but is exported for re-use with Custom Components if required
292294export const useKeyboardListener = ( isEditing : boolean , listener : ( e : unknown ) => void ) => {
293295 const timer = useRef < number | undefined > ( undefined )
296+ const currentListener = useRef ( listener )
294297
298+ // Always update the ref to point to the latest listener
295299 useEffect ( ( ) => {
296- if ( ! isEditing ) {
297- // The listener messes with other elements when switching rapidly (e.g.
298- // when "getNext" is called repeatedly on inaccessible elements), so we
299- // cancel the listener load before it even happens if this node gets
300- // switched from isEditing to not in less than 100ms
301- window . clearTimeout ( timer . current )
302- return
303- }
300+ currentListener . current = listener
301+ } , [ listener ] )
302+
303+ // Define our stable event handler function
304+ const eventHandler = ( e : unknown ) => {
305+ currentListener . current ( e )
306+ }
307+
308+ useEffect ( ( ) => {
309+ // The listener messes with other elements when switching rapidly (e.g. when
310+ // "getNext" is called repeatedly on inaccessible elements), so we cancel
311+ // the listener load before it even happens if this node gets switched from
312+ // isEditing to not in less than 100ms
313+ window . clearTimeout ( timer . current )
314+
315+ if ( ! isEditing ) return
316+
304317 // Small delay to prevent registering keyboard input from previous element
305318 // if switched using "Tab"
306- timer . current = window . setTimeout ( ( ) => window . addEventListener ( 'keydown' , listener ) , 100 )
319+ timer . current = window . setTimeout ( ( ) => {
320+ window . addEventListener ( 'keydown' , eventHandler )
321+ } , 100 )
307322
308- return ( ) => window . removeEventListener ( 'keydown' , listener )
309- } , [ isEditing , listener ] )
323+ // Cleanup function
324+ return ( ) => {
325+ window . clearTimeout ( timer . current )
326+ window . removeEventListener ( 'keydown' , eventHandler )
327+ }
328+ } , [ isEditing ] )
310329}
311330
312331export const NullValue : React . FC < InputProps > = ( {
0 commit comments