Skip to content

Commit 0b43471

Browse files
authored
Merge pull request #42 from lucasnetau/filter
Implement filter callback to optionally modify or suppress the tag creation
2 parents 6c560c5 + 9b2fd32 commit 0b43471

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default App
9393
* **link** `function(name): string|false` it should return what should be in href attribute or false
9494
* **tag_limit** `number` (default -1) limit number of tags, when set to -1 there are no limits
9595
* **placeholder** `string` (default unset) If set in options or on the initial input, this placeholder value will be shown in the tag entry input
96+
* **filter** `function(name): string` it should return the tag name after applying any filters (eg String.toUpperCase()), empty string to filter out tag and prevent creation.
9697

9798
**NOTE:** if you're familiar with TypeScript you can check the API by looking at
9899
TypeScript definition file:

tagger.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare namespace Tagger {
2020
min_length: number;
2121
}
2222
type link = (name: string) => (string | false);
23+
type filter = (name: string) => (string);
2324
}
2425

2526
interface tagger_options {
@@ -31,6 +32,7 @@ interface tagger_options {
3132
completion?: Tagger.completion;
3233
link?: Tagger.link;
3334
placeholder?: string;
35+
filter?: Tagger.filter;
3436
}
3537

3638
interface tagger_instance {

tagger.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
add_on_blur: false,
120120
link: function(name) {
121121
return '/tag/' + name;
122-
}
122+
},
123+
filter: (name) => name,
123124
};
124125
// ------------------------------------------------------------------------------------------
125126
tagger.fn = tagger.prototype = {
@@ -305,15 +306,16 @@
305306
},
306307
// --------------------------------------------------------------------------------------
307308
add_tag: function(name) {
308-
if (!this._settings.allow_duplicates && this._tags.indexOf(name) !== -1) {
309-
return false;
310-
}
311309
if (this._tag_limit()) {
312310
return false;
313311
}
312+
name = this._settings.filter(name);
314313
if (this.is_empty(name)) {
315314
return false;
316315
}
316+
if (!this._settings.allow_duplicates && this._tags.indexOf(name) !== -1) {
317+
return false;
318+
}
317319
this._new_tag(name);
318320
this._tags.push(name);
319321
this._input.value = this._tags.join(',');

0 commit comments

Comments
 (0)