JSPM

  • Created
  • Published
  • Downloads 923175
  • Score
    100M100P100Q174604F
  • License MIT

Formatting user's text input on-the-fly

Package Exports

  • input-format

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (input-format) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

input-format

[![NPM Version][npm-badge]][npm] [![Build Status][travis-badge]][travis] [![Test Coverage][coveralls-badge]][coveralls]

Formatting user's text input on-the-fly

Installation

npm install input-format --save

Usage

Phone number formatting example

import { templateFormatter } from 'input-format'

// Parses input text characters one-by-one.
// `character` is the currently parsed input text character.
// `value` is the parsed value so far.
//
const parse = (character, value) =>
{
  if (character >= '0' && character <= '9')
  {
    return character
  }
}

// Formats phone number digits using a template.
// Can also be a function of `value` returning `{ text, template }`.
const format = 'x (xxx) xxx-xx-xx'

React Component usage

import { ReactInput } from 'input-format'

<ReactInput
  value={this.state.phone}
  onChange={phone => this.setState({ phone })}
  parse={parse}
  format={format}/>

Low level API (for component developers)

import { InputController } from 'input-format'

const input = document.querySelector('input')

const inputController = new InputController(input, parse, format, onChange)

inputController.onCut(event)
inputController.onPaste(event)
inputController.onChange(event)
inputController.onKeyDown(event)

Contributing

After cloning this repo, ensure dependencies are installed by running:

npm install

This module is written in ES6 and uses Babel for ES5 transpilation. Widely consumable JavaScript can be produced by running:

npm run build

Once npm run build has run, you may import or require() directly from node.

After developing, the full test suite can be evaluated by running:

npm test

When you're ready to test your new functionality on a real project, you can run

npm pack

It will build, test and then create a .tgz archive which you can then install in your project folder

npm install [module name with version].tar.gz

License

MIT [npm]: https://www.npmjs.org/package/input-format [npm-badge]: https://img.shields.io/npm/v/input-format.svg?style=flat-square [travis]: https://travis-ci.org/halt-hammerzeit/input-format [travis-badge]: https://img.shields.io/travis/halt-hammerzeit/input-format/master.svg?style=flat-square [coveralls]: https://coveralls.io/r/halt-hammerzeit/input-format?branch=master [coveralls-badge]: https://img.shields.io/coveralls/halt-hammerzeit/input-format/master.svg?style=flat-square