From 2eb8a3e4cd8049b9ff20768b708c4f3830a75f2a Mon Sep 17 00:00:00 2001 From: Jeff Daley Date: Wed, 18 Oct 2023 14:49:45 -0400 Subject: [PATCH] Add popover settings to ProductSelect --- web/app/components/inputs/product-select.hbs | 2 + web/app/components/inputs/product-select.ts | 5 +- .../components/inputs/product-select-test.ts | 56 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/web/app/components/inputs/product-select.hbs b/web/app/components/inputs/product-select.hbs index 9a937b245..3ae9612c1 100644 --- a/web/app/components/inputs/product-select.hbs +++ b/web/app/components/inputs/product-select.hbs @@ -33,6 +33,8 @@ @placement={{@placement}} @isSaving={{@isSaving}} @renderOut={{@renderOut}} + @offset={{@offset}} + @matchAnchorWidth={{@matchAnchorWidth}} @secondaryFilterAttribute="abbreviation" class="product-select-dropdown-list w-[300px]" ...attributes diff --git a/web/app/components/inputs/product-select.ts b/web/app/components/inputs/product-select.ts index af251e8f7..26b5e8b94 100644 --- a/web/app/components/inputs/product-select.ts +++ b/web/app/components/inputs/product-select.ts @@ -1,7 +1,7 @@ import { assert } from "@ember/debug"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; -import { Placement } from "@floating-ui/dom"; +import { OffsetOptions, Placement } from "@floating-ui/dom"; import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { task } from "ember-concurrency"; @@ -10,6 +10,7 @@ import ProductAreasService, { ProductArea, } from "hermes/services/product-areas"; import getProductId from "hermes/utils/get-product-id"; +import { MatchAnchorWidthOptions } from "../floating-u-i/content"; interface InputsProductSelectSignature { Element: HTMLDivElement; @@ -20,6 +21,8 @@ interface InputsProductSelectSignature { placement?: Placement; isSaving?: boolean; renderOut?: boolean; + offset?: OffsetOptions; + matchAnchorWidth?: MatchAnchorWidthOptions; }; } diff --git a/web/tests/integration/components/inputs/product-select-test.ts b/web/tests/integration/components/inputs/product-select-test.ts index 75a212c02..47c72df04 100644 --- a/web/tests/integration/components/inputs/product-select-test.ts +++ b/web/tests/integration/components/inputs/product-select-test.ts @@ -1,13 +1,16 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "ember-qunit"; import { hbs } from "ember-cli-htmlbars"; -import { click, render } from "@ember/test-helpers"; +import { click, find, render } from "@ember/test-helpers"; import { setupMirage } from "ember-cli-mirage/test-support"; import { MirageTestContext } from "ember-cli-mirage/test-support"; import { Placement } from "@floating-ui/dom"; import { Response } from "miragejs"; +import { assert as emberAssert } from "@ember/debug"; +import htmlElement from "hermes/utils/html-element"; const TOGGLE = "[data-test-x-dropdown-list-toggle-select]"; +const POPOVER = "[data-test-x-dropdown-list-content]"; const DROPDOWN_PRODUCT = "[data-test-x-dropdown-list-content] [data-test-product-select-item]"; @@ -131,4 +134,55 @@ module("Integration | Component | inputs/product-select", function (hooks) { .dom("[data-test-product-select-failed-to-load-button]") .hasText("Retry"); }); + + test("the standard select can match the anchor width", async function (this: InputsProductSelectContext, assert) { + await render(hbs` +
+ +
+ `); + + /** + * We include `width` and `max-width` on the component + * to demonstrate that the popover is sized by `matchAnchorWidth` + */ + + await click(TOGGLE); + + const buttonWidth = htmlElement(TOGGLE)?.offsetWidth; + const popoverWidth = htmlElement(POPOVER)?.offsetWidth; + + assert.equal(buttonWidth, 800); + assert.equal(popoverWidth, 800); + }); + + test("the standard select can be positioned at an offset", async function (this: InputsProductSelectContext, assert) { + await render(hbs` + + `); + + const toggle = htmlElement(TOGGLE); + + const toggleLeft = toggle.offsetLeft; + const toggleBottom = toggle.offsetTop + toggle.offsetHeight; + + await click(TOGGLE); + + const popover = htmlElement(POPOVER); + + const popoverLeft = popover.offsetLeft; + const popoverTop = popover.offsetTop; + + assert.equal(toggleLeft + 100, popoverLeft); + assert.equal(toggleBottom + 100, popoverTop); + }); });