From 4901b053556be2cc9e3257ada7c392999b24a5a1 Mon Sep 17 00:00:00 2001 From: stanislavh Date: Mon, 23 Oct 2023 13:39:48 +0200 Subject: [PATCH] NO-JIRA Add keyboard handler for RadioCard --- .../main/js/components/controls/RadioCard.tsx | 12 +++- .../controls/__tests__/RadioCard-test.tsx | 55 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 server/sonar-web/src/main/js/components/controls/__tests__/RadioCard-test.tsx diff --git a/server/sonar-web/src/main/js/components/controls/RadioCard.tsx b/server/sonar-web/src/main/js/components/controls/RadioCard.tsx index edff2a73e18..a719c21b0c1 100644 --- a/server/sonar-web/src/main/js/components/controls/RadioCard.tsx +++ b/server/sonar-web/src/main/js/components/controls/RadioCard.tsx @@ -20,6 +20,7 @@ import classNames from 'classnames'; import * as React from 'react'; import { FormattedMessage } from 'react-intl'; +import { KeyboardKeys } from '../../helpers/keycodes'; import { translate } from '../../helpers/l10n'; import RecommendedIcon from '../icons/RecommendedIcon'; import './Radio.css'; @@ -55,6 +56,14 @@ export default function RadioCard(props: Props) { noRadio = false, } = props; const isActionable = Boolean(onClick); + const clickHandler = isActionable && !disabled && !selected ? onClick : undefined; + + const keyPressHandler = (event: React.KeyboardEvent) => { + if (event.code === KeyboardKeys.Enter) { + clickHandler?.(); + } + }; + return (
{ + it('renders & handles selection', async () => { + const user = userEvent.setup(); + const onClick = jest.fn(); + renderRadioCard({ onClick }); + + const card = byRole('radio').get(); + + expect(card).toBeInTheDocument(); + expect(byRole('heading', { name: 'body' }).get()).toBeInTheDocument(); + + // Keyboard selection + await user.keyboard('{Tab}'); + await user.keyboard('{Enter}'); + expect(onClick).toHaveBeenCalledTimes(1); + + // Mouse selection + await user.click(card); + expect(onClick).toHaveBeenCalledTimes(2); + }); +}); + +function renderRadioCard(overrides: Partial>) { + return renderComponent( + +

body

+
, + ); +} -- 2.39.5