Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
immutable-form
Advanced tools
[![npm](https://img.shields.io/npm/v/immutable-form.svg?maxAge=2592000)](https://www.npmjs.com/package/immutable-form) [![CircleCI](https://circleci.com/gh/Intelight/immutable-form.svg?style=svg)](https://circleci.com/gh/Intelight/immutable-form)
Install the package
yarn install immutable-form
To install the latest development version
yarn install immutable-form@devel
The intent of this package is to provide a simple to use implementation of managing form state for React components, streamlining common requirements such as validation, error handling, data fetching and submission.
This package consists of three modules, Form
, FormCollection
, and connectForm
.
Form
is a class which provides an API to read and write form's state. The state is stored within a Immutable object which in turn is resides within a Redux store.
FormCollection
holds references to all existing forms. This is useful if you're trying to access form values across different components.
connectForm
is a React higher order component which passes down form
and field
props into your components and re-renders on the form's state changes.
import { Form, connectForm } from 'immutable-form';
// This is just a mock promise for the sake of this code example, in reality this promise should originate from performing an async call to some api.
const createUserPromise = Promise.resolve({
userId: '123'
})
const userForm = new Form('userForm', {
fields: {
username: {
// All these fields are optional,
value: 'some value', // Set an initial value
// Any validation function in validate which returns a string will cause a validation error. Each validation function receives the field value and the form reference as parameters.
validate: [
username => username.trim().length === 0 && 'Username cannot be be empty',
]
}
password: {
validate: [
password => password.trim().length < 6 && 'Password must be longer than 6 characters'
]
}
}
})
// Send a request to the server
.setSubmit((form) => createUserPromise)
// If the promise resolves, do something with the results
.setOnSuccess(({userId}, form) => userId)
// If the promise is rejected, do something.
.setOnFailure((err, form) => err)
const UserForm = ({
// fields is a an Immutable Map
fields,
// This is the `Form` object, you can use it do extra actions such submitting the form.
form
}) =>
<div>
<input
type="text"
onChange={(e) => form.setField('username', e.target.value)}
value={fields.getIn(['username', 'value'])}
/>
{fields.getIn(['username', 'errors'].first())}
<input
type="text"
onChange={(e) => form.setField('password', e.target.value)}
value={fields.getIn(['password', 'value'])}
/>
{fields.getIn(['password', 'errors'].first())}
<button onClick={() => form.submit()} />
</div>
export default connectForm(userForm)(UserForm);
import { Form, connectForm } from 'immutable-form';
// This is just a mock promise for the sake of this code example, in reality this promise should originate from performing an async call to some api.
const loadFormPromise = Promise.resolve({
username: 'someusername',
password: 'somepassword'
});
const userForm = new Form('userForm', {
fields: {
username: {
validate: [
username => username.trim().length === 0 && 'Username cannot be be empty',
]
}
password: {
validate: [
password => password.trim().length < 6 && 'Password must be longer than 6 characters'
]
}
}
})
.setLoad(form => loadFormPromise.then(({ username, password }) => {
// When the promise resolves with the requested data
// you need to manually wire it into the form.
form.setField('username', username);
form.setField('email', email);
}));
If using connectForm
the load promise will be called automatically in the component's lifecycle. To trigger a manual load, call form.load()
.
import { Form, connectForm } from 'immutable-form';
const Form = new Form('form', {}, {
logger: true, // Will enable redux-logger (false by default)
addToFormCollection: true, // Refrains from adding a reference to FormCollection, (true by default)
})
FAQs
[![npm](https://img.shields.io/npm/v/immutable-form.svg)](https://www.npmjs.com/package/immutable-form) [![CircleCI](https://circleci.com/gh/Intelight/immutable-form.svg?style=svg)](https://circleci.com/gh/Intelight/immutable-form)
The npm package immutable-form receives a total of 7 weekly downloads. As such, immutable-form popularity was classified as not popular.
We found that immutable-form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.