
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
form-provider
Advanced tools
A set of React helpers to help with building forms. State is managed with a Redux store that is local to your component. This promotes keeping your ui state separate from your global application state while still being able to use the redux ecosystem.
Demos:
npm install --save react react-dom redux react-redux # peer dependencies
npm install --save form-provider
// BasicForm.js
import { withForm, FormProvider, Field } from 'form-provider'
function createForm(props) {
return {
field1: props.field1,
obj: {
field2: 4
}
}
}
function BasicForm({ form, onSubmit }) {
return (
<FormProvider form={form} onSubmit={onSubmit}>
<form onSubmit={preventDefault(form.submit)}>
<h3>Basic Form</h3>
<Field path='field1' validate={[isRequired('Field1'), isNotNumber('Field1')]}>
{({ value, setValue, error }) =>
<div>
<label>Field1*</label>
<input type='text' value={value} onChange={target(setValue)} />
{ error && <div>{ error.message }</div> }
</div>
}
</Field>
<Field path='obj.field2'>
{({ value, setValue }) =>
<div>
<label>Field2</label>
<input type='number' value={value} onChange={target(toNumber(setValue)))} />
</div>
}
</Field>
<button type='submit'>Save</button>
<button type='button' onClick={form.reset}>Reset</button>
</form>
</FormProvider>
)
}
export default withForm(createForm)(BasicForm)
Check out the basic form example for the entire source.
Setting initial form state is done by passing it into withForm
...
export default withForm({
user: {
firstName: 'john'
}
})(BasicForm)
// or as a function of props
export default withForm(props => ({
user: props.user
}))(BasicForm)
This lib currently doesn't provide any validation functions out of the box, only an API to provide your own. Validators are functions that accept the current value and return a promise. Pass in a single validator or an array to the <Field>
component. The form won't submit until all validators are resolved.
// validators.js
export const isRequired = (name) => (value) => new Promise((resolve, reject) => {
if (!value) return reject(new Error(`${name} is required`))
resolve()
})
export const isNotNumber = (name) => (value) => new Promise((resolve, reject) => {
if (!isNaN(value)) return reject(new Error(`${name} must not be a number`))
resolve()
})
Use the connectForm
function to map form state to props. This function has the exact same API as react-redux's connect
function. You can use this to conditionally display fields or other rendering logic based on the current form's state.
import { compose } from 'redux'
import { withForm, FormProvider, Field, connectForm } from 'form-provider'
function mapFormStateToProps(formState) {
return {
userFormState: formState.user,
allErrors: formState.errors
}
}
function BasicForm({ userFormState, allErrors, form, onSubmit }) {
...
})
export default compose(
withForm(createForm)
connectForm(mapFormStateToProps)
)(withForm)
FAQs
React helpers for building forms.
We found that form-provider 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.