Skip to content

Commit

Permalink
For Tooltips, prevent emitting events to child elements if they are d…
Browse files Browse the repository at this point in the history
…isabled. (#35254)

Fixes #30422
  • Loading branch information
desaiuditd authored Oct 14, 2021
1 parent 54d5faa commit 21c9f04
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/components/src/tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ const emitToChild = ( children, eventName, event ) => {
}

const child = Children.only( children );

// If the underlying element is disabled, do not emit the event.
if ( child.props.disabled ) {
return;
}

if ( typeof child.props[ eventName ] === 'function' ) {
child.props[ eventName ]( event );
}
Expand Down
34 changes: 34 additions & 0 deletions packages/components/src/tooltip/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* External dependencies
*/
import styled from '@emotion/styled';
import { text, select, number } from '@storybook/addon-knobs';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -41,3 +47,31 @@ export const _default = () => {
</Tooltip>
);
};

const Button = styled.button`
margin: 0 10px;
`;

export const DisabledElement = () => {
const [ showMessage, toggleMessage ] = useState( false );

return (
<>
<Tooltip text="Hey, I am tooltip" position="bottom center">
<Button onClick={ () => toggleMessage( ! showMessage ) }>
Hover me!
</Button>
</Tooltip>
<Tooltip text="Hey, I am tooltip" position="bottom center">
<Button
disabled
onClick={ () => toggleMessage( ! showMessage ) }
>
Hover me, but I am disabled
</Button>
</Tooltip>
<br />
{ showMessage ? <p>Hello World!</p> : null }
</>
);
};
20 changes: 20 additions & 0 deletions packages/components/src/tooltip/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ describe( 'Tooltip', () => {
}, TOOLTIP_DELAY );
} );

it( 'should not emit events back to children when they are disabled', () => {
const handleClick = jest.fn();

const wrapper = mount(
<Tooltip text="Show helpful text here">
<button disabled onClick={ handleClick }>
Click me
</button>
</Tooltip>
);

const eventCatcher = wrapper.find( '.event-catcher' );
eventCatcher.simulate( 'click', {
type: 'click',
currentTarget: {},
} );

expect( handleClick ).toHaveBeenCalledTimes( 0 );
} );

it( 'should cancel pending setIsOver on mouseleave', () => {
// Mount: Issues with using `setState` asynchronously with shallow-
// rendered components: https://github.com/airbnb/enzyme/issues/450
Expand Down

0 comments on commit 21c9f04

Please sign in to comment.