react-native-month-day-picker 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +269 -0
- package/lib/commonjs/BirthdayPicker.js +177 -0
- package/lib/commonjs/BirthdayPicker.js.map +1 -0
- package/lib/commonjs/BirthdayPickerModal.js +176 -0
- package/lib/commonjs/BirthdayPickerModal.js.map +1 -0
- package/lib/commonjs/constants.js +80 -0
- package/lib/commonjs/constants.js.map +1 -0
- package/lib/commonjs/hooks/useBirthdayPicker.js +90 -0
- package/lib/commonjs/hooks/useBirthdayPicker.js.map +1 -0
- package/lib/commonjs/index.js +89 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/dateUtils.js +103 -0
- package/lib/commonjs/utils/dateUtils.js.map +1 -0
- package/lib/commonjs/utils/localeUtils.js +90 -0
- package/lib/commonjs/utils/localeUtils.js.map +1 -0
- package/lib/module/BirthdayPicker.js +169 -0
- package/lib/module/BirthdayPicker.js.map +1 -0
- package/lib/module/BirthdayPickerModal.js +169 -0
- package/lib/module/BirthdayPickerModal.js.map +1 -0
- package/lib/module/constants.js +74 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/hooks/useBirthdayPicker.js +84 -0
- package/lib/module/hooks/useBirthdayPicker.js.map +1 -0
- package/lib/module/index.js +16 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/dateUtils.js +92 -0
- package/lib/module/utils/dateUtils.js.map +1 -0
- package/lib/module/utils/localeUtils.js +81 -0
- package/lib/module/utils/localeUtils.js.map +1 -0
- package/lib/typescript/BirthdayPicker.d.ts +25 -0
- package/lib/typescript/BirthdayPicker.d.ts.map +1 -0
- package/lib/typescript/BirthdayPickerModal.d.ts +24 -0
- package/lib/typescript/BirthdayPickerModal.d.ts.map +1 -0
- package/lib/typescript/constants.d.ts +39 -0
- package/lib/typescript/constants.d.ts.map +1 -0
- package/lib/typescript/hooks/useBirthdayPicker.d.ts +17 -0
- package/lib/typescript/hooks/useBirthdayPicker.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +8 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +160 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils/dateUtils.d.ts +43 -0
- package/lib/typescript/utils/dateUtils.d.ts.map +1 -0
- package/lib/typescript/utils/localeUtils.d.ts +28 -0
- package/lib/typescript/utils/localeUtils.d.ts.map +1 -0
- package/package.json +137 -0
- package/src/BirthdayPicker.tsx +210 -0
- package/src/BirthdayPickerModal.tsx +192 -0
- package/src/constants.ts +64 -0
- package/src/hooks/useBirthdayPicker.ts +106 -0
- package/src/index.ts +31 -0
- package/src/types.ts +189 -0
- package/src/utils/dateUtils.ts +101 -0
- package/src/utils/localeUtils.ts +99 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MONTHS_IN_YEAR,
|
|
3
|
+
DEFAULT_LOCALE,
|
|
4
|
+
DEFAULT_MONTH_FORMAT,
|
|
5
|
+
} from '../constants';
|
|
6
|
+
import type { MonthFormat } from '../types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Cache for month names to avoid repeated Intl.DateTimeFormat calls
|
|
10
|
+
*/
|
|
11
|
+
const monthNamesCache = new Map<string, string[]>();
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Generate a cache key for month names
|
|
15
|
+
*/
|
|
16
|
+
function getCacheKey(locale: string, format: MonthFormat): string {
|
|
17
|
+
return `${locale}-${format}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get month names for a given locale and format
|
|
22
|
+
* @param locale - BCP 47 locale string (e.g., 'en-US', 'ja-JP')
|
|
23
|
+
* @param format - Format for month names ('long', 'short', 'numeric')
|
|
24
|
+
* @returns Array of month names/numbers (1-indexed, index 0 is empty string)
|
|
25
|
+
*/
|
|
26
|
+
export function getMonthNames(
|
|
27
|
+
locale: string = DEFAULT_LOCALE,
|
|
28
|
+
format: MonthFormat = DEFAULT_MONTH_FORMAT
|
|
29
|
+
): string[] {
|
|
30
|
+
const cacheKey = getCacheKey(locale, format);
|
|
31
|
+
|
|
32
|
+
// Return cached result if available
|
|
33
|
+
if (monthNamesCache.has(cacheKey)) {
|
|
34
|
+
return monthNamesCache.get(cacheKey)!;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const names: string[] = [''];
|
|
38
|
+
|
|
39
|
+
if (format === 'numeric') {
|
|
40
|
+
// For numeric format, just use month numbers
|
|
41
|
+
for (let i = 1; i <= MONTHS_IN_YEAR; i++) {
|
|
42
|
+
names.push(String(i));
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
// Use Intl.DateTimeFormat for localized month names
|
|
46
|
+
const formatter = new Intl.DateTimeFormat(locale, { month: format });
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < MONTHS_IN_YEAR; i++) {
|
|
49
|
+
// Create a date in month i (0-indexed for Date constructor)
|
|
50
|
+
const date = new Date(2024, i, 1);
|
|
51
|
+
names.push(formatter.format(date));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Cache the result
|
|
56
|
+
monthNamesCache.set(cacheKey, names);
|
|
57
|
+
|
|
58
|
+
return names;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Format a single month number to a localized string
|
|
63
|
+
* @param month - Month number (1-12)
|
|
64
|
+
* @param locale - BCP 47 locale string
|
|
65
|
+
* @param format - Format for month name
|
|
66
|
+
* @returns Formatted month name
|
|
67
|
+
*/
|
|
68
|
+
export function formatMonth(
|
|
69
|
+
month: number,
|
|
70
|
+
locale: string = DEFAULT_LOCALE,
|
|
71
|
+
format: MonthFormat = DEFAULT_MONTH_FORMAT
|
|
72
|
+
): string {
|
|
73
|
+
if (month < 1 || month > MONTHS_IN_YEAR) {
|
|
74
|
+
throw new Error(`Invalid month: ${month}. Must be between 1 and 12.`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const names = getMonthNames(locale, format);
|
|
78
|
+
return names[month];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Format a day number with optional locale-specific formatting
|
|
83
|
+
* @param day - Day number (1-31)
|
|
84
|
+
* @param locale - BCP 47 locale string (currently unused, for future i18n)
|
|
85
|
+
* @returns Formatted day string
|
|
86
|
+
*/
|
|
87
|
+
export function formatDay(
|
|
88
|
+
day: number,
|
|
89
|
+
locale: string = DEFAULT_LOCALE
|
|
90
|
+
): string {
|
|
91
|
+
return new Intl.NumberFormat(locale).format(day);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Clear the month names cache (useful for testing)
|
|
96
|
+
*/
|
|
97
|
+
export function clearMonthNamesCache(): void {
|
|
98
|
+
monthNamesCache.clear();
|
|
99
|
+
}
|