# react-form-input

> An enhanced input component comes with validators and transformers.

Latest version **0.3.0** (published 2015-08-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-form-input
pnpm add react-form-input
yarn add react-form-input
bun add react-form-input
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2015-08-19 |
| First published | 2015-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tommy Chen |
| Maintainers | tommy351 |

## Links

- npm: https://www.npmjs.com/package/react-form-input
- Repository: https://github.com/tommy351/react-form-input
- Homepage: https://github.com/tommy351/react-form-input#readme
- Issues: https://github.com/tommy351/react-form-input/issues
- npm.io page: https://npm.io/package/react-form-input

## Dependencies (2)

- [validator](https://npm.io/package/validator.md) ^3.41.3
- [object-assign](https://npm.io/package/object-assign.md) ^3.0.0

## Recent versions

- 0.3.0 (latest) — 2015-08-19
- 0.2.0 — 2015-08-18
- 0.1.1 — 2015-07-30
- 0.1.0 — 2015-07-30
- 0.0.1 — 2015-07-28

## README

# react-form-input

[![npm version](https://badge.fury.io/js/react-form-input.svg)](http://badge.fury.io/js/react-form-input)

An enhanced input component comes with validators and transformers.

## Installation

``` bash
$ npm install react-form-input --save
```

## Example

``` js
import Input, {validators, transformers} from 'react-form-input';

class Form extends React.Component {
  constructor(props, context){
    super(props, context);

    this.state = {
      name: ''
    };
  }

  render(){
    return (
      <form>
        <Input
          value={this.state.name}
          validators={[
            validators.required('Please enter your name')
          ]}
          transformers={[
            transformers.trim
          ]}>
      </form>
    );
  }
}
```

## API

### Input

**Props:**

- `validators`: An array contains validator functions
- `transformers`: An array contains transformer functions
- `onChange`: Fired when the value changed
- `defaultValue`: The initial value of input
- `value`: The value of input
- `type`: The type of input (text, email, number, textarea, etc.)
- `dirtyClass`: The class name appended to the input when the value has been changed
- `pristineClass`: The class name appended to the input when the value has not been changed
- `validClass`: The class name appended to the input when the value is valid
- `invalidClass`: The class name appended to the input when the value is invalid

**getValue()**

Returns the value of the input.

**getError()**

Returns the error of the input.

**setValue()**

Sets the value of the input.

**setError()**

Sets the error of the input.

**reset()**

Reset the state of the input.

**isDirty()**

Returns true if the value has been changed.

**isPristine()**

Returns true if the value has not been changed.

### Validators

**required([message])**

Checks whether the value is empty.

**equals(str, [message])**

Checks whether the value equal to a string.

**startsWith(str, [message])**

Checks whether the value starts with a string.

**endsWith(str, [message])**

Checks whether the value ends with a string.

**match(regex, [message])**

Checks whether the value matches a regular expression.

**length(min, max, [message])**

Checks the length of the value.

**minLength(min, [message])**

Checks the minimum length of the value.

**maxLength(max, [message])**

Checks the maximum length of the value.

**email([message])**

Checks whether the value is a valid email.

**url([message])**

Checks whether the value is a valid URL.

### Transformers

**trim**

Trims the string.

**toNumber**

Transforms the value to a number.

**toLowerCase**

Transforms the value to lower case.

**toUpperCase**

Transforms the value to upper case.

## FAQ

### How to customize a validator?

A validator is a function that returns a error message when the value is invalid. For example:

``` js
function tweetValidator(value){
  return value.length <= 140;
}
```

### How to customize a transformer?

A transformer is a function that takes the value and returns a new transformed value, just like a reducer. For example:

``` js
function lowerCaseTransformer(value){
  return value.toLowerCase();
}
```

## License

MIT

---
_Source: https://npm.io/package/react-form-input · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
