Skip to content

Instantly share code, notes, and snippets.

@Dornhoth
Last active June 1, 2020 18:57
Show Gist options
  • Save Dornhoth/29d1c708b302f522b49b7e4e89f28afb to your computer and use it in GitHub Desktop.
Save Dornhoth/29d1c708b302f522b49b7e4e89f28afb to your computer and use it in GitHub Desktop.
export class MultiselectComponent implements ControlValueAccessor {
[...]
open = false;
disabled = false;
[...]
writeValue(items: Array<ListItem>): void {
this.selectedItems = items || [];
this.onChange(this.selectedItems);
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouch = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.disabled = isDisabled;
}
onItemClick(item: ListItem) {
if (!this.disabled) {
const found = this.isItemAlreadySelected(item);
if (!found) {
this.select(item);
} else {
this.unselect(item);
}
this.onChange(this.items);
}
}
[...]
toggleDropdown() {
if (!this.disabled) {
this.open = !this.open;
}
}
closeDropdown() {
this.open = false;
}
[...]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment