Skip to content

Commit 6255904

Browse files
authored
Merge pull request #1 from formly-js/2.0
2.0
2 parents 923bc3c + 82b4609 commit 6255904

File tree

12 files changed

+623
-520
lines changed

12 files changed

+623
-520
lines changed

README.md

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#vue-formly-bootstrap
2-
A plugin for [Vue Formly](https://github.com/matt-sanders/vue-formly) which adds multiple form fields according to Twitter Bootstrap.
2+
A plugin for [Vue Formly](https://github.com/matt-sanders/vue-formly) which adds multiple form fields according to Twitter Bootstrap.
3+
4+
##Version 2
5+
Note that this is version 2 of Vue Formly Bootstrap, compatible with Vue Formly 2 and Vue 2. If you are looking for version 1 compatibility check out the [Version 1 Branch](https://github.com/formly-js/vue-formly-bootstrap/tree/1.0).
36

47
##Installation
58
```
@@ -22,21 +25,30 @@ Vue.use(VueFormlyBootstrap);
2225
let vm = new Vue({
2326
el: '#app',
2427
data: {
25-
form: {
26-
name: {
28+
form: {},
29+
model: {
30+
name: '',
31+
sex: '',
32+
comments: ''
33+
}
34+
fields: [
35+
{
36+
key: 'name',
2737
type: 'input',
2838
label: 'Your name'
2939
},
30-
sex: {
40+
{
41+
key: 'sex',
3142
type: 'select',
3243
label: 'Sex',
3344
options: ['Male', 'Female']
3445
},
35-
comments: {
46+
{
47+
key: 'comments',
3648
type: 'textarea',
3749
label: 'Comments'
3850
}
39-
}
51+
]
4052
},
4153
methods: {
4254
doSomething: function(){}
@@ -45,27 +57,52 @@ let vm = new Vue({
4557
```
4658
```html
4759
<div id="el">
48-
<formly-form :form="form" @submit="doSomething">
60+
<formly-form :form="form" :model="model" :fields="fields" @submit="doSomething">
4961
<button>Submit</button>
5062
</formly-form>
5163
</div>
5264
```
5365
If you include the script it will be installed for you.
5466

55-
For more advanced details about how to use Vue Formly check out the [docs](https://www.gitbook.com/book/matt-sanders/vue-formly/details).
67+
For more advanced details about how to use Vue Formly check out the [docs](https://www.gitbook.com/book/matt-sanders/vue-formly/content/v/2.0).
5668

5769
Note that this is still a work in progress so some fields are under construction. See the [To Do](#to-do) section for what's on the watchlist.
5870

59-
##Options
71+
##Options & Attributes
6072

61-
###Global options
62-
These options are used by all the different field types. Some fields may have special options and these will be specified below.
73+
###Form Attributes
74+
The form object is used to track the state of the form. Whether it is valid or not, whether there are any errors etc. The following attributes will be set under each field key. e.g. if you had a field with the key of `name` you could access these under `form.name`
6375

64-
| Property | Type | Default | Description |
76+
| Attribute | Type | Default | Description |
6577
| --- | --- | --- | --- |
6678
| $dirty | `boolean` | `false` | ***RESTRICTED*** This is set by the system and is just there for your reference. It gets set to `true` upon blur or change, whichever happens first. |
6779
| $active | `boolean` | `false` | ***RESTRICTED*** Also set by the system and is set to true on focus. |
80+
81+
###Global options
82+
These options are used by all the different field types. Some fields may have special options and these will be specified below. Check the [Vue Formly docs](https://matt-sanders.gitbooks.io/vue-formly/content/v/2.0/) for more info.
83+
84+
| Property | Type | Default | Description |
85+
| --- | --- | --- | --- |
6886
| type | `string` | `null` | ***REQUIRED*** this is the input type. Check the [Available Inputs](#available-inputs) section for a list of currently available inputs.
87+
88+
####Select options
89+
90+
| Property | Type | Default | Description |
91+
| --- | --- | --- | --- |
92+
| options | `array` | `null` | Pass either an array of strings or objects. Objects require a `label` and `value` property. If a string is passed then it will be used for the value and the label. eg: `options: ['Foo', 'Bar']` or `options: [{ label: 'Foo', value: 'bar'},{label: 'Bar', value: 'foo'}]` |
93+
94+
####List options
95+
96+
| Property | Type | Default | Description |
97+
| --- | --- | --- | --- |
98+
| options | `array` | `null` | Pass either an array of strings or objects. Objects require a `label` and `value` property. If a string is passed then it will be used for the value and the label. eg: `options: ['Foo', 'Bar']` or `options: [{ label: 'Foo', value: 'bar'},{label: 'Bar', value: 'foo'}]` |
99+
| inputType | `string` | `checkbox` | The 'type' attribute to pass to each input. Can be either 'checkbox' or 'radio' |
100+
101+
###Template Options
102+
These should be added to the `templateOptions` property. Some input types may have specific options which can be used here and will be specified below.
103+
104+
| Property | Type | Default | Description |
105+
| --- | --- | --- | --- |
69106
| onBlur | `function(e)` | `null` | A function to run on @blur event |
70107
| onFocus | `function(e)` | `null` | A function to run on @focus event |
71108
| onClick | `function(e)` | `null` | A function to run on @click event |
@@ -76,27 +113,19 @@ These options are used by all the different field types. Some fields may have sp
76113
| classes | `object` | `null` | Pass an object of classes to be added to the element. Follows the Vue bindings where each key matches a boolean value. eg `{ 'class-a': true, 'class-b': false }` In this case class-a will be attached. |
77114
| id | `string` | `null` | An ID string to attach to the element |
78115

79-
80-
###Input options
116+
####Input options
81117

82118
| Property | Type | Default | Description |
83119
| --- | --- | --- | --- |
84-
| inputType | `string` | `text` | The 'type' attribute to pass to the input. |
85-
| files | `Object` | `null` | ***RESTRICTED*** If you have set `inputType` to equal `file` then the `files` parameter will be populated by VueFormly to contain the corresponding files that have been uploaded |
86-
120+
| type | `string` | `text` | The 'type' attribute to pass to the input. Can be any valid input type. |
87121

88-
###Select options
89-
90-
| Property | Type | Default | Description |
91-
| --- | --- | --- | --- |
92-
| options | `array` | `null` | Pass either an array of strings or objects. Objects require a `label` and `value` property. If a string is passed then it will be used for the value and the label. eg: `options: ['Foo', 'Bar']` or `options: [{ label: 'Foo', value: 'bar'},{label: 'Bar', value: 'foo'}]` |
122+
####List options
93123

94-
###List options
95124
| Property | Type | Default | Description |
96125
| --- | --- | --- | --- |
97-
| options | `array` | `null` | Pass either an array of strings or objects. Objects require a `label` and `value` property. If a string is passed then it will be used for the value and the label. eg: `options: ['Foo', 'Bar']` or `options: [{ label: 'Foo', value: 'bar'},{label: 'Bar', value: 'foo'}]` |
98126
| inputType | `string` | `checkbox` | The 'type' attribute to pass to each input. Can be either 'checkbox' or 'radio' |
99127

128+
100129
##Available Inputs
101130
* input
102131
* select

dist/vue-formly-bootstrap.js

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* vue-formly-bootstrap v1.0.4
2+
* vue-formly-bootstrap v2.0.0
33
* https://github.com/matt-sanders/vue-formly-bootstrap
44
* Released under the MIT License.
55
*/
@@ -123,22 +123,32 @@ return /******/ (function(modules) { // webpackBootstrap
123123
'use strict';
124124

125125
Object.defineProperty(exports, "__esModule", {
126-
value: true
126+
value: true
127127
});
128128

129129
var _keys = __webpack_require__(2);
130130

131131
var _keys2 = _interopRequireDefault(_keys);
132132

133133
exports.default = function (Vue) {
134-
Vue.directive('formly-atts', function (value) {
135-
var _this = this;
134+
Vue.directive('formly-atts', {
135+
bind: function bind(el, binding) {
136136

137-
if (typeof value == 'undefined') return;
138-
(0, _keys2.default)(value).forEach(function (key) {
139-
_this.el.setAttribute(key, value[key]);
140-
});
141-
});
137+
if (!binding.value) return;
138+
139+
(0, _keys2.default)(binding.value).forEach(function (key) {
140+
el.setAttribute(key, binding.value[key]);
141+
});
142+
}
143+
});
144+
145+
Vue.directive('formly-input-type', {
146+
bind: function bind(el, binding) {
147+
if (!binding.value) return;
148+
149+
el.setAttribute('type', binding.value);
150+
}
151+
});
142152
};
143153

144154
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -662,7 +672,7 @@ return /******/ (function(modules) { // webpackBootstrap
662672
var hotAPI = require("vue-hot-reload-api")
663673
hotAPI.install(require("vue"), false)
664674
if (!hotAPI.compatible) return
665-
var id = "_v-6282284c/fieldInput.vue"
675+
var id = "_v-78aa6b2e/fieldInput.vue"
666676
if (!module.hot.data) {
667677
hotAPI.createRecord(id, module.exports)
668678
} else {
@@ -677,7 +687,7 @@ return /******/ (function(modules) { // webpackBootstrap
677687
'use strict';
678688

679689
Object.defineProperty(exports, "__esModule", {
680-
value: true
690+
value: true
681691
});
682692

683693
var _baseField = __webpack_require__(40);
@@ -687,16 +697,17 @@ return /******/ (function(modules) { // webpackBootstrap
687697
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
688698

689699
exports.default = {
690-
mixins: [_baseField2.default],
691-
methods: {
692-
onChange: function onChange(e) {
693-
this.$set('form.' + this.key + '.$dirty', true);
694-
this.runFunction('onChange', e);
695-
if (this.form[this.key].inputType == 'file') {
696-
this.$set('form.' + this.key + '.files', this.$el.querySelector('input').files);
697-
}
698-
}
700+
mixins: [_baseField2.default],
701+
methods: {
702+
onChange: function onChange(e) {
703+
704+
this.$set(this.form[this.field.key], '$dirty', true);
705+
this.runFunction('onChange', e);
706+
if (this.to.type == 'file') {
707+
this.$set(this.model, this.field.key, this.$el.querySelector('input').files);
708+
}
699709
}
710+
}
700711
};
701712

702713
/***/ },
@@ -706,48 +717,51 @@ return /******/ (function(modules) { // webpackBootstrap
706717
'use strict';
707718

708719
Object.defineProperty(exports, "__esModule", {
709-
value: true
720+
value: true
710721
});
711722
exports.default = {
712-
props: ['form', 'key'],
713-
created: function created() {
714-
this.$set('form.' + this.key + '.$dirty', false);
715-
this.$set('form.' + this.key + '.$active', false);
723+
props: ['form', 'field', 'model', 'to'],
724+
created: function created() {
725+
var state = {
726+
'$dirty': false,
727+
'$active': false
728+
};
729+
this.$set(this.form, this.field.key, state);
730+
},
731+
methods: {
732+
runFunction: function runFunction(action, e) {
733+
if (typeof this.to[action] == 'function') this.to[action].call(this, e);
734+
},
735+
onFocus: function onFocus(e) {
736+
this.$set(this.form[this.field.key], '$active', true);
737+
this.runFunction('onFocus', e);
738+
},
739+
onBlur: function onBlur(e) {
740+
this.$set(this.form[this.field.key], '$dirty', true);
741+
this.$set(this.form[this.field.key], '$active', false);
742+
this.runFunction('onBlur', e);
716743
},
717-
methods: {
718-
runFunction: function runFunction(action, e) {
719-
if (typeof this.form[this.key][action] == 'function') this.form[this.key][action].call(this, e);
720-
},
721-
onFocus: function onFocus(e) {
722-
this.$set('form.' + this.key + '.$active', true);
723-
this.runFunction('onFocus', e);
724-
},
725-
onBlur: function onBlur(e) {
726-
this.$set('form.' + this.key + '.$dirty', true);
727-
this.$set('form.' + this.key + '.$active', false);
728-
this.runFunction('onBlur', e);
729-
},
730-
onClick: function onClick(e) {
731-
this.runFunction('onClick', e);
732-
},
733-
onChange: function onChange(e) {
734-
this.$set('form.' + this.key + '.$dirty', true);
735-
this.runFunction('onChange', e);
736-
},
737-
onKeyup: function onKeyup(e) {
738-
this.runFunction('onKeyup', e);
739-
},
740-
onKeydown: function onKeydown(e) {
741-
this.runFunction('onKeydown', e);
742-
}
744+
onClick: function onClick(e) {
745+
this.runFunction('onClick', e);
746+
},
747+
onChange: function onChange(e) {
748+
this.$set(this.form[this.field.key], '$dirty', true);
749+
this.runFunction('onChange', e);
750+
},
751+
onKeyup: function onKeyup(e) {
752+
this.runFunction('onKeyup', e);
753+
},
754+
onKeydown: function onKeydown(e) {
755+
this.runFunction('onKeydown', e);
743756
}
757+
}
744758
};
745759

746760
/***/ },
747761
/* 41 */
748762
/***/ function(module, exports) {
749763

750-
module.exports = "\n<div class=\"form-group formly-input\" :class=\"[ form[key].inputType, {'formly-has-value': form[key].value, 'formly-has-focus': form[key].$active}]\">\n <label v-if=\"form[key].label\" :for=\"form[key].id ? form[key].id : null\">{{form[key].label}}</label>\n <input class=\"form-control\" :class=\"form[key].classes\" :id=\"form[key].id ? form[key].id : null\" :type=\"form[key].inputType || text\" v-model=\"form[key].value\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"form[key].atts\">\n</div>\n";
764+
module.exports = "\n<div class=\"form-group formly-input\" :class=\"[ to.type, {'formly-has-value': model[field.key], 'formly-has-focus': form[field.key].$active}]\">\n <label v-if=\"to.label\" :for=\"to.id ? to.id : null\">{{to.label}}</label>\n <input class=\"form-control\" :class=\"to.classes\" :id=\"to.id ? to.id : null\" type=\"text\" v-model=\"model[field.key]\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"to.atts\" v-formly-input-type=\"to.type\">\n</div>\n";
751765

752766
/***/ },
753767
/* 42 */
@@ -769,7 +783,7 @@ return /******/ (function(modules) { // webpackBootstrap
769783
var hotAPI = require("vue-hot-reload-api")
770784
hotAPI.install(require("vue"), false)
771785
if (!hotAPI.compatible) return
772-
var id = "_v-584bcc0c/fieldList.vue"
786+
var id = "_v-487ea4ea/fieldList.vue"
773787
if (!module.hot.data) {
774788
hotAPI.createRecord(id, module.exports)
775789
} else {
@@ -796,16 +810,16 @@ return /******/ (function(modules) { // webpackBootstrap
796810
exports.default = {
797811
mixins: [_baseField2.default],
798812
created: function created() {
799-
var type = this.form[this.key].inputType;
800-
if ((!type || type == 'checkbox') && this.form[this.key].value == '') this.$set('form.' + this.key + '.value', []);
813+
var type = this.to.type;
814+
if ((!type || type == 'checkbox') && this.model[this.field.key] == '') this.$set(this.model, this.field.key, []);
801815
}
802816
};
803817

804818
/***/ },
805819
/* 44 */
806820
/***/ function(module, exports) {
807821

808-
module.exports = "\n<div class=\"checkbox\" :id=\"form[key].id\" :class=\"form[key].classes\">\n\n <label v-for=\"option in form[key].options\">\n <input :type=\"form[key].inputType || 'checkbox'\" v-model=\"form[key].value\" :value=\"option.value || option\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"form[key].atts\"> {{option.label || option}}\n </label>\n \n</div>\n";
822+
module.exports = "\n<div class=\"checkbox formly-list\" :id=\"to.id\" :class=\"to.classes\">\n\n <label v-for=\"option in field.options\">\n <input v-if=\"!to.type || to.type == 'checkbox'\" type=\"checkbox\" v-model=\"model[field.key]\" :value=\"option.value || option\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"to.atts\">\n <input v-if=\"to.type == 'radio'\" type=\"radio\" v-model=\"model[field.key]\" :value=\"option.value || option\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"to.atts\">\n {{option.label || option}}\n </label>\n \n</div>\n";
809823

810824
/***/ },
811825
/* 45 */
@@ -827,7 +841,7 @@ return /******/ (function(modules) { // webpackBootstrap
827841
var hotAPI = require("vue-hot-reload-api")
828842
hotAPI.install(require("vue"), false)
829843
if (!hotAPI.compatible) return
830-
var id = "_v-3b0853ea/fieldSelect.vue"
844+
var id = "_v-2c2f2570/fieldSelect.vue"
831845
if (!module.hot.data) {
832846
hotAPI.createRecord(id, module.exports)
833847
} else {
@@ -859,7 +873,7 @@ return /******/ (function(modules) { // webpackBootstrap
859873
/* 47 */
860874
/***/ function(module, exports) {
861875

862-
module.exports = "\n<div class=\"form-group\">\n <label v-if=\"form[key].label\" :for=\"form[key].id ? form[key].id : null\">{{form[key].label}}</label>\n <select class=\"form-control\" :class=\"form[key].classes\" :id=\"form[key].id ? form[key].id : null\" v-model=\"form[key].value\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"form[key].atts\">\n <option v-for=\"option in form[key].options\" :value=\"option.value || option\">{{option.label || option}}</option>\n </select>\n</div>\n";
876+
module.exports = "\n<div class=\"form-group formly-select\">\n <label v-if=\"to.label\" :for=\"to.id ? to.id : null\">{{to.label}}</label>\n <select class=\"form-control\" :class=\"to.classes\" :id=\"to.id ? to.id : null\" v-model=\"model[field.key]\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"to.atts\">\n <option v-for=\"option in field.options\" :value=\"option.value || option\">{{option.label || option}}</option>\n </select>\n</div>\n";
863877

864878
/***/ },
865879
/* 48 */
@@ -881,7 +895,7 @@ return /******/ (function(modules) { // webpackBootstrap
881895
var hotAPI = require("vue-hot-reload-api")
882896
hotAPI.install(require("vue"), false)
883897
if (!hotAPI.compatible) return
884-
var id = "_v-2f276cb0/fieldTextarea.vue"
898+
var id = "_v-5fab8386/fieldTextarea.vue"
885899
if (!module.hot.data) {
886900
hotAPI.createRecord(id, module.exports)
887901
} else {
@@ -895,7 +909,7 @@ return /******/ (function(modules) { // webpackBootstrap
895909
/* 50 */
896910
/***/ function(module, exports) {
897911

898-
module.exports = "\n<div class=\"form-group\">\n <label v-if=\"form[key].label\" :for=\"form[key].id ? form[key].id : null\">{{form[key].label}}</label>\n <textarea class=\"form-control\" :class=\"form[key].classes\" :id=\"form[key].id ? form[key].id : null\" v-model=\"form[key].value\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"form[key].atts\"></textarea>\n</div>\n";
912+
module.exports = "\n<div class=\"form-group formly-textarea\">\n <label v-if=\"to.label\" :for=\"to.id ? to.id : null\">{{to.label}}</label>\n <textarea class=\"form-control\" :class=\"to.classes\" :id=\"to.id ? to.id : null\" v-model=\"model[field.key]\" @blur=\"onBlur\" @focus=\"onFocus\" @click=\"onClick\" @change=\"onChange\" @keyup=\"onKeyup\" @keydown=\"onKeydown\" v-formly-atts=\"to.atts\"></textarea>\n</div>\n";
899913

900914
/***/ }
901915
/******/ ])))

0 commit comments

Comments
 (0)