1
0
mirror of synced 2024-11-30 00:56:09 +03:00
vue-formulario/src/FormulateInput.vue

157 lines
3.5 KiB
Vue
Raw Normal View History

<template>
<div
class="formulate-input"
:data-classification="classification"
:data-type="type"
>
<div class="formulate-input-wrapper">
<slot
v-if="context.label && context.labelPosition === 'before'"
name="label"
v-bind="context"
>
<label
class="formulate-input-label formulate-input-label--before"
:for="context.attributes.id"
v-text="context.label"
/>
</slot>
<slot v-bind="context">
<component
:is="context.component"
:context="context"
/>
</slot>
<slot
v-if="context.label && context.labelPosition === 'after'"
name="label"
v-bind="context.label"
>
<label
class="formulate-input-label formulate-input-label--after"
:for="context.attributes.id"
v-text="context.label"
/>
</slot>
</div>
<div
v-if="help"
class="formulate-input-help"
v-text="help"
/>
</div>
</template>
<script>
import context from './libs/context'
import { shallowEqualObjects } from './libs/utils'
import nanoid from 'nanoid'
export default {
name: 'FormulateInput',
inheritAttrs: false,
2019-10-09 06:54:16 +03:00
inject: {
formulateFormSetter: { default: undefined },
formulateFormRegister: { default: undefined }
},
model: {
prop: 'formulateValue',
event: 'input'
},
props: {
type: {
type: String,
default: 'text'
},
2019-10-09 06:54:16 +03:00
name: {
type: [Boolean, String],
default: true
},
/* eslint-disable */
formulateValue: {
2019-10-30 06:33:31 +03:00
default: ''
},
value: {
default: false
},
2019-10-09 06:54:16 +03:00
/* eslint-enable */
options: {
type: [Object, Array, Boolean],
default: false
},
optionGroups: {
type: [Object, Boolean],
default: false
},
id: {
type: [String, Boolean, Number],
default: false
},
label: {
type: [String, Boolean],
default: false
},
labelPosition: {
type: [String, Boolean],
default: false
},
help: {
type: [String, Boolean],
default: false
},
debug: {
type: Boolean,
default: false
}
},
data () {
return {
defaultId: nanoid(9),
2019-10-30 06:33:31 +03:00
localAttributes: {},
internalModelProxy: this.formulateValue
}
},
computed: {
...context,
classification () {
const classification = this.$formulate.classify(this.type)
return (classification === 'box' && this.options) ? 'group' : classification
},
component () {
return (this.classification === 'group') ? 'FormulateInputGroup' : this.$formulate.component(this.type)
}
},
watch: {
'$attrs': {
handler (value) {
this.updateLocalAttributes(value)
},
deep: true
2019-10-30 06:33:31 +03:00
},
internalModelProxy (newValue, oldValue) {
if (!this.isVmodeled && !shallowEqualObjects(newValue, oldValue)) {
this.context.model = newValue
}
},
formulateValue (newValue, oldValue) {
if (this.isVmodeled && !shallowEqualObjects(newValue, oldValue)) {
this.context.model = newValue
}
}
},
created () {
2019-10-09 06:54:16 +03:00
if (this.formulateFormRegister && typeof this.formulateFormRegister === 'function') {
2019-10-30 06:33:31 +03:00
this.formulateFormRegister(this.nameOrFallback, this)
2019-10-09 06:54:16 +03:00
}
this.updateLocalAttributes(this.$attrs)
},
methods: {
updateLocalAttributes (value) {
if (!shallowEqualObjects(value, this.localAttributes)) {
this.localAttributes = value
}
}
}
}
</script>