Skip to content

Commit

Permalink
[DataGrid] Stop checkbox ripple on blur (#3835)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw authored Feb 11, 2022
1 parent 996f727 commit b12b22b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const GridCellCheckboxForwardRef = React.forwardRef<HTMLInputElement, GridCellPa
const classes = useUtilityClasses(ownerState);
const checkboxElement = React.useRef<HTMLInputElement | null>(null);

const rippleRef = React.useRef<any>();
const handleRef = useForkRef(checkboxElement, ref);
const element = apiRef.current.getCellElement(id, field);

Expand All @@ -61,9 +62,12 @@ const GridCellCheckboxForwardRef = React.forwardRef<HTMLInputElement, GridCellPa
}, [element, tabIndex]);

React.useLayoutEffect(() => {
if (hasFocus && checkboxElement.current) {
const input = checkboxElement.current.querySelector('input')!;
input!.focus();
if (hasFocus) {
const input = checkboxElement.current?.querySelector('input');
input?.focus();
} else if (rippleRef.current) {
// Only available in @mui/material v5.4.1 or later
rippleRef.current.stop({});
}
}, [hasFocus]);

Expand Down Expand Up @@ -97,6 +101,7 @@ const GridCellCheckboxForwardRef = React.forwardRef<HTMLInputElement, GridCellPa
inputProps={{ 'aria-label': label }}
onKeyDown={handleKeyDown}
disabled={!isSelectable}
touchRippleRef={rippleRef}
{...rootProps.componentsProps?.baseCheckbox}
{...other}
/>
Expand Down
20 changes: 19 additions & 1 deletion packages/grid/x-data-grid/src/tests/selection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function fireClickEvent(cell: HTMLElement) {
}

describe('<DataGrid /> - Selection', () => {
const { render } = createRenderer();
const { render, clock } = createRenderer();

const defaultData = getData(4, 2);

Expand Down Expand Up @@ -450,6 +450,24 @@ describe('<DataGrid /> - Selection', () => {

expect(getSelectedRowIds()).to.deep.equal([]);
});

describe('ripple', () => {
clock.withFakeTimers();

it('should keep only one ripple visible when navigating between checkboxes', function test() {
if (isJSDOM) {
this.skip(); // JSDOM doesn't fire "blur" when .focus is called in another element
}
render(<TestDataGridSelection checkboxSelection />);
const cell = getCell(1, 1);
fireEvent.mouseUp(cell);
fireEvent.click(cell);
fireEvent.keyDown(cell, { key: 'ArrowLeft' });
fireEvent.keyDown(getCell(1, 0).querySelector('input'), { key: 'ArrowUp' });
clock.runToLast(); // Wait for transition
expect(document.querySelectorAll('.MuiTouchRipple-rippleVisible')).to.have.length(1);
});
});
});

describe('props: isRowSelectable', () => {
Expand Down

0 comments on commit b12b22b

Please sign in to comment.