Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reactive MobX Form State Management

License

NotificationsYou must be signed in to change notification settings

foxhound87/mobx-react-form

Repository files navigation

MobX React Form

Reactive MobX Form State Management

NPM

GitHub Workflow Status (with branch)GitHub release (latest by date)npm bundle sizeCodecov CoveragenodeGitHub licenseGitHub closed pull requestsGitHub closed issuesDownloadsDownloadsBackers on Open CollectiveSponsors on Open Collective


Features


Quick Start

Edit form-quickstart

npm install --save mobx-react-form

Choose and Setup a Validation Plugin

Below we are creating aplugins object using thevalidatorjs package to enableDVR functionalities (Declarative Validation Rules).

importdvrfrom'mobx-react-form/lib/validators/DVR';importvalidatorjsfrom'validatorjs';constplugins={dvr:dvr({package:validatorjs})};

SeeValidation Plugins for more info on supported packages.

Define the Form Fields

Define thefields as a collection with arules property for the validation.

constfields=[{name:'email',label:'Email',placeholder:'Insert Email',rules:'required|email|string|between:5,25',},{name:'password',label:'Password',placeholder:'Insert Password',rules:'required|string|between:5,25',type:'password',},{name:'passwordConfirm',label:'Password Confirmation',placeholder:'Confirm Password',rules:'required|string|same:password',type:'password',}];

SeeFields Definitions and all availableField Props on the docs.

Define the Validation Hooks

consthooks={onSuccess(form){alert('Form is valid! Send the request here.');// get field valuesconsole.log('Form Values!',form.values());},onError(form){alert('Form has errors!');// get all form errorsconsole.log('All form errors',form.errors());}}

See more on the docs about theValidation Hooks and theEvent Hooks

Initialize the Form

Simply pass thefields,plugins andhooks objects to the constructor

importMobxReactFormfrom'mobx-react-form';constmyForm=newMobxReactForm({ fields},{ plugins, hooks});

Learn more on the docs about theForm Instance and theForm Options

Pass the myForm to a react component

The package provide some built-in and ready to use Event Handlers:

onSubmit(e),onClear(e),onReset(e) &more...

importReactfrom'react';import{observer}from'mobx-react';exportdefaultobserver(({ myForm})=>(<formonSubmit={myForm.onSubmit}><labelhtmlFor={myForm.$('email').id}>{myForm.$('email').label}</label><input{...myForm.$('email').bind()}/><p>{myForm.$('email').error}</p>{/* ... other inputs ... */}<buttontype="submit"onClick={myForm.onSubmit}>Submit</button><buttontype="button"onClick={myForm.onClear}>Clear</button><buttontype="button"onClick={myForm.onReset}>Reset</button><p>{myForm.error}</p></form>));

See more on the docs about theField Props Bindings

Extending the Form class

See how to implement the same configuration of this quickstart extending the Form class


Contributing

  1. Fork the repository
  2. Make applicable changes (with tests!)
  3. To run tests:npm run test
  4. Ensure builds succeed:npm run build
  5. Commit and run pre-commit checks:npm run commit

New Issues

When opening new issues, provide the setup of your form in aCodeSandbox.

These issues, and the ones which provides also PR with failing tests will get higher priority.

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Sponsor this project

    Packages

    No packages published

    Contributors33

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp