2024-04-15 19:39:03 +02:00
|
|
|
import { ref, computed, watch } from "vue"
|
|
|
|
|
import { themes } from "~/lib/forms/form-themes.js"
|
|
|
|
|
import { default as _get } from "lodash/get"
|
|
|
|
|
import { default as _set } from "lodash/set"
|
|
|
|
|
import { default as _has } from "lodash/has"
|
2023-12-09 15:47:03 +01:00
|
|
|
|
|
|
|
|
export const inputProps = {
|
|
|
|
|
id: { type: String, default: null },
|
|
|
|
|
name: { type: String, required: true },
|
|
|
|
|
label: { type: String, required: false },
|
|
|
|
|
form: { type: Object, required: false },
|
|
|
|
|
theme: { type: Object, default: () => themes.default },
|
|
|
|
|
modelValue: { required: false },
|
|
|
|
|
required: { type: Boolean, default: false },
|
|
|
|
|
disabled: { type: Boolean, default: false },
|
|
|
|
|
placeholder: { type: String, default: null },
|
|
|
|
|
uppercaseLabels: { type: Boolean, default: false },
|
|
|
|
|
hideFieldName: { type: Boolean, default: false },
|
2024-04-22 16:42:38 +02:00
|
|
|
showCharLimit: { type: Boolean, default: false },
|
2023-12-09 15:47:03 +01:00
|
|
|
help: { type: String, default: null },
|
2024-04-15 19:39:03 +02:00
|
|
|
helpPosition: { type: String, default: "below_input" },
|
|
|
|
|
color: { type: String, default: "#3B82F6" },
|
|
|
|
|
wrapperClass: { type: String, default: "relative mb-3" },
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export function useFormInput(props, context, formPrefixKey = null) {
|
2023-12-09 15:47:03 +01:00
|
|
|
const content = ref(props.modelValue)
|
|
|
|
|
|
|
|
|
|
const inputStyle = computed(() => {
|
|
|
|
|
return {
|
2024-04-15 19:39:03 +02:00
|
|
|
"--tw-ring-color": props.color,
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const hasValidation = computed(() => {
|
2024-04-15 19:39:03 +02:00
|
|
|
return (
|
|
|
|
|
props.form !== null &&
|
|
|
|
|
props.form !== undefined &&
|
|
|
|
|
_has(props.form, "errors")
|
|
|
|
|
)
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const hasError = computed(() => {
|
2024-02-29 17:31:20 +01:00
|
|
|
return hasValidation.value && props.form?.errors?.has(props.name)
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const compVal = computed({
|
|
|
|
|
get: () => {
|
|
|
|
|
if (props.form) {
|
2024-04-15 19:39:03 +02:00
|
|
|
return _get(props.form, (formPrefixKey || "") + props.name)
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
return content.value
|
|
|
|
|
},
|
|
|
|
|
set: (val) => {
|
|
|
|
|
if (props.form) {
|
2024-04-15 19:39:03 +02:00
|
|
|
_set(props.form, (formPrefixKey || "") + props.name, val)
|
2023-12-09 15:47:03 +01:00
|
|
|
} else {
|
|
|
|
|
content.value = val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasValidation.value) {
|
|
|
|
|
props.form.errors.clear(props.name)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
context.emit("update:modelValue", compVal.value)
|
|
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const inputWrapperProps = computed(() => {
|
|
|
|
|
const wrapperProps = {}
|
|
|
|
|
Object.keys(inputProps).forEach((key) => {
|
2024-04-15 19:39:03 +02:00
|
|
|
if (!["modelValue", "disabled", "placeholder", "color"].includes(key)) {
|
2023-12-09 15:47:03 +01:00
|
|
|
wrapperProps[key] = props[key]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return wrapperProps
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Watch for changes in props.modelValue and update the local content
|
|
|
|
|
watch(
|
|
|
|
|
() => props.modelValue,
|
|
|
|
|
(newValue) => {
|
|
|
|
|
if (content.value !== newValue) {
|
|
|
|
|
content.value = newValue
|
|
|
|
|
}
|
2024-04-15 19:39:03 +02:00
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
compVal,
|
|
|
|
|
inputStyle,
|
|
|
|
|
hasValidation,
|
|
|
|
|
hasError,
|
2024-04-15 19:39:03 +02:00
|
|
|
inputWrapperProps,
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
}
|