|
1 |
| -import { LitElement, html, unsafeCSS } from 'lit'; |
2 |
| -import { RtlMixin, defineCustomElement } from '@justeattakeaway/pie-webc-core'; |
| 1 | +import { |
| 2 | + LitElement, |
| 3 | + html, |
| 4 | + nothing, |
| 5 | + unsafeCSS, |
| 6 | +} from 'lit'; |
| 7 | +import { |
| 8 | + RtlMixin, |
| 9 | + defineCustomElement, |
| 10 | + validPropertyValues, |
| 11 | +} from '@justeattakeaway/pie-webc-core'; |
| 12 | +import { |
| 13 | + property, |
| 14 | + query, |
| 15 | + queryAssignedElements, |
| 16 | + state, |
| 17 | +} from 'lit/decorators.js'; |
| 18 | +import { ifDefined } from 'lit/directives/if-defined.js'; |
| 19 | +import { classMap, type ClassInfo } from 'lit/directives/class-map.js'; |
| 20 | +import '@justeattakeaway/pie-icons-webc/dist/IconChevronDown.js'; |
3 | 21 |
|
4 | 22 | import styles from './select.scss?inline';
|
5 |
| -import { type SelectProps } from './defs'; |
| 23 | +import { |
| 24 | + defaultProps, |
| 25 | + sizes, |
| 26 | + statusTypes, |
| 27 | + type SelectProps, |
| 28 | +} from './defs'; |
6 | 29 |
|
7 | 30 | // Valid values available to consumers
|
8 | 31 | export * from './defs';
|
9 | 32 |
|
10 | 33 | const componentSelector = 'pie-select';
|
| 34 | +const assistiveTextIdValue = 'assistive-text'; |
11 | 35 |
|
12 | 36 | /**
|
13 | 37 | * @tagname pie-select
|
14 | 38 | */
|
15 | 39 | export class PieSelect extends RtlMixin(LitElement) implements SelectProps {
|
| 40 | + static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }; |
| 41 | + |
| 42 | + @property({ type: String }) |
| 43 | + @validPropertyValues(componentSelector, sizes, defaultProps.size) |
| 44 | + public size = defaultProps.size; |
| 45 | + |
| 46 | + @property({ type: Boolean }) |
| 47 | + public disabled = defaultProps.disabled; |
| 48 | + |
| 49 | + @property({ type: String }) |
| 50 | + @validPropertyValues(componentSelector, statusTypes, defaultProps.status) |
| 51 | + public status = defaultProps.status; |
| 52 | + |
| 53 | + @property({ type: String }) |
| 54 | + public assistiveText: SelectProps['assistiveText']; |
| 55 | + |
| 56 | + @property({ type: String }) |
| 57 | + public name: SelectProps['name']; |
| 58 | + |
| 59 | + @query('select') |
| 60 | + public focusTarget!: HTMLElement; |
| 61 | + |
| 62 | + @queryAssignedElements({ slot: 'leadingIcon', flatten: true }) |
| 63 | + private _leadingIconSlot!: Array<HTMLElement>; |
| 64 | + |
| 65 | + @state() |
| 66 | + private _hasLeadingIcon = false; |
| 67 | + |
| 68 | + private _handleLeadingIconSlotchange () { |
| 69 | + this._hasLeadingIcon = Boolean(this._leadingIconSlot.length); |
| 70 | + } |
| 71 | + |
| 72 | + private renderAssistiveText () { |
| 73 | + if (!this.assistiveText) { |
| 74 | + return nothing; |
| 75 | + } |
| 76 | + |
| 77 | + return html` |
| 78 | + <pie-assistive-text |
| 79 | + id="${assistiveTextIdValue}" |
| 80 | + variant=${ifDefined(this.status)} |
| 81 | + data-test-id="pie-textarea-assistive-text"> |
| 82 | + ${this.assistiveText} |
| 83 | + </pie-assistive-text> |
| 84 | + `; |
| 85 | + } |
| 86 | + |
16 | 87 | render () {
|
17 |
| - return html`<h1 data-test-id="pie-select">Hello world!</h1>`; |
| 88 | + const { |
| 89 | + assistiveText, |
| 90 | + disabled, |
| 91 | + status, |
| 92 | + size, |
| 93 | + name, |
| 94 | + _hasLeadingIcon, |
| 95 | + } = this; |
| 96 | + |
| 97 | + const classes : ClassInfo = { |
| 98 | + 'c-select': true, |
| 99 | + [`c-select--${size}`]: true, |
| 100 | + [`c-select--${status}`]: true, |
| 101 | + 'c-select--withLeadingIcon': _hasLeadingIcon, |
| 102 | + 'is-disabled': disabled, |
| 103 | + }; |
| 104 | + |
| 105 | + return html` |
| 106 | + <div |
| 107 | + class="${classMap(classes)}" |
| 108 | + data-test-id="pie-select-shell"> |
| 109 | + <slot name="leadingIcon" @slotchange=${this._handleLeadingIconSlotchange}></slot> |
| 110 | + <select |
| 111 | + name=${ifDefined(name)} |
| 112 | + ?disabled=${disabled} |
| 113 | + aria-describedby=${ifDefined(assistiveText ? assistiveTextIdValue : undefined)} |
| 114 | + aria-invalid=${status === 'error' ? 'true' : 'false'} |
| 115 | + aria-errormessage="${ifDefined(status === 'error' ? assistiveTextIdValue : undefined)}"> |
| 116 | + <option value="dog">Dog</option> |
| 117 | + <option value="cat">Cat</option> |
| 118 | + <option value="hamster">Hamster</option> |
| 119 | + </select> |
| 120 | + <icon-chevron-down size='s' class='c-select-trailingIcon'></icon-chevron-down> |
| 121 | + </div> |
| 122 | + ${this.renderAssistiveText()} |
| 123 | + `; |
18 | 124 | }
|
19 | 125 |
|
20 | 126 | // Renders a `CSSResult` generated from SCSS by Vite
|
|
0 commit comments