opnform-host-nginx/client/mixins/forms/input.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

import { themes } from "~/lib/forms/form-themes.js"
import { default as _has } from "lodash/has"
2023-12-09 15:47:03 +01:00
export default {
props: {
id: { type: String, default: null },
name: { type: String, required: true },
label: { type: String, required: false },
form: { type: Object, required: false },
value: { required: false },
required: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
placeholder: { type: String, default: null },
uppercaseLabels: { type: Boolean, default: false },
help: { type: String, default: null },
helpPosition: { type: String, default: "below_input" },
2023-12-09 15:47:03 +01:00
theme: { type: Object, default: () => themes.default },
color: { type: String, default: "#3B82F6" },
wrapperClass: { type: String, default: "relative mb-3" },
2023-12-09 15:47:03 +01:00
},
data() {
2023-12-09 15:47:03 +01:00
return {
content: this.value,
2023-12-09 15:47:03 +01:00
}
},
computed: {
inputStyle() {
2023-12-09 15:47:03 +01:00
return {
"--tw-ring-color": this.color,
2023-12-09 15:47:03 +01:00
}
},
hasValidation() {
return (
this.form !== null &&
this.form !== undefined &&
_has(this.form, "errors")
)
2023-12-09 15:47:03 +01:00
},
compVal: {
set(val) {
2023-12-09 15:47:03 +01:00
if (this.form) {
this.form[this.name] = val
} else {
this.content = val
}
if (this.hasValidation) {
this.form.errors.clear(this.name)
}
this.$emit("input", this.compVal)
2023-12-09 15:47:03 +01:00
},
get() {
2023-12-09 15:47:03 +01:00
if (this.form) {
return this.form[this.name]
}
return this.content
},
},
2023-12-09 15:47:03 +01:00
},
watch: {
value(val) {
2023-12-09 15:47:03 +01:00
if (val !== this.compVal) {
this.compVal = val
}
},
},
2023-12-09 15:47:03 +01:00
}