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

Commit

Permalink
fix: Stencil build warnings (#537)
Browse files Browse the repository at this point in the history
* mutable select prop testing

* remove prop

* Fix for item not matching correctly.
Update test to fix event call count.

* make sure event only fires when one is selected.

* fix test. only emit when selected or multiple

* change test app event.

* debounce event

* cleanup redundant code

* cleanup

* change debounce to next tick

* review fixes. thanks @jcfranco

* fix type typo
  • Loading branch information
driskull authored Nov 14, 2019
1 parent 2bff76a commit d2e1721
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 50 deletions.
61 changes: 24 additions & 37 deletions src/components/calcite-pick-list-item/calcite-pick-list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Host,
Method,
Prop,
State,
Watch,
h
} from "@stencil/core";
Expand Down Expand Up @@ -54,14 +53,18 @@ export class CalcitePickListItem {
/**
* Set this to true to pre-select an item. Toggles when an item is checked/unchecked.
*/
@Prop() selected = false;
@Prop({ reflect: true, mutable: true }) selected = false;

@Watch("selected")
selectedWatchHandler(newValue) {
if (this.isSelected !== newValue) {
this.isSelected = newValue;
this.emitChangeEvent();
}
selectedWatchHandler() {
this.calciteListItemChange.emit({
item: this.el,
value: this.value,
selected: this.selected,
shiftPressed: this.shiftPressed
});

this.shiftPressed = false;
}

/**
Expand Down Expand Up @@ -104,7 +107,7 @@ export class CalcitePickListItem {

@Element() el: HTMLCalcitePickListItemElement;

@State() isSelected = this.selected;
shiftPressed: boolean;

// --------------------------------------------------------------------------
//
Expand Down Expand Up @@ -135,16 +138,13 @@ export class CalcitePickListItem {
/**
* Used to toggle the selection state. By default this won't trigger an event.
* The first argument allows the value to be coerced, rather than swapping values.
* The second argument, when true, allows an event to be emitted, just as if a user had clicked.
*/
@Method() async toggleSelected(coerce?: boolean, emit = false) {
@Method() async toggleSelected(coerce?: boolean) {
if (this.disabled) {
return;
}
this.isSelected = typeof coerce === "boolean" ? coerce : !this.isSelected;
if (emit) {
this.emitChangeEvent();
}

this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
}

// --------------------------------------------------------------------------
Expand All @@ -157,40 +157,27 @@ export class CalcitePickListItem {
if (this.disabled) {
return;
}
this.isSelected = !this.isSelected;
this.emitChangeEvent(event.shiftKey);

this.shiftPressed = event.shiftKey;
this.selected = !this.selected;
};

pickListKeyDownHandler = (event: KeyboardEvent): void => {
if (event.key === " ") {
event.preventDefault();
this.isSelected = !this.isSelected;
this.emitChangeEvent(false);
this.selected = !this.selected;
}
};

secondaryActionContainerClickHandler(event: MouseEvent) {
event.stopPropagation();
}

emitChangeEvent(shiftPressed = false) {
this.calciteListItemChange.emit({
item: this.el,
value: this.value,
selected: this.isSelected,
shiftPressed
});
}

// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------

renderIcon() {
const { icon } = this;
if (!icon || this.compact) {
const { compact, icon, selected } = this;
if (!icon || compact) {
return null;
}
if (icon === ICON_TYPES.grip) {
Expand All @@ -202,10 +189,10 @@ export class CalcitePickListItem {
} else {
const path =
icon === ICON_TYPES.square
? this.isSelected
? selected
? checkSquare16
: square16
: this.isSelected
: selected
? circleFilled16
: circle16;
return (
Expand All @@ -223,7 +210,7 @@ export class CalcitePickListItem {
) : null;

return (
<Host selected={this.isSelected} role="checkbox" aria-checked={this.isSelected}>
<Host role="checkbox" aria-checked={this.selected}>
<label
class={CSS.label}
onClick={this.pickListClickHandler}
Expand All @@ -237,7 +224,7 @@ export class CalcitePickListItem {
{description}
</div>
</label>
<div class={CSS.action} onClick={this.secondaryActionContainerClickHandler}>
<div class={CSS.action}>
<slot name="secondaryAction" />
</div>
</Host>
Expand Down
2 changes: 2 additions & 0 deletions src/components/calcite-pick-list/calcite-pick-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export class CalcitePickList {

@Element() el: HTMLCalcitePickListElement;

emitCalciteListChange: () => void;

// --------------------------------------------------------------------------
//
// Lifecycle
Expand Down
12 changes: 9 additions & 3 deletions src/components/calcite-pick-list/shared-list-logic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CalcitePickList } from "./calcite-pick-list";
import { CalciteValueList } from "../calcite-value-list/calcite-value-list";
import { debounce } from "lodash-es";

type pickListItemMap = Map<string, HTMLCalcitePickListItemElement>;
type valueListItemMap = Map<string, HTMLCalciteValueListItemElement>;
Expand Down Expand Up @@ -30,6 +31,7 @@ export const sharedListMethods = {
initialize(this: CalcitePickList | CalciteValueList): void {
this.setUpItems();
this.setUpFilter();
this.emitCalciteListChange = debounce(sharedListMethods.internalCalciteListChangeEvent.bind(this), 0);
},
initializeObserver(this: CalcitePickList | CalciteValueList): void {
this.observer.observe(this.el, { childList: true, subtree: true });
Expand All @@ -56,17 +58,21 @@ export const sharedListMethods = {
} else {
selectedValues.delete(value);
}

this.lastSelectedItem = item;
this.calciteListChange.emit(selectedValues);
this.emitCalciteListChange();
},
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
internalCalciteListChangeEvent(this: CalcitePickList | CalciteValueList): void {
this.calciteListChange.emit(this.selectedValues);
},
setUpItems(
this: CalcitePickList | CalciteValueList,
tagname: "calcite-pick-list-item" | "calcite-pick-list-item"
tagname: "calcite-pick-list-item" | "calcite-value-list-item"
): void {
this.items = Array.from(this.el.querySelectorAll(tagname));
this.items.forEach((item) => {
Expand All @@ -84,7 +90,7 @@ export const sharedListMethods = {
},
deselectSiblingItems(this: CalcitePickList | CalciteValueList, item: pickOrValueListItem) {
this.items.forEach((currentItem) => {
if (currentItem !== item) {
if (currentItem.value !== item.value) {
currentItem.toggleSelected(false);
if (this.selectedValues.has(currentItem.value)) {
this.selectedValues.delete(currentItem.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CalciteValueListItem {
/**
* Set this to true to pre-select an item. Toggles when an item is checked/unchecked.
*/
@Prop() selected = false;
@Prop({ reflect: true, mutable: true }) selected = false;

/**
* The main label for this item. Appears next to the icon.
Expand Down Expand Up @@ -69,8 +69,8 @@ export class CalciteValueListItem {
//
// --------------------------------------------------------------------------

@Method() async toggleSelected(coerce?: boolean, emit = false) {
this.pickListItem.toggleSelected(coerce, emit);
@Method() async toggleSelected(coerce?: boolean) {
this.pickListItem.toggleSelected(coerce);
}

// --------------------------------------------------------------------------
Expand Down
8 changes: 2 additions & 6 deletions src/components/calcite-value-list/calcite-value-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ export class CalciteValueList {

sortables: Sortable[] = [];

// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------

@Element() el: HTMLCalciteValueListItemElement;

emitCalciteListChange: () => void;

// --------------------------------------------------------------------------
//
// Lifecycle
Expand Down
2 changes: 1 addition & 1 deletion src/demos/calcite-pick-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ <h2>Pick List with sub-groups</h2>
</calcite-pick-list>
<script>
const pickList3 = document.querySelector("#three");
pickList3.addEventListener("calcitePickListSelectionChange", event => {
pickList3.addEventListener("calciteListChange", (event) => {
console.log(event.detail);
});
</script>
Expand Down

0 comments on commit d2e1721

Please sign in to comment.