|
6 | 6 | emptyDataProvider, |
7 | 7 | type LocalDataProvider, |
8 | 8 | } from "./providers/provider"; |
9 | | -import {type CSSResultGroup, html, unsafeCSS} from 'lit'; |
| 9 | +import {type CSSResultGroup, html, unsafeCSS, PropertyValues} from 'lit'; |
10 | 10 | import {FormControlController} from "../../internal/form"; |
11 | 11 | import {ifDefined} from "lit/directives/if-defined.js"; |
12 | 12 | import {LocalizeController} from '../../utilities/localize'; |
@@ -54,8 +54,7 @@ export default class ZnDataSelect extends ZincElement implements ZincFormControl |
54 | 54 | /** The provider of the select. */ |
55 | 55 | @property() provider: 'color' | 'currency' | 'country'; |
56 | 56 |
|
57 | | - /** Whether we should show the prefix of the options, and the select. */ |
58 | | - @property({attribute: 'show-prefix', type: Boolean}) showPrefix: boolean; |
| 57 | + @property({attribute: 'icon-position'}) iconPosition: 'start' | 'end' | 'none' = 'none'; |
59 | 58 |
|
60 | 59 | /** An array of keys to use for filtering the options in the selected provider. */ |
61 | 60 | @property({ |
@@ -118,19 +117,31 @@ export default class ZnDataSelect extends ZincElement implements ZincFormControl |
118 | 117 | return this.select.setCustomValidity(message); |
119 | 118 | } |
120 | 119 |
|
| 120 | + protected async firstUpdated(_changedProperties: PropertyValues) { |
| 121 | + super.firstUpdated(_changedProperties); |
| 122 | + await this.updateComplete; |
| 123 | + |
| 124 | + if (this.prefix) this._updatePrefix(); // Update the prefix on first update |
| 125 | + } |
| 126 | + |
121 | 127 | @watch('value', {waitUntilFirstUpdate: true}) |
122 | 128 | async handleValueChange() { |
123 | 129 | await this.updateComplete; |
124 | 130 | this.formControlController.updateValidity(); |
| 131 | + this._updatePrefix(); |
| 132 | + } |
125 | 133 |
|
| 134 | + private _updatePrefix() { |
126 | 135 | // Set the prefix of the select to the selected values prefix |
127 | 136 | const selectedOption = this.select.selectedOptions[0]; |
128 | | - if (selectedOption && this.showPrefix) { |
129 | | - const prefix = selectedOption.querySelector('[slot="prefix"]'); |
| 137 | + if (selectedOption && (this.iconPosition !== 'none')) { |
| 138 | + const slot = this.iconPosition === 'start' |
| 139 | + ? selectedOption.querySelector('[slot="prefix"]') |
| 140 | + : selectedOption.querySelector('[slot="suffix"]'); |
130 | 141 |
|
131 | | - if (prefix) { |
| 142 | + if (slot) { |
132 | 143 | this.selectPrefix.innerHTML = ''; |
133 | | - this.selectPrefix.appendChild(prefix.cloneNode(true)); |
| 144 | + this.selectPrefix.appendChild(slot.cloneNode(true)); |
134 | 145 | } else { |
135 | 146 | this.selectPrefix.innerHTML = ''; |
136 | 147 | } |
@@ -185,11 +196,12 @@ export default class ZnDataSelect extends ZincElement implements ZincFormControl |
185 | 196 | value="${this.value}" |
186 | 197 | placeholder="Choose a ${localProvider.getName}" |
187 | 198 | exportparts="combobox,expand-icon,form-control-help-text,form-control-input"> |
188 | | - ${this.showPrefix ? html` |
| 199 | + ${this.iconPosition !== 'none' ? html` |
189 | 200 | <div id="select__prefix" slot="prefix" class="select__prefix"></div>` : ''} |
190 | 201 | ${data.map((item: DataProviderOption) => html` |
191 | 202 | <zn-option class="select__option" value="${item.key}"> |
192 | | - ${this.showPrefix ? html`<span slot="prefix">${item.prefix}</span>` : ''} |
| 203 | + ${this.iconPosition !== 'none' ? html`<span |
| 204 | + slot="${this.iconPosition === 'end' ? 'suffix' : 'prefix'}">${item.prefix}</span>` : ''} |
193 | 205 | ${item.value} |
194 | 206 | </zn-option>`)} |
195 | 207 | </zn-select> |
|
0 commit comments