From 65e81238de549e87e0bcced8df1eaa6663743646 Mon Sep 17 00:00:00 2001 From: Serhii Date: Wed, 19 Feb 2020 20:55:29 +0100 Subject: [PATCH 1/3] init --- packages/material-ui/src/ButtonBase/ButtonBase.js | 9 +++++---- packages/material-ui/src/ButtonBase/ButtonBase.test.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/material-ui/src/ButtonBase/ButtonBase.js b/packages/material-ui/src/ButtonBase/ButtonBase.js index f34c7002808023..dafb2fb9f58474 100644 --- a/packages/material-ui/src/ButtonBase/ButtonBase.js +++ b/packages/material-ui/src/ButtonBase/ButtonBase.js @@ -210,6 +210,9 @@ const ButtonBase = React.forwardRef(function ButtonBase(props, ref) { onKeyDown(event); } + if (event.target === event.currentTarget && isNonNativeButton() && event.key === ' ') { + event.preventDefault(); + } // Keyboard accessibility for non interactive elements if (event.target === event.currentTarget && isNonNativeButton() && event.key === 'Enter') { event.preventDefault(); @@ -240,15 +243,13 @@ const ButtonBase = React.forwardRef(function ButtonBase(props, ref) { // Keyboard accessibility for non interactive elements if ( + onClick && event.target === event.currentTarget && isNonNativeButton() && event.key === ' ' && !event.defaultPrevented ) { - event.preventDefault(); - if (onClick) { - onClick(event); - } + onClick(event); } }); diff --git a/packages/material-ui/src/ButtonBase/ButtonBase.test.js b/packages/material-ui/src/ButtonBase/ButtonBase.test.js index cc7e2e1022fc38..adfa2ff6da3aae 100644 --- a/packages/material-ui/src/ButtonBase/ButtonBase.test.js +++ b/packages/material-ui/src/ButtonBase/ButtonBase.test.js @@ -712,7 +712,7 @@ describe('', () => { expect(onClickSpy.callCount).to.equal(1); // defaultPrevented? - expect(onClickSpy.returnValues[0]).to.equal(true); + expect(onClickSpy.returnValues[0]).to.equal(false); }); it('does not call onClick when a spacebar is released and the default is prevented', () => { From 4e175e6c70ecb491b6dd15a4ad1d0642157cf13d Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Wed, 19 Feb 2020 23:22:15 +0100 Subject: [PATCH 2/3] update test to prevent regressions --- packages/material-ui/src/ButtonBase/ButtonBase.js | 7 ++++--- packages/material-ui/src/ButtonBase/ButtonBase.test.js | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/material-ui/src/ButtonBase/ButtonBase.js b/packages/material-ui/src/ButtonBase/ButtonBase.js index dafb2fb9f58474..82cc86dbbb8011 100644 --- a/packages/material-ui/src/ButtonBase/ButtonBase.js +++ b/packages/material-ui/src/ButtonBase/ButtonBase.js @@ -206,13 +206,14 @@ const ButtonBase = React.forwardRef(function ButtonBase(props, ref) { }); } + if (event.target === event.currentTarget && isNonNativeButton() && event.key === ' ') { + event.preventDefault(); + } + if (onKeyDown) { onKeyDown(event); } - if (event.target === event.currentTarget && isNonNativeButton() && event.key === ' ') { - event.preventDefault(); - } // Keyboard accessibility for non interactive elements if (event.target === event.currentTarget && isNonNativeButton() && event.key === 'Enter') { event.preventDefault(); diff --git a/packages/material-ui/src/ButtonBase/ButtonBase.test.js b/packages/material-ui/src/ButtonBase/ButtonBase.test.js index adfa2ff6da3aae..44554ad943fb55 100644 --- a/packages/material-ui/src/ButtonBase/ButtonBase.test.js +++ b/packages/material-ui/src/ButtonBase/ButtonBase.test.js @@ -680,9 +680,10 @@ describe('', () => { describe('keyboard accessibility for non interactive elements', () => { it('does not call onClick when a spacebar is pressed on the element', () => { + const onKeyDown = spy(event => event.defaultPrevented); const onClickSpy = spy(event => event.defaultPrevented); const { getByRole } = render( - + Hello , ); @@ -694,6 +695,8 @@ describe('', () => { }); expect(onClickSpy.callCount).to.equal(0); + // defaultPrevented? + expect(onKeyDown.returnValues[0]).to.equal(true); }); it('does call onClick when a spacebar is released on the element', () => { From 0222b2f5f61d4ccfefa924bc961dba7f5a52e854 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Thu, 20 Feb 2020 11:30:01 +0100 Subject: [PATCH 3/3] improve test description --- packages/material-ui/src/ButtonBase/ButtonBase.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/material-ui/src/ButtonBase/ButtonBase.test.js b/packages/material-ui/src/ButtonBase/ButtonBase.test.js index 44554ad943fb55..edbf34e11f00da 100644 --- a/packages/material-ui/src/ButtonBase/ButtonBase.test.js +++ b/packages/material-ui/src/ButtonBase/ButtonBase.test.js @@ -679,7 +679,7 @@ describe('', () => { }); describe('keyboard accessibility for non interactive elements', () => { - it('does not call onClick when a spacebar is pressed on the element', () => { + it('does not call onClick when a spacebar is pressed on the element but prevents the default', () => { const onKeyDown = spy(event => event.defaultPrevented); const onClickSpy = spy(event => event.defaultPrevented); const { getByRole } = render(