-
Notifications
You must be signed in to change notification settings - Fork 34
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
support listbox with multiple selection #171
base: master
Are you sure you want to change the base?
Changes from 2 commits
e9baedf
9a8f325
1e5b261
dad2bbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ import Component from '@glimmer/component'; | |
import { tracked } from '@glimmer/tracking'; | ||
import { action } from '@ember/object'; | ||
import { guidFor } from '@ember/object/internals'; | ||
import { debounce } from '@ember/runloop'; | ||
import { debounce, scheduleOnce } from '@ember/runloop'; | ||
|
||
import { TrackedSet } from 'tracked-maps-and-sets'; | ||
|
||
const ACTIVATE_NONE = 0; | ||
const ACTIVATE_FIRST = 1; | ||
|
@@ -30,7 +32,7 @@ export default class ListboxComponent extends Component { | |
optionElements = []; | ||
optionValues = {}; | ||
search = ''; | ||
@tracked selectedOptionIndex; | ||
@tracked selectedOptionIndexes = new TrackedSet(); | ||
|
||
get activeOptionGuid() { | ||
return this.optionElements[this.activeOptionIndex]?.id; | ||
|
@@ -40,8 +42,10 @@ export default class ListboxComponent extends Component { | |
return !!this.args.disabled; | ||
} | ||
|
||
get selectedOptionGuid() { | ||
return this.optionElements[this.selectedOptionIndex]?.id; | ||
get selectedOptionGuids() { | ||
return Array.from(this.selectedOptionIndexes).map( | ||
(i) => this.optionElements[i]?.id | ||
); | ||
} | ||
|
||
get isOpen() { | ||
|
@@ -51,7 +55,7 @@ export default class ListboxComponent extends Component { | |
set isOpen(isOpen) { | ||
if (isOpen) { | ||
this.activeOptionIndex = undefined; | ||
this.selectedOptionIndex = undefined; | ||
this.selectedOptionIndexes.clear(); | ||
this.optionElements = []; | ||
this.optionValues = {}; | ||
this._isOpen = true; | ||
|
@@ -132,7 +136,9 @@ export default class ListboxComponent extends Component { | |
this.activateBehaviour = ACTIVATE_FIRST; | ||
if (this.isOpen) { | ||
this.setSelectedOption(event.target, event); | ||
this.isOpen = false; | ||
if (!this.args.multiple) { | ||
this.isOpen = false; | ||
} | ||
} else { | ||
this.isOpen = true; | ||
} | ||
|
@@ -181,15 +187,27 @@ export default class ListboxComponent extends Component { | |
optionElement.setAttribute('data-index', this.optionElements.length - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any chance you'd be willing to do a PR that removes all this "registerElement" stuff? it's very side-effect heavy and causes double (or more!) renders |
||
|
||
if (this.args.value) { | ||
if (this.args.value === optionComponent.args.value) { | ||
this.selectedOptionIndex = this.activeOptionIndex = | ||
this.optionElements.length - 1; | ||
let isSelected; | ||
|
||
if (this.args.multiple) { | ||
isSelected = this.args.value.includes(optionComponent.args.value); | ||
} else { | ||
isSelected = this.args.value === optionComponent.args.value; | ||
} | ||
|
||
if (isSelected) { | ||
this.selectedOptionIndexes.add(this.optionElements.length - 1); | ||
this.activeOptionIndex = this.optionElements.length - 1; | ||
|
||
this.scrollIntoView(optionElement); | ||
} | ||
} | ||
|
||
if (!this.selectedOptionIndex) { | ||
scheduleOnce('afterRender', this, this.setDefaultActiveOption); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid hacking runloop usage? runloop is kinda anti-patterny and causes performance problems |
||
} | ||
|
||
setDefaultActiveOption() { | ||
if (this.selectedOptionIndexes.size === 0) { | ||
switch (this.activateBehaviour) { | ||
case ACTIVATE_FIRST: | ||
this.setFirstOptionActive(); | ||
|
@@ -198,6 +216,8 @@ export default class ListboxComponent extends Component { | |
this.setLastOptionActive(); | ||
break; | ||
} | ||
} else { | ||
this.activeOptionIndex = Math.min(...this.selectedOptionIndexes); | ||
} | ||
} | ||
|
||
|
@@ -239,13 +259,23 @@ export default class ListboxComponent extends Component { | |
} | ||
|
||
if (!this.optionElements[optionIndex].hasAttribute('disabled')) { | ||
this.selectedOptionIndex = optionIndex; | ||
|
||
if (this.args.onChange) { | ||
this.args.onChange(optionValue); | ||
if (this.args.multiple) { | ||
let value = this.args.value ?? []; | ||
|
||
if (this.selectedOptionIndexes.has(optionIndex)) { | ||
optionValue = value.filter((i) => i !== optionValue); | ||
this.selectedOptionIndexes.delete(optionIndex); | ||
} else { | ||
optionValue = [...value, optionValue]; | ||
this.selectedOptionIndexes.add(optionIndex); | ||
} | ||
} else { | ||
this.selectedOptionIndexes.add(optionIndex); | ||
} | ||
|
||
if (e.type === 'click') { | ||
this.args.onChange?.(optionValue); | ||
|
||
if (e.type === 'click' && !this.args.multiple) { | ||
this.isOpen = false; | ||
} | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
way make an array that ends up being a separate array each access?
does this need to be a getter?
can we access the optionElements directly?