Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAY-1120] Lint Date Picker kit #2990

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type DatePickerConfig = {
} & Pick<BaseOptions, "allowInput" | "defaultDate" | "enableTime" | "maxDate" | "minDate" | "mode" | "plugins" | "position" | "positionElement" >

const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HTMLElement) => {
const noop = () => {
// intentionally left empty as a no-op placeholder
}

const {
allowInput,
closeOnSelect = true,
Expand All @@ -45,8 +49,8 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT
maxDate,
minDate,
mode,
onChange = () => {},
onClose = () => {},
onChange = noop,
onClose = noop,
pickerId,
plugins,
position = "auto",
Expand All @@ -72,20 +76,11 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT
return defaultDate
}
}
const disabledParser = () => {
const disabledArray=[]

disableDate && disableDate.length > 0 && disabledArray.push(...disableDate)
disableRange && disableRange.length > 0 && disabledArray.push(...disableRange)
disableWeekdays && disableWeekdays.length > 0 && disabledArray.push(...disabledWeekDays())

return disabledArray
}

const disabledWeekDays = () => {
return (
[
(date:any) => {
(date: any) => {
const weekdayObj: {
[day: string]: number
} = {
Expand All @@ -110,6 +105,16 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT
]
)
}

const disabledParser = () => {
const disabledArray=[]

disableDate && disableDate.length > 0 && disabledArray.push(...disableDate)
disableRange && disableRange.length > 0 && disabledArray.push(...disableRange)
disableWeekdays && disableWeekdays.length > 0 && disabledArray.push(...disabledWeekDays())

return disabledArray
}
const calendarResizer = () => {
const cal = document.querySelector(`#cal-${pickerId}.open`) as HTMLElement
const parentInput = cal.parentElement
Expand All @@ -125,28 +130,46 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT

const setPlugins = (thisRangesEndToday: boolean, customQuickPickDates: any) => {
const pluginList = []

// month and week selection
if (selectionType === "month" || plugins.length > 0) {
pluginList.push(monthSelectPlugin({ shorthand: true, dateFormat: 'F Y', altFormat: 'F Y' }))
} else if ( selectionType === "week") {
pluginList.push(weekSelect())

} else if (selectionType === "quickpick") {
//------- QUICKPICK VARIANT PLUGIN -------------//
pluginList.push(quickPickPlugin(thisRangesEndToday, customQuickPickDates))
}

// time selection
if (enableTime) pluginList.push(timeSelectPlugin({ caption: timeCaption, showTimezone: showTimezone}))

return pluginList
}

const getDateFormat = () => {
return enableTime ? `${format} ${timeFormat}` : format
}

// Attach / detach to / from scroll events
const initialPicker = document.querySelector<HTMLElement & { [x: string]: any }>(`#${pickerId}`)._flatpickr
const scrollEvent = () => {
initialPicker._positionCalendar()
}
function attachToScroll(scrollParent: string | HTMLElement) {
document.querySelectorAll(scrollParent as string)[0]?.addEventListener("scroll", scrollEvent, { passive: true })
}
function detachFromScroll(scrollParent: string | HTMLElement = document.body) {
document.querySelectorAll(scrollParent as string)[0]?.removeEventListener("scroll", scrollEvent)
}

// two way binding
const initialDropdown = document.querySelector<HTMLElement & { [x: string]: any }>(`#year-${pickerId}`)
const yearChangeHook = () => {
initialDropdown.value = initialPicker.currentYear
}

// ===========================================================
// | Flatpickr initializer w/ config |
// ===========================================================
Expand Down Expand Up @@ -193,17 +216,6 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT
const picker = document.querySelector<HTMLElement & { [x: string]: any }>(`#${pickerId}`)._flatpickr
picker.innerContainer.parentElement.id = `cal-${pickerId}`

// Attach / detach to / from scroll events
const scrollEvent = () => {
picker._positionCalendar()
}
function attachToScroll(scrollParent: string | HTMLElement) {
document.querySelectorAll(scrollParent as string)[0]?.addEventListener("scroll", scrollEvent, { passive: true })
}
function detachFromScroll(scrollParent: string | HTMLElement = document.body) {
document.querySelectorAll(scrollParent as string)[0]?.removeEventListener("scroll", scrollEvent)
}

// replace year selector with dropdown
picker.yearElements[0].parentElement.innerHTML = `<select class="numInput cur-year" type="number" tabIndex="-1" aria-label="Year" id="year-${pickerId}"></select>`

Expand Down Expand Up @@ -242,11 +254,6 @@ const datePickerHelper = (config: DatePickerConfig, scrollContainer: string | HT
})
}

// two way binding
const yearChangeHook = () => {
dropdown.value = picker.currentYear
}

// Adding dropdown icons to year and month selects
dropdown.insertAdjacentHTML('afterend', '<i class="far fa-angle-down year-dropdown-icon" id="test-id"></i>')
if (picker.monthElements[0].parentElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const quickPickPlugin = (thisRangesEndToday: boolean, customQuickPickDates: cust

const calculateDateRange = (timePeriod: string, amount: number): Date[] => {
const endDate = new Date();
let startDate = new Date();
const startDate = new Date();

switch (timePeriod) {
case 'days':
startDate.setDate(endDate.getDate() - amount);
Expand All @@ -76,7 +76,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean, customQuickPickDates: cust
}
return [startDate, endDate];
};


type rangesType = {
[key: string]: Date[]
Expand All @@ -95,7 +95,7 @@ const quickPickPlugin = (thisRangesEndToday: boolean, customQuickPickDates: cust
'Last year': [lastYearStartDate, lastYearEndDate]
};


if (customQuickPickDates && Object.keys(customQuickPickDates).length !== 0) {
if (customQuickPickDates.dates.length && customQuickPickDates.override === false) {
customQuickPickDates.dates.forEach((item) => {
Expand Down