Skip to content

Commit f85206f

Browse files
committed
Added Max number of items per collection and a flag to block a specific collection
1 parent 426e21b commit f85206f

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,21 @@ Collection object shown with defaults:
209209

210210
// specify the minimum number of characters that must be typed before menu appears
211211
menuShowMinLength: 0,
212+
212213
// specify a regex to define after which characters the autocomplete option should open
213214
// If null is used then it will not split the string & search in the whole line
214215
// default value is /\s+/ means it will split on whitespace when this is not specified
215216
autocompleteSeparator: /\s+/,
216-
217-
217+
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: true,
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: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Tribute {
3232
menuItemLimit = null,
3333
menuShowMinLength = 0,
3434
closeOnScroll = false,
35+
maxDisplayItems = null,
36+
isBlocked = false
3537
}) {
3638
this.autocompleteMode = autocompleteMode;
3739
this.autocompleteSeparator = autocompleteSeparator;
@@ -119,7 +121,12 @@ class Tribute {
119121

120122
menuItemLimit: menuItemLimit,
121123

122-
menuShowMinLength: menuShowMinLength
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
123130
}
124131
];
125132
} else if (collection) {
@@ -165,7 +172,11 @@ class Tribute {
165172
requireLeadingSpace: item.requireLeadingSpace,
166173
searchOpts: item.searchOpts || searchOpts,
167174
menuItemLimit: item.menuItemLimit || menuItemLimit,
168-
menuShowMinLength: item.menuShowMinLength || menuShowMinLength
175+
menuShowMinLength: item.menuShowMinLength || menuShowMinLength,
176+
177+
// Set maximum number of items added to the input for the specific Collection
178+
maxDisplayItems: item.maxDisplayItems || maxDisplayItems,
179+
isBlocked: item.isBlocked || isBlocked
169180
};
170181
});
171182
} else {
@@ -281,6 +292,17 @@ class Tribute {
281292
}
282293

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

286308
// create the menu if it doesn't exist.
@@ -399,6 +421,17 @@ class Tribute {
399421
}
400422

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

0 commit comments

Comments
 (0)