Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chip] Add skipFocusWhenDisabled prop to not allow focussing deletable chip if disabled #35065

Merged
merged 15 commits into from Nov 25, 2022
Merged
4 changes: 4 additions & 0 deletions packages/mui-material/src/Chip/Chip.d.ts
Expand Up @@ -76,6 +76,10 @@ export interface ChipTypeMap<P = {}, D extends React.ElementType = 'div'> {
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
/**
* tab order of an element
*/
tabIndex?: number;
/**
* The variant to use.
* @default 'filled'
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-material/src/Chip/Chip.js
Expand Up @@ -346,6 +346,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
onKeyUp,
size = 'medium',
variant = 'filled',
tabIndex,
...other
} = props;

Expand Down Expand Up @@ -460,6 +461,7 @@ const Chip = React.forwardRef(function Chip(inProps, ref) {
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
ref={handleRef}
tabIndex={disabled ? tabIndex ?? -1 : tabIndex}
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
ownerState={ownerState}
{...moreProps}
{...other}
Expand Down Expand Up @@ -569,6 +571,10 @@ Chip.propTypes /* remove-proptypes */ = {
PropTypes.func,
PropTypes.object,
]),
/**
* tab order of an element
*/
tabIndex: PropTypes.number,
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
/**
* The variant to use.
* @default 'filled'
Expand Down
12 changes: 12 additions & 0 deletions packages/mui-material/src/Chip/Chip.test.js
Expand Up @@ -174,6 +174,18 @@ describe('<Chip />', () => {
expect(chip).to.have.class(classes.filledPrimary);
});

it('should not be focused when chip is disabled', () => {
const { getByTestId } = render(<Chip label="My Chip" disabled data-testid="chip" />);

const chip = getByTestId('chip');

fireEvent.keyDown(document.body, { key: 'Tab' });

expect(chip).to.have.class(classes.root);
expect(chip).to.have.property('tabIndex', -1);
expect(chip).not.to.have.class(classes.focusVisible);
});

it('should render with the root and filled clickable secondary class', () => {
const { getByRole } = render(
<Chip color="secondary" label="My Chip" onClick={() => {}} variant="filled" />,
Expand Down