Skip to content

Commit

Permalink
fix(v-select): add tooltip component as dep
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitano committed Mar 22, 2022
1 parent cb927cd commit 3aea3e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@gits-id/input": "^0.1.17-alpha.0",
"@gits-id/utils": "^0.1.17-alpha.0",
"@gits-id/tooltip": "^0.1.17-alpha.0",
"vue": "^3.2.31"
},
"devDependencies": {
Expand Down
70 changes: 43 additions & 27 deletions packages/select/src/VSelect.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<script setup lang="ts">
import { computed, PropType, ref, toRefs, watch } from "vue";
import { Listbox, ListboxButton, ListboxOptions, ListboxOption } from "@headlessui/vue";
import { CheckIcon, ChevronDownIcon, XIcon } from "@heroicons/vue/solid";
import { getBgColor, getTextColor } from "@gits-id/utils";
import VInput from "@gits-id/input";
import { ErrorMessage } from "vee-validate";
import {computed, PropType, ref, toRefs, watch} from 'vue';
import {
Listbox,
ListboxButton,
ListboxOptions,
ListboxOption,
} from '@headlessui/vue';
import {CheckIcon, ChevronDownIcon, XIcon} from '@heroicons/vue/solid';
import {getBgColor, getTextColor} from '@gits-id/utils';
import VInput from '@gits-id/input';
import VTooltip from '@gits-id/tooltip';
import {ErrorMessage} from 'vee-validate';
interface SelectItem {
text: string;
Expand All @@ -28,11 +34,11 @@ const props = defineProps({
},
color: {
type: String,
default: "primary",
default: 'primary',
},
placeholder: {
type: String,
default: "Select",
default: 'Select',
},
searchable: {
type: Boolean,
Expand All @@ -44,23 +50,23 @@ const props = defineProps({
},
btnClass: {
type: String,
default: "",
default: '',
},
top: {
type: Boolean,
default: false,
},
itemText: {
type: String,
default: "text",
default: 'text',
},
itemValue: {
type: String,
default: "value",
default: 'value',
},
name: {
type: String,
default: "",
default: '',
},
error: {
type: Boolean,
Expand All @@ -80,7 +86,7 @@ const props = defineProps({
},
clearText: {
type: String,
default: "Clear",
default: 'Clear',
},
disabled: {
type: Boolean,
Expand All @@ -107,15 +113,15 @@ const {
returnObject,
} = toRefs(props);
const emit = defineEmits(["update:modelValue", "update:value", "search"]);
const emit = defineEmits(['update:modelValue', 'update:value', 'search']);
const bgColor = getBgColor(color.value);
const textColor = getTextColor(color.value);
type Val = string | number | boolean | Record<string, any> | null;
const selectedItem = ref<Val>(modelValue.value || value.value);
const search = ref("");
const search = ref('');
const filteredItems = computed(() => {
if (searchable.value) {
Expand All @@ -137,7 +143,7 @@ const setValue = (val: Val) => {
} else {
const newVal = val || modelValue.value || value.value;
if (["string", "number", "boolean"].includes(typeof newVal)) {
if (['string', 'number', 'boolean'].includes(typeof newVal)) {
const itemVal = filteredItems.value.find((item) => {
return item[itemValue.value] === newVal;
});
Expand All @@ -151,33 +157,37 @@ const setValue = (val: Val) => {
};
watch(selectedItem, (item) => {
const val = returnObject.value ? item : (item as SelectItem)?.[itemValue.value];
emit("update:modelValue", val);
emit("update:value", val);
const val = returnObject.value
? item
: (item as SelectItem)?.[itemValue.value];
emit('update:modelValue', val);
emit('update:value', val);
});
watch(
modelValue,
(val) => {
setValue(val);
},
{ immediate: true }
{immediate: true},
);
watch(
value,
(val) => {
setValue(val);
},
{ immediate: true }
{immediate: true},
);
watch(search, (val) => {
emit("search", val);
emit('search', val);
});
const label = computed(() => {
return (selectedItem.value as SelectItem)?.[itemText.value] || placeholder.value;
return (
(selectedItem.value as SelectItem)?.[itemText.value] || placeholder.value
);
});
const clear = () => (selectedItem.value = null);
Expand All @@ -198,7 +208,7 @@ const clear = () => (selectedItem.value = null);
</slot>
</div>
<v-tooltip v-if="selectedItem && clearable">
<template #activator="{ on }">
<template #activator="{on}">
<span v-on="on" @click="clear" class="w-auto cursor-pointer">
<XIcon
class="w-5 h-5 text-gray-400 hover:text-gray-500"
Expand All @@ -225,12 +235,15 @@ const clear = () => (selectedItem.value = null);
<div v-if="searchable" class="px-2 border-b py-2">
<v-input v-model="search" size="sm" placeholder="Search..." />
</div>
<div v-if="searchable && !filteredItems.length" class="px-4 pb-2 pt-3">
<div
v-if="searchable && !filteredItems.length"
class="px-4 pb-2 pt-3"
>
<slot name="empty"> No results </slot>
</div>
<ListboxOption
v-for="(item, index) in filteredItems"
v-slot="{ active, selected }"
v-slot="{active, selected}"
:key="index"
:value="item"
as="template"
Expand All @@ -244,7 +257,10 @@ const clear = () => (selectedItem.value = null);
]"
>
<span
:class="[selected ? 'font-medium' : 'font-normal', 'block truncate']"
:class="[
selected ? 'font-medium' : 'font-normal',
'block truncate',
]"
>
<slot name="item" :item="item">
{{ item?.[itemText] }}
Expand Down
9 changes: 6 additions & 3 deletions packages/utils/sizes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed } from 'vue';
import {computed} from 'vue';

export type Sizes = 'xs' | 'sm' | 'default' | 'lg' | 'xl' | '' | string;

Expand All @@ -23,7 +23,7 @@ export const useTextSize = (size: string) => {
}
});

return { class: className, size };
return {class: className, size};
};

export const useHeight = (size: string) => {
Expand All @@ -37,10 +37,13 @@ export const useHeight = (size: string) => {
return 'h-[50px]';
case 'xl':
return 'h-[60px]';
case 'default':
case 'md':
case 'base':
default:
return 'h-[40px]';
}
});

return { class: className, size };
return {class: className, size};
};

0 comments on commit 3aea3e2

Please sign in to comment.