-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCOM-1330 FilterAccordionHeader - focus accordion header after clear…
… button is pressed. (#2335) * filterAccordionHeader - focus accordion header after clear button is pressed * log changes * add block of tests for FilterAccordionHeader as part of Accordion stuite.
- Loading branch information
Showing
4 changed files
with
193 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,120 @@ | ||
import React from 'react'; | ||
import { injectIntl } from 'react-intl'; | ||
import { useCallback, useRef, useImperativeHandle } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import Icon from '../../Icon'; | ||
import Headline from '../../Headline'; | ||
import IconButton from '../../IconButton'; | ||
import css from '../Accordion.css'; | ||
|
||
class FilterAccordionHeader extends React.Component { | ||
static propTypes = { | ||
autoFocus: PropTypes.bool, | ||
contentId: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
displayClearButton: PropTypes.bool, | ||
displayWhenClosed: PropTypes.element, | ||
displayWhenOpen: PropTypes.element, | ||
headerProps: PropTypes.object, | ||
headingLevel: PropTypes.oneOf([1, 2, 3, 4, 5, 6]), | ||
id: PropTypes.string, | ||
intl: PropTypes.object, | ||
label: PropTypes.oneOfType([PropTypes.element, PropTypes.string]).isRequired, | ||
labelId: PropTypes.string, | ||
name: PropTypes.string, | ||
onClearFilter: PropTypes.func, | ||
onToggle: PropTypes.func, | ||
open: PropTypes.bool, | ||
toggleRef: PropTypes.func, | ||
}; | ||
const FilterAccordionHeader = ({ | ||
autoFocus, | ||
contentId, | ||
disabled, | ||
displayWhenOpen, | ||
displayWhenClosed, | ||
displayClearButton = true, | ||
headingLevel = 3, | ||
headerProps, | ||
id, | ||
label, | ||
labelId, | ||
name, | ||
onClearFilter, | ||
onToggle, | ||
open, | ||
toggleRef: toggleRefProp, | ||
}) => { | ||
const { formatMessage } = useIntl(); | ||
const toggleRef = useRef(null); | ||
useImperativeHandle(toggleRefProp, () => toggleRef.current); | ||
|
||
static defaultProps = { | ||
displayClearButton: true, | ||
headingLevel: 3 | ||
}; | ||
|
||
handleHeaderClick = (e) => { | ||
const handleHeaderClick = useCallback((e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
const { id, label } = this.props; | ||
this.props.onToggle({ id, label }); | ||
} | ||
onToggle({ id, label }); | ||
}, [id, label, onToggle]); | ||
|
||
handleClearButtonClick = () => { | ||
this.props.onClearFilter(this.props.name); | ||
} | ||
const handleClearButtonClick = useCallback(() => { | ||
toggleRef.current?.focus(); | ||
onClearFilter(name); | ||
}, [onClearFilter, name, toggleRef]); | ||
|
||
handleKeyPress = (e) => { | ||
const handleKeyPress = useCallback((e) => { | ||
e.preventDefault(); | ||
if (e.charCode === 13) { // 13 = enter | ||
const { id, label } = this.props; | ||
this.props.onToggle({ id, label }); | ||
onToggle({ id, label }); | ||
} | ||
} | ||
}, [id, label, onToggle]); | ||
|
||
render() { | ||
const { | ||
label, | ||
open, | ||
disabled, | ||
displayWhenOpen, | ||
displayWhenClosed, | ||
displayClearButton, | ||
onClearFilter, | ||
contentId, | ||
headingLevel, | ||
intl: { | ||
formatMessage | ||
}, | ||
headerProps, | ||
labelId, | ||
} = this.props; | ||
const clearButtonVisible = displayClearButton && typeof onClearFilter === 'function'; | ||
const labelPropsId = label?.props?.id; | ||
const labelText = labelPropsId ? formatMessage({ id: labelPropsId }) : label || '-'; | ||
const clearButtonVisible = displayClearButton && typeof onClearFilter === 'function'; | ||
const labelPropsId = label?.props?.id; | ||
const labelText = labelPropsId ? formatMessage({ id: labelPropsId }) : label || '-'; | ||
|
||
return ( | ||
<div className={css.headerWrapper}> | ||
<Headline size="small" tag={`h${headingLevel}`} block={!clearButtonVisible}> | ||
<button | ||
type="button" | ||
tabIndex="0" | ||
onClick={this.handleHeaderClick} | ||
onKeyPress={this.handleKeyPress} | ||
aria-label={`${labelText} filter list`} | ||
aria-controls={contentId} | ||
aria-expanded={open} | ||
disabled={disabled} | ||
className={classNames(css.filterSetHeader, { [css.clearButtonVisible]: clearButtonVisible })} | ||
autoFocus={this.props.autoFocus} | ||
ref={this.props.toggleRef} | ||
id={labelId} | ||
{...headerProps} | ||
> | ||
{ | ||
disabled ? null : | ||
<Icon | ||
iconClassName={css.filterSetHeaderIcon} | ||
size="small" | ||
icon={`caret-${open ? 'up' : 'down'}`} | ||
/> | ||
} | ||
<div className={css.labelArea}> | ||
<div className={css.filterSetLabel}>{label}</div> | ||
</div> | ||
</button> | ||
</Headline> | ||
{ clearButtonVisible ? | ||
<IconButton | ||
data-test-clear-button | ||
size="small" | ||
iconSize="small" | ||
icon="times-circle-solid" | ||
aria-label={`Clear selected filters for "${label}"`} | ||
onClick={this.handleClearButtonClick} | ||
/> : null | ||
} | ||
{open ? displayWhenOpen : displayWhenClosed} | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className={css.headerWrapper}> | ||
<Headline size="small" tag={`h${headingLevel}`} block={!clearButtonVisible}> | ||
<button | ||
type="button" | ||
tabIndex="0" | ||
onClick={handleHeaderClick} | ||
onKeyPress={handleKeyPress} | ||
aria-label={`${labelText} filter list`} | ||
aria-controls={contentId} | ||
aria-expanded={open} | ||
disabled={disabled} | ||
className={classNames(css.filterSetHeader, { [css.clearButtonVisible]: clearButtonVisible })} | ||
autoFocus={autoFocus} | ||
ref={toggleRef} | ||
id={labelId} | ||
{...headerProps} | ||
> | ||
{ | ||
disabled ? null : | ||
<Icon | ||
iconClassName={css.filterSetHeaderIcon} | ||
size="small" | ||
icon={`caret-${open ? 'up' : 'down'}`} | ||
/> | ||
} | ||
<div className={css.labelArea}> | ||
<div className={css.filterSetLabel}>{label}</div> | ||
</div> | ||
</button> | ||
</Headline> | ||
{ clearButtonVisible ? | ||
<IconButton | ||
data-test-clear-button | ||
size="small" | ||
iconSize="small" | ||
icon="times-circle-solid" | ||
aria-label={`Clear selected filters for "${label}"`} | ||
onClick={handleClearButtonClick} | ||
/> : null | ||
} | ||
{open ? displayWhenOpen : displayWhenClosed} | ||
</div> | ||
); | ||
} | ||
|
||
export default injectIntl(FilterAccordionHeader); | ||
FilterAccordionHeader.propTypes = { | ||
autoFocus: PropTypes.bool, | ||
contentId: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
displayClearButton: PropTypes.bool, | ||
displayWhenClosed: PropTypes.element, | ||
displayWhenOpen: PropTypes.element, | ||
headerProps: PropTypes.object, | ||
headingLevel: PropTypes.oneOf([1, 2, 3, 4, 5, 6]), | ||
id: PropTypes.string, | ||
intl: PropTypes.object, | ||
label: PropTypes.oneOfType([PropTypes.element, PropTypes.string]).isRequired, | ||
labelId: PropTypes.string, | ||
name: PropTypes.string, | ||
onClearFilter: PropTypes.func, | ||
onToggle: PropTypes.func, | ||
open: PropTypes.bool, | ||
toggleRef: PropTypes.func, | ||
}; | ||
|
||
export default FilterAccordionHeader; |
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