2019-10-07 17:24:30 +03:00
|
|
|
<template>
|
|
|
|
<div
|
|
|
|
class="formulate-input"
|
|
|
|
:data-classification="classification"
|
2019-11-07 01:17:19 +03:00
|
|
|
:data-has-errors="hasErrors"
|
2019-10-07 17:24:30 +03:00
|
|
|
: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"
|
2019-10-08 20:50:53 +03:00
|
|
|
:for="context.attributes.id"
|
2019-10-07 17:24:30 +03:00
|
|
|
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"
|
2019-10-08 20:50:53 +03:00
|
|
|
:for="context.attributes.id"
|
2019-10-07 17:24:30 +03:00
|
|
|
v-text="context.label"
|
|
|
|
/>
|
|
|
|
</slot>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
v-if="help"
|
|
|
|
class="formulate-input-help"
|
|
|
|
v-text="help"
|
|
|
|
/>
|
2019-11-07 01:17:19 +03:00
|
|
|
<FormulateInputErrors
|
|
|
|
:errors="mergedErrors"
|
|
|
|
/>
|
2019-10-07 17:24:30 +03:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import context from './libs/context'
|
2019-10-08 20:50:53 +03:00
|
|
|
import { shallowEqualObjects } from './libs/utils'
|
2019-10-07 17:24:30 +03:00
|
|
|
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 }
|
|
|
|
},
|
2019-10-07 17:24:30 +03:00
|
|
|
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 */
|
2019-10-07 17:24:30 +03:00
|
|
|
formulateValue: {
|
2019-10-30 06:33:31 +03:00
|
|
|
default: ''
|
2019-10-07 17:24:30 +03:00
|
|
|
},
|
|
|
|
value: {
|
|
|
|
default: false
|
|
|
|
},
|
2019-10-09 06:54:16 +03:00
|
|
|
/* eslint-enable */
|
2019-10-07 17:24:30 +03:00
|
|
|
options: {
|
|
|
|
type: [Object, Array, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
optionGroups: {
|
|
|
|
type: [Object, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
type: [String, Boolean, Number],
|
2019-10-08 20:50:53 +03:00
|
|
|
default: false
|
2019-10-07 17:24:30 +03:00
|
|
|
},
|
|
|
|
label: {
|
|
|
|
type: [String, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
labelPosition: {
|
|
|
|
type: [String, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
help: {
|
|
|
|
type: [String, Boolean],
|
|
|
|
default: false
|
2019-10-08 20:50:53 +03:00
|
|
|
},
|
|
|
|
debug: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2019-11-07 01:17:19 +03:00
|
|
|
},
|
|
|
|
errors: {
|
|
|
|
type: [String, Array, Boolean],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
validation: {
|
|
|
|
type: [String, Boolean, Array],
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
validationBehavior: {
|
|
|
|
type: String,
|
|
|
|
default: 'blur',
|
|
|
|
validator: function (value) {
|
|
|
|
return ['blur', 'live'].includes(value)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
type: [String, Boolean],
|
|
|
|
default: false
|
2019-10-08 20:50:53 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
defaultId: nanoid(9),
|
2019-10-30 06:33:31 +03:00
|
|
|
localAttributes: {},
|
|
|
|
internalModelProxy: this.formulateValue
|
2019-10-07 17:24:30 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2019-10-08 20:50:53 +03:00
|
|
|
...context,
|
2019-10-07 17:24:30 +03:00
|
|
|
classification () {
|
|
|
|
const classification = this.$formulate.classify(this.type)
|
|
|
|
return (classification === 'box' && this.options) ? 'group' : classification
|
|
|
|
},
|
|
|
|
component () {
|
2019-10-08 20:50:53 +03:00
|
|
|
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
|
|
|
|
}
|
2019-10-08 20:50:53 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2019-10-08 20:50:53 +03:00
|
|
|
this.updateLocalAttributes(this.$attrs)
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
updateLocalAttributes (value) {
|
|
|
|
if (!shallowEqualObjects(value, this.localAttributes)) {
|
|
|
|
this.localAttributes = value
|
|
|
|
}
|
2019-10-07 17:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|