Skip to content

Commit 78a9181

Browse files
authored
refactor(ui5-segmentedbutton): focus selected button (#1208)
- All themes and browsers: readonly textarea does not change its background on hover - Quartz Dark and IE: the textarea has proper dark background, not a white one as used to.
1 parent c047da7 commit 78a9181

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

packages/main/src/SegmentedButton.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div
22
@click="{{_onclick}}"
3+
@focusin="{{_onfocusin}}"
34
class="ui5-segmentedbutton-root"
45
>
56
<slot></slot>

packages/main/src/SegmentedButton.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class SegmentedButton extends UI5Element {
9595

9696
this.absoluteWidthSet = false; // set to true whenever we set absolute width to the component
9797
this.percentageWidthSet = false; // set to true whenever we set 100% width to the component
98+
this.hasPreviouslyFocusedItem = false;
9899
}
99100

100101
onEnterDOM() {
@@ -143,13 +144,30 @@ class SegmentedButton extends UI5Element {
143144
selectedButton: this._selectedButton,
144145
});
145146
}
146-
this._selectedButton.pressed = true;
147147

148+
this._selectedButton.pressed = true;
148149
this._itemNavigation.update(this._selectedButton);
149150

150151
return this;
151152
}
152153

154+
_onfocusin(event) {
155+
// If the component was previously focused,
156+
// update the ItemNavigation to sync butons` tabindex values
157+
if (this.hasPreviouslyFocusedItem) {
158+
this._itemNavigation.update(event.target);
159+
return;
160+
}
161+
162+
// If the component is focused for the first time
163+
// focus the selected item if such present
164+
if (this.selectedButton) {
165+
this.selectedButton.focus();
166+
this._itemNavigation.update(this._selectedButton);
167+
this.hasPreviouslyFocusedItem = true;
168+
}
169+
}
170+
153171
_handleResize() {
154172
const parentWidth = this.parentNode.offsetWidth;
155173

packages/main/test/pages/SegmentedButton.html

+17
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,22 @@ <h1>SegmentedButton wrapped in a container with set width</h1>
8888
</div>
8989
</div>
9090
</section>
91+
92+
<section>
93+
<h3>Initial focus test</h3>
94+
<p>the focus hould go to the first button</p>
95+
<ui5-button id="button1">Press</ui5-button>
96+
<ui5-segmentedbutton id="testSB1">
97+
<ui5-togglebutton id="testSB1ToggleBtn" icon="employee"></ui5-togglebutton>
98+
<ui5-togglebutton icon="menu"></ui5-togglebutton>
99+
<ui5-togglebutton icon="accept"></ui5-togglebutton>
100+
</ui5-segmentedbutton>
101+
<ui5-button id="button2">Press</ui5-button>
102+
<ui5-segmentedbutton id="testSB2">
103+
<ui5-togglebutton icon="employee"></ui5-togglebutton>
104+
<ui5-togglebutton id="testSB2ToggleBtn" icon="menu" pressed></ui5-togglebutton>
105+
<ui5-togglebutton icon="accept"></ui5-togglebutton>
106+
</ui5-segmentedbutton>
107+
</section>
91108
</body>
92109
</html>

packages/main/test/specs/SegmentedButton.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,19 @@ describe("SegmentedButton general interaction", () => {
3232
assert.ok(toggleButton4.getProperty("pressed"), "ToggleButton has property pressed");
3333

3434
});
35+
36+
it("tests initial focus", () => {
37+
const button1 = browser.$("#button1");
38+
const button2 = browser.$("#button2");
39+
const toggleButton1 = browser.$("#testSB1ToggleBtn");
40+
const toggleButton2 = browser.$("#testSB2ToggleBtn");
41+
42+
button1.click();
43+
button1.keys("Tab");
44+
assert.ok(toggleButton1.isFocused(), "The first ToggleButton should be focused.");
45+
46+
button2.click();
47+
button2.keys("Tab");
48+
assert.ok(toggleButton2.isFocused(), "The selected ToggleButton should be focused.");
49+
});
3550
});

0 commit comments

Comments
 (0)