Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit f57dc82

Browse files
committed
feat: Remove uid prop
1 parent 878fc96 commit f57dc82

File tree

9 files changed

+38
-37
lines changed

9 files changed

+38
-37
lines changed

index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { DefineComponent, ComputedOptions, ComponentOptionsMixin, MethodOptions
44
type EmitEvents = 'update:modelValue' | 'textSubmit' | 'closed' | 'cleared';
55

66
interface Vue3DatePicker {
7-
uid?: string;
87
is24?: boolean;
98
enableTimePicker?: boolean;
109
range?: boolean;

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
range,
1717
isMenuOpen: isOpen,
1818
pattern: defaultPattern,
19-
uid,
2019
}"
2120
v-model:input-value="inputValue"
2221
@clear="clearValue"
@@ -35,7 +34,6 @@
3534
:class="theme"
3635
:style="menuPosition"
3736
v-bind="{
38-
uid,
3937
weekNumbers,
4038
weekStart,
4139
disableMonthYearSelect,
@@ -126,7 +124,6 @@
126124
127125
const emit = defineEmits(['update:modelValue', 'textSubmit', 'closed', 'cleared']);
128126
const props = defineProps({
129-
uid: { type: String as PropType<string>, default: 'dp' },
130127
is24: { type: Boolean as PropType<boolean>, default: true },
131128
enableTimePicker: { type: Boolean as PropType<boolean>, default: true },
132129
locale: { type: String as PropType<string>, default: 'en-US' },
@@ -231,8 +228,9 @@
231228
232229
const { openOnTop, menuPosition, setMenuPosition, recalculatePosition } = usePosition(
233230
props.position,
234-
props.uid,
235231
props.altPosition,
232+
dpMenuRef,
233+
inputRef,
236234
);
237235
238236
const { internalModelValue, inputValue, parseExternalModelValue, emitModelValue, checkBeforeEmit } =

src/Vue3DatePicker/components/DatepickerInput.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<template>
22
<div
33
@click="handleOpen"
4-
:id="`dp__input_${uid}`"
54
aria-label="Datepicker input"
65
role="textbox"
76
aria-multiline="false"
@@ -75,7 +74,6 @@
7574
textInputOptions: { type: Object as PropType<ITextInputOptions>, default: () => null },
7675
isMenuOpen: { type: Boolean as PropType<boolean>, default: false },
7776
pattern: { type: String as PropType<string>, default: '' },
78-
uid: { type: String as PropType<string>, default: 'dp' },
7977
});
8078
const parsedDate = ref();
8179

src/Vue3DatePicker/components/DatepickerMenu.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<template>
22
<div
33
:class="dpMenuClass"
4-
:id="`dp__menu_${uid}`"
54
@mouseleave="clearHoverDate"
65
role="dialog"
76
aria-label="Datepicker menu"
87
@click="handleDpMenuClick"
98
>
109
<div :class="arrowClass" v-if="!inline"></div>
11-
<div :class="menuCalendarClassWrapper" :id="`dp__calendar_wrapper_${uid}`" role="document">
10+
<div :class="menuCalendarClassWrapper" ref="calendarWrapperRef" role="document">
1211
<Calendar
1312
v-bind="calendarProps"
1413
:instance="1"
@@ -94,7 +93,7 @@
9493
WeekStartStr,
9594
} from '../interfaces';
9695
import { mapSlots } from './composition/slots';
97-
import { getCalendarDays, getMonths, getYears } from '../utils/util';
96+
import { getCalendarDays, getMonths, getYears, unrefElement } from '../utils/util';
9897
import { useCalendar } from './composition/calendar';
9998
import { isDateEqual } from '../utils/date-utils';
10099
@@ -107,7 +106,6 @@
107106
'timeUpdate',
108107
]);
109108
const props = defineProps({
110-
uid: { type: String as PropType<string>, default: 'dp' },
111109
internalModelValue: { type: [Date, Array] as PropType<InternalModuleValue>, default: null },
112110
weekNumbers: { type: Boolean as PropType<boolean>, default: false },
113111
weekStart: { type: [Number, String] as PropType<WeekStartNum | WeekStartStr>, default: 1 },
@@ -160,12 +158,13 @@
160158
allowedDates: { type: Array as PropType<string[] | Date[]>, default: () => [] },
161159
});
162160
const slots = useSlots();
161+
const calendarWrapperRef = ref(null);
163162
const calendarWidth = ref(0);
164163
const menuMount = ref(false);
165164
166165
onMounted(() => {
167166
menuMount.value = true;
168-
const el = document.getElementById(`dp__calendar_wrapper_${props.uid}`);
167+
const el = unrefElement(calendarWrapperRef);
169168
if (el) {
170169
calendarWidth.value = el.getBoundingClientRect().width;
171170
}

src/Vue3DatePicker/components/MonthYearInput.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</div>
2828
<SelectionGrid
2929
v-if="showMonthPicker"
30-
v-bind="{ modelValue: month, items: groupedMonths, disabledValues: filters.months, uid: instance }"
30+
v-bind="{ modelValue: month, items: groupedMonths, disabledValues: filters.months }"
3131
@update:modelValue="onMonthUpdate"
3232
@toggle="toggleMonthPicker"
3333
>
@@ -41,7 +41,7 @@
4141
</SelectionGrid>
4242
<SelectionGrid
4343
v-if="showYearPicker"
44-
v-bind="{ modelValue: year, items: groupedYears, disabledValues: filters.years, uid: instance }"
44+
v-bind="{ modelValue: year, items: groupedYears, disabledValues: filters.years }"
4545
@update:modelValue="onYearUpdate"
4646
@toggle="toggleYearPicker"
4747
><template #button-icon>
@@ -98,8 +98,6 @@
9898
modelValue: year,
9999
items: groupedYears,
100100
disabledValues: filters.years,
101-
gridId: 'dp__overlay_inner',
102-
uid: 'inner',
103101
}"
104102
@update:modelValue="onYearUpdate"
105103
@toggle="toggleYearPicker"

src/Vue3DatePicker/components/SelectionGrid.vue

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="dp__overlay" :id="gridId" :class="dpOverlayClass" role="dialog">
2+
<div class="dp__overlay" ref="gridWrapRef" :class="dpOverlayClass" role="dialog">
33
<div class="dp__overlay_container" role="grid">
44
<div class="dp__selection_grid_header"><slot name="header"></slot></div>
55
<div class="dp__overlay_row" v-for="(row, i) in mappedItems" :key="getKey(i)" role="row">
@@ -11,9 +11,7 @@
1111
@click="onClick(col.value)"
1212
:aria-selected="col.value === modelValue && !disabledValues.includes(col.value)"
1313
:aria-disabled="col.className.dp__overlay_cell_disabled"
14-
:id="
15-
col.value === modelValue && !disabledValues.includes(col.value) ? `selection-active${uid}` : ''
16-
"
14+
:ref="col.value === modelValue && !disabledValues.includes(col.value) ? `selectionActiveRef` : ''"
1715
>
1816
<div :class="col.className">
1917
<slot v-if="$slots.item" name="item" :item="col" />
@@ -38,28 +36,28 @@
3836
import { computed, onMounted, PropType, ref } from 'vue';
3937
4038
import { IDefaultSelect, DynamicClass } from '../interfaces';
41-
import { getKey } from '../utils/util';
39+
import { getKey, unrefElement } from '../utils/util';
4240
4341
const emit = defineEmits(['update:modelValue', 'selected', 'toggle']);
4442
4543
const props = defineProps({
46-
uid: { type: [String, Number] as PropType<string | number>, default: '' },
4744
items: { type: Array as PropType<IDefaultSelect[][]>, default: () => [] },
4845
modelValue: { type: [String, Number] as PropType<string | number>, default: null },
49-
gridId: { type: String as PropType<string>, default: 'dp__overlay' },
5046
disabledValues: { type: Array as PropType<number[]>, default: () => [] },
5147
minValue: { type: [Number, String] as PropType<number | string>, default: null },
5248
maxValue: { type: [Number, String] as PropType<number | string>, default: null },
5349
});
5450
5551
const scrollable = ref(false);
52+
const selectionActiveRef = ref(null);
53+
const gridWrapRef = ref(null);
5654
5755
/**
5856
* On mounted hook, set the scroll position, if any to a selected value when opening overlay
5957
*/
6058
onMounted(() => {
6159
setScrollPosition();
62-
const elm = document.getElementById(props.gridId);
60+
const elm = unrefElement(gridWrapRef);
6361
if (elm) {
6462
scrollable.value = elm.clientHeight < elm.scrollHeight;
6563
}
@@ -124,9 +122,9 @@
124122
* Set scroll position in overlay based on active selection
125123
*/
126124
const setScrollPosition = (): void => {
127-
const el = document.getElementById(`selection-active${props.uid}`);
125+
const el = unrefElement(selectionActiveRef);
128126
if (el) {
129-
const parent = document.getElementById(props.gridId);
127+
const parent = unrefElement(gridWrapRef);
130128
if (parent) {
131129
parent.scrollTop =
132130
el.offsetTop -

src/Vue3DatePicker/components/composition/position.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { OpenPosition } from '../../interfaces';
21
import { ref, Ref } from 'vue';
2+
import { OpenPosition } from '../../interfaces';
3+
import { unrefElement } from '../../utils/util';
34

45
interface IUsePosition {
56
menuPosition: Ref<{ top: string; left: string; transform: string }>;
@@ -8,10 +9,17 @@ interface IUsePosition {
89
recalculatePosition: () => void;
910
}
1011

12+
type ComponentRef = Ref<HTMLElement | null>;
13+
1114
/**
1215
* Extracted code from the main component, used for calculating the position of the menu
1316
*/
14-
export const usePosition = (openPosition: OpenPosition, uid: string, altPosition: boolean): IUsePosition => {
17+
export const usePosition = (
18+
openPosition: OpenPosition,
19+
altPosition: boolean,
20+
menuRef: ComponentRef,
21+
inputRef: ComponentRef,
22+
): IUsePosition => {
1523
const menuPosition = ref({ top: '0', left: '0', transform: 'none' });
1624
const openOnTop = ref(false);
1725
const diagonal = 10; // arrow square diagonal + 1
@@ -46,7 +54,7 @@ export const usePosition = (openPosition: OpenPosition, uid: string, altPosition
4654
* Recalculate param is added when the menu component is mounted so that we can check the correct space
4755
*/
4856
const setMenuPosition = (recalculate = true): void => {
49-
const el = document.getElementById(`dp__input_${uid}`);
57+
const el = unrefElement(inputRef);
5058
if (el) {
5159
const { left, width, height } = el.getBoundingClientRect();
5260
const { top: offset } = altPosition ? getOffsetAlt(el) : getOffset(el);
@@ -76,14 +84,14 @@ export const usePosition = (openPosition: OpenPosition, uid: string, altPosition
7684
* of the input field to place the menu
7785
*/
7886
const recalculatePosition = (): void => {
79-
const el = document.getElementById(`dp__input_${uid}`);
87+
const el = unrefElement(inputRef);
8088
if (el) {
8189
const { height: inputHeight, top } = el.getBoundingClientRect();
8290
const { top: offset } = altPosition ? getOffsetAlt(el) : getOffset(el);
8391
const fullHeight = window.innerHeight;
8492
const freeSpaceBottom = fullHeight - top - inputHeight;
8593

86-
const menuEl = document.getElementById(`dp__menu_${uid}`);
94+
const menuEl = unrefElement(menuRef);
8795

8896
if (menuEl) {
8997
const { height } = menuEl.getBoundingClientRect();

src/Vue3DatePicker/directives/clickOutside.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// Following code is a port of @vueuse/core clickOutside hook
2-
import { unref, ComponentPublicInstance, watch, getCurrentScope, onScopeDispose } from 'vue';
2+
import { unref, watch, getCurrentScope, onScopeDispose } from 'vue';
33
import { Fn, MaybeElementRef, MaybeRef, OnClickOutsideEvents, OnClickOutsideOptions } from '../interfaces';
4+
import { unrefElement } from '../utils/util';
45

56
const defaultWindow = typeof window !== 'undefined' ? window : undefined;
67

78
const noop = () => {
89
return;
910
};
1011

11-
const unrefElement = (elRef: MaybeElementRef): HTMLElement | SVGElement | undefined => {
12-
const plain = unref(elRef);
13-
return (plain as ComponentPublicInstance)?.$el ?? plain;
14-
};
15-
1612
const tryOnScopeDispose = (fn: Fn): boolean => {
1713
if (getCurrentScope()) {
1814
onScopeDispose(fn);

src/Vue3DatePicker/utils/util.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
IDateFilter,
55
IDefaultSelect,
66
ITextInputOptions,
7+
MaybeElementRef,
78
WeekStartNum,
89
} from '../interfaces';
910
import { getAddedDays, isDateEqual, resetDateTime } from './date-utils';
11+
import { ComponentPublicInstance, unref } from 'vue';
1012

1113
/**
1214
* Depending on a week start get starting date of the current calendar
@@ -164,3 +166,8 @@ export const getKey = (index: number): string => {
164166
}
165167
return makeKey(len);
166168
};
169+
170+
export const unrefElement = (elRef: MaybeElementRef): HTMLElement | undefined => {
171+
const plain = unref(elRef);
172+
return (plain as ComponentPublicInstance)?.$el ?? plain;
173+
};

0 commit comments

Comments
 (0)