Skip to content

Commit

Permalink
refactor: Group common props into config, add option to disable `st…
Browse files Browse the repository at this point in the history
…opPropagation` event (resolves #556)
  • Loading branch information
Jasenkoo committed Sep 8, 2023
1 parent be047a7 commit e092dad
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/VueDatePicker/VueDatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
const { setMenuFocused, setShiftKey } = useState();
const { clearArrowNav } = useArrowNavigation();
const { mapDatesArrToMap, validateDate, isValidTime } = useValidation(props);
const { defaultedTransitions, defaultedTextInput, defaultedInline } = useDefaults(props);
const { defaultedTransitions, defaultedTextInput, defaultedInline, defaultedConfig } = useDefaults(props);
const { menuTransition, showTransition } = useTransitions(defaultedTransitions);
onMounted(() => {
Expand Down Expand Up @@ -237,7 +237,7 @@
*/
const onScroll = (): void => {
if (isOpen.value) {
if (props.closeOnScroll) {
if (props.closeOnScroll || defaultedConfig.value.closeOnScroll) {
closeMenu();
} else {
setMenuPosition();
Expand Down
11 changes: 7 additions & 4 deletions src/VueDatePicker/components/Common/SelectionOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
<script lang="ts" setup>
import { computed, nextTick, onBeforeUpdate, onMounted, onUnmounted, ref } from 'vue';
import { convertType, unrefElement } from '@/utils/util';
import { checkStopPropagation, convertType, unrefElement } from '@/utils/util';
import { useArrowNavigation, useCommon, useDefaults } from '@/composables';
import type { DynamicClass, Flow, OverlayGridItem, TextInputProp } from '@/interfaces';
import type { Config, DynamicClass, Flow, OverlayGridItem, TextInputProp } from '@/interfaces';
import type { PickerBasePropsType } from '@/props';
const { setSelectionGrid, buildMultiLevelMatrix, setMonthPicker } = useArrowNavigation();
Expand All @@ -89,11 +89,14 @@
useRelative?: boolean;
height?: number | string;
textInput?: TextInputProp;
config?: Partial<Config>;
}
const props = defineProps<Props>();
const { defaultedAriaLabels, defaultedTextInput } = useDefaults(props as unknown as PickerBasePropsType);
const { defaultedAriaLabels, defaultedTextInput, defaultedConfig } = useDefaults(
props as unknown as PickerBasePropsType,
);
const { hideNavigationButtons } = useCommon();
const scrollable = ref(false);
Expand Down Expand Up @@ -250,7 +253,7 @@
const handleArrowKey = (ev: KeyboardEvent) => {
if (props.arrowNavigation) return;
ev.stopImmediatePropagation();
checkStopPropagation(ev, defaultedConfig.value, true);
};
const setHoverValue = (val: number) => {
Expand Down
21 changes: 17 additions & 4 deletions src/VueDatePicker/components/DatePicker/DpCalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
:aria-label="defaultedAriaLabels?.day?.(dayVal)"
tabindex="0"
:data-test="dayVal.value"
@click.stop.prevent="$emit('select-date', dayVal)"
@click.prevent="onDateSelect($event, dayVal)"
@keydown.enter="$emit('select-date', dayVal)"
@keydown.space="$emit('handle-space', dayVal)"
@mouseenter="onMouseOver(dayVal, weekInd, dayInd)"
Expand Down Expand Up @@ -92,7 +92,11 @@
ref="activeTooltip"
:style="markerTooltipStyle"
>
<div class="dp__tooltip_content" @click.stop v-if="dayVal.marker?.tooltip">
<div
class="dp__tooltip_content"
@click="onTpClick"
v-if="dayVal.marker?.tooltip"
>
<div
v-for="(tooltip, i) in dayVal.marker.tooltip"
:key="i"
Expand Down Expand Up @@ -129,7 +133,7 @@
import { computed, nextTick, onMounted, ref } from 'vue';
import { getISOWeek, getWeek } from 'date-fns';
import { getDayNames, getDefaultMarker, unrefElement } from '@/utils/util';
import { checkStopPropagation, getDayNames, getDefaultMarker, unrefElement } from '@/utils/util';
import { useArrowNavigation, useDefaults } from '@/composables';
import { PickerBaseProps } from '@/props';
import { getDate, isDateAfter, isDateEqual, resetDateTime, setDateMonthOrYear } from '@/utils/date-utils';
Expand Down Expand Up @@ -163,7 +167,7 @@
});
const { buildMultiLevelMatrix } = useArrowNavigation();
const { defaultedTransitions, defaultedAriaLabels, defaultedMultiCalendars } = useDefaults(props);
const { defaultedTransitions, defaultedConfig, defaultedAriaLabels, defaultedMultiCalendars } = useDefaults(props);
const showMakerTooltip = ref<Date | null>(null);
const markerTooltipStyle = ref<{ bottom: string; left?: string; right?: string; transform: string }>({
Expand Down Expand Up @@ -346,5 +350,14 @@
return '';
};
const onDateSelect = (ev: Event, dayVal: ICalendarDay) => {
checkStopPropagation(ev, defaultedConfig.value);
emit('select-date', dayVal);
};
const onTpClick = (ev: Event) => {
checkStopPropagation(ev, defaultedConfig.value);
};
defineExpose({ triggerTransition });
</script>
1 change: 1 addition & 0 deletions src/VueDatePicker/components/DatePicker/DpHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
:hide-navigation="hideNavigation"
:is-last="autoApply && !keepActionRow"
:skip-button-ref="false"
:config="config"
:type="type.type"
:header-refs="[]"
:esc-close="escClose"
Expand Down
19 changes: 13 additions & 6 deletions src/VueDatePicker/components/DatepickerInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
v-if="clearable && !$slots['clear-icon'] && inputValue && !disabled && !readonly"
class="dp__clear_icon dp__input_icons"
data-test="clear-icon"
@click.stop.prevent="onClear"
@click.prevent="onClear($event)"
/>
</div>
</div>
Expand All @@ -76,6 +76,7 @@
import type { PropType } from 'vue';
import type { DynamicClass } from '@/interfaces';
import { checkStopPropagation } from '@/utils/util';
defineOptions({
compatConfig: {
Expand Down Expand Up @@ -104,8 +105,14 @@
...AllProps,
});
const { defaultedTextInput, defaultedAriaLabels, defaultedInline, getDefaultPattern, getDefaultStartTime } =
useDefaults(props);
const {
defaultedTextInput,
defaultedAriaLabels,
defaultedInline,
defaultedConfig,
getDefaultPattern,
getDefaultStartTime,
} = useDefaults(props);
const parsedDate = ref();
const inputRef = ref<HTMLElement | null>(null);
Expand Down Expand Up @@ -226,8 +233,7 @@
const handleOpen = (ev: KeyboardEvent | MouseEvent) => {
ev.preventDefault();
ev.stopImmediatePropagation();
ev.stopPropagation();
checkStopPropagation(ev, defaultedConfig.value, true);
if (
defaultedTextInput.value.enabled &&
defaultedTextInput.value.openMenu &&
Expand All @@ -253,7 +259,8 @@
}
};
const onClear = () => {
const onClear = (ev?: Event) => {
checkStopPropagation(ev, defaultedConfig.value, true);
emit('clear');
};
Expand Down
16 changes: 7 additions & 9 deletions src/VueDatePicker/components/DatepickerMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
import ActionRow from '@/components/ActionRow.vue';
import { mapSlots, useArrowNavigation, useState, useFlow, useDefaults } from '@/composables';
import { unrefElement } from '@/utils/util';
import { checkStopPropagation, unrefElement } from '@/utils/util';
import { AllProps } from '@/props';
import MonthPicker from '@/components/MonthPicker/MonthPicker.vue';
Expand Down Expand Up @@ -172,7 +172,7 @@
const { setMenuFocused, setShiftKey, control } = useState();
const slots = useSlots();
const { defaultedTextInput, defaultedInline } = useDefaults(props);
const { defaultedTextInput, defaultedInline, defaultedConfig } = useDefaults(props);
const calendarWrapperRef = ref(null);
const calendarWidth = ref(0);
Expand All @@ -194,11 +194,10 @@
}
if (menu) {
const stopDefault = (event: Event) => {
if (props.allowPreventDefault) {
if (props.allowPreventDefault || defaultedConfig.value.allowPreventDefault) {
event.preventDefault();
}
event.stopImmediatePropagation();
event.stopPropagation();
checkStopPropagation(event, defaultedConfig.value, true);
};
menu.addEventListener('pointerdown', stopDefault);
menu.addEventListener('mousedown', stopDefault);
Expand Down Expand Up @@ -266,9 +265,8 @@
}),
);
const handleDpMenuClick = (e: Event) => {
e.stopPropagation();
e.stopImmediatePropagation();
const handleDpMenuClick = (ev: Event) => {
checkStopPropagation(ev, defaultedConfig.value, true);
};
const handleEsc = (): void => {
Expand Down Expand Up @@ -297,7 +295,7 @@
if (!props.disableMonthYearSelect && ev.code === 'Tab') {
if ((ev.target as HTMLElement).classList.contains('dp__menu') && control.value.shiftKeyInMenu) {
ev.preventDefault();
ev.stopImmediatePropagation();
checkStopPropagation(ev, defaultedConfig.value, true);
emit('close-picker');
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/VueDatePicker/components/MonthPicker/MonthPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
:arrow-navigation="arrowNavigation"
:is-last="autoApply && !keepActionRow"
:esc-close="escClose"
:height="modeHeight"
:height="modeHeight !== 255 ? modeHeight : defaultedConfig.modeHeight"
:config="config"
@selected="selectMonth($event, instance)"
@hover-value="setHoverDate($event, instance)"
use-relative
Expand Down Expand Up @@ -65,6 +66,7 @@
:items="groupedYears(instance)"
:text-input="textInput"
:esc-close="escClose"
:config="config"
@toggle="toggleYearPicker(instance)"
@selected="handleYearSelect($event, instance)"
:is-last="autoApply && !keepActionRow"
Expand Down Expand Up @@ -125,6 +127,7 @@
defaultedMultiCalendars,
defaultedAriaLabels,
defaultedTransitions,
defaultedConfig,
setHoverDate,
selectMonth,
selectYear,
Expand Down
3 changes: 2 additions & 1 deletion src/VueDatePicker/components/MonthPicker/month-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { IDefaultSelect, OverlayGridItem, VueEmit } from '@/interfaces';
import type { PickerBasePropsType } from '@/props';

export const useMonthPicker = (props: PickerBasePropsType, emit: VueEmit) => {
const { defaultedMultiCalendars, defaultedAriaLabels, defaultedTransitions } = useDefaults(props);
const { defaultedMultiCalendars, defaultedAriaLabels, defaultedTransitions, defaultedConfig } = useDefaults(props);

const { modelValue, year, month: instanceMonth, calendars } = useModel(props, emit);
const months = computed(() => getMonths(props.formatLocale, props.locale, props.monthNameFormat));
Expand Down Expand Up @@ -190,6 +190,7 @@ export const useMonthPicker = (props: PickerBasePropsType, emit: VueEmit) => {
defaultedMultiCalendars,
defaultedAriaLabels,
defaultedTransitions,
defaultedConfig,
setHoverDate,
selectMonth,
selectYear,
Expand Down
1 change: 1 addition & 0 deletions src/VueDatePicker/components/TimePicker/TimeInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
:esc-close="escClose"
:type="timeInput.type"
:text-input="textInput"
:config="config"
:arrow-navigation="arrowNavigation"
@selected="handleTimeFromOverlay(timeInput.type, $event)"
@toggle="toggleOverlay(timeInput.type)"
Expand Down
8 changes: 6 additions & 2 deletions src/VueDatePicker/components/TimePicker/TimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
'dp--overlay-absolute': !props.timePicker && !timePickerInline,
'dp--overlay-relative': props.timePicker,
}"
:style="timePicker ? { height: `${modeHeight}px` } : undefined"
:style="
timePicker
? { height: `${modeHeight !== 255 ? modeHeight : defaultedConfig.modeHeight}px` }
: undefined
"
ref="overlayRef"
:tabindex="timePickerInline ? undefined : 0"
>
Expand Down Expand Up @@ -138,7 +142,7 @@
const { buildMatrix, setTimePicker } = useArrowNavigation();
const slots = useSlots();
const { defaultedTransitions, defaultedAriaLabels, defaultedTextInput } = useDefaults(props);
const { defaultedTransitions, defaultedAriaLabels, defaultedTextInput, defaultedConfig } = useDefaults(props);
const { transitionName, showTransition } = useTransitions(defaultedTransitions);
const { hideNavigationButtons } = useCommon();
Expand Down
5 changes: 4 additions & 1 deletion src/VueDatePicker/components/YearPicker/YearPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<selection-overlay
:items="groupedYears"
:is-last="autoApply && !keepActionRow"
:height="modeHeight"
:height="modeHeight !== 255 ? modeHeight : defaultedConfig.modeHeight"
:config="config"
type="year"
use-relative
@selected="selectYear"
Expand All @@ -32,6 +33,7 @@
import { PickerBaseProps } from '@/props';
import { useYearPicker } from '@/components/YearPicker/year-picker';
import { useDefaults } from '@/composables';
const emit = defineEmits(['update:internal-model-value', 'reset-flow', 'range-start', 'range-end', 'auto-apply']);
const props = defineProps({
Expand All @@ -45,4 +47,5 @@
});
const { groupedYears, selectYear, setHoverValue } = useYearPicker(props, emit);
const { defaultedConfig } = useDefaults(props);
</script>
4 changes: 4 additions & 0 deletions src/VueDatePicker/composables/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultPreviewFormat,
defaultTransitions,
getDefaultActionRowData,
getDefaultConfig,
getDefaultFilters,
getDefaultInlineOptions,
getDefaultTextInputOptions,
Expand Down Expand Up @@ -62,6 +63,8 @@ export const useDefaults = (props: AllPropsType | PickerBasePropsType) => {

const defaultedInline = computed(() => getDefaultInlineOptions(props.inline));

const defaultedConfig = computed(() => getDefaultConfig(props.config));

return {
defaultedTransitions,
defaultedMultiCalendars,
Expand All @@ -72,6 +75,7 @@ export const useDefaults = (props: AllPropsType | PickerBasePropsType) => {
defaultedPreviewFormat,
defaultedTextInput,
defaultedInline,
defaultedConfig,
getDefaultPattern,
getDefaultStartTime,
};
Expand Down
7 changes: 7 additions & 0 deletions src/VueDatePicker/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,10 @@ export interface InlineOptions {
export type InlineProp = boolean | { input?: boolean };

export type DisabledTimesArrProp = (ind: number, hours?: number) => TimeValuesInv;

export interface Config {
allowStopPropagation: boolean;
closeOnScroll: boolean;
modeHeight: number;
allowPreventDefault: boolean;
}
2 changes: 2 additions & 0 deletions src/VueDatePicker/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
PresetDate,
InlineProp,
DisabledTimeArrProp,
Config,
} from '@/interfaces';

export const AllProps = {
Expand Down Expand Up @@ -168,6 +169,7 @@ export const AllProps = {
showLastInRange: { type: Boolean as PropType<boolean>, default: true },
timePickerInline: { type: Boolean as PropType<boolean>, default: false },
calendar: { type: Function as PropType<(month: ICalendarDate[]) => ICalendarDate[]>, default: null },
config: { type: Object as PropType<Partial<Config>>, default: undefined },
};

export const PickerBaseProps = {
Expand Down
11 changes: 11 additions & 0 deletions src/VueDatePicker/utils/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
TextInputProp,
InlineProp,
InlineOptions,
Config,
} from '@/interfaces';

export const mergeDefaultTransitions = (conf: Partial<Transition>): Transition => ({
Expand Down Expand Up @@ -132,3 +133,13 @@ export const getDefaultInlineOptions = (inline: InlineProp): InlineOptions => {
...defaultOptions,
};
};

export const getDefaultConfig = (config?: Partial<Config>): Config => {
const defaultConfig = {
allowStopPropagation: true,
closeOnScroll: false,
modeHeight: 255,
allowPreventDefault: false,
};
return { ...defaultConfig, ...(config ?? {}) };
};
Loading

0 comments on commit e092dad

Please # to comment.