Move gr-select to typescript

Change-Id: I8605e211b67a5c6d788304082e55ba8f19ea1e07
This commit is contained in:
Tao Zhou
2020-08-04 10:08:59 +02:00
parent c7b377f3b4
commit c840f607d9

View File

@@ -14,41 +14,41 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js'; import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js'; import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin';
import {PolymerElement} from '@polymer/polymer/polymer-element.js'; import {PolymerElement} from '@polymer/polymer/polymer-element';
import {html} from '@polymer/polymer/lib/utils/html-tag.js'; import {html} from '@polymer/polymer/lib/utils/html-tag';
import {customElement, property, observe} from '@polymer/decorators';
declare global {
interface HTMLElementTagNameMap {
'gr-select': GrSelect;
}
}
/** /**
* @extends PolymerElement * GrSelect `gr-select` component.
*/ */
class GrSelect extends GestureEventListeners( @customElement('gr-select')
LegacyElementMixin(PolymerElement)) { export class GrSelect extends GestureEventListeners(
static get is() { return 'gr-select'; } LegacyElementMixin(PolymerElement)
) {
static get template() { static get template() {
return html` return html` <slot></slot> `;
<slot></slot>
`;
} }
static get properties() { @property({type: String, notify: true})
return { bindValue?: string;
bindValue: {
type: String,
notify: true,
observer: '_updateValue',
},
};
}
get nativeSelect() { get nativeSelect() {
// gr-select is not a shadow component // gr-select is not a shadow component
// TODO(taoalpha): maybe we should convert // TODO(taoalpha): maybe we should convert
// it into a shadow dom component instead // it into a shadow dom component instead
return this.querySelector('select'); // TODO(TS): should warn if no `select` detected.
return this.querySelector('select')!;
} }
@observe('bindValue')
_updateValue() { _updateValue() {
// It's possible to have a value of 0. // It's possible to have a value of 0.
if (this.bindValue !== undefined) { if (this.bindValue !== undefined) {
@@ -58,7 +58,9 @@ class GrSelect extends GestureEventListeners(
// before options from a dom-repeat were rendered previously. // before options from a dom-repeat were rendered previously.
// See https://bugs.chromium.org/p/gerrit/issues/detail?id=7735 // See https://bugs.chromium.org/p/gerrit/issues/detail?id=7735
this.async(() => { this.async(() => {
this.nativeSelect.value = this.bindValue; // TODO(TS): maybe should check for undefined before assigning
// or fallback to ''
this.nativeSelect.value = this.bindValue!;
}, 1); }, 1);
} }
} }
@@ -74,20 +76,16 @@ class GrSelect extends GestureEventListeners(
/** @override */ /** @override */
created() { created() {
super.created(); super.created();
this.addEventListener('change', this.addEventListener('change', () => this._valueChanged());
() => this._valueChanged()); this.addEventListener('dom-change', () => this._updateValue());
this.addEventListener('dom-change',
() => this._updateValue());
} }
/** @override */ /** @override */
ready() { ready() {
super.ready(); super.ready();
// If not set via the property, set bind-value to the element value. // If not set via the property, set bind-value to the element value.
if (this.bindValue == undefined && this.nativeSelect.options.length > 0) { if (this.bindValue === undefined && this.nativeSelect.options.length > 0) {
this.bindValue = this.nativeSelect.value; this.bindValue = this.nativeSelect.value;
} }
} }
} }
customElements.define(GrSelect.is, GrSelect);