forked from EightfoldAI/octuple
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (EightfoldAI#156)
- Loading branch information
1 parent
b2a3812
commit 69650ac
Showing
19 changed files
with
753 additions
and
586 deletions.
There are no files selected for viewing
Git LFS file not shown
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
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> | ||
); | ||
} | ||
); |
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,26 +1,47 @@ | ||
import React, { FC } from 'react'; | ||
import { CheckBoxProps } from '../'; | ||
import React, { FC, Ref } from 'react'; | ||
import { CheckBox } from './CheckBox'; | ||
import { generateId } from '../../../shared/utilities'; | ||
import { mergeClasses } from '../../../shared/utilities'; | ||
import { CheckboxGroupProps } from './Checkbox.types'; | ||
|
||
export const CheckBoxGroup: FC<CheckBoxProps> = ({ | ||
defaultChecked = true, | ||
items, | ||
onChange, | ||
}) => { | ||
return ( | ||
<> | ||
{items.map((item, index) => ( | ||
<CheckBox | ||
ariaLabel={item.ariaLabel} | ||
checked={item.checked ? item.checked : defaultChecked} | ||
id={item.id || generateId()} | ||
key={index} | ||
name={item.name} | ||
value={item.value} | ||
onChange={onChange} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
import styles from './checkbox.module.scss'; | ||
|
||
export const CheckBoxGroup: FC<CheckboxGroupProps> = React.forwardRef( | ||
( | ||
{ items = [], onChange, value, classNames, style, ariaLabel, ...rest }, | ||
ref: Ref<HTMLDivElement> | ||
) => { | ||
const checkboxGroupClassNames = mergeClasses([ | ||
styles.checkboxGroup, | ||
classNames, | ||
]); | ||
|
||
return ( | ||
<div | ||
className={checkboxGroupClassNames} | ||
style={style} | ||
role="group" | ||
aria-label={ariaLabel} | ||
ref={ref} | ||
{...rest} | ||
> | ||
{items.map((item) => ( | ||
<CheckBox | ||
{...item} | ||
checked={value?.includes(item.value)} | ||
key={item.value} | ||
onChange={() => { | ||
const optionIndex = value?.indexOf(item.value); | ||
const newValue = [...value]; | ||
if (optionIndex === -1) { | ||
newValue.push(item.value); | ||
} else { | ||
newValue.splice(optionIndex, 1); | ||
} | ||
onChange?.(newValue); | ||
}} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} | ||
); |
Oops, something went wrong.