-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathvaadin-lit-combo-box.js
177 lines (157 loc) · 5.39 KB
/
vaadin-lit-combo-box.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @license
* Copyright (c) 2015 - 2025 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import '@vaadin/input-container/src/vaadin-lit-input-container.js';
import './vaadin-lit-combo-box-item.js';
import './vaadin-lit-combo-box-overlay.js';
import './vaadin-lit-combo-box-scroller.js';
import { css, html, LitElement } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
import { InputController } from '@vaadin/field-base/src/input-controller.js';
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
import { PatternMixin } from '@vaadin/field-base/src/pattern-mixin.js';
import { inputFieldShared } from '@vaadin/field-base/src/styles/input-field-shared-styles.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { ComboBoxDataProviderMixin } from './vaadin-combo-box-data-provider-mixin.js';
import { ComboBoxMixin } from './vaadin-combo-box-mixin.js';
/**
* LitElement based version of `<vaadin-combo-box>` web component.
*
* ## Disclaimer
*
* This component is an experiment and not yet a part of Vaadin platform.
* There is no ETA regarding specific Vaadin version where it'll land.
* Feel free to try this code in your apps as per Apache 2.0 license.
*/
class ComboBox extends ComboBoxDataProviderMixin(
ComboBoxMixin(PatternMixin(InputControlMixin(ThemableMixin(ElementMixin(PolylitMixin(LitElement)))))),
) {
static get is() {
return 'vaadin-combo-box';
}
static get styles() {
return [
inputFieldShared,
css`
:host([opened]) {
pointer-events: auto;
}
[part='toggle-button'] {
cursor: default;
background: var(--_vaadin-color-subtle);
height: var(--vaadin-icon-size, 1lh);
mask-image: var(--_vaadin-icon-chevron-down);
width: var(--vaadin-icon-size, 1lh);
}
`,
];
}
static get properties() {
return {
/**
* @protected
*/
_positionTarget: {
type: Object,
},
};
}
/**
* Used by `InputControlMixin` as a reference to the clear button element.
* @protected
* @return {!HTMLElement}
*/
get clearElement() {
return this.$.clearButton;
}
/** @protected */
render() {
return html`
<div class="vaadin-combo-box-container">
<div part="label">
<slot name="label"></slot>
<span part="required-indicator" aria-hidden="true" @click="${this.focus}"></span>
</div>
<vaadin-input-container
part="input-field"
.readonly="${this.readonly}"
.disabled="${this.disabled}"
.invalid="${this.invalid}"
theme="${ifDefined(this._theme)}"
>
<slot name="prefix" slot="prefix"></slot>
<slot name="input"></slot>
<div id="clearButton" part="clear-button" slot="suffix" aria-hidden="true"></div>
<div id="toggleButton" part="toggle-button" slot="suffix" aria-hidden="true"></div>
</vaadin-input-container>
<div part="helper-text">
<slot name="helper"></slot>
</div>
<div part="error-message">
<slot name="error-message"></slot>
</div>
</div>
<vaadin-combo-box-overlay
id="overlay"
.opened="${this._overlayOpened}"
?loading="${this.loading}"
theme="${ifDefined(this._theme)}"
.positionTarget="${this._positionTarget}"
no-vertical-overlap
></vaadin-combo-box-overlay>
<slot name="tooltip"></slot>
`;
}
/** @protected */
ready() {
super.ready();
this.addController(
new InputController(this, (input) => {
this._setInputElement(input);
this._setFocusElement(input);
this.stateTarget = input;
this.ariaTarget = input;
}),
);
this.addController(new LabelledInputController(this.inputElement, this._labelController));
this._tooltipController = new TooltipController(this);
this.addController(this._tooltipController);
this._tooltipController.setPosition('top');
this._tooltipController.setShouldShow((target) => !target.opened);
this._positionTarget = this.shadowRoot.querySelector('[part="input-field"]');
this._toggleElement = this.$.toggleButton;
}
/**
* Override the method from `InputControlMixin`
* to stop event propagation to prevent `ComboBoxMixin`
* from handling this click event also on its own.
*
* @param {Event} event
* @protected
* @override
*/
_onClearButtonClick(event) {
event.stopPropagation();
super._onClearButtonClick(event);
}
/**
* @param {Event} event
* @protected
*/
_onHostClick(event) {
const path = event.composedPath();
// Open dropdown only when clicking on the label or input field
if (path.includes(this._labelNode) || path.includes(this._positionTarget)) {
super._onHostClick(event);
}
}
}
defineCustomElement(ComboBox);
export { ComboBox };