Skip to content

Commit

Permalink
Added 'performSearchIfEmptyText' parameter to ChosenOption
Browse files Browse the repository at this point in the history
  • Loading branch information
bartmoc committed Apr 11, 2016
1 parent 64d2572 commit e20e47b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions plugin/src/main/java/com/arcbees/chosen/client/ChosenOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ChosenOptions {
private boolean searchContains;
private boolean singleBackstrokeDelete;
private boolean highlightSearchTerm;
private boolean performSearchIfEmptyText;
private ResultsFilter resultFilter;
private DropdownPosition dropdownPosition;
private Element dropdownBoundaries;
Expand Down Expand Up @@ -266,6 +267,15 @@ public boolean isSingleBackstrokeDelete() {
return singleBackstrokeDelete;
}

public ChosenOptions setPerformSearchIfEmptyText(boolean performSearchIfEmptyText) {
this.performSearchIfEmptyText = performSearchIfEmptyText;
return this;
}

public boolean isPerformSearchIfEmptyText() {
return performSearchIfEmptyText;
}

public ChosenOptions setSingleBackstrokeDelete(boolean singleBackstrokeDelete) {
this.singleBackstrokeDelete = singleBackstrokeDelete;
return this;
Expand Down Expand Up @@ -297,6 +307,7 @@ private void setDefault() {
maxSelectedOptions = -1;
highlightSearchTerm = true;
dropdownPosition = DropdownPosition.BELOW;
performSearchIfEmptyText = true;
mobileViewportMaxWidth = 649;
oneSelectedTextMultipleMobile = "{} item selected";
manySelectedTextMultipleMobile = "{} items selected";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public void filter(String searchText, ChosenImpl chosen, boolean isShowing) {
RegExp regex = RegExp.compile(regexAnchor + escapedSearchText, "i");
RegExp zregex = RegExp.compile("(" + escapedSearchText + ")", "i");

boolean performSearch = true;
if (!options.isPerformSearchIfEmptyText() && searchText.isEmpty()) {
performSearch = false;
}

int results = 0;

List<SelectItem> selectItems = chosen.getSelectItems();
Expand All @@ -63,13 +68,13 @@ public void filter(String searchText, ChosenImpl chosen, boolean isShowing) {
optionContent = option.getText();
}

if (regex.test(optionContent)) {
if (performSearch && regex.test(optionContent)) {
found = true;
results++;
} else if (optionContent.contains(" ") || optionContent.indexOf("[") == 0) {
String[] parts = optionContent.replaceAll("\\[|\\]", "").split(" ");
for (String part : parts) {
if (regex.test(part)) {
if (performSearch && regex.test(part)) {
found = true;
results++;
}
Expand Down

0 comments on commit e20e47b

Please sign in to comment.