npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-input

v0.0.23

Published

Form library for MobX, with observable state and validations

Downloads

47

Readme

Form validation for MobX

Taken strong inspiration from: react-bootstrap-validation, but mobx-input depends just on validator.js(18kb minified). React, MobX and mobx-react are peerDependencies, assuming you are already using those anyway.

Installation:

npm install mobx-input --save

Usage - Example Form:

import React from 'react'
import { ValidatedInput, submit } from 'mobx-input'

export class UserRegisterComponent extends React.Component {
	onSubmit = () => {
		const result = submit(this.props.controller.formData)
		if (result.valid) {
			this.props.controller.registerUser(result.values)
		}
	}

	render() {
		const {appState, controller} = this.props
		const job = appState.currentJob

		return (
			<div className='container'>
				<ValidatedInput
					type='text'
					label='First Name'
					name='firstName'
					validate='required'
					model={controller.formData}
					errorHelp={{
						required: 'First Name is required'
					}}
					/>
				<ValidatedInput
					type='text'
					label='Last Name'
					name='lastName'
					validate='required'
					model={controller.formData}
					errorHelp={{
						required: 'Last Name is required'
					}}
					/>
				<ValidatedInput
					type='text'
					label='Email'
					name='emailAddress'
					validate='required,isEmail'
					model={controller.formData}
					errorHelp={{
						isEmail: 'Email is invalid',
						required: 'Email is required'
					}}
					/>
				<ValidatedInput
					type='password'
					label='Password'
					name='password'
					validate='required,isLength:4:60'
					model={controller.formData}
					errorHelp={{
						isLength: 'Password must be at least 4 characters long',
						required: 'Password is required'
					}}
					/>
				<button
					type='submit'
					onClick={this.onSubmit}>
						Register
				</button>
			</div>
		)
	}
}

Just provide any MobX observable object as model to all form fields. All values and validation data will be stored there. Pass this object to submit function to trigger validations for untouched components. Validation rules are specified in validate attribute, and error messages in errorHelp

ValidatedInput

Should be used instead of the original one for all the fields that need to be validated. All ValidatedInputs should have name property defined.

Properties

name: String required

This property is inherited from Input with only difference that it is required for ValidatedInput.

<ValidatedInput
	name='email'
	validate='required,isEmail'
/>
validate: String

Validation rule is a combination of validator.js method names separated with comma.

<ValidatedInput
	name='email'
	validate='required,isEmail,isLength:5:60'
/>

In the example above, input's value will be validated with three methods. isLength method also receives additional params. Inverse rules (like !isNull) are supported, although in errorHelp object they're looked up without the exclamation mark.

errorHelp: Object|String

Can be either a string with error text or an object with map ruleName => errorText.

<ValidatedInput
	name="email"
	validate='required,isEmail',
	errorHelp={{
		required: 'Please enter your email',
		isEmail: 'Invalid email'
}}
/>
model: Object

Any MobX observable object to store all form data

Custom render function and validation function

It's also possible to provide custom render and validation functions to allow for different design or specialized validated components

const telephoneInputRenderer =

<ValidatedInput
	label='Phone number'
	name='callNumber'
	validate={(x) => PhoneNumber.parse(x)}
	errorHelp='Phone number is not valid'
	model={appState.currentForm}
	renderFunction={(props) =>
		<FormGroup controlId={props.id} validationState={props.validationState} >
			<ControlLabel>{props.label} </ControlLabel>
			<ReactTelephoneInput
				id={props.id}
				defaultCountry='nl'
				flagsImagePath='/images/flags.png'
				value={props.value}
				onChange={this.handlePhoneChange.bind(this, props.changeHandler)}
				/>
			{props.help && <HelpBlock>{props.help} </HelpBlock>}
		</FormGroup>
	}
/>

Default render function

You can also specify defaultRenderFunction in case you don't want to repeat yourself in every ValidatedInput. Just import { config } from mobx-input, and override it.

import { config, ValidatedInput, submit } from 'mobx-input'

const myDefaultRenderFunction = (props) =>
    <div className={`form-group ${props.validationState ? 'has-error' : ''}`}>
      <h1>My Own Mega Label</h1>
      <label for={props.id} className='control-label'>
        {props.label}
      </label>
      {props.componentClass === 'textarea' ?
        <textarea
          id={props.id}
          className='form-control'
          placeholder={props.placeholder}
          value={props.value}
          onChange = {props.changeHandler}
          />
        :
        <input
          id={props.id}
          className='form-control'
          type={props.type}
          placeholder={props.placeholder}
          value={props.value}
          onChange = {props.changeHandler}
          />
      }
      {props.help && <span className='help-block'>
        {props.help}
      </span>}
    </div>

config.defaultRenderFunction = myDefaultRenderFunction