Skip to content

refactor(web-components/pinwheel): update value property to be readonly #485

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

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/sad-sloths-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tapsioss/web-components": minor
---

Update `value` property to be readonly

16 changes: 1 addition & 15 deletions packages/web-components/src/pinwheel/item/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ class ItemSelectionController extends SelectionController<PinwheelItem> {
this._host.dispatchEvent(new Event("change", { bubbles: true }));
}

public override handleSelectionChange(): void {
super.handleSelectionChange();

const parent = this._parentTarget as Pinwheel | null;

if (!parent) return;
if (parent.disabled) return;

const selectedItem = this._elements.find(item => item.selected);

if (!selectedItem) return;

parent.value = selectedItem.value;
}

public override async handleClick(event: MouseEvent): Promise<boolean> {
const parent = this._parentTarget as Pinwheel | null;

Expand All @@ -53,6 +38,7 @@ class ItemSelectionController extends SelectionController<PinwheelItem> {
if (!(await super.handleClick(event))) return false;

this._emitValueChange();
parent.setViewOnItem(this._host);

return true;
}
Expand Down
23 changes: 21 additions & 2 deletions packages/web-components/src/pinwheel/item/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type CSSResultGroup,
type TemplateResult,
} from "lit";
import { property } from "lit/decorators.js";
import { property, query } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { logger } from "../../utils/index.ts";
import ItemSelectionController from "./Controller.ts";
Expand All @@ -30,7 +30,7 @@ export class PinwheelItem extends LitElement {
* @attr {string} selected
* @default false
*/
@property({ type: Boolean, reflect: true })
@property({ type: Boolean })
public get selected(): boolean {
return this._selected;
}
Expand All @@ -56,8 +56,27 @@ export class PinwheelItem extends LitElement {
@property()
public value = "";

@query("#root", true)
private _root!: HTMLElement | null;

private readonly _selectionController = new ItemSelectionController(this);

public override connectedCallback(): void {
super.connectedCallback();

this.addEventListener("click", this._handleClick);
}

public override disconnectedCallback(): void {
super.disconnectedCallback();

this.removeEventListener("click", this._handleClick);
}

private _handleClick() {
this._root?.click();
}

protected override render(): TemplateResult {
if (!this.value) {
logger(
Expand Down
64 changes: 49 additions & 15 deletions packages/web-components/src/pinwheel/pinwheel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
disposeMocks,
expect,
render,
setupMocks,
test,
} from "@internals/test-helpers";
import { ErrorMessages, scope } from "./constants.ts";
Expand Down Expand Up @@ -39,7 +40,7 @@ describe("🧩 pinwheel", () => {
await expect(component).toBeFocused();
});

test("🧪 should change active item and trigger events (`activate` and `activechange`) using click", async ({
test("🧪 should update selected item and trigger events using keyboard", async ({
page,
}) => {
await render(
Expand All @@ -53,9 +54,29 @@ describe("🧩 pinwheel", () => {
`,
);

const mocks = await setupMocks(page);
const handleChange = mocks.createFakeFn();

const container = page.getByTestId("test-pinwheel");

await mocks.events.attachMockedEvent(container, "change", handleChange.ref);

await page.waitForFunction(
expected => {
const container = document.querySelector("tapsi-pinwheel");

if (!container) return false;

return container.value === expected;
},
"value-1",
{
timeout: 5000,
},
);

await expect(container).toHaveJSProperty("value", "value-1");
await handleChange.matchResult({ callCount: 0 });

// Focus on pinwheel for keyboard interaction
await page.keyboard.press("Tab");
Expand All @@ -64,22 +85,27 @@ describe("🧩 pinwheel", () => {
// Test arrow keys
await page.keyboard.press("ArrowDown");
await expect(container).toHaveJSProperty("value", "value-2");
await handleChange.matchResult({ callCount: 1 });

await page.keyboard.press("ArrowDown");
await expect(container).toHaveJSProperty("value", "value-3");
await handleChange.matchResult({ callCount: 2 });

await page.keyboard.press("ArrowUp");
await expect(container).toHaveJSProperty("value", "value-2");
await handleChange.matchResult({ callCount: 3 });

// Test Home and End keys
await page.keyboard.press("Home");
await expect(container).toHaveJSProperty("value", "value-1");
await handleChange.matchResult({ callCount: 4 });

await page.keyboard.press("End");
await expect(container).toHaveJSProperty("value", "value-3");
await handleChange.matchResult({ callCount: 5 });
});

test("🧪 should change active item and trigger events (`activate` and `activechange`) using keyboard navigation", async ({
test("🧪 should update selected item and trigger events using click", async ({
page,
}) => {
await render(
Expand All @@ -94,27 +120,35 @@ describe("🧩 pinwheel", () => {
);

const container = page.getByTestId("test-pinwheel");
const item1 = page.getByTestId("test-pinwheel-item-1");
const item2 = page.getByTestId("test-pinwheel-item-2");
const item3 = page.getByTestId("test-pinwheel-item-3");

await item1.click();
await expect(container).toHaveJSProperty("value", "value-1");
const mocks = await setupMocks(page);
const handleChange = mocks.createFakeFn();

await item2.click();
await expect(container).toHaveJSProperty("value", "value-2");
const click = (itemId: string) => {
return page.getByTestId(itemId).click();
};

await item3.click();
await expect(container).toHaveJSProperty("value", "value-3");
await mocks.events.attachMockedEvent(container, "change", handleChange.ref);
await handleChange.matchResult({ callCount: 0 });

await item1.click();
await click("test-pinwheel-item-1");
await expect(container).toHaveJSProperty("value", "value-1");
await handleChange.matchResult({ callCount: 0 });

await item3.click();
await click("test-pinwheel-item-2");
await expect(container).toHaveJSProperty("value", "value-2");
await handleChange.matchResult({ callCount: 1 });

await click("test-pinwheel-item-3");
await expect(container).toHaveJSProperty("value", "value-3");
await handleChange.matchResult({ callCount: 2 });

await item2.click();
await click("test-pinwheel-item-2");
await expect(container).toHaveJSProperty("value", "value-2");
await handleChange.matchResult({ callCount: 3 });

await click("test-pinwheel-item-1");
await expect(container).toHaveJSProperty("value", "value-1");
await handleChange.matchResult({ callCount: 4 });
});

test("🧪 should throw error if no valid label was set for the input", async ({
Expand Down
Loading