forked from noxious/client
#78 : Fixed chipsInput component
This commit is contained in:
parent
50ae61bcb7
commit
7c4f626c5a
@ -1,46 +1,68 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="flex flex-wrap items-center input-cyan gap-1">
|
<div class="flex flex-wrap items-center input-cyan gap-1">
|
||||||
<div v-for="(chip, i) in modelValue" :key="i" class="flex gap-2.5 items-center bg-cyan rounded py-1 px-2">
|
<div v-for="(chip, i) in internalValue" :key="i" class="flex gap-2.5 items-center bg-cyan rounded py-1 px-2">
|
||||||
<span class="text-xs">{{ chip }}</span>
|
<span class="text-xs">{{ chip }}</span>
|
||||||
<button type="button" class="text-xs cursor-pointer text-white font-light font-default not-italic hover:text-gray-50" @click="deleteChip(i)" aria-label="Remove chip">X</button>
|
<button
|
||||||
|
type="button"
|
||||||
|
class="text-xs cursor-pointer text-white font-light font-default not-italic hover:text-gray-50"
|
||||||
|
@click="deleteChip(i)"
|
||||||
|
aria-label="Remove chip"
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<input class="outline-none border-none p-1" placeholder="Tag name" v-model="currentInput" @keypress.enter.prevent="addChip" @keydown.backspace="handleBackspace" />
|
<input
|
||||||
|
class="outline-none border-none p-1"
|
||||||
|
placeholder="Tag name"
|
||||||
|
v-model="currentInput"
|
||||||
|
@keypress.enter.prevent="addChip"
|
||||||
|
@keydown.backspace="handleBackspace"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
import type { Ref } from 'vue'
|
import type { Ref } from 'vue'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
modelValue: string[]
|
modelValue?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = defineProps<Props>()
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
|
modelValue: () => []
|
||||||
|
})
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
(e: 'update:modelValue', value: string[]): void
|
(e: 'update:modelValue', value: string[]): void
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const currentInput: Ref<string> = ref('')
|
const currentInput: Ref<string> = ref('')
|
||||||
|
const internalValue = ref<string[]>([])
|
||||||
|
|
||||||
|
// Initialize internalValue with props.modelValue
|
||||||
|
watch(() => props.modelValue, (newValue) => {
|
||||||
|
internalValue.value = newValue ? [...newValue] : []
|
||||||
|
}, { immediate: true })
|
||||||
|
|
||||||
const addChip = () => {
|
const addChip = () => {
|
||||||
if (currentInput.value.trim() && !props.modelValue.includes(currentInput.value)) {
|
const trimmedInput = currentInput.value.trim()
|
||||||
emit('update:modelValue', [...props.modelValue, currentInput.value.trim()])
|
if (trimmedInput && !internalValue.value.includes(trimmedInput)) {
|
||||||
|
internalValue.value.push(trimmedInput)
|
||||||
|
emit('update:modelValue', internalValue.value)
|
||||||
currentInput.value = ''
|
currentInput.value = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteChip = (index: number) => {
|
const deleteChip = (index: number) => {
|
||||||
emit(
|
internalValue.value.splice(index, 1)
|
||||||
'update:modelValue',
|
emit('update:modelValue', internalValue.value)
|
||||||
props.modelValue.filter((_, i) => i !== index)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleBackspace = (event: KeyboardEvent) => {
|
const handleBackspace = (event: KeyboardEvent) => {
|
||||||
if (event.key === 'Backspace' && currentInput.value === '' && props.modelValue.length > 0) {
|
if (event.key === 'Backspace' && currentInput.value === '' && internalValue.value.length > 0) {
|
||||||
emit('update:modelValue', props.modelValue.slice(0, -1))
|
internalValue.value.pop()
|
||||||
|
emit('update:modelValue', internalValue.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
@ -65,7 +65,7 @@ const zoneEditorStore = useZoneEditorStore()
|
|||||||
const selectedObject = computed(() => assetManagerStore.selectedObject)
|
const selectedObject = computed(() => assetManagerStore.selectedObject)
|
||||||
|
|
||||||
const objectName = ref('')
|
const objectName = ref('')
|
||||||
const objectTags = ref([] as string[])
|
const objectTags = ref<string[]>([])
|
||||||
const objectOriginX = ref(0)
|
const objectOriginX = ref(0)
|
||||||
const objectOriginY = ref(0)
|
const objectOriginY = ref(0)
|
||||||
const objectIsAnimated = ref(false)
|
const objectIsAnimated = ref(false)
|
||||||
|
@ -38,7 +38,7 @@ const zoneEditorStore = useZoneEditorStore()
|
|||||||
const selectedTile = computed(() => assetManagerStore.selectedTile)
|
const selectedTile = computed(() => assetManagerStore.selectedTile)
|
||||||
|
|
||||||
const tileName = ref('')
|
const tileName = ref('')
|
||||||
const tileTags = ref()
|
const tileTags = ref<string[]>([])
|
||||||
|
|
||||||
if (!selectedTile.value) {
|
if (!selectedTile.value) {
|
||||||
console.error('No tile selected')
|
console.error('No tile selected')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user