Skip to content
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

Issue #149 - It is possible to select more than maxSelectedItems when #158

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yegor-babiy
Copy link
Contributor

#Fixes #149

Proposed Changes

@yegor-babiy
Copy link
Contributor Author

@liorheber please take a look and what do you think?

@liorheber
Copy link
Collaborator

Sorry for the delay here.
I'll be going over the PR tomorrow.

@liorheber
Copy link
Collaborator

@talyak I've been having a hard time getting to this PR.
Any chance you'd be willing to take a look?

@talyak
Copy link
Collaborator

talyak commented Jul 31, 2019

@liorheber, @yegor-babiy - I will review it today.

@talyak
Copy link
Collaborator

talyak commented Jul 31, 2019

@yegor-babiy - why not to change the following function like this:

export const getMinMaxIndexes = (
currentIndex,
firstItemShiftSelected,
maxSelectedItems
) => {
const range = Math.abs(firstItemShiftSelected - currentIndex) + 1;
if (range > maxSelectedItems) {
return firstItemShiftSelected > currentIndex
? {
minIndex: firstItemShiftSelected - maxSelectedItems + 1,
maxIndex: firstItemShiftSelected
}
: {
minIndex: firstItemShiftSelected,
maxIndex: firstItemShiftSelected + maxSelectedItems - 1
};
}
return firstItemShiftSelected > currentIndex
? { minIndex: currentIndex, maxIndex: firstItemShiftSelected }
: { minIndex: firstItemShiftSelected, maxIndex: currentIndex };
};

@yegor-babiy
Copy link
Contributor Author

@talyak
first of all codeclimate will say that it's too complicated function with more than 5 operations and about logic in this case you don't count selected items that was selected outside range before

} = this.state;

const initialInterval = getMinMaxIndexes(index, firstItemShiftSelected);
const outsideIntervalSelectedItems = getSelectedItemsOutsideInterval(
Copy link
Collaborator

Choose a reason for hiding this comment

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

all this logic should happen just when the Shift pressed, right?
also, I prefer to extract all the logic to the utils.
the MinMax function will give the interval and will do all the necessary calculation in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, I believe that it looks better, thanks for your feedback and please review again

) =>
selectedItems.filter(selectedItem => {
const index = sourceItems.findIndex(item => item.id === selectedItem.id);
return !isWithin(index, interval) || selectedItem.disabled;
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do u need the disable check? can u give me scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot_5
Sure, please see attached screen

) => {
const { minIndex, maxIndex } = interval;
const shouldBeSelect = outsideSelectedItems + (maxIndex - minIndex);
return maxSelectedItems > 0 && maxSelectedItems <= shouldBeSelect
Copy link
Collaborator

Choose a reason for hiding this comment

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

why you need to check > 0 ? is it not enough to do it like this:
maxSelectedItems && maxSelectedItems <= shouldBeSelect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

Copy link
Collaborator

@talyak talyak left a comment

Choose a reason for hiding this comment

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

some small comments, sorry.
this logic is complicated and I want to keep it as clear as possible.

const shouldBeSelect =
outsideSelected + Math.abs(firstItemShiftSelected - currentIndex) + 1;

if (maxSelected && maxSelected <= shouldBeSelect) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can u extract this logic to function with clear name, something like: getIndexesWhenShiftSelected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

selectedItems,
maxSelected
) => {
const outsideSelected = getSelectedItemsOutsideInterval(
Copy link
Collaborator

Choose a reason for hiding this comment

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

numberOfItemsOutsideSelected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

items,
selectedItems
).length;
const shouldBeSelect =
Copy link
Collaborator

Choose a reason for hiding this comment

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

sumItemsToSelect or sumItemsShouldBeSelect ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

outsideSelected + Math.abs(firstItemShiftSelected - currentIndex) + 1;

if (maxSelected && maxSelected <= shouldBeSelect) {
const availableToSelect = maxSelected - outsideSelected - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

sumItemsAllowToSelect ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@yegor-babiy
Copy link
Contributor Author

@talyak can be merged?

@yegor-babiy
Copy link
Contributor Author

@liorheber @talyak guess it can be merged,
I made all your recommendation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

It is possible to select more than maxSelectedItems when using Shift
5 participants