import {
html,
unsafeCSS,
type TemplateResult,
} from 'lit';
import { PieElement } from '@justeattakeaway/pie-webc-core/src/internals/PieElement';
import {
FormControlMixin,
RtlMixin,
DelegatesFocusMixin,
safeCustomElement,
validPropertyValues,
wrapNativeEvent,
} from '@justeattakeaway/pie-webc-core';
import {
property,
query,
queryAssignedElements,
state,
} from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { classMap, type ClassInfo } from 'lit/directives/class-map.js';
import { live } from 'lit/directives/live.js';
import '@justeattakeaway/pie-icons-webc/dist/IconChevronDown.js';
import '@justeattakeaway/pie-assistive-text';
import styles from './select.scss?inline';
import {
defaultProps,
sizes,
statusTypes,
type SelectProps,
type SelectOptionProps,
} from './defs';
// Valid values available to consumers
export * from './defs';
const componentSelector = 'pie-select';
const assistiveTextIdValue = 'assistive-text';
/**
* @tagname pie-select
* @event {CustomEvent} change - when the selected option is changed.
*/
@safeCustomElement('pie-select')
export class PieSelect extends FormControlMixin(RtlMixin(DelegatesFocusMixin(PieElement))) implements SelectProps {
@property({ type: String })
@validPropertyValues(componentSelector, sizes, defaultProps.size)
public size = defaultProps.size;
@property({ type: Boolean })
public disabled = defaultProps.disabled;
@property({ type: String })
@validPropertyValues(componentSelector, statusTypes, defaultProps.status)
public status = defaultProps.status;
@property({ type: String })
public assistiveText: SelectProps['assistiveText'];
@property({ type: String, reflect: true })
public name: SelectProps['name'];
@property({ type: Array })
public options: SelectProps['options'] = defaultProps.options;
private _value: SelectProps['value'] = defaultProps.value;
@query('select')
public focusTarget!: HTMLSelectElement;
@query('select')
private _select!: HTMLSelectElement;
@queryAssignedElements({ slot: 'leadingIcon', flatten: true })
private _leadingIconSlot!: Array;
@state()
private _hasLeadingIcon = false;
protected firstUpdated (): void {
this._internals.setFormValue(this._select.value);
}
@property()
public get value (): SelectProps['value'] {
// If no value was assigned
// and the select element is available
// return its value as by default it will pick the first available option
if (this._value === '') {
if (!this._select) {
return '';
}
return this._select.value;
}
return this._value;
}
public set value (newValue: SelectProps['value']) {
const safeNewValue = newValue ? String(newValue) : '';
this._internals.setFormValue(safeNewValue);
this._value = safeNewValue;
this.requestUpdate();
}
/**
* (Read-only) returns a ValidityState with the validity states that this element is in.
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/validity
*/
public get validity (): ValidityState {
return this._select.validity;
}
/**
* Called after the disabled state of the element changes,
* either because the disabled attribute of this element was added or removed;
* or because the disabled state changed on a