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: {
default: '',
},
editable: {
default: true,
},
},
emits: ['update:modelValue', 'finish'],
data() {
return {
inputBorder: false,
tagText: '',
} }
},
computed: {}, type TEmits = {
methods: { (event:'update:modelValue', data: string): void
updateValue(value) { (event: 'finish', data: string): void
this.$emit('update:modelValue', value) }
},
focusInput() { const props = withDefaults(defineProps<TProps>(), {
this.inputBorder = true label: '',
this.tagText = this.modelValue modelValue: '',
}, editable: true,
blurInput() { })
this.inputBorder = false
if (this.modelValue !== this.tagText) { const emit = defineEmits<TEmits>()
this.$emit('finish', this.modelValue)
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)
} }
},
},
} }
</script> </script>