Gathering detailed insights and metrics for react-mobforms
Gathering detailed insights and metrics for react-mobforms
Gathering detailed insights and metrics for react-mobforms
Gathering detailed insights and metrics for react-mobforms
npm install react-mobforms
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
2
npm install react-mobforms
Note that react-mobforms
has mobx
and mobx-react
as required peer dependencies
Form fields come in a few flavors:
String
1@formField('myField', String, '') 2export default class MyField extends FieldComponent {
Number
1@formField('myField', Number, 0) 2export default class MyField extends FieldComponent {
String array
1@formField('myField', [String], ['']) 2export default class MyField extends FieldComponent {
Number array
1@formField('myField', [Number], [0]) 2export default class MyField extends FieldComponent {
formField
can also be used as a composable function, as such:
1class MyField extends FieldComponent { 2 /* your implementation */ 3} 4 5export default formField('name', String, '')(MyField)
The formField
decorator exposes two fields to the wrapped component.
this.formState
-- The state of the entire form that this field is a member ofthis.fieldValue
-- The value of the field defined by the decorator callFor example: If the decorator was used as @formField('myField', Number, 10)
this.fieldValue
will be a number with a starting value of 10this.fieldValue
is equivalent to this.formState.myField
If not provided, the type and default value are String
and ''
. For instance, in the following example the field on formState
will be called firstName
, be a string, and start with a value of ''
.
1import * as React from 'react' 2import { formField, FieldComponent } from 'react-mobforms' 3 4@formField('firstName') 5export default class FirstName extends FieldComponent { 6 render () { 7 return ( 8 <input 9 placeholder='First Name' 10 value={this.fieldValue} 11 onChange={ev => this.fieldValue = ev.currentTarget.value} 12 /> 13 ) 14 } 15}
You can also set a default value for the field. In this example, the field on formState
will be called lastName
, and will also be a string, but will instead start with a value of 'Doe'
.
1import * as React from 'react' 2import { formField, FieldComponent } from 'react-mobforms' 3 4@formField('lastName', String, 'Doe') 5export default class LastName extends FieldComponent { 6 render () { 7 return ( 8 <input 9 placeholder='Last Name' 10 value={this.fieldValue} 11 onChange={ev => this.fieldValue = ev.currentTarget.value} 12 /> 13 ) 14 } 15}
In the following example, the field on formState
is called age
, is a number, and starts with a value of 18
.
1import * as React from 'react' 2import { formField, FieldComponent } from 'react-mobforms' 3 4@formField('age', Number, 18) 5export default class Age extends FieldComponent { 6 render () { 7 return ( 8 <input 9 placeholder='Age' 10 value={`${this.fieldValue || ''}`} 11 onChange={ev => this.fieldValue = parseInt(ev.currentTarget.value, 10)} 12 /> 13 ) 14 } 15}
The form decorator can be used as such:
1@form 2export default class Form extends FormComponent {
It can also be used as a composable function:
1class MyForm extends FormComponent { 2 /* your implementation */ 3} 4 5export default form(MyForm)
The form
decorator exposes a single field to the wrapped component.
this.formState
-- The state of the the form and all of its fields
this.formState
will have all of the fields defined by its children (and their children)In the following example, let's assume that FirstName
, LastName
, and Age
are defined as above:
1import * as React from 'react' 2import { form, FormComponent } from 'react-mobforms' 3 4import FirstName from './FirstName' 5import LastName from './LastName' 6import Age from './Age' 7 8@form 9export default class UserForm extends FormComponent { 10 render () { 11 return ( 12 <form onSubmit={onSubmit}> 13 <FirstName /> 14 <LastName /> 15 <Age /> 16 <button type='submit'>Submit</button> 17 </form> 18 ) 19 } 20 21 onSubmit = () => { 22 console.log(this.formState) 23 } 24}
In this example, formState
will have firstName
, lastName
, and age
as fields, with starting values of ''
, 'Doe'
, and 18
, respectively. Whenever any of these fields is updated, the change is automatically propagated down to all of the children that consume the property.
Sometimes, you will want your form to contain information that is not strictly the defaults for each of the fields. Let's say you already have a user
with firstName
, lastName
, and age
. If you want to replace what is in the existing formState
, you simply write this.formState = user
. Note that only the fields defined by the form's fields will be copied to this.formState
. In the prior example, if you were to write this.formState = { foo: 'bar' }
, this would set firstname
, lastName
, and age
to undefined
, but would not copy the foo
property, because it is not a field defined by the form.
In practical terms, you can expose a prop from your form component which allows you to replace the defaults. For example:
1@form 2export default class UserForm extends FormComponent { 3 componentDidMount () { 4 this.formState = this.props.defaultState 5 } 6 /* rest of implementation */ 7}
In this way, if rendered as <UserForm defaultState={user} />
, then the fields from user
would replace the defaults for all of the form fields whose field name matches one from user
.
For fields that don't directly appear in your form, you can use the HiddenField
component. This component has three props:
name
-- The name of the field in formState
type
-- One of String
, Number
, [String]
, or [Number]
defaultValue
-- The default value for the fieldWhen using the HiddenField
component, all three of these props are required.
You can use this to drive other components using the hidden field. For example:
1@formField('primaryLanguage', String, 'English') 2class PrimaryLanguage extends FieldComponent { 3 render () { 4 const { languages } = this.formState 5 6 return languages.map(language => ( 7 <label key={language}> 8 <input 9 type='radio' 10 name='primaryLanguage' 11 value={language} 12 onChange={this.onChange} 13 /> 14 {language} 15 </label> 16 )) 17 } 18 19 @action onChange = (ev) => { 20 this.fieldValue = ev.currentTarget.value 21 } 22} 23 24@form 25export default class MyForm extends FormComponent { 26 render () { 27 return ( 28 <form> 29 <HiddenField name='languages' type={[String]} defaultValue={['English', 'Deutsch', 'Français', 'Español']} /> 30 <PrimaryLanguage /> 31 </form> 32 ) 33 } 34}
For more examples, see the examples directory.
JSON.stringify(formState)
No vulnerabilities found.
No security vulnerabilities found.