Skip to content

Commit e9f7d88

Browse files
TeodorTaushanovPetyaMarkovaBogdanova
authored and
PetyaMarkovaBogdanova
committed
feat(ui5-badge): add different design types (#7564)
* feat(ui5-badge): add different design types
1 parent 215e5ca commit e9f7d88

File tree

16 files changed

+1578
-121
lines changed

16 files changed

+1578
-121
lines changed

packages/main/src/Badge.hbs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
<div class="ui5-badge-root">
2-
<slot name="icon"></slot>
1+
{{#if interactive}}
2+
<button class="ui5-badge-root"
3+
title="{{_title}}"
4+
aria-roledescription="{{_roleDescription}}"
5+
aria-description="{{_valueState}}"
6+
@onclick={{_onclick}}
7+
>
8+
{{> content}}
9+
</button>
10+
{{else}}
11+
<div class="ui5-badge-root"
12+
title="{{_title}}">
13+
{{> content}}
14+
</div>
15+
{{/if}}
316

17+
{{#*inline "content"}}
18+
<slot name="icon"></slot>
19+
{{#if this._semanticIconName}}
20+
<ui5-icon class="ui5-badge-semantic-icon" name="{{_semanticIconName}}"></ui5-icon>
21+
{{/if}}
22+
<span class="ui5-hidden-text">{{badgeDescription}}</span>
423
{{#if hasText}}
5-
<label class="ui5-badge-text"><bdi><slot></slot></bdi></label>
24+
<span class="ui5-badge-text"><bdi><slot></slot></bdi></span>
625
{{/if}}
26+
{{/inline}}
27+
28+
29+
730

8-
<span class="ui5-hidden-text">{{badgeDescription}}</span>
9-
</div>

packages/main/src/Badge.ts

Lines changed: 158 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
3+
import event from "@ui5/webcomponents-base/dist/decorators/event.js";
34
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
45
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
56
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
67
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
78
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
89
import willShowContent from "@ui5/webcomponents-base/dist/util/willShowContent.js";
9-
10+
import Icon from "./Icon.js";
11+
import "@ui5/webcomponents-icons/dist/sys-help-2.js";
12+
import "@ui5/webcomponents-icons/dist/sys-enter-2.js";
13+
import "@ui5/webcomponents-icons/dist/error.js";
14+
import "@ui5/webcomponents-icons/dist/alert.js";
15+
import "@ui5/webcomponents-icons/dist/information.js";
16+
import WrappingType from "./types/WrappingType.js";
17+
import BadgeDesign from "./types/BadgeDesign.js";
1018
// Template
1119
import BadgeTemplate from "./generated/templates/BadgeTemplate.lit.js";
1220

13-
import { BADGE_DESCRIPTION } from "./generated/i18n/i18n-defaults.js";
21+
import {
22+
BADGE_DESCRIPTION_BADGE,
23+
BADGE_DESCRIPTION_TAG,
24+
BADGE_ROLE_DESCRIPTION,
25+
BADGE_ERROR,
26+
BADGE_WARNING,
27+
BADGE_SUCCESS,
28+
BADGE_INFORMATION,
29+
} from "./generated/i18n/i18n-defaults.js";
1430

1531
// Styles
1632
import badgeCss from "./generated/themes/Badge.css.js";
@@ -19,14 +35,15 @@ import badgeCss from "./generated/themes/Badge.css.js";
1935
* @class
2036
* <h3 class="comment-api-title">Overview</h3>
2137
*
22-
* The <code>ui5-badge</code> is a small non-interactive component which contains text information and color chosen from a list of predefined color schemes.
23-
* It serves the purpose to attract the user attention to some piece of information (state, quantity, condition, etc.).
38+
* The <code>ui5-badge</code> is a component which serves
39+
* the purpose to attract the user attention to some piece
40+
* of information (state, quantity, condition, etc.).
41+
* It can contain icon and text information, and its design can be chosen from specific design types.
2442
*
2543
* <h3>Usage Guidelines</h3>
2644
* <ul>
27-
* <li>If the text is longer than the width of the component, it doesn’t wrap, it shows ellipsis.</li>
28-
* <li>When truncated, the full text is not visible, therefore, it’s recommended to make more space for longer texts to be fully displayed.</li>
29-
* <li>Colors are not semantic and have no visual representation in High Contrast Black (sap_belize_hcb) theme.</li>
45+
* <li>If the text is longer than the width of the component, it can wrap, or it can show ellipsis, depending on the <code>wrappingType</code> property.</li>
46+
* <li>Colors can be semantic or not semantic.</li>
3047
* </ul>
3148
*
3249
* <h3>ES6 Module Import</h3>
@@ -47,8 +64,31 @@ import badgeCss from "./generated/themes/Badge.css.js";
4764
renderer: litRender,
4865
template: BadgeTemplate,
4966
styles: badgeCss,
67+
dependencies: [
68+
Icon,
69+
],
5070
})
71+
72+
/**
73+
* Fired when the user clicks on an interactive badge.
74+
* <b>Note:</b> The event will be fired if the <code>interactive</code> property is <code>true</code>
75+
* @event sap.ui.webc.main.Badge#click
76+
* @public
77+
* @since 1.20
78+
*/
79+
@event("click")
5180
class Badge extends UI5Element {
81+
/**
82+
* Defines the design type of the component.
83+
* @type {string}
84+
* @name sap.ui.webc.main.Badge.prototype.design
85+
* @defaultvalue "Set3"
86+
* @public
87+
* @since 1.20
88+
*/
89+
@property({ defaultValue: BadgeDesign.Set3 })
90+
design!: `${BadgeDesign}`;
91+
5292
/**
5393
* Defines the color scheme of the component.
5494
* There are 10 predefined schemes.
@@ -63,6 +103,44 @@ class Badge extends UI5Element {
63103
@property({ defaultValue: "1" })
64104
colorScheme!: string;
65105

106+
/**
107+
* Defines if the default state icon is shown.
108+
* @type {boolean}
109+
* @name sap.ui.webc.main.Badge.prototype.hideStateIcon
110+
* @defaultValue false
111+
* @public
112+
* @since 1.20
113+
*/
114+
@property({ type: Boolean })
115+
hideStateIcon!: boolean;
116+
117+
/**
118+
* Defines if the component is interactive (focusable and pressable).
119+
* <br><b>Note:</b> The badge cannot be <code>interactive</code>
120+
* when <code>design</code> property is <code>BadgeDesign.Set3</code>
121+
* @type {boolean}
122+
* @name sap.ui.webc.main.Badge.prototype.interactive
123+
* @defaultValue false
124+
* @public
125+
* @since 1.20
126+
*/
127+
@property({ type: Boolean })
128+
interactive!: boolean;
129+
130+
/**
131+
* Defines how the text of a component will be displayed when there is not enough space.
132+
* <br><b>Note:</b> For option "Normal" the text will wrap and the
133+
* words will not be broken based on hyphenation.
134+
*
135+
* @name sap.ui.webc.main.Badge.prototype.wrappingType
136+
* @type {sap.ui.webc.main.types.WrappingType}
137+
* @defaultvalue "None"
138+
* @public
139+
* @since 1.20
140+
*/
141+
@property({ type: WrappingType, defaultValue: WrappingType.None })
142+
wrappingType!: `${WrappingType}`;
143+
66144
/**
67145
* Defines if the badge has an icon.
68146
* @private
@@ -77,6 +155,13 @@ class Badge extends UI5Element {
77155
@property({ type: Boolean })
78156
_iconOnly!: boolean;
79157

158+
/**
159+
* Defines if the badge has "Tag" design type.
160+
* @private
161+
*/
162+
@property({ type: Boolean })
163+
_isTagDesign!: boolean;
164+
80165
/**
81166
* Defines the text of the component.
82167
* <br><b>Note:</b> Although this slot accepts HTML Elements, it is strongly recommended that you only use text in order to preserve the intended design.
@@ -107,8 +192,28 @@ class Badge extends UI5Element {
107192
}
108193

109194
onBeforeRendering() {
110-
this._hasIcon = this.hasIcon;
195+
this._hasIcon = this.hasIcon || !!this._semanticIconName;
111196
this._iconOnly = this.iconOnly;
197+
this._isTagDesign = this.design !== BadgeDesign.Set3;
198+
}
199+
200+
get _roleDescription() {
201+
return Badge.i18nBundle.getText(BADGE_ROLE_DESCRIPTION);
202+
}
203+
204+
get _valueState() {
205+
switch (this.design) {
206+
case BadgeDesign.Positive:
207+
return Badge.i18nBundle.getText(BADGE_SUCCESS);
208+
case BadgeDesign.Negative:
209+
return Badge.i18nBundle.getText(BADGE_ERROR);
210+
case BadgeDesign.Critical:
211+
return Badge.i18nBundle.getText(BADGE_WARNING);
212+
case BadgeDesign.Information:
213+
return Badge.i18nBundle.getText(BADGE_INFORMATION);
214+
}
215+
216+
return undefined;
112217
}
113218

114219
get hasText() {
@@ -123,8 +228,52 @@ class Badge extends UI5Element {
123228
return this.hasIcon && !this.hasText;
124229
}
125230

231+
get _title() {
232+
return this.title || undefined;
233+
}
234+
126235
get badgeDescription() {
127-
return Badge.i18nBundle.getText(BADGE_DESCRIPTION);
236+
if (this.interactive) {
237+
return undefined;
238+
}
239+
240+
if (this.design === BadgeDesign.Set3) {
241+
return Badge.i18nBundle.getText(BADGE_DESCRIPTION_BADGE);
242+
}
243+
244+
const valueState = this._valueState;
245+
let description = Badge.i18nBundle.getText(BADGE_DESCRIPTION_TAG);
246+
247+
if (valueState) {
248+
description = `${description} ${valueState}`;
249+
}
250+
251+
return description;
252+
}
253+
254+
get _semanticIconName() {
255+
if (this.hideStateIcon || this.hasIcon) {
256+
return null;
257+
}
258+
259+
switch (this.design) {
260+
case BadgeDesign.Neutral:
261+
return "sys-help-2";
262+
case BadgeDesign.Positive:
263+
return "sys-enter-2";
264+
case BadgeDesign.Negative:
265+
return "error";
266+
case BadgeDesign.Critical:
267+
return "alert";
268+
case BadgeDesign.Information:
269+
return "information";
270+
default:
271+
return null;
272+
}
273+
}
274+
275+
_onclick() {
276+
this.fireEvent("click");
128277
}
129278
}
130279

packages/main/src/i18n/messagebundle.properties

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ AVATAR_GROUP_ARIA_LABEL_GROUP=Conjoined avatars.
3232
AVATAR_GROUP_MOVE=Press ARROW keys to move.
3333

3434
#XACT: ARIA announcement for the badge
35-
BADGE_DESCRIPTION=Badge
35+
BADGE_DESCRIPTION_BADGE=Badge
36+
37+
#XACT: ARIA announcement for the badge with "Tag" design
38+
BADGE_DESCRIPTION_TAG=Tag
39+
40+
#XACT: ARIA announcement for the roledescription attribute
41+
BADGE_ROLE_DESCRIPTION=Tag button
42+
43+
#XACT: ARIA announcement for the "Error" state
44+
BADGE_ERROR=Error
45+
46+
#XACT: ARIA announcement for the "Warning" state
47+
BADGE_WARNING=Warning
48+
49+
#XACT: ARIA announcement for the "Success" state
50+
BADGE_SUCCESS=Success
51+
52+
#XACT: ARIA announcement for the "Information" state
53+
BADGE_INFORMATION=Information
3654

3755
#XACT: position (current and max. value) of a Breadcrumb item which should be announced by screenreaders
3856
BREADCRUMB_ITEM_POS={0} of {1}

0 commit comments

Comments
 (0)