From 8a7d3d5a40f360b5bb3314cc0cae66277151427b Mon Sep 17 00:00:00 2001 From: JesmoDev <26099018+JesmoDev@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:21:10 +0200 Subject: [PATCH] fix(uui-radio): keyboard navigation does not work as intended (#870) * remove tabindex * fix wrapping keyboard nav * Improve keyboard navigation by emulating native radio group behavior * remove unused code * fix double keypress issue * cleanup * fix selection bug * rename function * change focus outline functionality to pure css * remove aria hidden as it is not needed anymore * remove redundant get,set * cleanup radio * cleanup * fix findRadio function to fix focus issues * remove keypress eventListener * Fix pre checked radios * fix test to use same keydown event as the component * make sonar happy * fix tab navigation when there is a pre-checked radio --- .../uui-radio/lib/uui-radio-group.element.ts | 315 +++++++++--------- .../uui-radio/lib/uui-radio-group.test.ts | 2 +- packages/uui-radio/lib/uui-radio.element.ts | 93 ++---- 3 files changed, 182 insertions(+), 228 deletions(-) diff --git a/packages/uui-radio/lib/uui-radio-group.element.ts b/packages/uui-radio/lib/uui-radio-group.element.ts index 78ff05dec..6cff562be 100644 --- a/packages/uui-radio/lib/uui-radio-group.element.ts +++ b/packages/uui-radio/lib/uui-radio-group.element.ts @@ -12,6 +12,7 @@ const ARROW_UP = 'ArrowUp'; const ARROW_RIGHT = 'ArrowRight'; const ARROW_DOWN = 'ArrowDown'; const SPACE = ' '; +const ENTER = 'Enter'; /** * @element uui-radio-group @@ -49,22 +50,21 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') { } set value(newValue) { super.value = newValue; - if (!newValue || newValue === '') { - this._makeFirstEnabledFocusable(); - } - this._updateRadioElementsCheckedState(newValue as string); + this.#updateRadioElementsCheckedState(newValue as string); } - private _selected: number | null = null; + #selected: number | null = null; + #radioElements: UUIRadioElement[] = []; constructor() { super(); - this.addEventListener('keydown', this._onKeydown); - this.addEventListener('keypress', this._onKeypress); + this.addEventListener('keydown', this.#onKeydown); + this.addEventListener('focusin', this.#onFocusIn); + this.addEventListener('focusout', this.#onFocusOut); // Wait for the radio elements to be added to the dom before updating the checked state. this.updateComplete.then(() => { - this._updateRadioElementsCheckedState(this.value as string); + this.#updateRadioElementsCheckedState(this.value as string); }); } @@ -73,111 +73,165 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') { if (!this.hasAttribute('role')) this.setAttribute('role', 'radiogroup'); } + updated(_changedProperties: Map) { + super.updated(_changedProperties); + if (_changedProperties.has('disabled')) { + this.#setDisableOnRadios(this.disabled); + } + + if (_changedProperties.has('readonly')) { + this.#setReadonlyOnRadios(this.readonly); + } + + if (_changedProperties.has('name')) { + this.#setNameOnRadios(_changedProperties.get('name') as string); + } + } + /** * This method enables