| 1 |
import { Fragment } from 'react'; |
| 2 |
import { Button, KeyTypes } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
const handleKeydown = (event: React.KeyboardEvent<HTMLButtonElement>) => { |
| 5 |
const { key } = event; |
| 6 |
const isEnterKey: boolean = key === KeyTypes.Enter; |
| 7 |
const isEnterOrSpaceKey: boolean = isEnterKey || key === KeyTypes.Space; |
| 8 |
|
| 9 |
if (isEnterOrSpaceKey) { |
| 10 |
event.preventDefault(); |
| 11 |
alert(`${isEnterKey ? 'Enter' : 'Space'} key default behavior stopped by onKeyDown`); |
| 12 |
} |
| 13 |
}; |
| 14 |
|
| 15 |
export const ButtonInlineSpanLink: React.FunctionComponent = () => ( |
| 16 |
<Fragment> |
| 17 |
<p> |
| 18 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 19 |
<Button variant="link" isInline component="span"> |
| 20 |
This is long button text that needs to be a span so that it will wrap inline with the text around it. |
| 21 |
</Button> |
| 22 |
Sed hendrerit nisi in cursus maximus. Ut malesuada nisi turpis, in condimentum velit elementum non. |
| 23 |
</p> |
| 24 |
|
| 25 |
<br /> |
| 26 |
|
| 27 |
<p> |
| 28 |
Note that using a <b>span</b> as a button does not fire the <b>onclick</b> event for Enter or Space keys. |
| 29 |
<Button variant="link" isInline component="span" onKeyDown={handleKeydown}> |
| 30 |
An <b>onKeyDown</b> event listener is needed for Enter and Space key presses to prevent their default behavior |
| 31 |
and trigger your code. |
| 32 |
</Button> |
| 33 |
Pressing the Enter or Space keys on the inline link as span above demonstrates this by triggering an alert. |
| 34 |
</p> |
| 35 |
</Fragment> |
| 36 |
); |