-
-
Notifications
You must be signed in to change notification settings - Fork 902
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
feat: can set a maximum of selected items [maxSelectedItems] when mult… #239
feat: can set a maximum of selected items [maxSelectedItems] when mult… #239
Conversation
e875e31
to
8b87d5f
Compare
3466cf4
to
68411f1
Compare
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.
Nice work 👍 Added few comments regarding code style, but if you don't have time let me know, I will merge it and fix.
src/ng-select/items-list.ts
Outdated
@@ -188,4 +188,8 @@ export class ItemsList { | |||
private get _lastSelectedItem() { | |||
return this._selected[this._selected.length - 1]; | |||
} | |||
|
|||
isMaximumNumberOfSelectedItemsReached(): boolean { |
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.
Move this public method before all private methods.
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 also would suggest renaming this method to less verbose, eg.: maxItemsSelected
or similar
src/ng-select/ng-select.component.ts
Outdated
@@ -269,7 +274,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie | |||
} | |||
|
|||
open() { | |||
if (this.isDisabled || this.isOpen) { | |||
if (this.isDisabled || this.isOpen || this.isMaximumNumberOfSelectedItemsReaches()) { |
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 can remove that method and use one prod itemsList directly
if (this.isDisabled || this.isOpen || this.itemsList.isMaximumNumberOfSelectedItemsReaches()) {
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 think in this case it should be this.itemsList.isMaximumNumberOfSelectedItemsReached()
then
I will fix the issues this evening after work, thank you for the review |
Hi @anjmao, As using a single select behaves differently: Once you select a new option is just replaces the previous one. Maybe this could also be an option, just replace the last item in the list with the newly selected. I am not sure if this is a better behavior than disabling the select box. |
@andreasfranz replacing last selected item can look a bit strange and user always can remove item from the list so he have a choose. I think current implementation is good and it would be even less clear if dropdown is opened but you can't select items because max items are reached. Best approach would be to add some kind of hint (label) for user that he can select only x items. |
…tiple selection is activated
68411f1
to
6159db1
Compare
I made the fixes. And yes, I made a choice, which seemed to me the most natural: not to replace the last item, but to block the selection of items. |
…iple selection is activated
#181