-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BadgeDropdownList component (#185)
* BadgeDropdownList component * BadgeDropdownList component * Add custom-item test * Remove unnecessary comment * Resolve merge conflict * Remove product specificity
- Loading branch information
Showing
8 changed files
with
254 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{{! @glint-nocheck - not typesafe yet }} | ||
<X::DropdownList | ||
@items={{@items}} | ||
@listIsOrdered={{@listIsOrdered}} | ||
@onItemClick={{@onItemClick}} | ||
@selected={{@selected}} | ||
...attributes | ||
> | ||
<:anchor as |dd|> | ||
<div class="relative w-full"> | ||
{{#if @isSaving}} | ||
<div class="absolute right-0 top-1/2 -translate-y-1/2"> | ||
<FlightIcon | ||
data-test-badge-dropdown-list-saving-icon | ||
@name="loading" | ||
/> | ||
</div> | ||
{{/if}} | ||
<dd.ToggleAction | ||
data-test-badge-dropdown-trigger | ||
class="relative {{if @isSaving 'opacity-50'}}" | ||
> | ||
<Hds::Badge | ||
data-test-badge-dropdown-list-icon | ||
data-test-icon={{if @selected (get-product-id @selected)}} | ||
@text={{or @selected "--"}} | ||
@icon={{@icon}} | ||
class="hds-badge-dropdown" | ||
/> | ||
<FlightIcon | ||
data-test-badge-dropdown-list-chevron-icon | ||
data-test-chevron-position={{if dd.contentIsShown "up" "down"}} | ||
@name={{if dd.contentIsShown "chevron-up" "chevron-down"}} | ||
class="dropdown-caret" | ||
/> | ||
</dd.ToggleAction> | ||
</div> | ||
</:anchor> | ||
<:item as |dd|> | ||
{{#if (has-block "item")}} | ||
{{yield dd to="item"}} | ||
{{else}} | ||
<dd.Action data-test-badge-dropdown-list-default-action> | ||
<X::DropdownList::CheckableItem | ||
@selected={{dd.selected}} | ||
@value={{dd.value}} | ||
/> | ||
</dd.Action> | ||
{{/if}} | ||
</:item> | ||
</X::DropdownList> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Placement } from "@floating-ui/dom"; | ||
import Component from "@glimmer/component"; | ||
|
||
interface InputsBadgeDropdownListComponentSignature { | ||
Element: HTMLDivElement; | ||
Args: { | ||
items: any; | ||
selected?: any; | ||
listIsOrdered?: boolean; | ||
isSaving?: boolean; | ||
onItemClick: ((e: Event) => void) | ((e: string) => void); | ||
placement?: Placement; | ||
icon: string; | ||
}; | ||
Blocks: { | ||
default: []; | ||
item: [dd: any]; | ||
}; | ||
} | ||
|
||
export default class InputsBadgeDropdownListComponent extends Component<InputsBadgeDropdownListComponentSignature> {} | ||
|
||
declare module "@glint/environment-ember-loose/registry" { | ||
export default interface Registry { | ||
"Inputs::BadgeDropdownList": typeof InputsBadgeDropdownListComponent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
web/tests/integration/components/inputs/badge-dropdown-list-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { module, test } from "qunit"; | ||
import { setupRenderingTest } from "ember-qunit"; | ||
import { hbs } from "ember-cli-htmlbars"; | ||
import { click, findAll, 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 getProductId from "hermes/utils/get-product-id"; | ||
|
||
interface BadgeDropdownListTestContext extends MirageTestContext { | ||
items: any; | ||
selected?: any; | ||
listIsOrdered?: boolean; | ||
isSaving?: boolean; | ||
onItemClick: ((e: Event) => void) | ((selected: string) => void); | ||
placement?: Placement; | ||
icon: string; | ||
updateIcon: () => void; | ||
} | ||
|
||
const TRIGGER_SELECTOR = "[data-test-badge-dropdown-trigger]"; | ||
const ITEM_SELECTOR = "[data-test-x-dropdown-list-item]"; | ||
const DEFAULT_ACTION_SELECTOR = | ||
"[data-test-badge-dropdown-list-default-action]"; | ||
|
||
module( | ||
"Integration | Component | inputs/badge-dropdown-list", | ||
function (hooks) { | ||
setupRenderingTest(hooks); | ||
setupMirage(hooks); | ||
|
||
hooks.beforeEach(function (this: BadgeDropdownListTestContext) { | ||
this.items = { Waypoint: {}, Labs: {}, Boundary: {} }; | ||
this.selected = Object.keys(this.items)[1]; | ||
|
||
const updateIcon = () => { | ||
let icon = "folder"; | ||
if (this.selected && getProductId(this.selected)) { | ||
icon = getProductId(this.selected) as string; | ||
} | ||
this.set("icon", icon); | ||
}; | ||
|
||
updateIcon(); | ||
|
||
this.onItemClick = (selected: string) => { | ||
this.set("selected", selected); | ||
updateIcon(); | ||
}; | ||
}); | ||
|
||
test("it functions as expected (default checkable item)", async function (this: BadgeDropdownListTestContext, assert) { | ||
await render<BadgeDropdownListTestContext>(hbs` | ||
{{! @glint-ignore: not typed yet }} | ||
<Inputs::BadgeDropdownList | ||
@items={{this.items}} | ||
@selected={{this.selected}} | ||
@onItemClick={{this.onItemClick}} | ||
@icon={{this.icon}} | ||
/> | ||
`); | ||
|
||
const iconSelector = "[data-test-badge-dropdown-list-icon] .flight-icon"; | ||
const chevronSelector = "[data-test-badge-dropdown-list-chevron-icon]"; | ||
|
||
assert.dom(iconSelector).hasAttribute("data-test-icon", "folder"); | ||
assert.dom(TRIGGER_SELECTOR).hasText("Labs"); | ||
assert | ||
.dom(chevronSelector) | ||
.hasAttribute("data-test-chevron-position", "down"); | ||
|
||
await click(TRIGGER_SELECTOR); | ||
|
||
let listItemsText = findAll(DEFAULT_ACTION_SELECTOR).map((el) => | ||
el.textContent?.trim() | ||
); | ||
|
||
assert.deepEqual( | ||
listItemsText, | ||
["Waypoint", "Labs", "Boundary"], | ||
"correct list items are rendered" | ||
); | ||
|
||
assert | ||
.dom( | ||
`${ITEM_SELECTOR}:nth-child(2) [data-test-x-dropdown-list-checkable-item-check]` | ||
) | ||
.hasAttribute("data-test-is-checked"); | ||
|
||
await click(DEFAULT_ACTION_SELECTOR); | ||
|
||
assert.dom(TRIGGER_SELECTOR).hasText("Waypoint"); | ||
assert.dom(iconSelector).hasAttribute("data-test-icon", "waypoint"); | ||
assert | ||
.dom(chevronSelector) | ||
.hasAttribute("data-test-chevron-position", "down"); | ||
}); | ||
|
||
test("it functions as expected (custom interactive item)", async function (this: BadgeDropdownListTestContext, assert) { | ||
await render<BadgeDropdownListTestContext>(hbs` | ||
{{! @glint-ignore: not typed yet }} | ||
<Inputs::BadgeDropdownList | ||
@items={{this.items}} | ||
@selected={{this.selected}} | ||
@onItemClick={{this.onItemClick}} | ||
@icon={{this.icon}} | ||
> | ||
<:item as |dd|> | ||
<dd.Action> | ||
{{dd.value}} | ||
{{#if dd.selected}} | ||
<span>(selected)</span> | ||
{{/if}} | ||
</dd.Action> | ||
</:item> | ||
</Inputs::BadgeDropdownList> | ||
`); | ||
|
||
await click(TRIGGER_SELECTOR); | ||
|
||
assert.dom(ITEM_SELECTOR).hasText("Waypoint"); | ||
|
||
assert | ||
.dom(DEFAULT_ACTION_SELECTOR) | ||
.doesNotExist("default action is not rendered"); | ||
|
||
assert.dom(`${ITEM_SELECTOR}:nth-child(2)`).hasText("Labs (selected)"); | ||
}); | ||
} | ||
); |