Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/alexkuz/react-input-enhancements

Set of enhancements for input control
https://github.com/alexkuz/react-input-enhancements

autocomplete autosize autosuggest datepicker dropdown input input-mask javascript react typeahead

Last synced: 2 months ago
JSON representation

Set of enhancements for input control

Lists

README

        

🏚

**This project was originally thought to be an experiment and currently is unmaintained (and buggy)**

**Use it at your own risk**

Also, consider using more modern, WAI-ARIA compliant approach like [downshift](https://github.com/downshift-js/downshift)

# react-input-enhancements [![Gitter chat](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/react-input-enhancements)

Set of enhancements for input control

The intention of creating this library was to bring `input` component out of the dropdown/autocomplete/whatever code, so it could be easily replaced with your custom component, and also to split independent functionality into different components, which could be combined with each other (still not quite sure it was worth it, though).

There are currently five components:

1. [``](#autosize)
2. [``](#autocomplete)
3. [``](#dropdown)
4. [``](#mask)
5. [``](#datepicker)

[``](#combobox) is a combination of `Dropdown`, `Autosize` and/or `Autocomplete` components.

## Demo

http://alexkuz.github.io/react-input-enhancements/

## How it works

* Each component is responsible for a corresponding behaviour (`` resizes `` according to it's content length, `` adds popup with options, and so on).
* All components accept `function` as a child, providing props as a first argument, which you should pass to your `input` component. If there is nothing else except `input`, it could be passed as a child directly (for simplicity).
* If you need to have combined behaviour in your component, let's say `` with `` just pass `` as a child to `` (see `` source code for reference)

#### Registering ``

All components needs an access to `` DOM element. To provide it, use `getInputComponent` prop:

```jsx
let input;

getInput() {
return input;
}

{props =>
input = c}
{...props}
/>
}

```
Or, if you don't want to store the node in your component:

```jsx

{(props, otherProps, registerInput) =>
registerInput(c)}
{...props}
/>
}

```
The first option also allows you to use shorter form with implicit parameters passing:
```jsx
let input;

getInput() {
return input;
}

input = c}
/>

```
However, this is not preferable as there is too much magic happening.

If `` element wasn't provided, component tries to find node automatically, however this behaviour is deprecated and will be removed in future versions.

## Autosize

`Autosize` resizes component to fit it's content.

```jsx

{(inputProps, { width, registerInput }) =>
registerInput(c)} />
}

```

### Autosize Props

* **`value`** *string* - Input value (for a controlled component)
* **`defaultValue`** *string* - Initial value (for a uncontrolled component)
* **`getInputElement`** *function()* - Optional callback that provides `` DOM element
* **`registerInput`** *function* - Registers `` DOM element
* **`defaultWidth`** *number* - Minimum input width

## Autocomplete

`Autocomplete` prompts a value based on provided `options` (see also [react-autocomplete](https://github.com/reactjs/react-autocomplete) for the same behaviour)

```jsx

{(inputProps, { matchingText, value, registerInput }) =>
registerInput(c)} />
}

```

### Autocomplete Props

* **`value`** *string* - Input value (for a controlled component)
* **`defaultValue`** *string* - Initial value (for a uncontrolled component)
* **`getInputElement`** *function* - Optional callback that provides `` DOM element
* **`registerInput`** *function* - Registers `` DOM element
* **`options`** *array* - Array of options that are used to predict a value

`options` is an array of strings or objects with a `text` or `value` string properties.

## Dropdown

`Dropdown` shows a dropdown with a (optionally filtered) list of suitable options.

```jsx

{(inputProps, { textValue }) =>

}

```

### Dropdown Props

* **`value`** *string* - Input value (for a controlled component)
* **`defaultValue`** *string* - Initial value (for a uncontrolled component)
* **`options`** *array* - Array of shown options
* **`onRenderOption`** *function(className, style, option)* - Renders option in list
* **`onRenderCaret`** *function(className, style, isActive, children)* - Renders a caret
* **`onRenderList`** *function(className, style, isActive, listShown, children, header)* - Renders list of options
* **`onRenderListHeader`** *function(allCount, shownCount, staticCount)* - Renders list header
* **`dropdownProps`** *object* - Custom props passed to dropdown root element
* **`optionFilters`** *array* - List of option filters
* **`getInputElement`** *function* - Optional callback that provides `` DOM element
* **`registerInput`** *function* - Registers `` DOM element

`options` is an array of strings or objects with a shape:

* **`value`** - "real" value of on option
* **`text`** - text used as input value when option is selected
* **`label`** - text or component rendered in list
* **`static`** - option is never filtered out or sorted
* **`disabled`** - option is not selectable

`null` option is rendered as a separator

`optionFilters` is an array of filters for options (for convenience). By default, these filters are used:

* `filters.filterByMatchingTextWithThreshold(20)` - filters options by matching value, if options length is more than 20
* `filters.sortByMatchingText` - sorting by matching value
* `filters.limitBy(100)` - cuts options longer than 100
* `filters.notFoundMessage('No matches found')` - shows option with 'No matches found' label if all options are filtered out
* `filters.filterRedudantSeparators` - removes redudant separators (duplicated or at the begin/end of the list)

## Mask

`Mask` formats input value.

```jsx

{(inputProps, { value }) =>

}

```

### Mask Props

* **`value`** *string* - Input value (for a controlled component)
* **`defaultValue`** *string* - Initial value (for a uncontrolled component)
* **`getInputElement`** *function* - Optional callback that provides `` DOM element
* **`registerInput`** *function* - Registers `` DOM element
* **`pattern`** *string* - String formatting pattern. Only '0' (digit) or 'a' (letter) pattern chars are currently supported.
* **`emptyChar`** *string* - Character used as an empty symbol (`' '` by default)
* **`placeholder`** *string* - If set, it is shown when `unmaskedValue` is empty
* **`onUnmaskedValueChange`** *function(text)* - Fires when value is changed, providing unmasked value
* **`onValuePreUpdate`** *function* - Optional callback to update value before it is parsed by `Mask`

## DatePicker

`DatePicker` uses `Mask` to format date and shows calendar ([react-date-picker](https://github.com/zippyui/react-date-picker) by default) in popup.

```jsx

{(inputProps, { value }) =>

}

```

### DatePicker Props

* **`value`** *string* - Input value (for a controlled component)
* **`defaultValue`** *string* - Initial value (for a uncontrolled component)
* **`pattern`** *string* - Date formatting pattern. For now, only these tokens are supported:
* `DD` - day of month
* `MM` - month
* `YYYY` - year
* `ddd` - day of week *(not editable)*
* **`placeholder`** *string* - If set, it is shown when `unmaskedValue` is empty
* **`locale`** *string* - Date locale
* **`todayButtonText`** *string* - Text for 'Go to Today' button label
* **`onRenderCalendar`** *function({ styling, style, date, isActive, popupShown, onSelect, locale, todayButtonText })* - Returns calendar component shown in popup ([react-day-picker-themeable](https://github.com/alexkuz/react-day-picker-themeable) by default)
* **`onChange`** *function(date)* - Fires when date is selected, providing [moment.js](http://momentjs.com/) object
* **`getInputElement`** *function* - Optional callback that provides `` DOM element
* **`registerInput`** *function* - Registers `` DOM element
* **`onValuePreUpdate`** *function* - Optional callback to update value before it is parsed by `DatePicker`. In this example, it parses inserted timestamp:
```js
onValuePreUpdate={v => parseInt(v, 10) > 1e8 ?
moment(parseInt(v, 10)).format('ddd DD/MM/YYYY') : v
}
```

## Combobox

`Combobox` combines `Dropdown`, `Autosize` and/or `Autocomplete` components.

```jsx

{(inputProps, { matchingText, width }) =>

}

```

`Autosize` and `Autocomlete` are enabled with corresponding bool props, other properties are proxied to `Dropdown` component.

See [demo](http://alexkuz.github.io/react-input-enhancements/) for code examples.

## Some other (probably better) implementations

* [react-autocomplete](https://github.com/rackt/react-autocomplete) - Dropdown with autocompletion by Ryan Florence (that led me to create this library)
* [react-maskedinput](https://github.com/insin/react-maskedinput) - More advanced masked input by Jonny Buchanan
* [react-autosuggest](https://github.com/moroshko/react-autosuggest) - Beautifully crafted input with dropdown suggestions