|
1 | | -<pf-label-group add-label-mode="customForm"> |
| 1 | +<pf-label-group id="label-group" closeable> |
2 | 2 | <h2 slot="category">group name</h2> |
3 | 3 | <pf-label color="red" icon="info-circle" removable>label-1</pf-label> |
4 | 4 | <pf-label color="green" icon="info-circle" removable>label-2</pf-label> |
5 | 5 | <pf-label color="blue" icon="info-circle" removable>label-3</pf-label> |
| 6 | + |
| 7 | + <pf-label id="add-button" color="blue" variant="outline" dismissible="false" > |
| 8 | + Add label </pf-label> |
6 | 9 | </pf-label-group> |
7 | 10 |
|
8 | 11 | <script type="module"> |
9 | 12 | import '@patternfly/elements/pf-label-group/pf-label-group.js'; |
10 | 13 | import '@patternfly/elements/pf-label/pf-label.js'; |
11 | 14 | import '@patternfly/elements/pf-button/pf-button.js'; |
12 | | - import '@patternfly/elements/pf-modal/pf-modal.js'; |
13 | | - import '@patternfly/elements/pf-dropdown/pf-dropdown.js'; |
| 15 | + import '@patternfly/elements/pf-modal/pf-modal.js'; |
| 16 | + |
| 17 | + const group = document.getElementById('label-group'); |
| 18 | + const addButton = document.getElementById('add-button'); |
| 19 | + console.log('addButton', addButton); |
| 20 | + |
| 21 | + |
| 22 | + // create a modal element with form controls and buttons, return it |
| 23 | + function createModal() { |
| 24 | + const modal = document.createElement('pf-modal'); |
| 25 | + modal.setAttribute('aria-describedby', 'custom-label-modal-description'); |
| 26 | + console.log('modal is created'); |
| 27 | + |
| 28 | + modal.innerHTML = ` |
| 29 | + <h2 slot="header">Add a new label</h2> |
| 30 | + <p slot="header" id="custom-label-modal-description">Fill in the details below to create your custom label.</p> |
| 31 | +
|
| 32 | + <form id="add-label-form"> |
| 33 | + <label> |
| 34 | + Label text: |
| 35 | + <input id="label-text" class="pf-c-form-control" value="New label" /> |
| 36 | + </label> |
| 37 | +
|
| 38 | + <label> |
| 39 | + Color: |
| 40 | + <select id="label-color" class="pf-c-form-control"> |
| 41 | + <option value="blue">Blue</option> |
| 42 | + <option value="cyan">Cyan</option> |
| 43 | + <option value="green">Green</option> |
| 44 | + <option value="orange">Orange</option> |
| 45 | + <option value="purple">Purple</option> |
| 46 | + <option value="red">Red</option> |
| 47 | + <option value="grey" selected>Grey</option> |
| 48 | + <option value="gold">Gold</option> |
| 49 | + </select> |
| 50 | + </label> |
| 51 | +
|
| 52 | + <label> |
| 53 | + Icon: |
| 54 | + <select id="label-icon" class="pf-c-form-control"> |
| 55 | + <option value="">None</option> |
| 56 | + <option value="info">Info</option> |
| 57 | + <option value="check">Check</option> |
| 58 | + <option value="exclamation">Exclamation</option> |
| 59 | + <option value="star">Star</option> |
| 60 | + </select> |
| 61 | + </label> |
| 62 | +
|
| 63 | + <div> |
| 64 | + <label>Dismissable:</label> |
| 65 | + <div> |
| 66 | + <label><input type="radio" name="dismissable" value="true" /> Yes</label> |
| 67 | + <label><input type="radio" name="dismissable" value="false" checked /> No</label> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | +
|
| 71 | + <div> |
| 72 | + <label>Label type:</label> |
| 73 | + <div> |
| 74 | + <label><input type="radio" name="filled" value="true" checked /> Filled</label> |
| 75 | + <label><input type="radio" name="filled" value="false" /> Outlined</label> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </form> |
| 79 | +
|
| 80 | + <div slot="footer" style="display:flex; justify-content:flex-end; gap:0.5rem;"> |
| 81 | + <pf-button id="modal-add" variant="primary">Save</pf-button> |
| 82 | + <pf-button id="modal-cancel" variant="secondary">Cancel</pf-button> |
| 83 | + </div> |
| 84 | + `; |
| 85 | +console.log('modal innerHTML set'); |
| 86 | + return modal; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + function resetForm(form) { |
| 92 | + const inputText = form.querySelector('#label-text'); |
| 93 | + const selectColor = form.querySelector('#label-color'); |
| 94 | + const selectIcon = form.querySelector('#label-icon'); |
| 95 | + if (inputText) inputText.value = 'New label'; |
| 96 | + if (selectColor) selectColor.value = 'grey'; |
| 97 | + if (selectIcon) selectIcon.value = ''; |
| 98 | + const dismissNo = form.querySelector('input[name="dismissable"][value="false"]'); |
| 99 | + const filledYes = form.querySelector('input[name="filled"][value="true"]'); |
| 100 | + if (dismissNo) dismissNo.checked = true; |
| 101 | + if (filledYes) filledYes.checked = true; |
| 102 | + } |
| 103 | + |
| 104 | + function openModal() { |
| 105 | + const modal = createModal(); |
| 106 | + document.body.appendChild(modal); |
| 107 | + |
| 108 | + const form = modal.querySelector('#add-label-form'); |
| 109 | + const inputText = modal.querySelector('#label-text'); |
| 110 | + const selectColor = modal.querySelector('#label-color'); |
| 111 | + const selectIcon = modal.querySelector('#label-icon'); |
| 112 | + const modalAdd = modal.querySelector('#modal-add'); |
| 113 | + const modalCancel = modal.querySelector('#modal-cancel'); |
| 114 | + |
| 115 | + resetForm(form); |
| 116 | + |
| 117 | + function cleanup() { |
| 118 | + try { modal.remove(); } catch (e) {} |
| 119 | + requestAnimationFrame(() => addButton.focus()); |
| 120 | + } |
| 121 | + |
| 122 | + modal.addEventListener('close', cleanup, { once: true }); |
| 123 | + |
| 124 | + modalCancel.addEventListener('click', () => { |
| 125 | + modal.open = false; |
| 126 | + }, { once: true }); |
| 127 | + |
| 128 | + modalAdd.addEventListener('click', () => { |
| 129 | + const text = (inputText.value || '').trim() || 'New label'; |
| 130 | + const color = selectColor.value; |
| 131 | + const icon = selectIcon.value; |
| 132 | + const dismissable = !!form.querySelector('input[name="dismissable"]:checked') && |
| 133 | + form.querySelector('input[name="dismissable"]:checked').value === 'true'; |
| 134 | + const filled = !!form.querySelector('input[name="filled"]:checked') && |
| 135 | + form.querySelector('input[name="filled"]:checked').value === 'true'; |
| 136 | + |
| 137 | + const newLabel = document.createElement('pf-label'); |
| 138 | + newLabel.textContent = text; |
| 139 | + if (color) newLabel.setAttribute('color', color); |
| 140 | + if (icon) newLabel.setAttribute('icon', icon); |
| 141 | + if (dismissable) newLabel.setAttribute('removable', 'true'); |
| 142 | + if (!filled) newLabel.setAttribute('variant', 'outline'); |
| 143 | + |
| 144 | + const firstLabel = group.querySelector('pf-label:not([slot])'); |
| 145 | + if (firstLabel) group.insertBefore(newLabel, firstLabel); |
| 146 | + else group.appendChild(newLabel); |
| 147 | + |
| 148 | + group.dispatchEvent(new CustomEvent('label-added', { detail: { text, color, icon, dismissable, filled }, bubbles: true })); |
| 149 | + |
| 150 | + modal.open = false; |
| 151 | + }, { once: true }); |
| 152 | + |
| 153 | + // open and focus |
| 154 | + modal.open = true; |
| 155 | + requestAnimationFrame(() => { |
| 156 | + const first = modal.querySelector('#label-text'); |
| 157 | + if (first) first.focus(); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + addButton.addEventListener('click', openModal); |
14 | 162 | </script> |
| 163 | +<style> |
| 164 | + #add-button { |
| 165 | + cursor: pointer; |
| 166 | + align-items: center; |
| 167 | + gap: 4px; |
| 168 | + transition: border 0.2s ease; |
| 169 | + border-radius: 0.99rem; |
| 170 | + } |
| 171 | + pf-modal#custom-label-modal { |
| 172 | + --pf-c-modal--BoxShadow: 0 0 15px rgba(0,0,0,0.2); |
| 173 | + --pf-c-modal-box--BorderRadius: 8px; |
| 174 | + --pf-c-modal-box--Padding: 1rem; |
| 175 | + width: 200px; |
| 176 | + font-size: 0.875rem; |
| 177 | +} |
| 178 | +</style> |
0 commit comments