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

@gforge/react-typeahead-ts

v1.2.1

Published

A typescript rewrite of the react-typehead npm package

Downloads

93

Readme

Build Status npm version

The @gforge/react-typeahead-ts

A typeahead/autocomplete component for React

@gforge/react-typeahead-ts is a TypeScript (a superset of JavaScript) library that provides a react-based typeahead, or autocomplete text entry, as well as a "typeahead tokenizer", a typeahead that allows you to select multiple results. It can be used with regular JavaScript usin standard syntax although TypeScript is recommended as the library has dropped the deprecated prop-types. The project is a fork from the react-typeahead as suggested by the name.

Usage

For a typeahead input:

import { Typeahead } from '@gforge/react-typeahead-ts';
React.render(
  <Typeahead options={['John', 'Paul', 'George', 'Ringo']} maxVisible={2} />
);

For a tokenizer typeahead input:

import { Tokenizer } from '@gforge/react-typeahead-ts';
React.render(
  <Tokenizer
    options={['John', 'Paul', 'George', 'Ringo']}
    onTokenAdd={function(token) {
      console.log('token added: ', token);
    }}
  />
);

Examples

API

It is strongly recommended to look at the typescipt files in order to understand the API and what input is expected. The library is fully typed in the API and has a minimal reliance on any.

Typeahead(props)

Type: React Component

Basic typeahead input and results list.

| Name | Type | Description | | ----------------------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | name | String | If provided there will be a hidden input wiht a name with the selected value | | options | Array | An array supplied to the filtering function. Can be a list of strings or a list of arbitrary objects. In the latter case, filterOption and displayOption should be provided. | | defaultValue | String | A default value used when the component has no value. If it matches any options a option list will show. | | value | String | Specify a value for the text input. | | initialValue | String | The value when mounting | | maxVisible | Number | Limit the number of options rendered in the results list. | | resultsTruncatedMessage | String | If maxVisible is set, display this custom message at the bottom of the list of results when the result are truncated. | | customClasses | Object | An object containing custom class names for child elements (see description below) | | placeholder | String | Placeholder text for the typeahead input. | | disabled | Boolean | Set to true to add disable attribute in the <input> or <textarea> element | | textarea | Boolean | Set to true to use a <textarea> element rather than an <input> element | | inputProps | Object | Props to pass directly to the <input> element. | | onKeyDown | Function | Event handler for the keyDown event on the typeahead input. | | onKeyPress | Function | Event handler for the keyPress event on the typeahead input. | | onKeyUp | Function | Event handler for the keyUp event on the typeahead input. | | onBlur | Function | Event handler for the blur event on the typeahead input. | | onFocus | Function | Event handler for the focus event on the typeahead input. | | onChange | Function | Event handler for the change event on the typeahead input. | | onOptionSelected | Function | Event handler triggered whenever a user picks an option. | | displayOption | String or Function | A function to map an option onto a string for display in the list (see description below). | | filterOption | String or Function | A function to filter the provided options based on the current input value (see description below). | | inputDisplayOption | String or Function | A function that maps the internal state of the visible options into the value stored in the text value field of the visible input (see description below). | | formInputOption | String or Function | A function to map an option onto a string to include in HTML forms as a hidden field (see name and description below) | | searchOptions | Function | Search the provided options based on the current input value (see description below, defaults to fuzzy string matching). | | defaultClassNames | Boolean | If false, the default classNames are removed from the typeahead. | | customListComponent | React Component | A React Component that renders the list of typeahead results. This replaces the default list of results (see below description below) | | showOptionsWhenEmpty | Boolean | If true, options will still be rendered when there is no value. | | allowCustomValues | Boolean | If true, custom tags can be added without a matching typeahead selection | | clearOnSelection | Boolean | Clear value after selecting. Primarily used with Tokenizer. | | className | String | String with class name | | innerRef | React reference | A createRef object | | separateByComma | Boolean | Allows you to select an option using a comma. |

Tokenizer(props)

Type: React Component

Typeahead component that allows for multiple options to be selected. The following properties are identical as for the Typeahead:

  • name
  • options
  • initialValue
  • maxVisible
  • resultsTruncatedMessage
  • customClasses
  • placeholder
  • disabled
  • inputProps
  • onKeyDown
  • onKeyPress
  • onKeyUp
  • onBlur
  • onFocus
  • onChange
  • displayOption
  • filterOption
  • formInputOption
  • searchOptions
  • defaultClassNames
  • showOptionsWhenEmpty
  • className
  • innerRef

The new additional arguments are:

| Name | Type | Description | | -------------------- | ---------- | --------------------------------------------------------------- | | defaultSelected | Array | A set of values of tokens to be loaded on first render. | | onTokenRemove | Function | Event handler triggered whenever a token is removed. | | onTokenAdd | Function | Event handler triggered whenever a token is added. | | showOptionsWhenEmpty | Boolean | If true, options will still be rendered when there is no value. |

Specific props details

The searchOptions argument

Type: (value: string, options: Option[]) => Option[]

A function to filter and/or sort the provided options based on the current input value. Compared to filterOption it allows you to sort the results any way you want.

If not supplied, defaults to the filterOption.

Note: the function can be used to store other information besides the string in the internal state of the component. Make sure to use the displayOption, inputDisplayOption, and formInputOption props to extract/generate the correct format of data that each expects if you do this.

The filterOption argument

Type: String or (value: string, option: Option) => boolean

A function to filter the provided options based on the current input value. For each option, receives (inputValue, option). If not supplied, defaults to fuzzy string matching.

If provided as a string, it will interpret it as a field name and use that field from each option object.

The displayOption argument

Type: String or (option: Option) => string | number

A function to map an option onto a string for display in the list. Receives (option, index) where index is relative to the results list, not all the options. Can either return a string or a React component.

If provided as a string, it will interpret it as a field name and use that field from each option object.

The inputDisplayOption argument

Type: String or (option: Option, index?: number) => string | number

A function that maps the internal state of the visible options into the value stored in the text value field of the visible input when an option is selected.

Receives (option).

If provided as a string, it will interpret it as a field name and use that field from each option object.

If no value is set, the input will be set using displayOption when an option is selected.

The formInputOption argument

Type: String or (option: Option) => string | number

A function to map an option onto a string to include in HTML forms as a hidden field (see name).

If specified as a string, it will interpret it as a field name and use that field from each option object.

If not specified, it will fall back onto the semantics described in displayOption.

The customClasses argument

Type: Object

Allowed Keys: input, results, listItem, listAnchor, hover, resultsTruncated. For the Tokenizer you can also provide token, typeahead and tokenList.

An object containing custom class names for child elements. Useful for integrating with 3rd party UI kits.

The customListComponent argument

A React Component that renders the list of typeahead results. This replaces the default list of results.

This component receives the following props :

Passed through
  • displayOptions
  • customClasses
  • onOptionSelected
Created or modified
  • options
    • This is the Typeahead's options filtered and limited to Typeahead.maxVisible.
  • selectionIndex
    • The index of the highlighted option for rendering

Developing

Setting Up

You will need npm to develop on react-typeahead-ts. [Installing npm][4].

Once that's done, to get started, run npm install in your checkout directory. This will install all the local development dependences, such as gulp and mocha

Testing

react-typeahead-ts uses jest and enzyme for unit tests. Large changes should include unittests. After updating or creating new tests, run npm run test to regenerate the test package.

Contributing

Basically, fork the repository and send a pull request. It can be difficult to review these, so here are some general rules to follow for getting your PR accepted more quickly:

  • All new properties and exposed component function should be documented in the README.md
  • Break your changes into smaller, easy to understand commits.
  • Send separate PRs for each commit when possible.
  • Feel free to rebase, merge, and rewrite commits to make them more readible.
  • Add comments explaining anything that's not painfully obvious.
  • Add unittests for your change if possible.