Skip to content

Commit

Permalink
fix: min and max date with initialValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-master committed Jun 12, 2021
1 parent fb54655 commit fa92ef9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 66 deletions.
68 changes: 33 additions & 35 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,38 +347,36 @@ export function endYear(year: number) {
* @type {Record<string, (inp?: any) => Array<PickerItemModel>>}
* @private
*/
export const pickerData: Record<
string,
(inp?: any) => Array<PickerItemModel>
> = {
getYears: ({ min, max } = {}) =>
generateYearsRange(min, max).map((year) => ({
value: year,
type: 'year',
})),
getMonths: (monthsMap = jalaliMonths) =>
Object.keys(monthsMap).map((value) => ({
type: 'month',
value: Number(value),
})),
getDays: (days = 29) =>
generateAnArrayOfNumbers(days).map((day) => ({
value: day,
type: 'day',
})),
getHours: () =>
generateAnArrayOfNumbers(24).map((hour) => ({
value: hour,
type: 'hour',
})),
getSeconds: () =>
generateAnArrayOfNumbers(59).map((hour) => ({
value: hour,
type: 'second',
})),
getMinutes: () =>
generateAnArrayOfNumbers(59).map((hour) => ({
value: hour,
type: 'minute',
})),
};
export const pickerData: Record<string, (inp?: any) => Array<PickerItemModel>> =
{
getYears: ({ min, max } = {}) =>
generateYearsRange(min, max).map((year) => ({
value: year,
type: 'year',
})),
getMonths: (monthsMap = jalaliMonths) =>
Object.keys(monthsMap).map((value) => ({
type: 'month',
value: Number(value),
})),
getDays: (days = 29) =>
generateAnArrayOfNumbers(days).map((day) => ({
value: day,
type: 'day',
})),
getHours: () =>
generateAnArrayOfNumbers(24).map((hour) => ({
value: hour,
type: 'hour',
})),
getSeconds: () =>
generateAnArrayOfNumbers(59).map((hour) => ({
value: hour,
type: 'second',
})),
getMinutes: () =>
generateAnArrayOfNumbers(59).map((hour) => ({
value: hour,
type: 'minute',
})),
};
72 changes: 41 additions & 31 deletions src/hooks/usePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ export function usePicker(props: WheelPickerProps) {
year = currentYear + startYear;
}

return toPositive(currentYear - year);
const result = toPositive(currentYear - year);

return result === 0 ? year : result;
}, [
isMinDateValid,
minDateObject,
isInitialValueValid,
initialValueDateObject,
props.startYear,
]);

/**
* * Get Max Year of the [Year Column] which should be rendered
*
Expand All @@ -156,17 +159,19 @@ export function usePicker(props: WheelPickerProps) {
const endYear = Number(props.endYear);

let year: number;
if (isMinDateValid) {
if (isMaxDateValid) {
year = maxDateObject.year;
} else if (isInitialValueValid) {
year = initialValueDateObject.year + endYear;
} else {
year = currentYear + endYear;
}

return toPositive(currentYear - year);
const result = toPositive(currentYear - year);

return result === 0 ? currentYear : result;
}, [
isMinDateValid,
isMaxDateValid,
maxDateObject,
initialValueDateObject,
props.endYear,
Expand Down Expand Up @@ -249,14 +254,14 @@ export function usePicker(props: WheelPickerProps) {
return maxDateObject;
}

// I tried my best but `initialValue`, `maxDate` and `minDate` are not valid dates.
// I tried my best but `value`, `initialValue`, `maxDate` and `minDate` are not valid dates.
throw new Error(
`[PersianMobileDatePicker] I tried my best but can't consider a valid default value for using in the Picker's Columns.`,
);
}, [
props.maxDate,
props.minDate,
isMinDateValid,
maxDateObject,
minDateObject,
isMaxDateValid,
props.value?.date,
props.value?.object,
Expand Down Expand Up @@ -306,6 +311,30 @@ export function usePicker(props: WheelPickerProps) {
]);

// Handlers
/**
* This function will call `shouldRender` in the Config object, which gave us one of the DatePicker component's props and checks to render the Item(This property is optional).
*
* @param {PickerDateModel} currentSelectedDate
* @param {DateConfigTypes} key
* @param {PickerSelectedDateValue} value
* @return {boolean}
* @private
*/
function configShouldRender(
currentSelectedDate: PickerDateModel,
key: DateConfigTypes,
value?: PickerSelectedDateValue,
): boolean {
return (
configs?.[key]?.shouldRender?.(
addExtraDateInfo(currentSelectedDate, {
type: key,
value: value ?? currentSelectedDate[key]!,
}),
) ?? true
);
}

/**
* Check if entered Year is in Range of Min and Max
*
Expand Down Expand Up @@ -333,30 +362,6 @@ export function usePicker(props: WheelPickerProps) {
return true;
}

/**
* This function will call `shouldRender` in the Config object, which gave us one of the DatePicker component's props and checks to render the Item(This property is optional).
*
* @param {PickerDateModel} currentSelectedDate
* @param {DateConfigTypes} key
* @param {PickerSelectedDateValue} value
* @return {boolean}
* @private
*/
function configShouldRender(
currentSelectedDate: PickerDateModel,
key: DateConfigTypes,
value?: PickerSelectedDateValue,
): boolean {
return (
configs?.[key]?.shouldRender?.(
addExtraDateInfo(currentSelectedDate, {
type: key,
value: value ?? currentSelectedDate[key]!,
}),
) ?? true
);
}

/**
* * Check if the Month or Day Column's item should be rendered or not
*
Expand All @@ -374,6 +379,11 @@ export function usePicker(props: WheelPickerProps) {
// Create new Date object and clone the SelectedDate and assign the entered day to the Object
const $date = { ...selectedDate, [key]: value };

// If [minDate] is valid and we are validating the month column,
// we should pass the [minDate] day to the [selectedDate] as day value
if (key === 'month' && isMinDateValid) {
$date.day = minDateObject.day;
}
// Call the Config's shouldRender method to find that we should render this item or not
// User can prevent rendering the weekend's holidays in Date Picker
if (!configShouldRender($date, key, value)) return false;
Expand Down

0 comments on commit fa92ef9

Please sign in to comment.