-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCombobox.ts
More file actions
134 lines (109 loc) · 3.36 KB
/
Copy pathCombobox.ts
File metadata and controls
134 lines (109 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { customElement, WebComponent } from '@/lib/components'
import { html } from 'lit'
import { property, query, state } from 'lit/decorators.js'
import InputTrait from '@/lib/components/traits/InputTrait'
import type ComboboxOption from '@/components/combobox-option/ComboboxOption'
import '~icons/lucide/chevron-down'
import styles from './Combobox.styles.css'
@customElement('solid-ui-combobox')
export default class Combobox extends WebComponent {
static styles = styles
static formAssociated = true
@property({ type: String, reflect: true })
accessor label = ''
@property({ type: String, reflect: true })
accessor name = ''
@property({ type: String })
accessor value = ''
@property({ type: String, reflect: true })
accessor placeholder = ''
@property({ type: Boolean, reflect: true })
accessor required = false
@query('[popover]')
private accessor popoverElement: HTMLDivElement | null = null
@query('input')
private accessor inputElement: HTMLInputElement | null = null
@property({ type: Boolean, reflect: true })
accessor readonly = false
@state()
private accessor filter = ''
private inputTrait: InputTrait
constructor () {
super()
this.inputTrait = this.addTrait(
new InputTrait(this, {
getInputElement: () => this.inputElement,
getInternals: () => this.getInternals(),
onValueChanged: (value) => {
this.filter = value.toLowerCase()
},
})
)
}
protected render () {
const options = this.getOptions().filter((option) =>
option.label.toLowerCase().includes(this.filter)
)
return html`
${this.inputTrait.renderLabel()}
<div class="input-wrapper">
<input
id="${this.inputTrait.inputId}"
type="text"
name=${this.name}
?placeholder=${this.placeholder}
?required=${this.required}
?readonly=${this.readonly}
.value=${this.value}
@keydown=${this.onInputKeyDown}
@click=${this.onInputClick}
@input=${() => this.inputTrait.onInput()}
/>
<icon-lucide-chevron-down></icon-lucide-chevron-down>
</div>
<div role="listbox" aria-labelledby="${this.inputTrait.inputId}" popover>
${options.map(
(option) =>
html`<div
role="option"
aria-selected="false"
@click=${() => this.onOptionClick(option.value)}
>
${option.label}
</div>`
)}
</div>
`
}
private getOptions (): { value: string; label: string }[] {
const options = this.querySelectorAll<ComboboxOption>(
'solid-ui-combobox-option'
)
return Array.from(options).map((option) => ({
value: option.value,
label: option.textContent,
}))
}
private onInputKeyDown (e: KeyboardEvent) {
switch (e.key) {
case 'ArrowDown':
e.preventDefault()
this.popoverElement?.showPopover()
break
case 'Enter':
if (!this.popoverElement?.matches(':popover-open')) {
e.preventDefault()
this.inputTrait.onSubmit()
}
break
}
}
private onInputClick (e: MouseEvent) {
e.preventDefault()
this.popoverElement?.showPopover()
}
private onOptionClick (option: string) {
this.inputTrait.setValue(option)
this.popoverElement?.hidePopover()
}
}