Skip to content

SSR & Boolean values #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*~
\#*\#
\.\#*
/.emacs.desktop
/.emacs.desktop.lock

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"babel-preset-es2015": "6.9.0",
"babel-preset-es2015-loose-rollup": "^7.0.0",
"babel-preset-stage-0": "6.5.0",
"babel-runtime": "^6.23.0",
"chai": "3.5.0",
"conventional-changelog-cli": "1.2.0",
"conventional-github-releaser": "1.1.3",
Expand Down Expand Up @@ -74,8 +75,8 @@
"sinon-chai": "2.8.0",
"style-loader": "0.13.1",
"uglify-js": "^2.6.4",
"vue": "~2.0.0",
"vue-formly": "^2.3.9",
"vue": "^2.2.6",
"vue-formly": "^2.5.0",
"vue-hot-reload-api": "1.3.2",
"vue-html-loader": "1.2.3",
"vue-loader": "8.5.3",
Expand Down
22 changes: 22 additions & 0 deletions src/components/errorDisplay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default {
render(h){
if ( this.message ) return h('span', {
'class': 'help-block form-text text-danger'
}, this.message);
},
props: ['field', 'form'],
computed: {
message(){
let message = false;
if ( !( this.field in this.form.$errors ) || !( this.field in this.form ) || this.form[ this.field ].$active || !this.form[this.field].$dirty ) return message;
let errors = this.form.$errors[ this.field ];
Object.keys( errors ).some( errorKey => {
if ( typeof errors[ errorKey ] != 'boolean' ){
message = errors[ errorKey ];
return true;
}
});
return message;
}
}
}
23 changes: 0 additions & 23 deletions src/components/errorDisplay.vue

This file was deleted.

5 changes: 4 additions & 1 deletion src/fields/baseField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errorDisplay from '../components/errorDisplay.vue';
import errorDisplay from '../components/errorDisplay';
export default
{
props: [
Expand All @@ -16,6 +16,9 @@ export default
this.$set(this.form, this.field.key, state);
},
methods: {
booleanValue(value){
return ( value === 'true' || value === 'false' ) ? value === 'true' : value;
},
runFunction: function(action, e){
if ( typeof this.to[action] == 'function' ) this.to[action].call(this, e);
},
Expand Down
80 changes: 80 additions & 0 deletions src/fields/fieldInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import baseField from './baseField';
export default {
render(h){
const children = [];
const self = this;

// add the label if it needs it
if ( this.to.label ) children.push(
h('label', {
attrs: {
'for': this.to.id
}
}, this.to.label )
);

// add the input
children.push(
h('input', {
attrs: {
id: this.to.id,
type: this.to.inputType || 'text',
...this.to.atts
},
'class': {
'form-control': true,
...this.to.classes
},
domProps: {
value: this.model[ this.field.key ]
},
on: {
input(event){
self.model[ self.field.key ] = event.target.value;
self.$emit('input', event.target.value);
},
blur: this.onBlur,
focus: this.onFocus,
click: this.onClick,
change: this.onChange,
keyup: this.onKeyup,
keydown: this.onKeydown
}
})
);

// add the error element
children.push(
h('error-display', {
props: {
form: this.form,
field: this.field.key
}
})
);
return h('div', {
'class': [
'form-group formly-input',
this.to.inputType,
this.to.wrapperClasses,
{
'formly-has-value': this.model[ this.field.key ],
'formly-has-focus': this.form[ this.field.key ].$active,
'has-error has-danger': this.hasError
}
]
}, children);
},
mixins: [baseField],
methods: {
onChange: function(e){

this.$set(this.form[this.field.key], '$dirty', true);
this.runFunction('onChange', e);
if ( this.to.inputType == 'file' ){
this.$set(this.model, this.field.key, this.$el.querySelector('input').files);
}

}
}
}
25 changes: 0 additions & 25 deletions src/fields/fieldInput.vue

This file was deleted.

103 changes: 103 additions & 0 deletions src/fields/fieldList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import baseField from './baseField';
export default {
computed: {
inputType(){
return this.to.inputType || 'checkbox';
}
},
created(){
// set the default value to array
if ( this.inputType === 'checkbox' && ( this.model[ this.field.key ].constructor !== Array || !this.model[ this.field.key ].constructor) ) this.$set( this.model, this.field.key, [] );
},
mixins: [baseField],
render(h){
const children = [];
const self = this;
const isArray = this.inputType === 'checkbox';

// add the label if it needs it
if ( this.to.label ) children.push(
h('label', this.to.label )
);

// create each option
if ( 'options' in this.field ){
this.field.options.forEach( option => {
const optionLabel = option.hasOwnProperty('label') ? option.label : option;
const optionVal = option.hasOwnProperty('value') ? option.value : option;
const optionChecked = isArray ? this.model[ this.field.key ].indexOf( optionVal ) > -1 : this.model[ this.field.key ] === optionVal;

children.push(
// wrap each option in a label
h('label', {
'class': this.to.labelClasses
}, [
// create an input
h('input', {
attrs: {
type: this.inputType,
...this.to.atts
},
'class': {
...this.to.classes
},
domProps: {
value: optionVal,
checked: optionChecked
},
on: {
click: this.onClick,
blur: this.onBlur,
focus: this.onFocus,
keyup: this.onKeyup,
keydown: this.onKeydown,
change(event){
const isChecked = event.target.checked;
const val = self.booleanValue( event.target.value );
// we need to add/remove differently depending on the type of input we're using
if ( isArray ){
if ( isChecked ){
// if it's a checkbox, and hence an array, push it
self.model[ self.field.key ].push( val );
} else {
// otherwise remove it
const valueIdx = self.model[ self.field.key ].indexOf( val );
if ( valueIdx > -1 ) self.model[ self.field.key ].splice( valueIdx, 1 );
}
} else {
self.model[ self.field.key ] = isChecked ? val : null;
}
self.$emit('change', val);
if ( typeof self.onChange === 'function' ) self.onChange(event);
}
}
}),
// display the label
optionLabel
])
);
});
}

// add the error element
children.push(
h('error-display', {
props: {
form: this.form,
field: this.field.key
}
})
);

// create the wrapper element
return h('div', {
'class': [
'form-group formly-list',
this.to.wrapperClasses,
{
'has-error has-danger': this.hasError
}
]
}, children);
}
}
23 changes: 0 additions & 23 deletions src/fields/fieldList.vue

This file was deleted.

Loading