Skip to content

Commit

Permalink
refactor: Update arrow positioning logic (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasenkoo committed Apr 6, 2024
1 parent e3914d8 commit aae1c57
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/VueDatePicker/VueDatePicker.vue
Expand Up @@ -37,7 +37,7 @@
:open-on-top="openOnTop"
:no-overlay-focus="noOverlayFocus"
:collapse="collapse"
:input-width="inputWidth"
:get-input-rect="getInputRect"
@close-picker="closeMenu"
@select-date="selectDate"
@auto-apply="autoApplyValue"
Expand Down Expand Up @@ -259,9 +259,9 @@
);
});
const inputWidth = computed(() => {
return inputRef.value?.$el?.getBoundingClientRect()?.width ?? 0;
});
const getInputRect = () => {
return inputRef.value?.$el?.getBoundingClientRect();
};
/**
* Event listener for 'scroll'
Expand Down
11 changes: 7 additions & 4 deletions src/VueDatePicker/components/DatepickerMenu.vue
Expand Up @@ -173,10 +173,10 @@
internalModelValue: { type: [Date, Array] as PropType<InternalModuleValue>, default: null },
noOverlayFocus: { type: Boolean as PropType<boolean>, default: false },
collapse: { type: Boolean as PropType<boolean>, default: false },
inputWidth: { type: Number as PropType<number>, default: 0 },
getInputRect: { type: Function as PropType<() => DOMRect>, default: () => ({}) },
});
const dpMenuRef = ref(null);
const dpMenuRef = ref<HTMLElement | null>(null);
const baseProps = computed(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -247,8 +247,11 @@
});
const arrowPos = computed(() => {
if (props.inputWidth < calendarWidth.value) {
return `${props.inputWidth / 2}px`;
if (defaultedConfig.value.arrowLeft) return defaultedConfig.value.arrowLeft;
const menuRect = dpMenuRef.value?.getBoundingClientRect();
const inputRect = props.getInputRect();
if (inputRect.width < calendarWidth.value && inputRect.left <= (menuRect?.left ?? 0)) {
return `${inputRect.width / 2}px`;
}
return '50%';
});
Expand Down
1 change: 1 addition & 0 deletions src/VueDatePicker/interfaces.ts
Expand Up @@ -281,6 +281,7 @@ export interface Config {
keepActionRow: boolean;
onClickOutside?: (validate: () => boolean) => void;
tabOutClosesMenu: boolean;
arrowLeft?: string;
}

export interface Highlight {
Expand Down
2 changes: 1 addition & 1 deletion src/VueDatePicker/props.ts
Expand Up @@ -174,7 +174,7 @@ export const PickerBaseProps = {
noOverlayFocus: { type: Boolean as PropType<boolean>, default: false },
collapse: { type: Boolean as PropType<boolean>, default: false },
menuWrapRef: { type: Object as PropType<HTMLElement | null>, default: null },
inputWidth: { type: Number as PropType<number>, default: 0 },
getInputRect: { type: Function as PropType<() => DOMRect>, default: () => ({}) },
};

export type AllPropsType = ExtractPropTypes<typeof AllProps>;
Expand Down
1 change: 1 addition & 0 deletions src/VueDatePicker/utils/defaults.ts
Expand Up @@ -167,6 +167,7 @@ export const getDefaultConfig = (config?: Partial<Config>): Config => {
keepActionRow: false,
onClickOutside: undefined,
tabOutClosesMenu: true,
arrowLeft: undefined,
};
return { ...defaultConfig, ...(config ?? {}) };
};
Expand Down

0 comments on commit aae1c57

Please sign in to comment.