
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
joiful-react-forms
Advanced tools
npm i joiful-react-forms
import { default as React, Component } from 'react'
import { default as Joi } from 'joi'
import { Form, Input } from 'joiful-react-forms'
class Form extends Component {
render() {
return (
<Form
onChange={(event, values) => this.setState({ values }) }
onSubmit={(error, values, event) => ... }
schema={{
name: Joi.string().required(),
email: Joi.string().email().required(),
phone: Joi.string().min(10).max(12)
}}
values={this.state.values}
>
<Input name="name" />
<Input name="email" />
<Input name="phone" />
</Form>
)
}
}
<Form />
Prop | Type | Description |
---|---|---|
schema | object | Joi validation schema. |
values | object | Object with keys corresponding to the schema |
onChange | func | (event, values) Fires when any value in the form changes |
onSubmit | func | (error, values) |
<Input />
Prop | Type | Description |
---|---|---|
is ('text') | func or string | Either a key from the joifulReactForms.Input.types object stored in context (see below), or a React component instance. |
name | string | The name of the input. (Must correspond to the schema prop on <Form /> ) |
joiful-react-forms
gives you default html inputs. You can define a custom input inline using the is
prop. See example below:
const TextInput = ({ error, ...props }) =>
<div>
<input type='text' {...props}/>
{error}
</div>
const Textarea = ({ error, ...props }) =>
<div>
<textarea {...props}/>
{error}
</div>
const Form = () =>
<Form
onSubmit={handleSubmit}
schema={{
name: Joi.string().required(),
message: Joi.string().required()
}}
>
<Input is={TextInput} name="name" />
<Input is={Textarea} name="message" />
</Form>
Or if you prefer, you can supply your application context a joifulReactForms
object. See example:
class App extends Component {
static childContextTypes = {
joifulReactForms: PropTypes.object
};
getChildContext() {
return {
joifulReactForms: {
Input: {
types: {
text: TextInput,
textarea: Textarea,
special: () => <input type='special'/>
}
}
}
}
}
render() {
...
}
}
The is
property also serves as a reference the types of inputs you have in your context. We have defaults for keys like text, textarea and checkbox. As demonstrated above, you can override these with your own and may supply custom inputs which can be named anything you like and referenced as a string in the is
prop. Take a look:
<Input is='textarea' />
<Input is='special' />
FAQs
Automatically generate validated React forms using Joi
We found that joiful-react-forms demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.