Git Product home page Git Product logo

searchbox's Introduction

SearchBox

SearchBox is a component for Vaadin 8 that helps performing searches and has autocomplete functionality.

Usage

Instantiate SearchBox with the button caption or icon and position attributes and attach a listener to be notified on search events.

SearchBox searchBox = new SearchBox("Search", SearchBox.ButtonPosition.RIGHT);
searchBox.addSearchListener(e -> Notification.show(e.getSearchTerm()));

Optionally, set a suggestion generator to show search suggestions for the user.

searchBox.setSuggestionGenerator(this::suggestPlanets);

// ...

private List<String> suggestPlanets(String query, int limit) {
    return DataSource.getPlanets().stream().filter(p -> p.contains(query))
                    .limit(cap).collect(Collectors.toList());
}

Building Parts

The component is made of a search field (TextField) and a search button (Button). The search button can be on either side of the search field, or it can be hidden. If a suggestion generator is set, AutocompleteExtension is applied to the search field.

Search Button

The search button can have a caption, an icon or both, just like the Button component in Vaadin.

Its position, relative to the search field, can be defined in the search box's constructor or later with the setSearchButtonPosition() method. The button can be on either side of the field (LEFT, RIGHT) or invisible (HIDDEN).

To join the button visually into the text field, call setButtonJoined(true).

The button component can be accessed and further customized using the getSearchButton() method.

Search Field

Set a placeholder text to the search field using the setPlaceholder() method.

If you need to customize the search field, use getSearchField() to access the TextField component.

Search Modes

There are three search modes to choose from that determines the frequency of search events.

  • EXPLICIT (default): event is fired when search button is clicked or when ENTER key is pressed.
  • DEBOUNCE: event is fired when typing stops for a set time.
  • EXPLICIT: event is fired at each key press while typing.
searchBox.setSearchMode(SearchMode.DEBOUNCE);
searchBox.setDebounceTime(200); // event fires 200 ms after typing

Search Suggestions

To activate search suggestions, set a suggestion generator to the search box component.

searchBox.setSuggestionGenerator(this::suggestUsers);

// ...

// The suggestion generator method that returns a list of suggestions
private List<String> suggestUsers(String query, int limit) {
    return DataSource.getUsers().stream().map(User::getName)
                    .filter(p -> p.contains(query))
                    .limit(cap).collect(Collectors.toList());
}

It is also possible to customize how the suggestions are displayed. You can do this by adding value and caption converters along with the generator. Note that the generator now returns a list of User objects instead of a list of names.

searchBox.setSuggestionGenerator(this::suggestUsers, this::convertValueUser, this::convertCaptionUser);

// ...

// The suggestion generator method. Returns a list of users.
private List<User> suggestUsers(String query, int limit) {
    return DataSource.getUsers().stream().filter(p -> p.contains(query))
                    .limit(cap).collect(Collectors.toList());
}

// Converts a User object into a String to be set as the value of the search field.
private String convertValueUser(DataSource.User user) {
    return user.getName();
}

// Converts a User object into an HTML string to be displayed as suggestion item
private String convertCaptionUser(DataSource.User user, String query) {
    return "<div class='suggestion-container'>"
            + "<img src='" + user.getPicture() + "' class='userimage'>"
            + "<span class='username'>"
            + user.getName().replaceAll("(?i)(" + query + ")", "<b>$1</b>")
            + "</span>"
            + "</div>";
}

Please refer to AutocompleteExtension's documentation for more details.

Examples

Appearance

Simple search box

SearchBox searchBox = new SearchBox("Search", SearchBox.ButtonPosition.RIGHT);

Simple search box, button with icon on the left and

SearchBox searchBox = new SearchBox(VaadinIcons.SEARCH, SearchBox.ButtonPosition.LEFT);

Hidden search button and placeholder text

SearchBox searchBox = new SearchBox((String) null, SearchBox.ButtonPosition.HIDDEN);
searchBox.setPlaceholder("Search...");

Joined button

SearchBox searchBox = new SearchBox(VaadinIcons.SEARCH, SearchBox.ButtonPosition.RIGHT);
searchBox.setButtonJoined(true);
searchBox.setPlaceholder("Search...");

searchbox's People

Contributors

wbadam avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

searchbox's Issues

Only 5 suggestions show in dropdown

Probably I am missing something obvious, but in my example (which closely follows your your example) only the first 5 items ever appear in the search box dropdown with no way of scrolling to the next 5 (my searches sometimes return up to 100 matches). How do I change the limit or make sure that the user is able to scroll through multiple pages in the dropdown (just like in a "regular" vaadin ComboBox)?

Images with com.vaadin.ui.Image

The code samples show Images. These images are referenced by external links.

Could you please provide a mechanism to use Images with Vaadins com.vaadin.ui.Image component?

Thanks

Suggestion method is not called

Hi,

The SearchBox is not calling the suggestion method this::suggestEmployees in the code below.

SearchBox searchBox = new SearchBox(VaadinIcons.SEARCH, ButtonPosition.RIGHT); searchBox.setSearchMode(SearchMode.EAGER); searchBox.setPlaceholder("type employee name..."); searchBox.setButtonJoined(false); searchBox.setSuggestionGenerator(this::suggestEmployees);

Thanks
-Saad

Scroll through the list by holding down the arrow keys

I would very much like that it would be possible to scroll through the list of results by keeping the arrow-down key down. As of know you have to release the arrow-down key and press it again to scroll down to the next element the list. The regular comboboxes in Vaadin work this way.

If you need to go down the list a bit it would be nicer to just hold down the arrow-down key.

This behaviour also shows in the online demo.

SearchEvent.getSelectedItem() returns an Optional that contains a new Optional

For some reason the getSelectedItem() returned Optional does not contain the Selected Item but a new Optional so One have to have some really dirty code like:

Object object = event.getSelectedItem().orElse(null);
if (object != null) {
MyObject as = ((Optional) object).orElse(null);
System.out.printLn(as.getName());
}
This must be a bug...

Vaadin 8.6.0 support

It seems that .setButtonJoined(true) and usage of the ENTER key is not working anymore: button is kept outside of the textfield and pressing ENTER does nothing.

I'm using SearchBox 0.1.3 and Vaadin 8.6.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.