Allow pressing enter to pick item at the top of the list (#11139)

This commit is contained in:
Paulus Schoutsen 2022-01-12 17:52:29 -08:00 committed by GitHub
parent 5e388b1f02
commit 74a05929be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,7 +51,7 @@ class StepFlowPickHandler extends LitElement {
private _height?: number; private _height?: number;
private _getHandlers = memoizeOne( private _filterHandlers = memoizeOne(
(h: string[], filter?: string, _localize?: LocalizeFunc) => { (h: string[], filter?: string, _localize?: LocalizeFunc) => {
const handlers: HandlerObj[] = h.map((handler) => ({ const handlers: HandlerObj[] = h.map((handler) => ({
name: domainToName(this.hass.localize, handler), name: domainToName(this.hass.localize, handler),
@ -75,11 +75,7 @@ class StepFlowPickHandler extends LitElement {
); );
protected render(): TemplateResult { protected render(): TemplateResult {
const handlers = this._getHandlers( const handlers = this._getHandlers();
this.handlers,
this._filter,
this.hass.localize
);
return html` return html`
<h2>${this.hass.localize("ui.panel.config.integrations.new")}</h2> <h2>${this.hass.localize("ui.panel.config.integrations.new")}</h2>
@ -89,6 +85,7 @@ class StepFlowPickHandler extends LitElement {
.filter=${this._filter} .filter=${this._filter}
@value-changed=${this._filterChanged} @value-changed=${this._filterChanged}
.label=${this.hass.localize("ui.panel.config.integrations.search")} .label=${this.hass.localize("ui.panel.config.integrations.search")}
@keypress=${this._maybeSubmit}
></search-input> ></search-input>
<div <div
style=${styleMap({ style=${styleMap({
@ -180,6 +177,14 @@ class StepFlowPickHandler extends LitElement {
} }
} }
private _getHandlers() {
return this._filterHandlers(
this.handlers,
this._filter,
this.hass.localize
);
}
private async _filterChanged(e) { private async _filterChanged(e) {
this._filter = e.detail.value; this._filter = e.detail.value;
} }
@ -190,6 +195,20 @@ class StepFlowPickHandler extends LitElement {
}); });
} }
private _maybeSubmit(ev: KeyboardEvent) {
if (ev.key !== "Enter") {
return;
}
const handlers = this._getHandlers();
if (handlers.length > 0) {
fireEvent(this, "handler-picked", {
handler: handlers[0].slug,
});
}
}
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return [ return [
configFlowContentStyles, configFlowContentStyles,