npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@open-pioneer/search

v0.8.0

Published

This package provides a UI component to perform a search on given search sources.

Downloads

156

Readme

@open-pioneer/search

This package provides a UI component to perform a search on given search sources.

The search results are presented as a grouped list, one group for each search source. This list is sorted by the given order of search sources and the order of the search results inside is defined by the implementation of the search source.

Usage

To use the search in your app, insert the following snippet and reference a map ID and sources (sources to be searched on):

<Search mapId={MAP_ID} sources={searchsources} />

To change the typing delay, add the optional property searchTypingDelay (in ms). The default value is 200ms.

<Search mapId={MAP_ID} sources={searchsources} searchTypingDelay={500} />

To limit the maximum number of search results to be displayed per search source (group), configure the optional property maxResultsPerGroup. The default value is 5.

<Search mapId={MAP_ID} sources={searchsources} maxResultsPerGroup={10} />

To change the placeholder text of the search field, use the optional property placeholder:

<Search sources={searchsources} placeholder="Search for cities" />

Listening to events

To listen to the events onSelect and onClear, provide optional callback functions to the component. In case of the onSelect event, you can access the selected search result (and its search source) from the parameter SelectSearchEvent.

import { Search, SearchSelectEvent } from "@open-pioneer/search";
// ...
<Search
    mapId={MAP_ID}
    sources={datasources}
    onSelect={(event: SearchSelectEvent) => {
        // do something
    }}
    onClear={() => {
        // do something
    }}
/>;

Positioning the search bar

The search bar is not placed at a certain location by default; it will simply fill its parent.

To achieve a certain position in a parent node, the following snippet might be helpful:

/* top center placement */
.search-top-center-placement {
    position: absolute;
    left: 50%;
    top: 0px;
    transform: translate(-50%, 0px);
    width: 500px;
    z-index: 1;
}
<Box
    // ...
    className="search-top-center-placement"
>
    <Search /* ... */ />
</Box>

Implementing a search source

To provide search sources that are used by the search component, implement the function search for each datasource:

import { Search, SearchSource, SearchResult } from "@open-pioneer/search";
import { MAP_ID } from "./MapConfigProviderImpl";

class MySearchSource implements SearchSource {
    // The label of this source, used as a title for this source's results.
    label = "My sample REST-Service";

    // Attempts to retrieve results for the given input string. For more details,
    // see the API documentation of `SearchSource`.
    async search(inputValue: string, options: SearchOptions): Promise<SearchResult[]> {
        // implement
    }
}

const searchsources: SearchSource[] = [new MySearchSource()];

// In your JSX template:
<Search mapId={MAP_ID} sources={searchsources} />;

The configured maximum number of maxResultsPerGroup is passed as maxResults inside the option parameter of the search function, so you are able to fetch no more results than necessary.

License

Apache-2.0 (see LICENSE file)