Open
Conversation
eliyas5044
requested changes
Dec 12, 2023
src/components/DTextInput.vue
Outdated
| @@ -0,0 +1,72 @@ | |||
| <template> | |||
Contributor
There was a problem hiding this comment.
<template>
<label :for="id" class="block">
<span>{{ label }}</span>
<div class="relative">
<input
:id="id"
v-model="inputValue"
:type="type"
:placeholder="placeholder"
:disabled="disabled"
:readonly="readonly"
:autofocus="autofocus"
:required="required"
class="w-full border px-2 py-1 rounded"
@focus="$emit('focus')"
@blur="$emit('blur')"
@keyup.enter="$emit('enter')"
/>
<div
v-if="clearable && inputValue"
class="absolute right-2 top-0 bottom-0 cursor-pointer flex items-center h-full"
@click="clearInput"
>
<slot name="clear-icon">
<svg class="inline" xmlns="http://www.w3.org/2000/svg" height="14px" viewBox="0 0 512 512" />
</slot>
</div>
</div>
<p v-if="hint">{{ hint }}</p>
</label>
</template>
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
type Props = {
modelValue?: string;
label?: string;
id?: string;
type?: string;
placeholder?: string;
hint?: string;
disabled?: boolean;
readonly?: boolean;
autofocus?: boolean;
required?: boolean;
clearable?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
type: 'text'
})
const emit = defineEmits(['focus', 'blur', 'enter', 'update:modelValue'])
const inputValue = ref(props.modelValue)
watchEffect(() => {
emit('update:modelValue', inputValue.value)
})
const clearInput = () => {
inputValue.value = ''
}
</script>
Contributor
There was a problem hiding this comment.
<template>
<label :for="id" class="block">
<span>{{ label }}</span>
<div class="relative">
<input
:id="id"
v-model="inputValue"
:type="type"
:placeholder="placeholder"
:disabled="disabled"
:readonly="readonly"
:autofocus="autofocus"
:required="required"
:class="['w-full border px-2 py-1 rounded', inputClass]" <!-- Merging classes -->
@focus="$emit('focus')"
@blur="$emit('blur')"
@keyup.enter="$emit('enter')"
/>
<!-- Rest of your template... -->
</div>
</label>
</template>
<script setup lang="ts">
import { ref, watchEffect } from 'vue'
type Props = {
// ...other props...
inputClass?: string; // New prop for custom input classes
}
const props = withDefaults(defineProps<Props>(), {
type: 'text',
inputClass: '' // Default to empty string if not provided
})
const emit = defineEmits(['focus', 'blur', 'enter', 'update:modelValue'])
const inputValue = ref(props.modelValue)
watchEffect(() => {
emit('update:modelValue', inputValue.value)
})
const clearInput = () => {
inputValue.value = ''
}
</script>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
text-input