-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lot of stuff fixed, new tests, new apis, demo project added
- Loading branch information
1 parent
7498a36
commit 7564909
Showing
20 changed files
with
553 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/coverage | ||
/demo/dist | ||
/demo/build | ||
/demo/node_modules | ||
/es6 | ||
/lib | ||
/node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "react", "stage-0"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/build | ||
/node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# MobX Ajv Form Demo | ||
|
||
> This demo project is using the build files from the `lib` directory instead of install it into npm dependecies. | ||
|
||
### Install | ||
|
||
1. In the main project directory: | ||
|
||
`npm install` | ||
|
||
`npm run build` | ||
|
||
|
||
2. In the `demo` directory: | ||
|
||
`npm install` | ||
|
||
`npm run build` | ||
|
||
`npm run start` | ||
|
||
|
||
### Development | ||
|
||
1. In the main project directory: | ||
|
||
`npm install` | ||
|
||
`npm run build` | ||
|
||
|
||
2. In the `demo` directory: | ||
|
||
`npm install` | ||
|
||
`npm run start:dev` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "mobx-ajv-form-demo", | ||
"license": "MIT", | ||
"version": "1.0.0", | ||
"author": "Claudio Savino <[email protected]> (https://twitter.com/foxhound87)", | ||
"description": "Easly manage Forms with MobX and automatic validation with AJV json-schema rules.", | ||
"homepage": "https://www.npmjs.com/package/mobx-ajv-form", | ||
"scripts": { | ||
"build": "webpack -p", | ||
"start:dev": "webpack-dev-server --port 8888", | ||
"start": "node ./src/server.js" | ||
}, | ||
"dependencies": { | ||
"ejs": "2.5.1", | ||
"express": "4.14.0", | ||
"json-stringify-safe": "5.0.1", | ||
"lodash": "4.15.0", | ||
"mobx": "2.4.3", | ||
"mobx-react": "3.5.5", | ||
"react": "15.3.1", | ||
"react-dom": "15.3.1" | ||
}, | ||
"devDependencies": { | ||
"babel-loader": "6.2.5", | ||
"babel-preset-es2015": "6.13.2", | ||
"babel-preset-react": "6.11.1", | ||
"babel-preset-stage-0": "6.5.0", | ||
"html-webpack-plugin": "2.22.0", | ||
"json-loader": "0.5.4", | ||
"webpack": "1.13.2", | ||
"webpack-dev-server": "1.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react'; | ||
import { observer } from 'mobx-react'; | ||
import jsonStringifySafe from 'json-stringify-safe'; | ||
import _ from 'lodash'; | ||
|
||
const merge = (field) => ({ | ||
isValid: field.isValid, | ||
isDirty: field.isDirty, | ||
isPristine: field.isPristine, | ||
isEmpty: field.isEmpty, | ||
value: field.value, | ||
error: field.error, | ||
}); | ||
|
||
const toJson = (data) => { | ||
const $data = jsonStringifySafe(data); | ||
return JSON.stringify(JSON.parse($data), null, 2); | ||
}; | ||
|
||
const parseFormData = (form) => { | ||
const omit = ['fields', 'schema', 'extend', 'options']; | ||
return toJson(_.merge(_.omit(form, omit), merge(form))); | ||
}; | ||
|
||
const parseFieldData = (field) => { | ||
const omit = [ | ||
'$valid', | ||
'$value', | ||
'form', | ||
'related', | ||
'label', | ||
'name', | ||
'key', | ||
'disabled', | ||
'errorMessage', | ||
'validationFunctionsValues', | ||
'validateProperty', | ||
]; | ||
|
||
return toJson(_.merge(_.omit(field, omit), merge(field))); | ||
}; | ||
|
||
export const DebugForm = observer(({ form }) => ( | ||
<div> | ||
<h4>Form</h4> | ||
<pre> | ||
{parseFormData(form)} | ||
</pre> | ||
</div> | ||
)); | ||
|
||
export const DebugField = observer(({ field }) => ( | ||
<div> | ||
<h4>{field.label}</h4> | ||
<pre> | ||
{parseFieldData(field)} | ||
</pre> | ||
</div> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React from 'react'; | ||
import { observer } from 'mobx-react'; | ||
import { DebugForm, DebugField } from './DebugComponent'; | ||
|
||
const FormComponent = ({ form, events }) => ( | ||
<div className="container"> | ||
<div className="splitted-35 fixed container-left"> | ||
<form> | ||
<a href="https://www.npmjs.com/package/mobx-ajv-form"> | ||
<h3>mobx-ajv-form</h3> | ||
</a> | ||
<div> | ||
{form.fields.username.label} | ||
<i>{form.fields.username.error}</i> | ||
</div> | ||
<input | ||
type="text" | ||
name={form.fields.username.name} | ||
value={form.fields.username.value} | ||
placeholder={form.fields.username.label} | ||
onChange={form.syncValue} | ||
/> | ||
|
||
<div> | ||
{form.fields.email.label} | ||
<i>{form.fields.email.error}</i> | ||
</div> | ||
<input | ||
type="text" | ||
name={form.fields.email.name} | ||
value={form.fields.email.value} | ||
placeholder={form.fields.email.label} | ||
onChange={form.syncValue} | ||
/> | ||
|
||
<div> | ||
{form.fields.password.label} | ||
<i>{form.fields.password.error}</i> | ||
</div> | ||
<input | ||
type="password" | ||
name={form.fields.password.name} | ||
value={form.fields.password.value} | ||
placeholder={form.fields.password.label} | ||
onChange={form.syncValue} | ||
/> | ||
|
||
<div> | ||
<button | ||
type="submit" | ||
disabled={!form.isValid} | ||
onClick={events.handleOnSubmit} | ||
>Submit</button> | ||
<button | ||
type="submit" | ||
onClick={events.handleOnClear} | ||
>Clear</button> | ||
<button | ||
type="submit" | ||
onClick={events.handleOnReset} | ||
>Reset</button> | ||
</div> | ||
|
||
<p>{form.genericError}</p> | ||
|
||
</form> | ||
</div> | ||
<div className="splitted-65 container-right"> | ||
<DebugForm form={form} /> | ||
<hr /> | ||
<DebugField field={form.fields.username} /> | ||
<DebugField field={form.fields.email} /> | ||
<DebugField field={form.fields.password} /> | ||
</div> | ||
</div> | ||
); | ||
|
||
FormComponent.propTypes = { | ||
events: React.PropTypes.object, | ||
form: React.PropTypes.object, | ||
}; | ||
|
||
export default observer(FormComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import form from './form'; | ||
|
||
/** | ||
On Submit | ||
*/ | ||
const handleOnSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
// check if the form is valid, otherwise exit | ||
if (!form.validate()) return; | ||
|
||
alert('Form is valid! Send the requrest here.'); // eslint-disable-line | ||
|
||
// get fields values | ||
console.log('Form Values', form.values()); // eslint-disable-line | ||
|
||
// or show a custom generic error after a beckend call | ||
form.invalidate('The user already exist.'); | ||
}; | ||
|
||
/** | ||
On Clear | ||
*/ | ||
const handleOnClear = (e) => { | ||
e.preventDefault(); | ||
|
||
// clear the form | ||
form.clear(); | ||
}; | ||
|
||
/** | ||
On Reset | ||
*/ | ||
const handleOnReset = (e) => { | ||
e.preventDefault(); | ||
|
||
// reset to the default initial values | ||
form.reset(); | ||
}; | ||
|
||
export default { | ||
handleOnReset, | ||
handleOnClear, | ||
handleOnSubmit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Form from '../../lib'; | ||
// import Form from 'mobx-ajv-form'; | ||
|
||
// define a json schema | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string', minLength: 6, maxLength: 20 }, | ||
email: { type: 'string', format: 'email', minLength: 5, maxLength: 20 }, | ||
password: { type: 'string', minLength: 6, maxLength: 20 }, | ||
}, | ||
}; | ||
|
||
// define fields | ||
const fields = { | ||
username: { | ||
label: 'Username', | ||
value: 'SteveJobs', | ||
}, | ||
email: { | ||
label: 'Email', | ||
value: '[email protected]', | ||
}, | ||
password: { | ||
label: 'Password', | ||
value: 'thinkdifferent', | ||
}, | ||
}; | ||
|
||
// create the form | ||
export default new Form({ fields, schema }); |
Oops, something went wrong.