-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve usability of checkbox, checkbox group, radio and radio g…
…roup component
- Loading branch information
1 parent
4307427
commit 134c1ab
Showing
17 changed files
with
567 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,79 @@ | ||
import React, { FC, useState } from 'react'; | ||
import { CheckBoxProps } from '../'; | ||
import { mergeClasses, generateId } from '../../../shared/utilities'; | ||
import React, { FC, Ref, useEffect, useRef, useState } from 'react'; | ||
import { generateId, mergeClasses } from '../../../shared/utilities'; | ||
import { CheckboxProps } from './Checkbox.types'; | ||
|
||
import styles from './checkbox.module.scss'; | ||
|
||
export const CheckBox: FC<CheckBoxProps> = ({ | ||
ariaLabel, | ||
checked = false, | ||
defaultChecked, | ||
disabled = false, | ||
name, | ||
value = '', | ||
id, | ||
onChange, | ||
}) => { | ||
const [checkBoxId] = useState<string>(id || generateId()); | ||
const [isChecked, setIsChecked] = useState<boolean>(checked); | ||
|
||
const checkBoxCheckClassNames: string = mergeClasses([ | ||
styles.checkmark, | ||
{ [styles.disabled]: disabled }, | ||
]); | ||
|
||
const toggleChecked = (): void => { | ||
if (!disabled) setIsChecked(!isChecked); | ||
}; | ||
|
||
const handleKeyDown = (event: React.KeyboardEvent) => { | ||
if (event.key !== 'Tab') event.preventDefault(); | ||
if (event.key === 'Enter' || event.key === ' ') toggleChecked(); | ||
}; | ||
|
||
return ( | ||
<div className={styles.selector} onKeyDown={handleKeyDown}> | ||
<input | ||
aria-label={ariaLabel} | ||
checked={isChecked} | ||
defaultChecked={defaultChecked} | ||
disabled={disabled} | ||
id={checkBoxId} | ||
onChange={onChange ? onChange : toggleChecked} | ||
name={name} | ||
tabIndex={-1} | ||
type={'checkbox'} | ||
value={value} | ||
readOnly | ||
/> | ||
<label | ||
htmlFor={checkBoxId} | ||
className={value === '' ? styles.labelNoValue : ''} | ||
export const CheckBox: FC<CheckboxProps> = React.forwardRef( | ||
( | ||
{ | ||
ariaLabel, | ||
checked = false, | ||
defaultChecked, | ||
disabled = false, | ||
name, | ||
value, | ||
id, | ||
onChange, | ||
label, | ||
classNames, | ||
style, | ||
'data-test-id': dataTestId, | ||
}, | ||
ref: Ref<HTMLInputElement> | ||
) => { | ||
const checkBoxId = useRef<string>(id || generateId()); | ||
const [isChecked, setIsChecked] = useState<boolean>( | ||
defaultChecked || checked | ||
); | ||
|
||
useEffect(() => { | ||
setIsChecked(checked); | ||
}, [checked]); | ||
|
||
const checkboxWrapperClassNames: string = mergeClasses([ | ||
styles.selector, | ||
classNames, | ||
]); | ||
|
||
const checkBoxCheckClassNames: string = mergeClasses([ | ||
styles.checkmark, | ||
]); | ||
|
||
const labelClassNames: string = mergeClasses([ | ||
{ [styles.labelNoValue]: value === '' }, | ||
]); | ||
|
||
const toggleChecked = ( | ||
e: React.ChangeEvent<HTMLInputElement> | ||
): void => { | ||
setIsChecked(e.target.checked); | ||
onChange?.(e); | ||
}; | ||
|
||
return ( | ||
<div | ||
className={checkboxWrapperClassNames} | ||
style={style} | ||
data-test-id={dataTestId} | ||
> | ||
<span className={checkBoxCheckClassNames} tabIndex={0}></span> | ||
<span className={styles.selectorLabel}>{value}</span> | ||
</label> | ||
</div> | ||
); | ||
}; | ||
<input | ||
ref={ref} | ||
aria-label={ariaLabel} | ||
checked={isChecked} | ||
disabled={disabled} | ||
id={checkBoxId.current} | ||
onChange={toggleChecked} | ||
name={name} | ||
type={'checkbox'} | ||
value={value} | ||
readOnly | ||
/> | ||
<label htmlFor={checkBoxId.current} className={labelClassNames}> | ||
<span className={checkBoxCheckClassNames} /> | ||
<span className={styles.selectorLabel}>{label}</span> | ||
</label> | ||
</div> | ||
); | ||
} | ||
); |
Oops, something went wrong.