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

Commit

Permalink
feat: Add recalculatePosition event and custom position function (#81)
Browse files Browse the repository at this point in the history
- altPosition altered to accept both boolean or function
  • Loading branch information
Jasenkoo committed Jan 20, 2022
1 parent d5ddab0 commit 79cfb89
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type EmitEvents =
| 'open'
| 'focus'
| 'blur'
| 'internalModelChange';
| 'internalModelChange'
| 'recalculatePosition';

interface Vue3DatePicker {
uid?: string;
Expand Down Expand Up @@ -156,7 +157,7 @@ interface Vue3DatePicker {
noHoursOverlay?: boolean;
noMinutesOverlay?: boolean;
noSecondsOverlay?: boolean;
altPosition?: boolean;
altPosition?: boolean | ((el: HTMLElement | undefined) => { top: string; left: string; transform: string });
disabledWeekDays?: number[] | string[];
allowedDates?: string[] | Date[];
showNowButton?: boolean;
Expand Down
5 changes: 4 additions & 1 deletion src/Vue3DatePicker/Vue3DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
IMarker,
ITransition,
IDisableDates,
AltPosition,
} from './interfaces';
import { getDefaultPattern, isValidTime } from './utils/date-utils';
import { getDefaultTextInputOptions, getDefaultFilters, mergeDefaultTransitions } from './utils/util';
Expand All @@ -169,6 +170,7 @@
'focus',
'blur',
'internalModelChange',
'recalculatePosition',
]);
const props = defineProps({
uid: { type: String as PropType<string>, default: null },
Expand Down Expand Up @@ -245,7 +247,7 @@
noHoursOverlay: { type: Boolean as PropType<boolean>, default: false },
noMinutesOverlay: { type: Boolean as PropType<boolean>, default: false },
noSecondsOverlay: { type: Boolean as PropType<boolean>, default: false },
altPosition: { type: Boolean as PropType<boolean>, default: false },
altPosition: { type: [Boolean, Function] as PropType<AltPosition>, default: false },
allowedDates: { type: Array as PropType<string[] | Date[]>, default: () => [] },
showNowButton: { type: Boolean as PropType<boolean>, default: false },
nowButtonLabel: { type: String as PropType<string>, default: 'Now' },
Expand Down Expand Up @@ -305,6 +307,7 @@
props.altPosition,
dpMenuRef,
inputRef,
emit,
);
const { internalModelValue, inputValue, parseExternalModelValue, emitModelValue, checkBeforeEmit } =
Expand Down
10 changes: 7 additions & 3 deletions src/Vue3DatePicker/components/composition/position.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, Ref } from 'vue';
import { OpenPosition } from '../../interfaces';
import { AltPosition, OpenPosition, VueEmit } from '../../interfaces';
import { unrefElement } from '../../utils/util';

interface IUsePosition {
Expand All @@ -16,9 +16,10 @@ type ComponentRef = Ref<HTMLElement | null>;
*/
export const usePosition = (
openPosition: OpenPosition,
altPosition: boolean,
altPosition: AltPosition,
menuRef: ComponentRef,
inputRef: ComponentRef,
emit: VueEmit,
): IUsePosition => {
const menuPosition = ref({ top: '0', left: '0', transform: 'none' });
const openOnTop = ref(false);
Expand Down Expand Up @@ -55,7 +56,9 @@ export const usePosition = (
*/
const setMenuPosition = (recalculate = true): void => {
const el = unrefElement(inputRef);
if (el) {
if (altPosition && typeof altPosition !== 'boolean') {
menuPosition.value = altPosition(el);
} else if (el) {
const { left, width, height } = el.getBoundingClientRect();
const { top: offset } = altPosition ? getOffsetAlt(el) : getOffset(el);
const position = { top: `${height + offset + diagonal}px`, left: '', transform: 'none' };
Expand Down Expand Up @@ -112,6 +115,7 @@ export const usePosition = (
}
}
}
emit('recalculatePosition');
};

return { openOnTop, menuPosition, setMenuPosition, recalculatePosition };
Expand Down
2 changes: 2 additions & 0 deletions src/Vue3DatePicker/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ export interface ITransition {
}
export type IDisableDates = (date: Date) => boolean;
export type ITimeType = 'hours' | 'minutes' | 'seconds';

export type AltPosition = boolean | ((el: HTMLElement | undefined) => { top: string; left: string; transform: string });

0 comments on commit 79cfb89

Please sign in to comment.