Skip to content

Commit cffdff2

Browse files
author
Sara Dahan
committed
chore(label-group): transferring the insertion logic from TS to Demos
1 parent b00fa46 commit cffdff2

File tree

3 files changed

+10
-266
lines changed

3 files changed

+10
-266
lines changed

elements/pf-label-group/demo/label-group-add-label-custom-form.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ <h2 slot="header">Add label</h2>
118118
resetForm(form);
119119

120120
function cleanup() {
121-
try { modal.remove(); } catch (e) {}
121+
try { modal.remove(); }
122+
catch (e) {
123+
console.error('Error removing modal:', e);
124+
}
122125
requestAnimationFrame(() => addButton.focus());
123126
}
124127

elements/pf-label-group/demo/label-group-add-label-from-list.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ <h2 slot="category">group name</h2>
5353
if (firstLabel) group.insertBefore(newLabel, firstLabel);
5454
else group.appendChild(newLabel);
5555
// close the dropdown if it exposes an API
56-
try { dropdown.hide?.(); } catch (e) {}
56+
try { dropdown.hide?.(); }
57+
catch (e) {
58+
console.error('Error hiding dropdown:', e);
59+
}
5760
});
5861
</script>
5962

@@ -95,6 +98,4 @@ <h2 slot="category">group name</h2>
9598
line-height: normal !important;
9699
box-sizing: border-box !important;
97100
}
98-
99-
</style>
100-
101+
</style>

elements/pf-label-group/pf-label-group.ts

Lines changed: 1 addition & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type { PfLabel } from '@patternfly/elements/pf-label/pf-label.js';
1111
import styles from './pf-label-group.css';
1212
import '@patternfly/elements/pf-button/pf-button.js';
1313
import '@patternfly/elements/pf-label/pf-label.js';
14-
import '@patternfly/elements/pf-select/pf-select.js';
1514

1615
/**
1716
* Event fired when the label group is expanded to show all labels.
@@ -106,53 +105,6 @@ export class PfLabelGroup extends LitElement {
106105
*/
107106
@property({ reflect: true, type: Boolean }) closeable = false;
108107

109-
/** --- ADD LABEL FUNCTIONALITY --- */
110-
111-
/**
112-
* Mode controlling how labels can be added.
113-
* - `none`: No add label functionality
114-
* - `autoNoEdit`: Adds a default label automatically
115-
* - `fromList`: Allows choosing a label from a predefined list
116-
* - `customForm`: Opens a form for custom label creation
117-
*/
118-
@property({ type: String, reflect: true, attribute: 'add-label-mode' })
119-
addLabelMode: 'none' | 'autoNoEdit' | 'fromList' | 'customForm' = 'none';
120-
121-
/**
122-
* Controls visibility of the label dropdown for `fromList` mode.
123-
*/
124-
@state() showDropdown = false;
125-
126-
/**
127-
* Controls visibility of the custom label form for `customForm` mode.
128-
*/
129-
@state() showCustomForm = false;
130-
131-
/**
132-
* Text value for a new custom label.
133-
*/
134-
@state() newLabelText = 'Text box';
135-
136-
/**
137-
* Color value for a new custom label.
138-
*/
139-
@state() newLabelColor = 'grey';
140-
141-
/**
142-
* Icon value for a new custom label.
143-
*/
144-
@state() newLabelIcon = '';
145-
146-
/**
147-
* Variant of the new label. Can be 'filled' or 'outline'.
148-
*/
149-
@state() newLabelVariant = 'filled';
150-
151-
/**
152-
* Whether the new custom label is dismissable.
153-
*/
154-
@state() newLabelDismissable = false;
155-
156108
/**
157109
* Current text shown in the overflow label (e.g., "3 more" or "show less").
158110
* Managed internally.
@@ -164,17 +116,6 @@ export class PfLabelGroup extends LitElement {
164116
*/
165117
@query('#close-button') private _button?: HTMLButtonElement;
166118

167-
/**
168-
* Reference to the add button element in the shadow DOM.
169-
*/
170-
@query('#add-button') private _addButton?: HTMLButtonElement;
171-
172-
/**
173-
* Reference to the custom label modal element.
174-
*/
175-
@query('#custom-label-modal')
176-
private _customModal!: HTMLElement & { show: () => void; close: () => void };
177-
178119
/**
179120
* Retrieves all elements assigned to the `category` slot.
180121
*/
@@ -214,7 +155,6 @@ export class PfLabelGroup extends LitElement {
214155
this.open ? this.#labels.length : Math.min(this.#labels.length, this.numLabels)
215156
),
216157
this._button,
217-
this._addButton,
218158
].filter(x => !!x),
219159
});
220160

@@ -243,36 +183,6 @@ export class PfLabelGroup extends LitElement {
243183
</pf-label>`
244184
: ''}
245185
246-
${this.addLabelMode !== 'none' ?
247-
html`
248-
<div style="position: relative; display: inline-block;">
249-
<pf-label
250-
id="add-button"
251-
color="blue"
252-
variant="outline"
253-
dismissible="false"
254-
class="add-label"
255-
@click="${this.#onAddClick}"
256-
>
257-
Add label
258-
</pf-label>
259-
260-
${this.showDropdown ?
261-
html`
262-
<div style="position: absolute; top: 100%; left: 0; z-index: 100%; background: white; border: 1px solid #ccc; box-shadow: 0 2px 6px rgba(0,0,0,0.15);">
263-
${this._defaultLabelList.map(
264-
label => html`
265-
<pf-dropdown-item
266-
@click="${() => this.#addLabelFromList(label)}"
267-
>
268-
${label.name}
269-
</pf-dropdown-item>`
270-
)}
271-
</div>`
272-
: ''}
273-
</div>`
274-
: ''}
275-
276186
${this.closeable ?
277187
html`<pf-button
278188
id="close-button"
@@ -286,56 +196,7 @@ export class PfLabelGroup extends LitElement {
286196
: ''}
287197
</div>
288198
289-
${this.showCustomForm ?
290-
html`
291-
<section>
292-
<pf-modal id="custom-label-modal" aria-describedby="custom-label-modal-description" variant="small" @close=${this.#closeCustomForm}>
293-
<h2 slot="header">Add label</h2>
294-
<div id="add-label-form">
295-
<label>
296-
Label text
297-
<input class="pf-c-form-control" .value=${this.newLabelText} @input=${(e: Event) => (this.newLabelText = (e.target as HTMLInputElement).value)} />
298-
</label>
299-
<label>Color</label>
300-
<pf-select id="color-select" @change=${this.#onSelectColor} style="display:inline-block; --pf-c-button--PaddingLeft:0.5rem; --pf-c-button--PaddingRight:0.5rem; --pf-c-button--MinWidth:0;">
301-
<pf-option value="Grey">Grey</pf-option>
302-
<pf-option value="Blue">Blue</pf-option>
303-
<pf-option value="Green">Green</pf-option>
304-
<pf-option value="Cyan">Cyan</pf-option>
305-
<pf-option value="Gold">Gold</pf-option>
306-
<pf-option value="Red">Red</pf-option>
307-
<pf-option value="Orange">Orange</pf-option>
308-
<pf-option value="Purple">Purple</pf-option>
309-
</pf-select>
310-
311-
<label>Icon</label>
312-
<pf-select id="icon-select" @change=${this.#onSelectIcon} style="display:inline-block; --pf-c-button--PaddingLeft:0.5rem; --pf-c-button--PaddingRight:0.5rem; --pf-c-button--MinWidth:0;">
313-
<pf-option value="No icon">No icon</pf-option>
314-
<pf-option value="Info">Info</pf-option>
315-
<pf-option value="Check">Check</pf-option>
316-
<pf-option value="Exclamation">Exclamation</pf-option>
317-
</pf-select>
318-
319-
<label>Label type</label>
320-
<div class="radio-group" id="label-type-group">
321-
<label><input type="radio" name="labelType" value="filled" .checked=${this.newLabelVariant === 'filled'} @change=${() => (this.newLabelVariant = 'filled')} />Filled</label>
322-
<label><input type="radio" name="labelType" value="outline" .checked=${this.newLabelVariant === 'outline'} @change=${() => (this.newLabelVariant = 'outline')} />Outline</label>
323-
</div>
324-
325-
<label>Dismissable</label>
326-
<div class="radio-group" id="dismissable-group">
327-
<label><input type="radio" name="dismissable" value="true" .checked=${this.newLabelDismissable === true} @change=${() => (this.newLabelDismissable = true)} />Yes</label>
328-
<label><input type="radio" name="dismissable" value="false" .checked=${this.newLabelDismissable === false} @change=${() => (this.newLabelDismissable = false)} />No</label>
329-
</div>
330-
</div>
331-
332-
<div slot="footer" style="display:flex; justify-content:flex-end; gap:0.5rem;">
333-
<pf-button variant="primary" @click=${this.#addCustomLabel}>Save</pf-button>
334-
<pf-button variant="secondary" @click=${this.#closeCustomForm}>Cancel</pf-button>
335-
</div>
336-
</pf-modal>
337-
</section>`
338-
: ''}
199+
339200
`;
340201
}
341202

@@ -394,120 +255,6 @@ export class PfLabelGroup extends LitElement {
394255
}
395256
}
396257

397-
/**
398-
* Handles click on the add label button according to `addLabelMode`.
399-
*/
400-
#onAddClick() {
401-
switch (this.addLabelMode) {
402-
case 'none': break;
403-
case 'autoNoEdit': this.#addDefaultLabel(); break;
404-
case 'fromList': this.#showLabelDropdown(); break;
405-
case 'customForm': this.#openCustomForm(); break;
406-
}
407-
}
408-
409-
/**
410-
* Adds a default label programmatically in `autoNoEdit` mode.
411-
*/
412-
#addDefaultLabel() {
413-
const newLabel = document.createElement('pf-label');
414-
newLabel.textContent = 'New label';
415-
(newLabel as any).removable = true;
416-
this.insertBefore(newLabel, this.firstElementChild);
417-
this.requestUpdate();
418-
this.labelsChanged();
419-
}
420-
421-
/**
422-
* Shows or hides the label dropdown for `fromList` mode.
423-
*/
424-
#showLabelDropdown() {
425-
this.showDropdown = !this.showDropdown;
426-
}
427-
428-
/**
429-
* Adds a label from the predefined list.
430-
* @param label - Label object with `name`, optional `color` and `icon`
431-
*/
432-
#addLabelFromList(label: { name: string; color?: string; icon?: string }) {
433-
const newLabel = document.createElement('pf-label');
434-
newLabel.textContent = label.name;
435-
if (label.color) {
436-
(newLabel as any).color = label.color;
437-
}
438-
if (label.icon) {
439-
(newLabel as any).icon = label.icon;
440-
}
441-
this.insertBefore(newLabel, this.firstElementChild);
442-
this.labelsChanged();
443-
this.showDropdown = false;
444-
}
445-
446-
/**
447-
* Opens the custom label form modal in `customForm` mode.
448-
*/
449-
#openCustomForm() {
450-
this.showCustomForm = true;
451-
this.updateComplete.then(() => this._customModal?.show());
452-
}
453-
454-
/**
455-
* Closes the custom label form modal.
456-
*/
457-
#closeCustomForm() {
458-
if (this._customModal) {
459-
this.showCustomForm = false;
460-
}
461-
}
462-
463-
/**
464-
* Adds a custom label from form input values.
465-
*/
466-
#addCustomLabel() {
467-
const newLabel = document.createElement('pf-label');
468-
newLabel.textContent = this.newLabelText;
469-
newLabel.setAttribute('color', this.newLabelColor);
470-
newLabel.setAttribute('variant', this.newLabelVariant);
471-
if (this.newLabelIcon) {
472-
newLabel.setAttribute('icon', this.newLabelIcon);
473-
}
474-
if (this.newLabelDismissable) {
475-
newLabel.setAttribute('removable', '');
476-
}
477-
this.insertBefore(newLabel, this.firstElementChild);
478-
479-
this.newLabelText = 'Text box';
480-
this.newLabelColor = 'grey';
481-
this.newLabelIcon = '';
482-
this.newLabelVariant = 'filled';
483-
this.newLabelDismissable = false;
484-
this.showCustomForm = false;
485-
486-
this.requestUpdate();
487-
this.labelsChanged();
488-
}
489-
490-
/**
491-
* Updates the new label's color from the color select dropdown.
492-
* @param e - Change event from select element
493-
*/
494-
#onSelectColor(e: Event) {
495-
const select = e.target as HTMLSelectElement;
496-
const selectedValue = select.value;
497-
this.newLabelColor = selectedValue.toLowerCase();
498-
}
499-
500-
/**
501-
* Updates the new label's icon from the icon select dropdown.
502-
* @param e - Change event from select element
503-
*/
504-
#onSelectIcon(e: Event) {
505-
const select = e.target as HTMLSelectElement;
506-
const selectedValue = select.value;
507-
this.newLabelIcon = selectedValue === 'No icon' ? ''
508-
: selectedValue.toLowerCase().concat('-circle');
509-
}
510-
511258
/**
512259
* Updates which labels are hidden based on `numLabels` and `open`.
513260
*/
@@ -540,13 +287,6 @@ export class PfLabelGroup extends LitElement {
540287
}
541288
}
542289
}
543-
544-
private _defaultLabelList: { name: string; color?: string; icon?: string }[] = [
545-
{ name: 'Label 1', color: 'blue' },
546-
{ name: 'Label 2', color: 'green', icon: 'check-circle' },
547-
{ name: 'Label 3' },
548-
{ name: 'Label 4', color: 'red' },
549-
];
550290
}
551291

552292
declare global {

0 commit comments

Comments
 (0)