primevue-mirror/apps/showcase/components/layout/designer/DesignTokenField.vue

76 lines
2.2 KiB
Vue
Raw Normal View History

2024-11-12 09:18:07 +00:00
<template>
2024-11-13 16:09:13 +00:00
<div>
<label :for="inputId" class="text-sm">{{ label }}</label>
<div :id="id" class="relative">
<input :id="inputId" :list="listId" type="text" :value="modelValue" @input="onInput" @change="onChange" :class="['relative border border-surface-300 dark:border-surface-600 rounded-lg py-2 px-2 w-full', { 'pr-8': type === 'color' }]" />
<datalist :id="listId" class="max-h-40 overflow-auto">
<option v-for="item of items" :key="item">{{ item }}</option>
</datalist>
<div v-if="type === 'color'" class="absolute right-[4px] top-1/2 -mt-3 w-6 h-6 rounded-md border border-surface-300 dark:border-surface-600" :style="{ backgroundColor: previewColor }"></div>
</div>
2024-11-12 09:18:07 +00:00
</div>
</template>
<script>
2024-11-13 16:09:13 +00:00
import { UniqueComponentId } from '@primevue/core/utils';
2024-11-12 09:18:07 +00:00
import { $dt } from '@primevue/themes';
export default {
emits: ['update:modelValue'],
props: {
label: {
type: String,
default: undefined
},
type: {
type: String,
default: undefined
},
modelValue: {
type: null,
default: undefined
}
},
2024-11-12 23:14:58 +00:00
inject: ['$acTokens'],
2024-11-12 13:44:52 +00:00
data() {
return {
2024-11-13 16:09:13 +00:00
id: null,
items: null
2024-11-12 13:44:52 +00:00
};
},
2024-11-13 16:09:13 +00:00
mounted() {
this.id = 'dt_field_' + UniqueComponentId();
},
2024-11-12 09:18:07 +00:00
methods: {
onInput(event) {
2024-11-13 16:09:13 +00:00
const value = event.target.value;
this.$emit('update:modelValue', value);
2024-11-12 13:44:52 +00:00
2024-11-13 16:09:13 +00:00
if (value.startsWith('{')) {
this.search(value);
2024-11-12 13:44:52 +00:00
} else {
2024-11-13 16:09:13 +00:00
this.items = null;
2024-11-12 13:44:52 +00:00
}
2024-11-13 16:09:13 +00:00
},
onChange() {
this.items = null;
},
search(query) {
this.items = this.$acTokens.filter((t) => t.startsWith(query));
2024-11-12 09:18:07 +00:00
}
},
computed: {
previewColor() {
return this.modelValue && this.modelValue.startsWith('{') && this.modelValue.endsWith('}') ? $dt(this.modelValue).variable : this.modelValue;
2024-11-13 16:09:13 +00:00
},
inputId() {
return this.id + '_input';
},
listId() {
return this.id + '_list';
2024-11-12 09:18:07 +00:00
}
}
};
</script>