@@ -11,16 +11,41 @@ import { query } from 'lit/decorators/query.js';
1111import { queryAssignedNodes } from 'lit/decorators/query-assigned-nodes.js' ;
1212import { observes } from '@patternfly/pfe-core/decorators/observes.js' ;
1313import { RovingTabindexController } from '@patternfly/pfe-core/controllers/roving-tabindex-controller.js' ;
14- import { PfLabel } from '.. /pf-label/pf-label.js' ;
14+ import type { PfLabel } from '@patternfly/elements /pf-label/pf-label.js' ;
1515
16+ /**
17+ * Event fired when the label group is expanded to show all labels.
18+ */
1619export class PfLabelGroupExpandEvent extends Event {
1720 constructor ( ) {
1821 super ( 'expand' , { bubbles : true , cancelable : true } ) ;
1922 }
2023}
2124
25+ /**
26+ * Regex used to replace the `${remaining}` variable in collapsed text.
27+ */
2228const REMAINING_RE = / \$ \{ \s * r e m a i n i n g \s * \} / g;
2329
30+ /**
31+ * A **label group** is a collection of labels that can be grouped by category
32+ * and used to represent one or more values assigned to a single attribute.
33+ * When the number of labels exceeds `numLabels`, additional labels will be hidden
34+ * using an overflow label.
35+ *
36+ * @fires expand - Fired when label group is expanded to show all labels
37+ *
38+ * @slot category-name - Category name text for label group category. If supplied, label group will have category styling applied
39+ * @slot - Default slot for `<pf-label>` elements
40+ *
41+ * @example
42+ * <pf-label-group num-labels="2 ">
43+ * <span slot="category-name">Fruit Types</span>
44+ * <pf-label>Apple</pf-label>
45+ * <pf-label>Banana</pf-label>
46+ * <pf-label>Orange</pf-label>
47+ * </pf-label-group>
48+ */
2449@customElement ( 'pf-label-group' )
2550export class PfLabelGroup extends LitElement {
2651 static readonly styles : CSSStyleSheet [ ] = [ styles ] ;
@@ -30,25 +55,64 @@ export class PfLabelGroup extends LitElement {
3055 delegatesFocus : true ,
3156 } ;
3257
58+ /**
59+ * Orientation of the label group.
60+ * @default 'horizontal'
61+ */
3362 @property ( ) orientation : 'horizontal' | 'vertical' = 'horizontal' ;
63+
64+ /**
65+ * Accessible label for the label group when no category name is provided.
66+ * @default ''
67+ */
3468 @property ( { attribute : 'accessible-label' , type : String } ) accessibleLabel = '' ;
69+
70+ /**
71+ * Accessible label for the close button.
72+ * @default 'Close'
73+ */
3574 @property ( { attribute : 'accessible-close-label' , type : String } ) accessibleCloseLabel = 'Close' ;
75+
76+ /**
77+ * Text for collapsed overflow label. Use `${remaining}` to indicate number of hidden labels.
78+ * @default '${remaining} more'
79+ */
3680 @property ( { attribute : 'collapsed-text' , type : String } ) collapsedText = '${remaining} more' ;
81+
82+ /**
83+ * Text for expanded overflow label.
84+ * @default 'show less'
85+ */
3786 @property ( { attribute : 'expanded-text' , type : String } ) expandedText = 'show less' ;
87+
88+ /**
89+ * Number of labels to show before creating an overflow label.
90+ * @default 3
91+ */
3892 @property ( { attribute : 'num-labels' , type : Number } ) numLabels = 3 ;
93+
94+ /**
95+ * Whether the overflow labels are visible (expanded state).
96+ * @default false
97+ */
3998 @property ( { reflect : true , type : Boolean } ) open = false ;
40- @property ( { reflect : true , type : Boolean } ) closeable = true ;
99+
100+ /**
101+ * Whether the label group can be closed.
102+ * @default true
103+ */
104+ @property ( { reflect : true , type : Boolean } ) closeable = false ;
41105
42106 @property ( ) private _overflowText = '' ;
43107
44108 @query ( '#close-button' ) private _button ?: HTMLButtonElement ;
109+
45110 @queryAssignedNodes ( {
46111 slot : 'category-name' ,
47112 flatten : true ,
48113 } )
49114 private _categorySlotted ?: HTMLElement [ ] ;
50115
51-
52116 get #labels( ) : NodeListOf < PfLabel > {
53117 return this . querySelectorAll < PfLabel > ( 'pf-label:not([slot]):not([overflow-label])' ) ;
54118 }
@@ -79,18 +143,18 @@ export class PfLabelGroup extends LitElement {
79143 < slot id ="labels " @slotchange ="${ this . #onSlotchange} "> </ slot >
80144
81145 ${ this . _overflowText ?
82- html `< pf-label
146+ html `< pf-label
83147 id ="overflow "
84148 aria-controls ="labels "
85149 overflow-label
86150 @click ="${ this . #onMoreClick} "
87151 >
88152 ${ this . _overflowText }
89153 </ pf-label > `
90- : '' }
154+ : '' }
91155
92156 ${ this . closeable ?
93- html `< pf-button
157+ html `< pf-button
94158 id ="close-button "
95159 plain
96160 icon ="times-circle "
@@ -99,11 +163,14 @@ export class PfLabelGroup extends LitElement {
99163 aria-describedby ="category "
100164 @click ="${ this . #onCloseClick} "
101165 > </ pf-button > `
102- : '' }
166+ : '' }
103167 </ div >
104168 ` ;
105169 }
106170
171+ /**
172+ * Updates labels when relevant properties change
173+ */
107174 @observes ( 'accessibleCloseLabel' )
108175 @observes ( 'numLabels' )
109176 @observes ( 'closeable' )
@@ -118,7 +185,7 @@ export class PfLabelGroup extends LitElement {
118185 }
119186
120187 /**
121- * Tooltip logic for truncated category title
188+ * Tooltip logic for truncated category title.
122189 */
123190 async #onCategorySlotChange( ) {
124191 await this . updateComplete ;
@@ -132,6 +199,10 @@ export class PfLabelGroup extends LitElement {
132199 }
133200 }
134201
202+ /**
203+ * Handles overflow label's click event.
204+ * @param {Event } event - Click event
205+ */
135206 async #onMoreClick( event : Event ) {
136207 event . stopPropagation ( ) ;
137208 this . open = ! this . open ;
@@ -144,12 +215,18 @@ export class PfLabelGroup extends LitElement {
144215 this . dispatchEvent ( new PfLabelGroupExpandEvent ( ) ) ;
145216 }
146217
218+ /**
219+ * Handles label group close.
220+ */
147221 #onCloseClick( ) {
148222 if ( this . isConnected ) {
149223 this . remove ( ) ;
150224 }
151225 }
152226
227+ /**
228+ * Updates which labels are hidden based on `numLabels` and `open`.
229+ */
153230 #updateOverflow( ) {
154231 const labels = Array . from ( this . #labels) ;
155232 labels . forEach ( ( label , i ) => {
@@ -162,8 +239,8 @@ export class PfLabelGroup extends LitElement {
162239 this . _overflowText = rem < 1 ?
163240 ''
164241 : this . open ?
165- this . expandedText
166- : this . collapsedText . replace ( REMAINING_RE , rem . toString ( ) ) ;
242+ this . expandedText
243+ : this . collapsedText . replace ( REMAINING_RE , rem . toString ( ) ) ;
167244
168245 if ( rem < 1 && this . open ) {
169246 this . open = false ;
0 commit comments