Skip to content

Commit

Permalink
fix: Deleteable chips forcing tab order
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Nov 13, 2019
1 parent 2633d78 commit 0b160e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/material-ui/src/Chip/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ const Chip = React.forwardRef(function Chip(props, ref) {
onDelete,
onKeyUp,
size = 'medium',
tabIndex = 0,
variant = 'default',
...other
} = props;
Expand Down Expand Up @@ -363,7 +364,7 @@ const Chip = React.forwardRef(function Chip(props, ref) {
className={clsx(classes.deleteIconButton, { [classes.deleteIconButtonSmall]: small })}
onMouseDown={event => event.stopPropagation()}
onClick={handleDeleteIconClick}
tabIndex={clickable ? -1 : 0}
tabIndex={clickable ? -1 : tabIndex}
>
<CancelIcon className={clsx(classes.deleteIcon, customClasses)} viewBox="2 2 20 20" />
</ButtonBase>
Expand Down Expand Up @@ -419,7 +420,7 @@ const Chip = React.forwardRef(function Chip(props, ref) {
className,
)}
aria-disabled={disabled ? true : undefined}
tabIndex={clickable ? 0 : undefined}
tabIndex={clickable ? tabIndex : undefined}
onClick={onClick}
onKeyUp={handleKeyUp}
ref={handleRef}
Expand Down Expand Up @@ -512,6 +513,11 @@ Chip.propTypes = {
* The size of the chip.
*/
size: PropTypes.oneOf(['small', 'medium']),
/**
* Tab index of the element that should be in tab order. By
* using `-1` no Chip will be on tab order.
*/
tabIndex: PropTypes.number,
/**
* The variant to use.
*/
Expand Down
19 changes: 19 additions & 0 deletions packages/material-ui/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ describe('<Chip />', () => {
expect(getByRole('button')).to.have.property('tabIndex', 0);
expect(container.querySelector('#avatar')).to.be.ok;
});

it('should apply user value of tabIndex', () => {
const { container, getByRole } = render(
<Chip
avatar={<Avatar id="avatar">MB</Avatar>}
label="Text Avatar Chip"
onDelete={() => {}}
// eslint-disable-next-line jsx-a11y/tabindex-no-positive
tabIndex={5}
/>,
);

expect(getByRole('button')).to.have.property('tabIndex', 5);
const elementsInTabOrder = Array.from(container.querySelectorAll('[tabIndex]')).filter(
element => element.tabIndex >= 0,
);
expect(elementsInTabOrder).to.have.length(1);
});

it('fires onDelete when clicking the delete button', () => {
const handleDelete = spy();
const { getByRole } = render(
Expand Down

0 comments on commit 0b160e3

Please sign in to comment.