Skip to content

Commit

Permalink
feat: allow searching whole string in autocomplete mode by allowing a…
Browse files Browse the repository at this point in the history
…utocompleteSeparator to be null zurb#646

- Default value of autocompleteSeparator = /\s+/
- Update types & readme
- Fix: searchOpts.caseSensitive not working
  • Loading branch information
rajatkantinandi authored and tohosaku committed Aug 12, 2024
1 parent 5a2a06c commit 3be11e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,20 @@ Collection object shown with defaults:
searchOpts: {
pre: '<span>',
post: '</span>',
skip: false // true will skip local search, useful if doing server-side search
skip: false, // true will skip local search, useful if doing server-side search
caseSensitive: false
},

// Limits the number of items in the menu
menuItemLimit: 25,

// specify the minimum number of characters that must be typed before menu appears
menuShowMinLength: 0
menuShowMinLength: 0,

// specify a regex to define after which characters the autocomplete option should open
// If null is used then it will not split the string & search in the whole line
// default value is /\s+/ means it will split on whitespace when this is not specified
autocompleteSeparator: /\s+/,
}
```

Expand Down
5 changes: 3 additions & 2 deletions src/Tribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Tribute {
itemClass = "",
trigger = "@",
autocompleteMode = false,
autocompleteSeparator = null,
autocompleteSeparator = /\s+/,
selectTemplate = null,
menuItemTemplate = null,
lookup = "key",
Expand Down Expand Up @@ -302,7 +302,8 @@ class Tribute {
let items = this.search.filter(this.current.mentionText, values, {
pre: this.current.collection.searchOpts.pre || "<span>",
post: this.current.collection.searchOpts.post || "</span>",
skip: this.current.collection.searchOpts.skip,
skip: this.current.collection.searchOpts.skip || false,
caseSensitive: this.current.collection.searchOpts.caseSensitive || false,
extract: el => {
if (typeof this.current.collection.lookup === "string") {
return el[this.current.collection.lookup];
Expand Down
9 changes: 7 additions & 2 deletions src/TributeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,13 @@ class TributeRange {

getLastWordInText(text) {
var wordsArray;
if (this.tribute.autocompleteSeparator) {
wordsArray = text.split(this.tribute.autocompleteSeparator);
if (this.tribute.autocompleteMode) {
if (this.tribute.autocompleteSeparator) {
wordsArray = text.split(this.tribute.autocompleteSeparator);
}
else {
wordsArray = [text];
}
} else {
wordsArray = text.split(/\s+/);
}
Expand Down
11 changes: 7 additions & 4 deletions tributejs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export type TributeItem<T extends {}> = {
};

export type TributeSearchOpts = {
pre: string;
post: string;
skip: boolean;
pre?: string;
post?: string;
skip?: boolean;
caseSensitive?: boolean;
};

export type TributeCollection<T extends {}> = {
Expand Down Expand Up @@ -72,7 +73,9 @@ export type TributeCollection<T extends {}> = {
autocompleteMode?: boolean;

// specify a regex to define after which characters the autocomplete option should open
autocompleteSeparator?: RegExp;
// If null is used then it will not split the string & search in the whole line
// default value is /\s+/ means it will split on whitespace when this is not specified
autocompleteSeparator?: RegExp | null;

// Customize the elements used to wrap matched strings within the results list
searchOpts?: TributeSearchOpts;
Expand Down

0 comments on commit 3be11e8

Please sign in to comment.