opnform/resources/js/components/forms/TextInput.vue

85 lines
2.2 KiB
Vue
Raw Normal View History

2022-09-20 19:59:52 +00:00
<template>
2023-10-24 09:00:54 +00:00
<input-wrapper
v-bind="$props"
>
<template #label>
<slot name="label" />
</template>
2022-09-20 19:59:52 +00:00
<input :id="id?id:name" v-model="compVal" :disabled="disabled"
:type="nativeType"
:pattern="pattern"
2022-09-20 19:59:52 +00:00
:style="inputStyle"
2023-10-19 08:46:04 +00:00
:class="[theme.default.input, { '!ring-red-500 !ring-2': hasError, '!cursor-not-allowed !bg-gray-200': disabled }]"
2022-09-20 19:59:52 +00:00
:name="name" :accept="accept"
:placeholder="placeholder" :min="min" :max="max" :maxlength="maxCharLimit"
@change="onChange" @keydown.enter.prevent="onEnterPress"
>
2023-10-24 09:00:54 +00:00
2023-10-28 10:17:50 +00:00
<template v-if="maxCharLimit && showCharLimit" #bottom_after_help>
<small :class="theme.default.help">
{{ charCount }}/{{ maxCharLimit }}
</small>
</template>
2023-10-24 09:00:54 +00:00
<template #error>
<slot name="error" />
</template>
</input-wrapper>
2022-09-20 19:59:52 +00:00
</template>
<script>
2023-10-19 08:46:04 +00:00
import { inputProps, useFormInput } from './useFormInput.js'
2023-10-24 09:00:54 +00:00
import InputWrapper from './components/InputWrapper.vue'
2023-10-19 08:46:04 +00:00
2022-09-20 19:59:52 +00:00
export default {
name: 'TextInput',
2023-10-27 09:58:01 +00:00
components: { InputWrapper },
2022-09-20 19:59:52 +00:00
props: {
2023-10-19 08:46:04 +00:00
...inputProps,
2022-09-20 19:59:52 +00:00
nativeType: { type: String, default: 'text' },
accept: { type: String, default: null },
min: { type: Number, required: false, default: null },
max: { type: Number, required: false, default: null },
maxCharLimit: { type: Number, required: false, default: null },
showCharLimit: { type: Boolean, required: false, default: false },
pattern: { type: String, default: null }
2022-09-20 19:59:52 +00:00
},
2023-10-24 09:00:54 +00:00
setup (props, context) {
const {
compVal,
inputStyle,
hasValidation,
hasError
} = useFormInput(props, context, props.nativeType === 'file' ? 'file-' : null)
2022-09-20 19:59:52 +00:00
2023-10-19 08:46:04 +00:00
const onChange = (event) => {
if (props.nativeType !== 'file') return
2022-09-20 19:59:52 +00:00
const file = event.target.files[0]
2023-10-19 08:46:04 +00:00
// eslint-disable-next-line vue/no-mutating-props
props.form[props.name] = file
}
const onEnterPress = (event) => {
2022-09-20 19:59:52 +00:00
event.preventDefault()
return false
}
2023-10-19 08:46:04 +00:00
return {
compVal,
inputStyle,
hasValidation,
hasError
}
2023-10-24 09:00:54 +00:00
},
computed: {
charCount () {
return (this.compVal) ? this.compVal.length : 0
}
2022-09-20 19:59:52 +00:00
}
}
</script>