From f4a59275543345c061c45be270c0ea30d24e0a52 Mon Sep 17 00:00:00 2001 From: Stoyan Date: Mon, 1 Sep 2025 13:06:49 +0300 Subject: [PATCH] fix(ui5-dynamic-date-range): focus last selected option, upon navigate back --- packages/main/src/DynamicDateRange.ts | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/main/src/DynamicDateRange.ts b/packages/main/src/DynamicDateRange.ts index 2c584d8d4f02..1b5d45235ab6 100644 --- a/packages/main/src/DynamicDateRange.ts +++ b/packages/main/src/DynamicDateRange.ts @@ -180,6 +180,10 @@ class DynamicDateRange extends UI5Element { @property({ type: Object }) _currentOption?: IDynamicDateRangeOption; + _lastSelectedOption?: IDynamicDateRangeOption; + + _shouldFocusLastSelected = false; + @property({ type: Object }) currentValue?: DynamicDateRangeValue; @@ -198,6 +202,15 @@ class DynamicDateRange extends UI5Element { this._focusSelectedItem(); } + onAfterRendering() { + if (this._shouldFocusLastSelected) { + setTimeout(() => { + this._focusLastSelectedItem(); + }, 0); + this._shouldFocusLastSelected = false; + } + } + /** * Creates and normalizes options from the options string */ @@ -241,6 +254,25 @@ class DynamicDateRange extends UI5Element { } } + _focusLastSelectedItem() { + if (!this._lastSelectedOption) { + return; + } + + // Ensure the list exists and has items + if (!this._list || !this._list.items.length) { + return; + } + + // Find the index of the last selected option in the options array + const optionIndex = this.optionsObjects.findIndex(option => option.operator === this._lastSelectedOption?.operator); + + if (optionIndex >= 0 && optionIndex < this._list.items.length) { + const listItem = this._list.items[optionIndex]; + this._list.focusItem(listItem as ListItem); + } + } + /** * Defines whether the value help icon is hidden * @private @@ -263,6 +295,7 @@ class DynamicDateRange extends UI5Element { _selectOption(e: CustomEvent): void { this._currentOption = this.optionsObjects.find(option => option.text === e.detail.item.textContent); + this._lastSelectedOption = this._currentOption; if (!this._currentOption?.template) { this.currentValue = this._currentOption?.parse(this._currentOption.text); @@ -325,6 +358,7 @@ class DynamicDateRange extends UI5Element { onButtonBackClick() { this._currentOption = undefined; + this._shouldFocusLastSelected = true; } /**