Skip to content

Commit

Permalink
fix(datetime): default date accounts for values props
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi committed Oct 11, 2022
1 parent cd6cac2 commit 36fec81
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
14 changes: 7 additions & 7 deletions core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { is24Hour, isLocaleDayPeriodRTL, isMonthFirstLocale, getNumDaysInMonth }
import {
calculateHourFromAMPM,
convertDataToISO,
getClosestValidDate,
getEndOfWeek,
getNextDay,
getNextMonth,
Expand Down Expand Up @@ -1235,15 +1236,14 @@ export class Datetime implements ComponentInterface {
this.processMinParts();
this.processMaxParts();
this.processValue(this.value);
this.parsedHourValues = convertToArrayOfNumbers(this.hourValues);
this.parsedMinuteValues = convertToArrayOfNumbers(this.minuteValues);
this.parsedMonthValues = convertToArrayOfNumbers(this.monthValues);
this.parsedYearValues = convertToArrayOfNumbers(this.yearValues);
this.parsedDayValues = convertToArrayOfNumbers(this.dayValues);
const hourValues = (this.parsedHourValues = convertToArrayOfNumbers(this.hourValues));
const minuteValues = (this.parsedMinuteValues = convertToArrayOfNumbers(this.minuteValues));
const monthValues = (this.parsedMonthValues = convertToArrayOfNumbers(this.monthValues));
const yearValues = (this.parsedYearValues = convertToArrayOfNumbers(this.yearValues));
const dayValues = (this.parsedDayValues = convertToArrayOfNumbers(this.dayValues));

const todayParts = (this.todayParts = parseDate(getToday()));
// TODO: Account for *Values props
this.defaultParts = { ...todayParts };
this.defaultParts = getClosestValidDate(todayParts, monthValues, dayValues, yearValues, hourValues, minuteValues);

this.emitStyle();
}
Expand Down
65 changes: 62 additions & 3 deletions core/src/components/datetime/utils/manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,69 @@ export const validateParts = (parts: DatetimeParts): DatetimeParts => {
* Returns the closest date to refParts
* that also meets the constraints of
* the *Values params.
* @param refParts The reference date
* @param hourValues The allowed hour values
* @param minuteValues The allowed minute values
*/
export const getClosestValidDate = (
refParts: DatetimeParts
refParts: DatetimeParts,
monthValues?: number[],
dayValues?: number[],
yearValues?: number[],
hourValues?: number[],
minuteValues?: number[]
) => {
const { hour, minute, day, month, year } = refParts;
const copyParts = { ...refParts };

return refParts;
}
if (monthValues !== undefined) {
copyParts.month = findClosestValue(month, monthValues);
}

// Day is nullable but cannot be undefined
if (day !== null && dayValues !== undefined) {
copyParts.day = findClosestValue(day, dayValues);
}

if (yearValues !== undefined) {
copyParts.year = findClosestValue(year, yearValues);
}

if (hour !== undefined && hourValues !== undefined) {
copyParts.hour = findClosestValue(hour, hourValues);
}

if (minute !== undefined && minuteValues !== undefined) {
copyParts.minute = findClosestValue(minute, minuteValues);
}

return copyParts;
};

/**
* Finds the value in "values" that is
* numerically closest to "reference".
* This function assumes that "values" is
* already sorted in ascending order.
*/
const findClosestValue = (reference: number, values: number[]) => {
let closestValue = values[0];
let rank = Math.abs(closestValue - reference);

for (const value of values) {
/**
* This code prioritizes the first
* closest result. Given two values
* with the same distance from reference,
* this code will prioritize the smaller of
* the two values.
*/
const valueRank = Math.abs(value - reference);
if (rank === undefined || valueRank < rank) {
closestValue = value;
rank = valueRank;
}
}

return closestValue;
};

0 comments on commit 36fec81

Please sign in to comment.