Skip to content

Commit 56352be

Browse files
SrPatinhastohosaku
authored andcommitted
feat: Added Max number of items per collection and a flag to block a specific collection
1 parent 5e0a368 commit 56352be

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ Collection object shown with defaults:
218218
// An option to hide the tribute when scrolled
219219
// defaults to false, can accept true, or a container to bind the scroll event to.
220220
closeOnScroll: false,
221+
222+
// Set maximum number of items added to the input for the specific Collection, if no limit, set to null.
223+
maxDisplayItems: null,
224+
225+
// Block specific collection, so it can be triggered or not
226+
isBlocked: false
221227
}
222228
```
223229

src/Tribute.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class Tribute {
3131
searchOpts = {},
3232
menuItemLimit = null,
3333
menuShowMinLength = 0,
34-
closeOnScroll = false
35-
}) {
34+
closeOnScroll = false,
35+
maxDisplayItems = null,
36+
isBlocked = false
37+
}) {
3638
this.autocompleteMode = autocompleteMode;
3739
this.autocompleteSeparator = autocompleteSeparator;
3840
this.menuSelected = 0;
@@ -119,8 +121,13 @@ class Tribute {
119121

120122
menuItemLimit: menuItemLimit,
121123

122-
menuShowMinLength: menuShowMinLength
123-
}
124+
menuShowMinLength: menuShowMinLength,
125+
126+
// Fix for maximum number of items added to the input for the specific Collection
127+
maxDisplayItems: maxDisplayItems,
128+
129+
isBlocked: isBlocked
130+
}
124131
];
125132
} else if (collection) {
126133
if (this.autocompleteMode)
@@ -164,7 +171,11 @@ class Tribute {
164171
requireLeadingSpace: item.requireLeadingSpace,
165172
searchOpts: item.searchOpts || searchOpts,
166173
menuItemLimit: item.menuItemLimit || menuItemLimit,
167-
menuShowMinLength: item.menuShowMinLength || menuShowMinLength
174+
menuShowMinLength: item.menuShowMinLength || menuShowMinLength,
175+
176+
// Set maximum number of items added to the input for the specific Collection
177+
maxDisplayItems: item.maxDisplayItems || maxDisplayItems,
178+
isBlocked: item.isBlocked || isBlocked
168179
};
169180
});
170181
} else {
@@ -282,6 +293,17 @@ class Tribute {
282293
}
283294

284295
showMenuFor(element, scrollTo) {
296+
// Check for maximum number of items added to the input for the specific Collection
297+
if(
298+
(
299+
this.current.collection.maxDisplayItems &&
300+
element.querySelectorAll('[data-tribute-trigger="' + this.current.collection.trigger + '"]').length >= this.current.collection.maxDisplayItems
301+
) || this.current.collection.isBlocked
302+
) {
303+
//console.log("Tribute: Maximum number of items added!");
304+
return;
305+
}
306+
285307
this.currentMentionTextSnapshot = this.current.mentionText;
286308

287309
// create the menu if it doesn't exist.
@@ -400,6 +422,16 @@ class Tribute {
400422
}
401423

402424
showMenuForCollection(element, collectionIndex) {
425+
// Check for maximum number of items added to the input for the specific Collection
426+
if(
427+
(
428+
this.collection[collectionIndex || 0].maxDisplayItems &&
429+
element.querySelectorAll('[data-tribute-trigger="' + this.collection[collectionIndex || 0].trigger + '"]').length >= this.collection[collectionIndex || 0].maxDisplayItems
430+
) || this.collection[collectionIndex || 0].isBlocked
431+
) {
432+
//console.log("Tribute: Maximum number of items added!");
433+
return;
434+
}
403435

404436
if (element !== document.activeElement) {
405437
this.placeCaretAtEnd(element);

0 commit comments

Comments
 (0)