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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@clearc2/c2-form

v3.2.1

Published

a react/redux form component

Downloads

21

Readme

@clearc2/c2-form CircleCI

Flexible forms that use ImmutableJS and can be integrated with redux.

Install

yarn add @clearc2/c2-form

Usage

The easiest way to get started is using the useForm hook.

import React from 'react'
import {useForm} from '@clearc2/c2-form'

function NewsletterSignUpForm () {
  const form = useForm({name: '', email: ''}) // pass initial values
  const values = form.currentValues
  return (
    <form>
      <label>Name</label>
      <input
      	type='text'
        value={values.get('name')}
        onChange={(e) => form.setValue('name', e.target.value)}
      />
      <label>Email</label>
      <input
      	type='text'
        value={values.get('email')}
        onChange={(e) => form.setValue('email', e.target.value)}
      />
      <button type='submit' disabled={!form.isDirty}>
        Submit
      </button>
    </form>
  )
}

See props below to see all properties of form

Redux support

To use the redux HOC, formHOC, add the reducer to your main application's reducer using the required key.

import {combineReducers} from 'redux-immutable'
import {reducer as formReducer} from '@clearc2/c2-form'

const reducer = combineReducers({
  // ...other reducers
  [formReducer.key]: formReducer
})

Redux usage

import {connect} from 'react-redux'
import {formHOC} from '@clearc2/c2-form'
import SiteForm from './SiteForm'

const props = {siteId} => ({formName: `site-form-${siteId}`})

export default connect(props)(formHOC(SiteForm))

The formHOC gives your component props and connected actions scoped to this specific form based on the formName.

See the working example.

Redux-less HOC

There is also a redux-less HOC that uses component state. You do not need to pass in a formName. The passed down props are exactly the same except for deleteForm. This is not passed down because deleteForm is only relevant for the redux enabled formHOC HOC.

import {withForm} from '@clearc2/c2-form'
import SiteForm from './SiteForm'

export default withForm(SiteForm)

Props

initialValues: object

The initial values of the form. Defaults to Map().

currentValues: object

The current values of the form. Defaults to Map().

isDirty: boolean

true if the initialValues do not equal the currentValues. Defaults to false.

isClean: boolean

true if the initialValues equal the currentValues. Defaults to true.

setInitialValues(initialValues: object, currentValues: ?object): func

Initialize your form in the componentDidMount lifecycle method with this action and pass in an object of field/value pairs. If a second argument is passed, it will set the current values equal to that argument. Needing to send currentValues should be very rare.

setValue(field: string, value: any): func

Set a value in the form. Use this in an input's onChange callback.

setValues(values: object): func

Set multiple values in the form at once. The values object should be field/value pairs.

deleteField(field: string): func

Delete a form field.

deleteFields(fields: array): func

Delete multiple form fields.

reset(): func

Sets the currentValues back to the initialValues.

deleteForm(): func

Deletes the formName key and data from redux.

useValues<T>()

This is a version of useForm that does not convert the underlying data to immutable-js. It also supports a type generic so type safety is enforced.

Tips

Default values

Always ensure a non-null default in your inputs. React will complain about the input changing from an uncontrolled to a controlled input if the value goes from null to something non-null while having an onChange defined.

<input
 value={this.props.currentValues.get('foobar') || ''}
 onChange={e => this.props.setValue('foobar', e.target.value)}
/>

Updating child objects

It is a common requirement to be able to update child objects. This can be done with setValue.

const lineItems = this.props.currentValues.get('lineItems') || List()

{lineItems.map((lineItem, i) => (
  <div key={i}>
    <input
      value={lineItem.get('name') || ''}
      onChange={e => {
        const updatedItems = lineItems.update(i, item => item.set('name', e.target.value))
        this.props.setValue('lineItems', updatedItems)
      }}
    />
  </div>
))}