Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sbb-select): fix display value in SSR context #3027

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/elements/select/select.ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,48 @@ import '../option.js';
describe(`sbb-select ssr`, () => {
let root: SbbSelectElement;

beforeEach(async () => {
it('renders', async () => {
root = await ssrHydratedFixture(
html`
<sbb-select placeholder="Placeholder">
<sbb-select placeholder="Placeholder" value="1">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be reverted (test with no value)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Will fix this

<sbb-option id="option-1" value="1">First</sbb-option>
<sbb-option id="option-2" value="2">Second</sbb-option>
<sbb-option id="option-3" value="3">Third</sbb-option>
</sbb-select>
`,
{ modules: ['./select.js', '../option.js'] },
);

assert.instanceOf(root, SbbSelectElement);
});

it('renders', () => {
it('renders with value attribute', async () => {
root = await ssrHydratedFixture(
html`
<sbb-select placeholder="Placeholder" value="1">
<sbb-option id="option-1" value="1">First</sbb-option>
<sbb-option id="option-2" value="2">Second</sbb-option>
<sbb-option id="option-3" value="3">Third</sbb-option>
</sbb-select>
`,
{ modules: ['./select.js', '../option.js'] },
);

assert.instanceOf(root, SbbSelectElement);
});

it('renders with value property', async () => {
root = await ssrHydratedFixture(
html`
<sbb-select placeholder="Placeholder" .value=${1}>
<sbb-option id="option-1" value="1">First</sbb-option>
<sbb-option id="option-2" value="2">Second</sbb-option>
<sbb-option id="option-3" value="3">Third</sbb-option>
</sbb-select>
`,
{ modules: ['./select.js', '../option.js'] },
);

assert.instanceOf(root, SbbSelectElement);
});
});
29 changes: 22 additions & 7 deletions src/elements/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import type { CSSResultGroup, PropertyValues, TemplateResult } from 'lit';
import { html, nothing } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { ref } from 'lit/directives/ref.js';
import { until } from 'lit/directives/until.js';

import { getNextElementIndex } from '../core/a11y.js';
import { SbbOpenCloseBaseElement } from '../core/base-elements.js';
import { SbbConnectedAbortController } from '../core/controllers.js';
import { hostAttributes } from '../core/decorators.js';
import { getDocumentWritingMode, isNextjs, isSafari } from '../core/dom.js';
import { EventEmitter } from '../core/eventing.js';
import { SbbDisabledMixin, SbbNegativeMixin, SbbUpdateSchedulerMixin } from '../core/mixins.js';
import {
SbbDisabledMixin,
SbbHydrationMixin,
SbbNegativeMixin,
SbbUpdateSchedulerMixin,
} from '../core/mixins.js';
import { isEventOnElement, overlayGapFixCorners, setOverlayPosition } from '../core/overlay.js';
import type { SbbOptGroupElement, SbbOptionElement } from '../option.js';

Expand Down Expand Up @@ -49,7 +55,7 @@ export interface SelectChange {
role: ariaRoleOnHost ? 'listbox' : null,
})
export class SbbSelectElement extends SbbUpdateSchedulerMixin(
SbbDisabledMixin(SbbNegativeMixin(SbbOpenCloseBaseElement)),
SbbDisabledMixin(SbbNegativeMixin(SbbHydrationMixin(SbbOpenCloseBaseElement))),
) {
public static override styles: CSSResultGroup = style;

Expand Down Expand Up @@ -163,7 +169,7 @@ export class SbbSelectElement extends SbbUpdateSchedulerMixin(

/** Gets the current displayed value. */
public getDisplayValue(): string {
return !this._displayValue ? '' : this._displayValue;
return this._displayValue ?? '';
}

/** Listens to option changes. */
Expand Down Expand Up @@ -679,7 +685,18 @@ export class SbbSelectElement extends SbbUpdateSchedulerMixin(
}
}

private async _deferredDisplayValue(placeholder: TemplateResult): Promise<TemplateResult | null> {
if (this.hydrationRequired) {
await this.hydrationComplete;
}
return this._displayValue ? html`${this._displayValue}` : placeholder;
}

protected override render(): TemplateResult {
const placeholder = html`<span class="sbb-select__trigger--placeholder">
${this.placeholder}
</span>`;

return html`
<!-- This element is visually hidden and will be appended to the light DOM to allow screen
readers to work properly -->
Expand All @@ -696,14 +713,12 @@ export class SbbSelectElement extends SbbUpdateSchedulerMixin(
@click=${this._toggleOpening}
${ref((ref) => (this._triggerElement = ref as HTMLElement))}
>
${this._displayValue ? html`${this._displayValue}` : html`<span>${this.placeholder}</span>`}
${until(this._deferredDisplayValue(placeholder), placeholder)}
</div>

<!-- Visually display the value -->
<div class="sbb-select__trigger" aria-hidden="true">
${this._displayValue
? html`${this._displayValue}`
: html`<span class="sbb-select__trigger--placeholder">${this.placeholder}</span>`}
${until(this._deferredDisplayValue(placeholder), placeholder)}
</div>

<div class="sbb-select__gap-fix"></div>
Expand Down
Loading