• This repository has been archived on 27/Oct/2022
  • Stars
    star
    1,376
  • Rank 34,176 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set of enhancements for input control

๐Ÿš

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

react-input-enhancements Gitter chat

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 (<Autosize> resizes <input> according to it's content length, <Dropdown> 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 <Autosize> with <Autocomplete> just pass <Autocomplete> as a child to <Autosize> (see <Combobox> source code for reference)

Registering <input>

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

let input;

getInput() {
  return input;
}

<Autocomplete
  options={options}
  getInputComponent={getInput}
>
  {props =>
    <input
      ref={c => input = c}
      {...props}
    />
  }
</Autocomplete>

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

<Autocomplete
  options={options}
>
  {(props, otherProps, registerInput) =>
    <input
      ref={c => registerInput(c)}
      {...props}
    />
  }
</Autocomplete>

The first option also allows you to use shorter form with implicit parameters passing:

let input;

getInput() {
  return input;
}

<Autocomplete
  options={options}
  getInputComponent={getInput}
>
  <input
    ref={c => input = c}
  />
</Autocomplete>

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

If <input> 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.

<Autosize defaultValue={value}
          minWidth={100}>
  {(inputProps, { width, registerInput }) =>
    <input type='text' {...inputProps} ref={c => registerInput(c)} />
  }
</Autosize>

Autosize Props

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

Autocomplete

Autocomplete prompts a value based on provided options (see also react-autocomplete for the same behaviour)

<Autocomplete defaultValue={value}
              options={options}>
  {(inputProps, { matchingText, value, registerInput }) =>
    <input type='text' {...inputProps} ref={c => registerInput(c)} />
  }
</Autocomplete>

Autocomplete Props

  • value string - Input value (for a controlled component)
  • defaultValue string - Initial value (for a uncontrolled component)
  • getInputElement function - Optional callback that provides <input> DOM element
  • registerInput function - Registers <input> 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.

<Dropdown defaultValue={value}
          options={options}>
  {(inputProps, { textValue }) =>
    <input type='text' {...inputProps} />
  }
</Dropdown>

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 <input> DOM element
  • registerInput function - Registers <input> 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.

<Mask defaultValue={value}
      pattern='0000-0000-0000-0000'>
  {(inputProps, { value }) =>
    <input type='text' {...inputProps} />
  }
</Mask>

Mask Props

  • value string - Input value (for a controlled component)
  • defaultValue string - Initial value (for a uncontrolled component)
  • getInputElement function - Optional callback that provides <input> DOM element
  • registerInput function - Registers <input> 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 by default) in popup.

<DatePicker defaultValue={moment(value).format('ddd DD/MM/YYYY')}
            placeholder={moment().format('ddd DD/MM/YYYY')}
            pattern='ddd DD/MM/YYYY'
            locale='en'>
  {(inputProps, { value }) =>
    <input type='text' {...inputProps} />
  }
</DatePicker>

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 by default)
  • onChange function(date) - Fires when date is selected, providing moment.js object
  • getInputElement function - Optional callback that provides <input> DOM element
  • registerInput function - Registers <input> DOM element
  • onValuePreUpdate function - Optional callback to update value before it is parsed by DatePicker. In this example, it parses inserted timestamp:
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.

<Combobox defaultValue={value}
          options={options}
          autosize
          autocomplete>
  {(inputProps, { matchingText, width }) =>
    <input type='text' {...inputProps} />
  }
</Combobox>

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

See demo for code examples.

Some other (probably better) implementations

More Repositories

1

react-json-tree

React JSON Viewer Component, Extracted from redux-devtools
JavaScript
992
star
2

react-dock

Resizable dockable react component
JavaScript
544
star
3

webpack-chart

Webpack Chart
JavaScript
437
star
4

cake-chart

Interactive multi-layer pie chart
JavaScript
426
star
5

flask-react-boilerplate

JavaScript
368
star
6

redux-devtools-inspector

Another Redux DevTools Monitor
JavaScript
240
star
7

script-progress

Estimate script execution time
JavaScript
177
star
8

nyan-progress-webpack-plugin

Meow
JavaScript
172
star
9

markdown-react-js

Markdown to React Component converter
JavaScript
104
star
10

stellar-webpack

A little experiment
JavaScript
92
star
11

redux-pagan

managing internationalization via redux
JavaScript
78
star
12

restore-source-tree

Restores file structure from source map
JavaScript
62
star
13

additive-animation

Additive animation npm module
JavaScript
46
star
14

react-transform-debug-inspector

React inspector tranformation function for babel-plugin-wrap-react-components
JavaScript
29
star
15

react-base16-styling

React styling with base16 color scheme support
JavaScript
21
star
16

export-files-webpack-plugin

Exports files to FS (even in devserver mode)
JavaScript
17
star
17

SublimeLinter-inline-errors

Shows linting errors inline with Phantom API
Python
15
star
18

react-bootstrap-breadcrumbs

Breadcrumbs for React-Router and Bootstrap
JavaScript
15
star
19

react-pagan

JavaScript
14
star
20

jss-css

JavaScript
14
star
21

Tomato

Pebble pomodoro app
C
7
star
22

pg_unidecode

Postgres Unidecode extension
C
7
star
23

git-swap

Interactive, pure-shell menu for switching between git branches
Shell
5
star
24

sublime-bun

Bun binary files viewer and other Bun-related stuff
Python
5
star
25

sublime-yarn-lock

Syntax highlighting for yarn.lock and bun.lockb files
4
star
26

shittify-js

So you can honestly say your code is full of shit
JavaScript
4
star
27

babel-plugin-jss-css

JavaScript
4
star
28

svg-flag-editor

Online tool for editing flag svg image source
JavaScript
2
star
29

react-component-boilerplate

React component template
JavaScript
2
star
30

tslint-no-commented-code-rule

TSLint rule that bans commented code
TypeScript
2
star
31

sqlalchemy-flux-serializer

Serialize sqlalchemy models into flux-friendly json
Python
2
star
32

nodejs-dashboard-layout-progress

Layout for nodejs-dashboard with progress and status
JavaScript
2
star
33

alexkuz

HTML
2
star
34

eslint-plugin-jinja

This plugin treats Jinja template expressions as Javascript literals and ignores template statements and comments
JavaScript
2
star
35

mono-coffeescript-addin

CoffeeScript language addin for MonoDevelop
C#
1
star
36

custom-stickers

Python
1
star
37

SublimeLinter-contrib-zig-check

SublimeLinter plugin for Zig built-in linter (zig ast-check)
Python
1
star
38

alexkuz.github.io

HTML
1
star
39

bun-playground

Test repo to reproduce bun errors
TypeScript
1
star
40

python-rust-dprint

JavaScript
1
star
41

wds-ws-proxy-example

JavaScript
1
star
42

next-netlify-starter

JavaScript
1
star
43

nextjs-netlify-blog-template

TypeScript
1
star
44

shell-menu

Shell function for interactive menu
Shell
1
star
45

promisegate

Limits promise concurrency
JavaScript
1
star
46

micro-flag-icons

Collection of minimal country flag svg images
HTML
1
star