Skip to content

Commit

Permalink
refactor(Select): props -> dependency injection (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuniel Acosta Pérez <[email protected]>
  • Loading branch information
Shyam-Chen and yacosta738 authored Apr 3, 2024
1 parent 311f4c1 commit 15b486d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { XBreadcrumb, XCard, XSelect } from '@x/ui';
import WithTable from './WithTable.vue';
import WithTableInDialog from './WithTableInDialog.vue';
import InPopover from './InPopover.vue';
import WithinPopover from './WithinPopover.vue';
const flux = reactive({
select1: '',
Expand Down Expand Up @@ -106,10 +106,10 @@ const flux = reactive({
</section>

<section class="my-8">
<h2 class="text-3xl font-bold my-4 pt-6">In Popover</h2>
<h2 class="text-3xl font-bold my-4 pt-6">Within Popover</h2>

<XCard>
<InPopover />
<WithinPopover />
</XCard>
</section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const flux = reactive({
v-model:value="flux.select"
:label="'Example label'"
:options="flux.select1Options"
useParentOffset
/>
</XCard>
</template>
Expand Down
6 changes: 5 additions & 1 deletion ui/src/components/popover/Popover.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { nextTick, ref, computed, reactive, watch } from 'vue';
import { nextTick, ref, computed, reactive, watch, provide } from 'vue';
import { vOnClickOutside } from '@vueuse/components';
import useScrollParent from '../../composables/scroll-parent/useScrollParent';
Expand Down Expand Up @@ -88,6 +88,10 @@ watch(
}
},
);
provide('Popover', {
withinPopover: true,
});
</script>

<template>
Expand Down
12 changes: 6 additions & 6 deletions ui/src/components/select/Select.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { ref, reactive, computed, watch, watchEffect, nextTick } from 'vue';
import { ref, reactive, computed, watch, watchEffect, nextTick, inject } from 'vue';
import { onClickOutside } from '@vueuse/core';
import { useLocale } from 'vue-localer';
Expand Down Expand Up @@ -35,7 +35,6 @@ const props = withDefaults(
notFoundContent?: string;
invalid?: boolean | string;
help?: string;
useParentOffset?: boolean;
}>(),
{
label: '',
Expand All @@ -50,7 +49,6 @@ const props = withDefaults(
notFoundContent: '',
invalid: undefined,
help: '',
useParentOffset: false,
},
);
Expand All @@ -61,6 +59,8 @@ const emit = defineEmits<{
const locale = useLocale();
const popover = inject('Popover', { withinPopover: false });
const flux = reactive({
show: false,
direction: 'down' as 'down' | 'up',
Expand Down Expand Up @@ -116,19 +116,19 @@ function resizePanel() {
const parentRect = offsetParent.getBoundingClientRect();
selectPanel.value.style.width = `${rect.width}px`;
selectPanel.value.style.left = props.useParentOffset
selectPanel.value.style.left = popover.withinPopover
? `${rect.left - parentRect.left}px`
: `${rect.left}px`;
const center = window.innerHeight / 2;
if (rect.top > center) {
selectPanel.value.style.top = props.useParentOffset
selectPanel.value.style.top = popover.withinPopover
? `${rect.top - parentRect.top}px`
: `${rect.top}px`;
flux.direction = 'up';
} else {
selectPanel.value.style.top = props.useParentOffset
selectPanel.value.style.top = popover.withinPopover
? `${rect.bottom - parentRect.top}px`
: `${rect.bottom}px`;
flux.direction = 'down';
Expand Down

0 comments on commit 15b486d

Please sign in to comment.