Created
April 23, 2018 15:58
-
-
Save larizzatg/41d0c3a99d6d7d4c881003abacf6c5a0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<template> | |
<div class="form-group"> | |
<label for="" v-if="typeof label !== 'undefined'">{{ label }}</label> | |
<!-- GROUP --> | |
<template v-if="isGroup"> | |
<div class="input-group"> | |
<!-- PREPEND --> | |
<div v-if="hasPrepend" class="input-group-prepend" | |
:class="{'inside bg-transparent' : prependInside, 'pointer': prependPointer}" | |
@click="clickPrepend"> | |
<span class="input-group-text" | |
:class="{'bg-transparent' : prependInside}"> | |
<i aria-hidden="true" | |
v-if="prependType === 'icon'" | |
:class="'fa fa-' + prependContent"></i> | |
<template v-if="prependType === 'text'">{{ prependContent }}</template> | |
</span> | |
</div> | |
<!-- INPUT --> | |
<input class="form-control" | |
:type="type" | |
:class="generatedInputClass" | |
:readonly="readonly" | |
:disabled="disabled" | |
:value="value" | |
@input="inputEvent" | |
@change="onChange"> | |
<!-- APPEND --> | |
<div v-if="hasAppend" class="input-group-append" | |
:class="{'inside bg-transparent' : appendInside, 'pointer': appendPointer}" | |
@click="clickAppend"> | |
<span class="input-group-text" | |
:class="{'bg-transparent' : appendInside}"> | |
<i aria-hidden="true" | |
v-if="appendType === 'icon'" | |
:class="'fa fa-' + appendContent"></i> | |
<template v-if="appendType === 'text'">{{ appendContent }}</template> | |
</span> | |
</div> | |
</div> | |
</template> | |
<!-- INPUT --> | |
<template v-else> | |
<input class="form-control" | |
:type="type" | |
:class="generatedInputClass" | |
:readonly="readonly" | |
:disabled="disabled" | |
:value="value" | |
@input="inputEvent" | |
@change="onChange" | |
> | |
</template> | |
<small class="form-text" | |
v-if="typeof helpText !== 'undefined'" | |
:class="generatedHelperClass"> | |
{{ helpText }} | |
</small> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'InputGroup', | |
props: { | |
value: String, | |
label: String, | |
helpText: String, | |
size: String, | |
prependContent: String, | |
appendContent: String, | |
prependType: { | |
type: String, | |
default: 'icon', | |
}, | |
appendType: { | |
type: String, | |
default: 'icon', | |
}, | |
prependInside: { | |
type: Boolean, | |
default: false, | |
}, | |
appendInside: { | |
type: Boolean, | |
default: false, | |
}, | |
prependPointer: { | |
type: Boolean, | |
default: false, | |
}, | |
appendPointer: { | |
type: Boolean, | |
default: false, | |
}, | |
readonly: { | |
type: Boolean, | |
default: false, | |
}, | |
disabled: { | |
type: Boolean, | |
default: false, | |
}, | |
type: { | |
type: String, | |
default: 'text', | |
}, | |
valid: { | |
type: Boolean, | |
default: null, | |
}, | |
}, | |
watch: { | |
valid() { | |
}, | |
}, | |
computed: { | |
isGroup() { | |
return this.hasPrepend || this.hasAppend; | |
}, | |
hasPrepend() { | |
return typeof this.prependContent !== 'undefined'; | |
}, | |
hasAppend() { | |
return typeof this.appendContent !== 'undefined'; | |
}, | |
generatedInputClass() { | |
const size = typeof this.size !== 'undefined' ? `form-control-${this.size}` : ''; | |
let valid = ''; | |
if (this.valid !== null) { | |
valid = this.valid ? 'is-valid' : 'is-invalid'; | |
} | |
return `${size} ${valid}`; | |
}, | |
generatedHelperClass() { | |
let valid = 'text-muted'; | |
if (this.valid !== null) { | |
valid = this.valid ? 'valid-feedback' : 'invalid-feedback'; | |
} | |
return `${valid}`; | |
}, | |
}, | |
methods: { | |
inputEvent(e) { | |
this.$emit('input', e.target.value); | |
}, | |
clickPrepend(e) { | |
this.$emit('click-prepend', e); | |
}, | |
clickAppend(e) { | |
this.$emit('click-append', e); | |
}, | |
onChange(e) { | |
this.$emit('change', this.value, e); | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment