From aee0113382a216861e6c65ca08a15a834150b765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Wed, 25 Oct 2023 21:14:48 +0200 Subject: [PATCH] fix(forms): enhance accessibility by using toggle buttons (#2781) --- .../Field/Boolean/__tests__/Boolean.test.tsx | 29 +++++++- .../extensions/forms/Field/Toggle/Toggle.tsx | 69 +++++++++---------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/packages/dnb-eufemia/src/extensions/forms/Field/Boolean/__tests__/Boolean.test.tsx b/packages/dnb-eufemia/src/extensions/forms/Field/Boolean/__tests__/Boolean.test.tsx index 5573fc03dc9..5c3ad105c06 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Field/Boolean/__tests__/Boolean.test.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Field/Boolean/__tests__/Boolean.test.tsx @@ -219,7 +219,28 @@ describe('Field.Boolean', () => { describe('variant: buttons', () => { it('renders label', () => { render() - expect(screen.getByLabelText('Boolean label')).toBeInTheDocument() + expect(screen.getByText('Boolean label')).toBeInTheDocument() + }) + + it('has no selected value by defualt', () => { + render() + const buttons = document.querySelectorAll('button') + expect(buttons[0].getAttribute('aria-pressed')).toBe('false') + expect(buttons[1].getAttribute('aria-pressed')).toBe('false') + }) + + it('has "false" selected', () => { + render() + const buttons = document.querySelectorAll('button') + expect(buttons[0].getAttribute('aria-pressed')).toBe('false') + expect(buttons[1].getAttribute('aria-pressed')).toBe('true') + }) + + it('has "true" selected', () => { + render() + const buttons = document.querySelectorAll('button') + expect(buttons[0].getAttribute('aria-pressed')).toBe('true') + expect(buttons[1].getAttribute('aria-pressed')).toBe('false') }) it('renders error', () => { @@ -241,8 +262,10 @@ describe('Field.Boolean', () => { error={new Error('This is what went wrong')} /> ) - const element = document.querySelector('.dnb-button') - expect(element.className).toContain('dnb-button__status--error') + const element = document.querySelector('.dnb-toggle-button') + expect(element.className).toContain( + 'dnb-toggle-button__status--error' + ) }) it('should show error when no value is given', () => { diff --git a/packages/dnb-eufemia/src/extensions/forms/Field/Toggle/Toggle.tsx b/packages/dnb-eufemia/src/extensions/forms/Field/Toggle/Toggle.tsx index ac1ab5a9535..71e7e879f14 100644 --- a/packages/dnb-eufemia/src/extensions/forms/Field/Toggle/Toggle.tsx +++ b/packages/dnb-eufemia/src/extensions/forms/Field/Toggle/Toggle.tsx @@ -1,10 +1,5 @@ import React, { useContext, useCallback } from 'react' -import { - Checkbox, - ToggleButton, - Button, - Space, -} from '../../../../components' +import { Checkbox, ToggleButton } from '../../../../components' import classnames from 'classnames' import ButtonRow from '../../Form/ButtonRow' import FieldBlock from '../../FieldBlock' @@ -12,6 +7,7 @@ import { useDataValue } from '../../hooks' import { FieldProps } from '../../types' import { pickSpacingProps } from '../../../../components/flex/utils' import SharedContext from '../../../../shared/Context' +import ToggleButtonGroupContext from '../../../../components/toggle-button/ToggleButtonGroupContext' export type Props = FieldProps & { valueOn: unknown @@ -49,18 +45,13 @@ function Toggle(props: Props) { }, [handleChange, valueOn, valueOff] ) - - const setOn = useCallback(() => { - if (value !== valueOn) { - handleChange?.(valueOn) - } - }, [handleChange, value, valueOn]) - - const setOff = useCallback(() => { - if (value !== valueOff) { - handleChange?.(valueOff) - } - }, [handleChange, value, valueOff]) + const handleToggleChange = useCallback( + ({ value }) => { + console.log('value', value) + handleChange?.(value === 'on' ? valueOn : valueOff) + }, + [handleChange, valueOn, valueOff] + ) const cn = classnames('dnb-forms-field-toggle', className) @@ -121,26 +112,30 @@ function Toggle(props: Props) { ) case 'buttons': return ( - - -