Skip to content

Commit

Permalink
only toggle for ButtonCheckbox if focused
Browse files Browse the repository at this point in the history
  • Loading branch information
Connoropolous committed Nov 9, 2023
1 parent e546713 commit 592c57e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions web/src/components/ButtonCheckbox/ButtonCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React, { useEffect, useState } from 'react'
import Checkbox from '../Checkbox/Checkbox'

import './ButtonCheckbox.scss'
Expand All @@ -18,6 +18,7 @@ const ButtonCheckbox: React.FC<ButtonCheckboxProps> = ({
icon,
text,
}) => {
const [isFocused, setIsFocused] = useState(false)
useEffect(() => {
// listen for Enter key to be pressed, but
// ignore if Command/Ctrl is also pressed
Expand All @@ -26,11 +27,15 @@ const ButtonCheckbox: React.FC<ButtonCheckboxProps> = ({
onChange(!isChecked)
}
}
document.addEventListener('keydown', handleKeyDown)
if (isFocused) {
document.addEventListener('keydown', handleKeyDown)
}
return () => {
document.removeEventListener('keydown', handleKeyDown)
if (isFocused) {
document.removeEventListener('keydown', handleKeyDown)
}
}
}, [isChecked, onChange])
}, [isFocused, isChecked, onChange])
return (
<div
role="button"
Expand All @@ -45,6 +50,8 @@ const ButtonCheckbox: React.FC<ButtonCheckboxProps> = ({
: ''
}`}
onClick={() => onChange(!isChecked)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
>
<div className="button-checkbox-icon-text">
<div className="button-checkbox-icon">{icon}</div>
Expand Down

0 comments on commit 592c57e

Please sign in to comment.