Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

feat: lists react to value prop changes in items #750

Merged
merged 6 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions src/components/calcite-pick-list-item/calcite-pick-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class CalcitePickListItem {
*/
@Prop({ reflect: true }) value!: string;

@Watch("value") valueWatchHandler(newValue, oldValue) {
this.calciteListItemValueChange.emit({ oldValue, newValue });
}

// --------------------------------------------------------------------------
//
// Private Properties
Expand Down Expand Up @@ -127,6 +131,13 @@ export class CalcitePickListItem {
*/
@Event() calciteListItemPropsUpdated: EventEmitter;

/**
* Emitted whenever the the item's value property is modified.
* @event calciteListItemValueChange
* @internal
*/
@Event() calciteListItemValueChange: EventEmitter;

// --------------------------------------------------------------------------
//
// Public Methods
Expand Down
5 changes: 5 additions & 0 deletions src/components/calcite-pick-list/calcite-pick-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
initializeObserver,
cleanUpObserver,
calciteListItemChangeHandler,
calciteListItemValueChangeHandler,
setUpItems,
deselectSiblingItems,
selectSiblings,
Expand Down Expand Up @@ -135,6 +136,10 @@ export class CalcitePickList {
this.setUpFilter();
}

@Listen("calciteListItemValueChange") calciteListItemValueChangeHandler(event: CustomEvent) {
calciteListItemValueChangeHandler.call(this, event);
}

// --------------------------------------------------------------------------
//
// Private Methods
Expand Down
9 changes: 9 additions & 0 deletions src/components/calcite-pick-list/shared-list-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export const sharedListMethods = {
this.lastSelectedItem = item;
this.emitCalciteListChange();
},
calciteListItemValueChangeHandler(this: CalcitePickList | CalciteValueList, event: CustomEvent): void {
event.stopPropagation();
const oldValue = event.detail.oldValue;
if (this.selectedValues.has(oldValue)) {
const item = this.selectedValues.get(oldValue);
this.selectedValues.delete(oldValue);
this.selectedValues.set(event.detail.newValue, item);
}
},
// --------------------------------------------------------------------------
//
// Private Methods
Expand Down
43 changes: 43 additions & 0 deletions src/components/calcite-pick-list/shared-list-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,49 @@ export const tests = {
expect(properties.get("hasItem")._remoteObject.value).toBe(true);
});
});
describe("value changes after item is selected", () => {
it("should update the value in selectedValues map", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-${listType}-list>
<calcite-${listType}-list-item value="one" text-label="One" selected></calcite-${listType}-list-item>
<calcite-${listType}-list-item value="two" text-label="Two" selected></calcite-${listType}-list-item>
<calcite-${listType}-list-item value="three" text-label="Three" selected></calcite-${listType}-list-item>
</calcite-${listType}-list>`);

const list = await page.find(`calcite-${listType}-list`);
const item1 = await list.find("[value=one]");
await item1.click();

const hasValueOne = await page.evaluate((type) => {
const pageList: HTMLCalcitePickListElement | HTMLCalciteValueListElement = document.querySelector(
`calcite-${type}-list`
);
return pageList.getSelectedItems().then((result) => {
return result.has("one");
});
}, listType);

expect(hasValueOne).toBe(true);

item1.setProperty("value", "four");
await page.waitForChanges();

const hasValues = await page.evaluate((type) => {
const pageList: HTMLCalcitePickListElement | HTMLCalciteValueListElement = document.querySelector(
`calcite-${type}-list`
);
return pageList.getSelectedItems().then((result) => {
return {
four: result.has("four"),
one: result.has("one")
};
});
}, listType);

expect(hasValues.one).toBe(false);
expect(hasValues.four).toBe(true);
});
});
},
filterBehavior(listType: "pick" | "value") {
let page: E2EPage = null;
Expand Down
5 changes: 5 additions & 0 deletions src/components/calcite-value-list/calcite-value-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
initializeObserver,
cleanUpObserver,
calciteListItemChangeHandler,
calciteListItemValueChangeHandler,
setUpItems,
deselectSiblingItems,
selectSiblings,
Expand Down Expand Up @@ -152,6 +153,10 @@ export class CalciteValueList {
this.setUpFilter();
}

@Listen("calciteListItemValueChange") calciteListItemValueChangeHandler(event: CustomEvent) {
calciteListItemValueChangeHandler.call(this, event);
}

// --------------------------------------------------------------------------
//
// Private Methods
Expand Down
57 changes: 57 additions & 0 deletions src/demos/value-list/valueUpdate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
Copy link
Member

Choose a reason for hiding this comment

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

For consistency, the demo filename should be kebab-cased.

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" />
<title>Demos - ValueList</title>
<script src="../_assets/head.js"></script>
<script src="../_assets/toggles.js"></script>
</head>
<body>
<main>
<section class="example-container">
<h1>calcite-value-list</h1>
<p>
see the
<a
href="https://github.com/Esri/calcite-app-components/tree/master/src/components/calcite-value-list/readme.md"
>calcite-value-list readme.</a
>
</p>

<h3>multiple</h3>
<calcite-value-list id="one">
<calcite-action slot="menu-actions" indicator text="Cool">
<calcite-icon icon="hamburger" scale="s"></calcite-icon>
</calcite-action>
<calcite-value-list-item text-label="Dogs" text-description="Man's best friend" value="dogs">
<calcite-action slot="secondary-action" text="click-me" onClick="console.log('clicked');">
<calcite-icon icon="x" scale="s"></calcite-icon>
</calcite-action>
</calcite-value-list-item>
<calcite-value-list-item text-label="Cats" text-description="Independent and fluffy" value="cats">
<calcite-action slot="secondary-action" text="click-me" onClick="console.log('clicked');">
<calcite-icon icon="x" scale="s"></calcite-icon>
</calcite-action>
</calcite-value-list-item>
<calcite-value-list-item
text-label="Fish. But not just any fish, a tiger fish caught live in the Atlantic Ocean while on vacation."
text-description="Easy to care for."
value="fish"
>
<calcite-action slot="secondary-action" text="click-me" onClick="console.log('clicked');">
<calcite-icon icon="x" scale="s"></calcite-icon>
</calcite-action>
</calcite-value-list-item>
</calcite-value-list>
<button id="click">Click</button>
<script>
document.querySelector("#click").addEventListener("click", (event) => {
const valueList = document.querySelector("#one");
valueList.querySelector("calcite-value-list-item").value = "not dogs";
});
</script>
</section>
</main>
</body>
</html>