feat: convert text input component to composition API

This commit is contained in:
IchliebedichZhu 2024-03-10 10:09:53 +00:00
parent c23edc33b6
commit c63ca83894

View File

@ -9,51 +9,59 @@
<div class="text-input"> <div class="text-input">
<p v-if="label" class="input-label">{{ label }}</p> <p v-if="label" class="input-label">{{ label }}</p>
<div :class="{ 'input-wrap': true, active: inputBorder }"> <div :class="{ 'input-wrap': true, active: inputBorder }">
<input :class="{ 'real-input': true, disable: !editable }" type="text" :value="modelValue" :readonly="editable ? false : 'readonly'" @input="updateValue($event.target.value)" @focus="focusInput" @blur="blurInput" /> <input
:class="{ 'real-input': true, disable: !props.editable }"
type="text" :value="props.modelValue"
:readonly="!editable"
@input="updateValue(($event.target as HTMLInputElement).value)"
@focus="focusInput"
@blur="blurInput"
/>
</div> </div>
</div> </div>
</template> </template>
<script> <script lang="ts" setup>
import { ref } from 'vue'
// //
const NAME = 'text-input' const NAME = 'text-input'
export default { type TProps = {
name: NAME, label?: string
props: { modelValue?: string
label: { editable?: boolean
default: '', }
},
modelValue: { type TEmits = {
default: '', (event:'update:modelValue', data: string): void
}, (event: 'finish', data: string): void
editable: { }
default: true,
}, const props = withDefaults(defineProps<TProps>(), {
}, label: '',
emits: ['update:modelValue', 'finish'], modelValue: '',
data() { editable: true,
return { })
inputBorder: false,
tagText: '', const emit = defineEmits<TEmits>()
const inputBorder = ref(false)
const tagText = ref<string>('')
function updateValue(value: string) {
emit('update:modelValue', value)
}
function focusInput() {
inputBorder.value = true
tagText.value = props.modelValue
}
function blurInput() {
inputBorder.value = false
if (props.modelValue !== tagText.value) {
emit('finish', props.modelValue)
} }
},
computed: {},
methods: {
updateValue(value) {
this.$emit('update:modelValue', value)
},
focusInput() {
this.inputBorder = true
this.tagText = this.modelValue
},
blurInput() {
this.inputBorder = false
if (this.modelValue !== this.tagText) {
this.$emit('finish', this.modelValue)
}
},
},
} }
</script> </script>