Skip to content

Instantly share code, notes, and snippets.

@lkostrowski
Last active April 14, 2017 15:57
Show Gist options
  • Save lkostrowski/e517f9c7abe8e7ba91296ac5ba1579a7 to your computer and use it in GitHub Desktop.
Save lkostrowski/e517f9c7abe8e7ba91296ac5ba1579a7 to your computer and use it in GitHub Desktop.
Input in vue.js
import createRandomId from 'utilities/helpers/createRandomId';
<template>
<div class="input">
<div class="input__field-container" >
<label
:for="thisId" class="input__label"
:class="required ? 'input__label--required' : '' ">{{label}}</label>
<input
class="input__field"
v-model="value"
@focus="_focusHandler"
@blur="_blurHandler"
:id="thisId"
:name="name"/>
<icon class="input__icon input__icon--success" v-if="valid === true" icon="icon-tick"></icon>
<icon class="input__icon input__icon--error" v-if="valid === false" icon="icon-cross"></icon>
</div>
</div>
</template>
<script>
export default {
name: 'input-vue',
props: {
id: {
type: String,
default: null
},
label: String,
required: Boolean,
name: String,
initialValue: {
default: '',
type: String
},
},
watch: {
value( val ) {
this.$emit( 'change', {value: val} );
}
},
data() {
return {
value: this.initialValue,
thisId: this.id ? this.id : createRandomId(),
valid: null,
status: {
dirty: false // If focused and blurred
},
};
},
methods: {
_blurHandler( e ) {
this.setDirty();
this.$emit( 'blur', e );
},
_focusHandler( e ) {
this.$emit( 'focus', e );
},
setDirty() {
this.status.dirty = true;
},
isDirty() {
return this.status.dirty;
},
setValue( value ) {
this.value = value;
},
getValue() {
return this.value;
},
isRequired () {
return this.required;
},
/**
* @param {Boolean} type - sets if success, error or null to reset
*/
setValidation(type) {
this.valid = type;
}
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment