From e1750140ce386a0c87b40c951248c7bdb907f3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 29 Mar 2022 14:36:20 +0100 Subject: [PATCH] fix: Ensure error container is always in DOM (autocomplete) (#136) --- src/components/address-autocomplete/index.ts | 64 ++++++++++++++------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/src/components/address-autocomplete/index.ts b/src/components/address-autocomplete/index.ts index 11f3eab..c10114a 100644 --- a/src/components/address-autocomplete/index.ts +++ b/src/components/address-autocomplete/index.ts @@ -1,4 +1,4 @@ -import { html, LitElement, unsafeCSS } from "lit"; +import { html, LitElement, TemplateResult, unsafeCSS } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import accessibleAutocomplete from "accessible-autocomplete"; @@ -176,6 +176,45 @@ export class AddressAutocomplete extends LitElement { return styles; } + /** + * Render an errorMessage container + * Must always be visible to ensure that role="status" works for screenreaders + * @param errorMessage + * @returns TemplateResult + */ + _getErrorMessageContainer(errorMessage: string | undefined): TemplateResult { + const className = errorMessage ? "govuk-warning-text" : ""; + const content = errorMessage + ? html` + + Warning + ${errorMessage} + ` + : null; + + return html`
${content}
`; + } + + /** + * If not in state of error, return the autocomplete + * @param errorMessage + * @returns TemplateResult | null + */ + _getAutocomplete(errorMessage: string | undefined): TemplateResult | null { + return errorMessage + ? null + : html` + + +
+ `; + } + render() { // handle various error states let errorMessage; @@ -184,24 +223,11 @@ export class AddressAutocomplete extends LitElement { else if (this._totalAddresses === 0) errorMessage = `No addresses found in postcode ${this.postcode}`; - return errorMessage - ? html` -
- - - Warning - ${errorMessage} - -
` - : html` - - -
`; + return html` + + ${this._getErrorMessageContainer(errorMessage)} + ${this._getAutocomplete(errorMessage)} + `; } /**