Skip to content

Commit 04e94de

Browse files
authored
feat(ui5-avatar-group): add accessibleName and accessibleNameRef (#11699)
- Add accessibleName and accessibleNameRef properties - Add _ariaLabelledBy getter for aria-labelledby support - Update _ariaLabelText getter to handle custom accessible names - Add Cypress tests
1 parent 972fddf commit 04e94de

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

packages/main/cypress/specs/AvatarGroup.cy.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,62 @@ describe("Avatars", () => {
6464
});
6565
});
6666
});
67+
68+
describe("Accessibility", () => {
69+
it("checks if accessibleName is properly set and applied as aria-label", () => {
70+
const customLabel = "Development Team Members";
71+
72+
cy.mount(<AvatarGroup id="ag" accessible-name={customLabel}>
73+
<Avatar initials="JD"></Avatar>
74+
<Avatar initials="SM"></Avatar>
75+
<Avatar initials="KL"></Avatar>
76+
</AvatarGroup>);
77+
78+
cy.get("#ag")
79+
.should("have.attr", "accessible-name", customLabel)
80+
.then(($el) => {
81+
const avatarGroup = $el.get(0) as AvatarGroup;
82+
expect(avatarGroup.accessibleName).to.equal(customLabel);
83+
});
84+
85+
cy.get("#ag")
86+
.shadow()
87+
.find(".ui5-avatar-group-items")
88+
.should("have.attr", "aria-label", customLabel)
89+
.and("not.have.attr", "aria-labelledby");
90+
});
91+
92+
it("checks if accessibleNameRef is properly set and applied as aria-label", () => {
93+
const labelId = "team-header";
94+
95+
cy.mount(
96+
<>
97+
<h3 id={labelId}>Quality Assurance Team</h3>
98+
<AvatarGroup id="ag" accessible-name-ref={labelId}>
99+
<Avatar initials="AB"></Avatar>
100+
<Avatar initials="CD"></Avatar>
101+
<Avatar initials="EF"></Avatar>
102+
</AvatarGroup>
103+
</>
104+
);
105+
106+
cy.get("#ag")
107+
.should("have.attr", "accessible-name-ref", labelId)
108+
.then(($el) => {
109+
const avatarGroup = $el.get(0) as AvatarGroup;
110+
expect(avatarGroup.accessibleNameRef).to.equal(labelId);
111+
});
112+
113+
cy.get("#ag")
114+
.shadow()
115+
.find(".ui5-avatar-group-items")
116+
.should("have.attr", "aria-label", "Quality Assurance Team");
117+
118+
cy.get("#ag")
119+
.shadow()
120+
.find(".ui5-avatar-group-items")
121+
.should("not.have.attr", "aria-labelledby");
122+
123+
cy.get(`#${labelId}`).should("exist");
124+
});
125+
});

packages/main/src/AvatarGroup.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import AvatarGroupCss from "./generated/themes/AvatarGroup.css.js";
3636

3737
// Template
3838
import AvatarGroupTemplate from "./AvatarGroupTemplate.js";
39+
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
3940

4041
/**
4142
* Interface for components that represent an avatar and may be slotted in numerous higher-order components such as `ui5-avatar-group`
@@ -200,6 +201,26 @@ class AvatarGroup extends UI5Element {
200201
@property({ noAttribute: true })
201202
_overflowButtonText?: string;
202203

204+
/**
205+
* Defines the accessible name of the AvatarGroup.
206+
* When provided, this will override the default aria-label text.
207+
* @default undefined
208+
* @public
209+
* @since 2.12.0
210+
*/
211+
@property()
212+
accessibleName?: string;
213+
214+
/**
215+
* Receives id(s) of the elements that describe the AvatarGroup.
216+
* When provided, this will be used as aria-labelledby instead of aria-label.
217+
* @default undefined
218+
* @public
219+
* @since 2.12.0
220+
*/
221+
@property()
222+
accessibleNameRef?: string;
223+
203224
/**
204225
* Defines the items of the component. Use the `ui5-avatar` component as an item.
205226
*
@@ -265,6 +286,10 @@ class AvatarGroup extends UI5Element {
265286
}
266287

267288
get _ariaLabelText() {
289+
if (this.accessibleName || this.accessibleNameRef) {
290+
return getEffectiveAriaLabelText(this);
291+
}
292+
// Fallback to existing default behavior
268293
const hiddenItemsCount = this.hiddenItems.length;
269294
const typeLabelKey = this._isGroup ? AVATAR_GROUP_ARIA_LABEL_GROUP : AVATAR_GROUP_ARIA_LABEL_INDIVIDUAL;
270295

0 commit comments

Comments
 (0)