1.2.1 • Published 4 years ago

@gforge/react-typeahead-ts v1.2.1

Weekly downloads
39
License
MIT
Repository
github
Last release
4 years ago

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.

NameTypeDescription
nameStringIf provided there will be a hidden input wiht a name with the selected value
optionsArrayAn 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.
defaultValueStringA default value used when the component has no value. If it matches any options a option list will show.
valueStringSpecify a value for the text input.
initialValueStringThe value when mounting
maxVisibleNumberLimit the number of options rendered in the results list.
resultsTruncatedMessageStringIf maxVisible is set, display this custom message at the bottom of the list of results when the result are truncated.
customClassesObjectAn object containing custom class names for child elements (see description below)
placeholderStringPlaceholder text for the typeahead input.
disabledBooleanSet to true to add disable attribute in the <input> or <textarea> element
textareaBooleanSet to true to use a <textarea> element rather than an <input> element
inputPropsObjectProps to pass directly to the <input> element.
onKeyDownFunctionEvent handler for the keyDown event on the typeahead input.
onKeyPressFunctionEvent handler for the keyPress event on the typeahead input.
onKeyUpFunctionEvent handler for the keyUp event on the typeahead input.
onBlurFunctionEvent handler for the blur event on the typeahead input.
onFocusFunctionEvent handler for the focus event on the typeahead input.
onChangeFunctionEvent handler for the change event on the typeahead input.
onOptionSelectedFunctionEvent handler triggered whenever a user picks an option.
displayOptionString or FunctionA function to map an option onto a string for display in the list (see description below).
filterOptionString or FunctionA function to filter the provided options based on the current input value (see description below).
inputDisplayOptionString or FunctionA 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).
formInputOptionString or FunctionA function to map an option onto a string to include in HTML forms as a hidden field (see name and description below)
searchOptionsFunctionSearch the provided options based on the current input value (see description below, defaults to fuzzy string matching).
defaultClassNamesBooleanIf false, the default classNames are removed from the typeahead.
customListComponentReact ComponentA React Component that renders the list of typeahead results. This replaces the default list of results (see below description below)
showOptionsWhenEmptyBooleanIf true, options will still be rendered when there is no value.
allowCustomValuesBooleanIf true, custom tags can be added without a matching typeahead selection
clearOnSelectionBooleanClear value after selecting. Primarily used with Tokenizer.
classNameStringString with class name
innerRefReact referenceA createRef object
separateByCommaBooleanAllows 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:

NameTypeDescription
defaultSelectedArrayA set of values of tokens to be loaded on first render.
onTokenRemoveFunctionEvent handler triggered whenever a token is removed.
onTokenAddFunctionEvent handler triggered whenever a token is added.
showOptionsWhenEmptyBooleanIf 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.

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.
1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago