-
Notifications
You must be signed in to change notification settings - Fork 25
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
Comparator functionality #102
Conversation
addon/components/frost-list.js
Outdated
expandedItem => itemComparator(expandedItem, item)) >= 0) | ||
set(item, 'isSelected', | ||
isEmpty(selectedItems) ? false : selectedItems.findIndex( | ||
selectedItem => itemComparator(selectedItem, item)) >= 0) |
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.
ItemComparator should return a boolean, not an index, so you should go with something more like this:
set(item, 'isSelected', isEmpty(selectedItems) ? false : selectedItems.some(selectedItem => itemComparator(selectedItem, item)))
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.
I switched it over to use the .some function now.
addon/utils/selection.js
Outdated
const isSelected = !isCurrentlySelected | ||
if (isSelected) { | ||
selectedItems.pushObject(item) | ||
} else { | ||
selectedItems.removeObject(item) | ||
selectedItems.removeAt(index) | ||
// selectedItems.removeObject(item) |
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.
Remove obsolete code, if we do want to keep your updated code. You already have the item, so I'm not sure what we gain by switching to .removeAt()
.
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.
Removed it.
addon/utils/selection.js
Outdated
@@ -88,7 +93,8 @@ export default { | |||
|
|||
// If an endpoint was already selected remove selected items that were | |||
// in the previous range but aren't in the new range | |||
const previousEndpoint = items.indexOf(rangeState['endpoint']) | |||
// const previousEndpoint = items.indexOf(rangeState['endpoint']) |
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.
Remove obsolete code.
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.
Removed it.
const isCurrentlySelected = selectedItems.indexOf(item) >= 0 | ||
specific (selectedItems, item, rangeState, itemComparator) { | ||
const index = selectedItems.findIndex(selectedItem => itemComparator(selectedItem, item)) | ||
const isCurrentlySelected = (index >= 0) |
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.
You could just do this instead, so you don't have to .splice()
later on:
const isCurrentlySelected = selectedItems.some(selectedItem => itemComparator(selectedItem, item))
...
selectedItems.removeObject(item)
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.
Can't use .removeObject in most places where you use a comparator as the objects are not guaranteed to be the same even if they have the same contents.
).to.eql(true) | ||
describe('when doing basic click', function () { | ||
beforeEach(function () { | ||
this.$(hook('my-list-item', {index: 0})).click() |
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.
You should use $hook()
when you want a jQuery object rather than wrapping it in this.$()
.
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.
I believe I removed out all the this.$
}) | ||
|
||
it('item 0 is selected', function () { | ||
expect(this.$($hook('my-list-item-container', {index: 0})).hasClass('is-selected')).to.eql(true) |
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.
Please remove the this.$()
here and subsequent tests - it's not doing anything for you.
).to.eql(false) | ||
describe('when doing basic click and all is selected', function () { | ||
beforeEach(function () { | ||
this.$($(hook('my-list-item-container', {index: 0})).find(hook('my-list-selection-checkbox'))).click() |
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.
Like above, you should prefer $hook()
over $(hook())
.
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.
Also, we should try to just pass the index to the selection checkbox so you can select it with $hook('my-list-selection-checkbox', {index: 0})
This should do the trick in the frost-list-item-selection
template:
{{! Template for the frost-list-item-selection component }}
{{frost-checkbox
checked=isSelected
hook=(concat hookPrefix '-checkbox')
hookQualifiers=hookQualifiers
size='medium'
}}
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.
I was unable to get that to work in that template but I did get it to work in
{{frost-list-item-selection
hook=(concat hookPrefix '-selection')
hookQualifiers=(hash index=index)
model=model
onSelect=(action '_select')
}}
{{if (is-lead-selection _items model) ' is-lead-selection'}} | ||
'> | ||
{{if (is-lead-selection _items model) ' is-lead-selection'}}' | ||
data-test={{hook (concat hook '-item-container') index=index }} |
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.
I'd remove a couple spaces here so that data-test
aligns with {{if ...}}
.
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.
It's aligned now.
|
@@ -143,11 +143,11 @@ describe(test.label, function () { | |||
}) | |||
|
|||
it('item 0 is selected', function () { | |||
expect(this.$($hook('my-list-item-container', {index: 0})).hasClass('is-selected')).to.eql(true) | |||
expect($($hook('my-list-item-container', {index: 0})).hasClass('is-selected')).to.eql(true) |
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.
You are still wrapping these in an extra $()
.
And it's minor, but since you're going to change this anyway, you could take advantage of jquery-chai
and write:
expect($hook('my-list-item-container', {index: 0})).to.have.class('is-selected')
That way you get a better failure message instead of false does not deeply equal true
.
const _rangeState = this.get('_rangeState') | ||
|
||
// Selects are proccessed in order of precedence: specific, range, basic | ||
if (isSpecificSelect) { | ||
selection.specific(clonedSelectedItems, item, _rangeState) | ||
selection.specific(clonedSelectedItems, item, _rangeState, this.get('itemComparator')) |
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.
Future note, best to store this against a const
and then refer for each usage (when more than one usage is present)
{{if (is-lead-selection _items model) ' is-lead-selection'}} | ||
'> | ||
{{if (is-lead-selection _items model) ' is-lead-selection'}}' | ||
data-test={{hook (concat hook '-item-container') index=index }} |
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.
Minor - it's clearer if the concat uses hookPrefix
to delineate
Very nice PR - well done 👍 |
This project uses semver, please check the scope of this pr:
CHANGELOG