From c79dfd1348a9e8f0be20f5452478387836f6aa94 Mon Sep 17 00:00:00 2001 From: Anders Date: Fri, 31 Mar 2023 13:12:04 +0200 Subject: [PATCH] fix(ToggleButton): fixes error when pressing enter (#2183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ToggleButton): fixes error when pressing enter * Add a test --------- Co-authored-by: Tobias Høegh --- .../src/components/toggle-button/ToggleButton.js | 6 +++--- .../toggle-button/__tests__/ToggleButton.test.tsx | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/dnb-eufemia/src/components/toggle-button/ToggleButton.js b/packages/dnb-eufemia/src/components/toggle-button/ToggleButton.js index 34640d1e2cd..b6bd47d1946 100644 --- a/packages/dnb-eufemia/src/components/toggle-button/ToggleButton.js +++ b/packages/dnb-eufemia/src/components/toggle-button/ToggleButton.js @@ -205,7 +205,7 @@ export default class ToggleButton extends React.PureComponent { onKeyDownHandler = (event) => { switch (keycode(event)) { case 'enter': - this.onClickHandler(event) + this.onClickHandler({ event }) break } } @@ -213,7 +213,7 @@ export default class ToggleButton extends React.PureComponent { onKeyUpHandler = (event) => { switch (keycode(event)) { case 'enter': - this.onClickHandler(event) + this.onClickHandler({ event }) break } } @@ -232,7 +232,7 @@ export default class ToggleButton extends React.PureComponent { return } - // else we change the checked sstate + // else we change the checked state const checked = !this.state.checked this.setState({ checked, diff --git a/packages/dnb-eufemia/src/components/toggle-button/__tests__/ToggleButton.test.tsx b/packages/dnb-eufemia/src/components/toggle-button/__tests__/ToggleButton.test.tsx index 07d81956e97..19e7fab1592 100644 --- a/packages/dnb-eufemia/src/components/toggle-button/__tests__/ToggleButton.test.tsx +++ b/packages/dnb-eufemia/src/components/toggle-button/__tests__/ToggleButton.test.tsx @@ -3,7 +3,7 @@ * */ -import { render } from '@testing-library/react' +import { fireEvent, render } from '@testing-library/react' import React from 'react' import { mount, @@ -235,6 +235,19 @@ describe('ToggleButton component', () => { ) }) + it('should support enter key', () => { + const onChange = jest.fn() + render() + + const element = document.querySelector('button') + + fireEvent.keyDown(element, { keyCode: 13 }) // enter + expect(onChange).toHaveBeenCalledTimes(1) + + fireEvent.keyUp(element, { keyCode: 13 }) // enter + expect(onChange).toHaveBeenCalledTimes(2) + }) + it('should support spacing props', () => { render()