react-native-paper-dates 0.8.4 → 0.8.5
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/lib/commonjs/Date/dateUtils.js +4 -3
- package/lib/commonjs/Date/dateUtils.js.map +1 -1
- package/lib/commonjs/Date/inputUtils.js +3 -2
- package/lib/commonjs/Date/inputUtils.js.map +1 -1
- package/lib/module/Date/dateUtils.js +4 -3
- package/lib/module/Date/dateUtils.js.map +1 -1
- package/lib/module/Date/inputUtils.js +3 -2
- package/lib/module/Date/inputUtils.js.map +1 -1
- package/lib/typescript/Date/dateUtils.d.ts +2 -1
- package/package.json +1 -1
- package/src/Date/dateUtils.tsx +4 -2
- package/src/Date/inputUtils.ts +5 -2
|
@@ -229,13 +229,14 @@ function getEndOfDay(d) {
|
|
|
229
229
|
|
|
230
230
|
function useInputFormat(_ref7) {
|
|
231
231
|
let {
|
|
232
|
-
formatter
|
|
232
|
+
formatter,
|
|
233
|
+
locale
|
|
233
234
|
} = _ref7;
|
|
234
235
|
return React.useMemo(() => {
|
|
235
236
|
// TODO: something cleaner and more universal?
|
|
236
237
|
const inputDate = formatter.format(new Date(2020, 10 - 1, 1));
|
|
237
|
-
return inputDate.replace('2020', 'YYYY').replace('10', 'MM').replace('01', 'DD');
|
|
238
|
-
}, [formatter]);
|
|
238
|
+
return inputDate.replace('2020', locale === 'pt' ? 'AAAA' : 'YYYY').replace('10', 'MM').replace('01', 'DD');
|
|
239
|
+
}, [formatter, locale]);
|
|
239
240
|
}
|
|
240
241
|
|
|
241
242
|
function differenceInMonths(firstDate, secondDate) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["dateUtils.tsx"],"names":["showWeekDay","dayIndex","disableWeekDays","some","di","dateToUnix","d","Math","round","getTime","addMonths","date","count","n","getDate","n2","Date","setDate","setMonth","getMonth","min","getDaysInMonth","year","getFullYear","month","isLeapYear","getFirstDayOfMonth","getDay","useRangeChecker","validRange","validStart","startDate","validEnd","endDate","startUnix","getStartOfDay","undefined","endUnix","getEndOfDay","validDisabledDatesRef","disabledDates","isWithinValidRange","React","useCallback","day","isDateWithinOptionalRange","isDisabled","current","disabledDate","areDatesOnSameDay","a","b","isDateBetween","dateUnix","daySize","estimatedMonthHeight","startAtIndex","totalMonths","beginOffset","gridCounts","Array","getGridCount","index","cHeight","monthDate","getRealIndex","h","getGridCountForDate","daysInMonth","dayOfWeek","ceil","getInitialIndex","today","months","differenceInMonths","useInputFormatter","locale","useMemo","Intl","DateTimeFormat","useInputFormat","formatter","inputDate","format","replace","firstDate","secondDate","diffMonths"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;;;;;AAKO,SAASA,WAAT,CACLC,QADK,EAELC,eAFK,EAGI;AACT,SAAO,EAAEA,eAAe,IAAIA,eAAe,CAACC,IAAhB,CAAsBC,EAAD,IAAQA,EAAE,KAAKH,QAApC,CAArB,CAAP;AACD;;AAEM,SAASI,UAAT,CAAoBC,CAApB,EAAqC;AAC1C,SAAOC,IAAI,CAACC,KAAL,CAAWF,CAAC,CAACG,OAAF,KAAc,IAAzB,CAAP;AACD;;AAEM,SAASC,SAAT,CAAmBC,IAAnB,EAA+BC,KAA/B,EAA8C;AACnD,MAAIC,CAAC,GAAGF,IAAI,CAACG,OAAL,EAAR;AACA,MAAIC,EAAE,GAAG,IAAIC,IAAJ,CAASL,IAAI,CAACF,OAAL,EAAT,CAAT;AACAM,EAAAA,EAAE,CAACE,OAAH,CAAW,CAAX;AACAF,EAAAA,EAAE,CAACG,QAAH,CAAYH,EAAE,CAACI,QAAH,KAAgBP,KAA5B;AACAG,EAAAA,EAAE,CAACE,OAAH,CACEV,IAAI,CAACa,GAAL,CACEP,CADF,EAEEQ,cAAc,CAAC;AAAEC,IAAAA,IAAI,EAAEP,EAAE,CAACQ,WAAH,EAAR;AAA0BC,IAAAA,KAAK,EAAET,EAAE,CAACI,QAAH;AAAjC,GAAD,CAFhB,CADF;AA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ACQ,OAAF,EAAxC,EAAqD,EAArD,EAAyD,EAAzD,EAA6D,EAA7D,CAAP;AACD;;AACM,SAASkE,cAAT,QAIJ;AAAA,MAJ4B;AAC7BC,IAAAA;AAD6B,GAI5B;AACD,SAAOvC,KAAK,CAACmC,OAAN,CAAc,MAAM;AACzB;AACA,UAAMK,SAAS,GAAGD,SAAS,CAACE,MAAV,CAAiB,IAAInE,IAAJ,CAAS,IAAT,EAAe,KAAK,CAApB,EAAuB,CAAvB,CAAjB,CAAlB;AACA,WAAOkE,SAAS,CACbE,OADI,CACI,MADJ,EACY,MADZ,EAEJA,OAFI,CAEI,IAFJ,EAEU,IAFV,EAGJA,OAHI,CAGI,IAHJ,EAGU,IAHV,CAAP;AAID,GAPM,EAOJ,CAACH,SAAD,CAPI,CAAP;AAQD;;AAEM,SAASP,kBAAT,CAA4BW,SAA5B,EAA6CC,UAA7C,EAA+D;AACpE,MAAIC,UAAU,GAAG,CAACD,UAAU,CAAC/D,WAAX,KAA2B8D,SAAS,CAAC9D,WAAV,EAA5B,IAAuD,EAAxE;AACAgE,EAAAA,UAAU,IAAIF,SAAS,CAAClE,QAAV,EAAd;AACAoE,EAAAA,UAAU,IAAID,UAAU,CAACnE,QAAX,EAAd;AACA,SAAOoE,UAAP;AACD","sourcesContent":["import * as React from 'react'\nimport { useLatest } from '../utils'\nimport type { ValidRangeType } from './Calendar'\n\nexport type DisableWeekDaysType = number[]\n\nexport function showWeekDay(\n dayIndex: number,\n disableWeekDays?: DisableWeekDaysType\n): boolean {\n return !(disableWeekDays && disableWeekDays.some((di) => di === dayIndex))\n}\n\nexport function dateToUnix(d: Date): number {\n return Math.round(d.getTime() / 1000)\n}\n\nexport function addMonths(date: Date, count: number) {\n let n = date.getDate()\n let n2 = new Date(date.getTime())\n n2.setDate(1)\n n2.setMonth(n2.getMonth() + count)\n n2.setDate(\n Math.min(\n n,\n getDaysInMonth({ year: n2.getFullYear(), month: n2.getMonth() })\n )\n )\n\n return n2\n}\n\n// https://stackoverflow.com/a/1185068/2508481\n// pass in any date as parameter anyDateInMonth based on dayjs\nexport function getDaysInMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return [\n 31,\n isLeapYear({ year }) ? 29 : 28,\n 31,\n 30,\n 31,\n 30,\n 31,\n 31,\n 30,\n 31,\n 30,\n 31,\n ][month]\n}\n\nexport function getFirstDayOfMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return new Date(year, month, 1).getDay()\n}\n\nexport function useRangeChecker(validRange: ValidRangeType | undefined) {\n const validStart = validRange?.startDate\n const validEnd = validRange?.endDate\n const startUnix =\n validStart instanceof Date\n ? dateToUnix(getStartOfDay(validStart))\n : undefined\n\n const endUnix =\n validEnd instanceof Date ? dateToUnix(getEndOfDay(validEnd)) : undefined\n\n const validDisabledDatesRef = useLatest(validRange?.disabledDates)\n\n const isWithinValidRange = React.useCallback(\n (day: Date) => {\n return isDateWithinOptionalRange(day, {\n startUnix: startUnix,\n endUnix: endUnix,\n })\n },\n [startUnix, endUnix]\n )\n\n const isDisabled = React.useCallback(\n (day: Date) => {\n return validDisabledDatesRef.current\n ? validDisabledDatesRef.current.some((disabledDate) =>\n areDatesOnSameDay(disabledDate, day)\n )\n : false\n },\n [validDisabledDatesRef]\n )\n\n return { isDisabled, isWithinValidRange, validStart, validEnd }\n}\n\nexport function areDatesOnSameDay(a: Date, b?: Date | null | undefined) {\n if (!b) {\n return false\n }\n\n return (\n a.getFullYear() === b.getFullYear() &&\n a.getMonth() === b.getMonth() &&\n a.getDate() === b.getDate()\n )\n}\n\nexport function isDateBetween(\n date: Date,\n {\n startDate,\n endDate,\n }: {\n startDate?: Date | null | undefined\n endDate?: Date | null | undefined\n }\n): boolean {\n if (!startDate || !endDate) {\n return false\n }\n return date <= endDate && date >= startDate\n}\n\n/**\n * Check if a date is within an optional range.\n *\n * If the range doesn't exist, it defaults to `true`.\n */\nexport function isDateWithinOptionalRange(\n date: Date,\n {\n startUnix,\n endUnix,\n }: { startUnix: number | undefined; endUnix: number | undefined }\n) {\n const dateUnix = dateToUnix(date)\n // if startUnix is provided and date is before start\n if (startUnix && dateUnix < startUnix) {\n return false\n }\n\n // if endUnix is provided and date is after end\n if (endUnix && dateUnix > endUnix) {\n return false\n }\n\n return true\n}\n\nexport function isLeapYear({ year }: { year: number }) {\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0\n}\n\nexport const daySize = 46\nexport const estimatedMonthHeight = 360\nexport const startAtIndex = 1200\nexport const totalMonths = startAtIndex * 2\nexport const beginOffset = estimatedMonthHeight * startAtIndex\nexport const gridCounts = new Array<number | undefined>(totalMonths)\n\nexport function getGridCount(index: number) {\n const cHeight = gridCounts[index]\n if (cHeight) {\n return cHeight\n }\n const monthDate = addMonths(new Date(), getRealIndex(index))\n const h = getGridCountForDate(monthDate)\n gridCounts[index] = h\n return h\n}\n\nexport function getGridCountForDate(date: Date) {\n const year = date.getFullYear()\n const month = date.getMonth()\n const daysInMonth = getDaysInMonth({ year, month })\n const dayOfWeek = getFirstDayOfMonth({ year, month })\n return Math.ceil((daysInMonth + dayOfWeek) / 7)\n}\n\nexport function getRealIndex(index: number) {\n return index - startAtIndex\n}\n\nexport function getInitialIndex(date: Date | undefined) {\n if (!date) {\n return startAtIndex\n }\n\n const today = new Date()\n const months = differenceInMonths(today, date)\n\n return startAtIndex + months\n}\n\nexport function useInputFormatter({ locale }: { locale: string | undefined }) {\n return React.useMemo(() => {\n return new Intl.DateTimeFormat(locale, {\n month: '2-digit',\n day: '2-digit',\n year: 'numeric',\n })\n }, [locale])\n}\nexport function getStartOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0)\n}\nexport function getEndOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)\n}\nexport function useInputFormat({\n formatter,\n}: {\n formatter: Intl.DateTimeFormat\n}) {\n return React.useMemo(() => {\n // TODO: something cleaner and more universal?\n const inputDate = formatter.format(new Date(2020, 10 - 1, 1))\n return inputDate\n .replace('2020', 'YYYY')\n .replace('10', 'MM')\n .replace('01', 'DD')\n }, [formatter])\n}\n\nexport function differenceInMonths(firstDate: Date, secondDate: Date) {\n let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12\n diffMonths -= firstDate.getMonth()\n diffMonths += secondDate.getMonth()\n return diffMonths\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["dateUtils.tsx"],"names":["showWeekDay","dayIndex","disableWeekDays","some","di","dateToUnix","d","Math","round","getTime","addMonths","date","count","n","getDate","n2","Date","setDate","setMonth","getMonth","min","getDaysInMonth","year","getFullYear","month","isLeapYear","getFirstDayOfMonth","getDay","useRangeChecker","validRange","validStart","startDate","validEnd","endDate","startUnix","getStartOfDay","undefined","endUnix","getEndOfDay","validDisabledDatesRef","disabledDates","isWithinValidRange","React","useCallback","day","isDateWithinOptionalRange","isDisabled","current","disabledDate","areDatesOnSameDay","a","b","isDateBetween","dateUnix","daySize","estimatedMonthHeight","startAtIndex","totalMonths","beginOffset","gridCounts","Array","getGridCount","index","cHeight","monthDate","getRealIndex","h","getGridCountForDate","daysInMonth","dayOfWeek","ceil","getInitialIndex","today","months","differenceInMonths","useInputFormatter","locale","useMemo","Intl","DateTimeFormat","useInputFormat","formatter","inputDate","format","replace","firstDate","secondDate","diffMonths"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;;;;;AAKO,SAASA,WAAT,CACLC,QADK,EAELC,eAFK,EAGI;AACT,SAAO,EAAEA,eAAe,IAAIA,eAAe,CAACC,IAAhB,CAAsBC,EAAD,IAAQA,EAAE,KAAKH,QAApC,CAArB,CAAP;AACD;;AAEM,SAASI,UAAT,CAAoBC,CAApB,EAAqC;AAC1C,SAAOC,IAAI,CAACC,KAAL,CAAWF,CAAC,CAACG,OAAF,KAAc,IAAzB,CAAP;AACD;;AAEM,SAASC,SAAT,CAAmBC,IAAnB,EAA+BC,KAA/B,EAA8C;AACn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AAV,EAAd;AACAoE,EAAAA,UAAU,IAAID,UAAU,CAACnE,QAAX,EAAd;AACA,SAAOoE,UAAP;AACD","sourcesContent":["import * as React from 'react'\nimport { useLatest } from '../utils'\nimport type { ValidRangeType } from './Calendar'\n\nexport type DisableWeekDaysType = number[]\n\nexport function showWeekDay(\n dayIndex: number,\n disableWeekDays?: DisableWeekDaysType\n): boolean {\n return !(disableWeekDays && disableWeekDays.some((di) => di === dayIndex))\n}\n\nexport function dateToUnix(d: Date): number {\n return Math.round(d.getTime() / 1000)\n}\n\nexport function addMonths(date: Date, count: number) {\n let n = date.getDate()\n let n2 = new Date(date.getTime())\n n2.setDate(1)\n n2.setMonth(n2.getMonth() + count)\n n2.setDate(\n Math.min(\n n,\n getDaysInMonth({ year: n2.getFullYear(), month: n2.getMonth() })\n )\n )\n\n return n2\n}\n\n// https://stackoverflow.com/a/1185068/2508481\n// pass in any date as parameter anyDateInMonth based on dayjs\nexport function getDaysInMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return [\n 31,\n isLeapYear({ year }) ? 29 : 28,\n 31,\n 30,\n 31,\n 30,\n 31,\n 31,\n 30,\n 31,\n 30,\n 31,\n ][month]\n}\n\nexport function getFirstDayOfMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return new Date(year, month, 1).getDay()\n}\n\nexport function useRangeChecker(validRange: ValidRangeType | undefined) {\n const validStart = validRange?.startDate\n const validEnd = validRange?.endDate\n const startUnix =\n validStart instanceof Date\n ? dateToUnix(getStartOfDay(validStart))\n : undefined\n\n const endUnix =\n validEnd instanceof Date ? dateToUnix(getEndOfDay(validEnd)) : undefined\n\n const validDisabledDatesRef = useLatest(validRange?.disabledDates)\n\n const isWithinValidRange = React.useCallback(\n (day: Date) => {\n return isDateWithinOptionalRange(day, {\n startUnix: startUnix,\n endUnix: endUnix,\n })\n },\n [startUnix, endUnix]\n )\n\n const isDisabled = React.useCallback(\n (day: Date) => {\n return validDisabledDatesRef.current\n ? validDisabledDatesRef.current.some((disabledDate) =>\n areDatesOnSameDay(disabledDate, day)\n )\n : false\n },\n [validDisabledDatesRef]\n )\n\n return { isDisabled, isWithinValidRange, validStart, validEnd }\n}\n\nexport function areDatesOnSameDay(a: Date, b?: Date | null | undefined) {\n if (!b) {\n return false\n }\n\n return (\n a.getFullYear() === b.getFullYear() &&\n a.getMonth() === b.getMonth() &&\n a.getDate() === b.getDate()\n )\n}\n\nexport function isDateBetween(\n date: Date,\n {\n startDate,\n endDate,\n }: {\n startDate?: Date | null | undefined\n endDate?: Date | null | undefined\n }\n): boolean {\n if (!startDate || !endDate) {\n return false\n }\n return date <= endDate && date >= startDate\n}\n\n/**\n * Check if a date is within an optional range.\n *\n * If the range doesn't exist, it defaults to `true`.\n */\nexport function isDateWithinOptionalRange(\n date: Date,\n {\n startUnix,\n endUnix,\n }: { startUnix: number | undefined; endUnix: number | undefined }\n) {\n const dateUnix = dateToUnix(date)\n // if startUnix is provided and date is before start\n if (startUnix && dateUnix < startUnix) {\n return false\n }\n\n // if endUnix is provided and date is after end\n if (endUnix && dateUnix > endUnix) {\n return false\n }\n\n return true\n}\n\nexport function isLeapYear({ year }: { year: number }) {\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0\n}\n\nexport const daySize = 46\nexport const estimatedMonthHeight = 360\nexport const startAtIndex = 1200\nexport const totalMonths = startAtIndex * 2\nexport const beginOffset = estimatedMonthHeight * startAtIndex\nexport const gridCounts = new Array<number | undefined>(totalMonths)\n\nexport function getGridCount(index: number) {\n const cHeight = gridCounts[index]\n if (cHeight) {\n return cHeight\n }\n const monthDate = addMonths(new Date(), getRealIndex(index))\n const h = getGridCountForDate(monthDate)\n gridCounts[index] = h\n return h\n}\n\nexport function getGridCountForDate(date: Date) {\n const year = date.getFullYear()\n const month = date.getMonth()\n const daysInMonth = getDaysInMonth({ year, month })\n const dayOfWeek = getFirstDayOfMonth({ year, month })\n return Math.ceil((daysInMonth + dayOfWeek) / 7)\n}\n\nexport function getRealIndex(index: number) {\n return index - startAtIndex\n}\n\nexport function getInitialIndex(date: Date | undefined) {\n if (!date) {\n return startAtIndex\n }\n\n const today = new Date()\n const months = differenceInMonths(today, date)\n\n return startAtIndex + months\n}\n\nexport function useInputFormatter({ locale }: { locale: string | undefined }) {\n return React.useMemo(() => {\n return new Intl.DateTimeFormat(locale, {\n month: '2-digit',\n day: '2-digit',\n year: 'numeric',\n })\n }, [locale])\n}\nexport function getStartOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0)\n}\nexport function getEndOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)\n}\nexport function useInputFormat({\n formatter,\n locale,\n}: {\n formatter: Intl.DateTimeFormat\n locale: string | undefined\n}) {\n return React.useMemo(() => {\n // TODO: something cleaner and more universal?\n const inputDate = formatter.format(new Date(2020, 10 - 1, 1))\n return inputDate\n .replace('2020', locale === 'pt' ? 'AAAA' : 'YYYY')\n .replace('10', 'MM')\n .replace('01', 'DD')\n }, [formatter, locale])\n}\n\nexport function differenceInMonths(firstDate: Date, secondDate: Date) {\n let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12\n diffMonths -= firstDate.getMonth()\n diffMonths += secondDate.getMonth()\n return diffMonths\n}\n"]}
|
|
@@ -34,14 +34,15 @@ function useDateInput(_ref) {
|
|
|
34
34
|
locale
|
|
35
35
|
});
|
|
36
36
|
const inputFormat = (0, _dateUtils.useInputFormat)({
|
|
37
|
-
formatter
|
|
37
|
+
formatter,
|
|
38
|
+
locale
|
|
38
39
|
});
|
|
39
40
|
const formattedValue = formatter.format(value);
|
|
40
41
|
|
|
41
42
|
const onChangeText = date => {
|
|
42
43
|
const dayIndex = inputFormat.indexOf('DD');
|
|
43
44
|
const monthIndex = inputFormat.indexOf('MM');
|
|
44
|
-
const yearIndex = inputFormat.indexOf('YYYY');
|
|
45
|
+
const yearIndex = locale === 'pt' ? inputFormat.indexOf('AAAA') : inputFormat.indexOf('YYYY');
|
|
45
46
|
const day = Number(date.slice(dayIndex, dayIndex + 2));
|
|
46
47
|
const year = Number(date.slice(yearIndex, yearIndex + 4));
|
|
47
48
|
const month = Number(date.slice(monthIndex, monthIndex + 2));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["inputUtils.ts"],"names":["useDateInput","locale","value","validRange","inputMode","onChange","isDisabled","isWithinValidRange","validStart","validEnd","error","setError","React","useState","formatter","inputFormat","formattedValue","format","onChangeText","date","dayIndex","indexOf","monthIndex","yearIndex","day","Number","slice","year","month","isNaN","finalDate","Date","errors","filter","n","join"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;;;;;AAEe,SAASA,YAAT,OAYZ;AAAA,MAZkC;AACnCC,IAAAA,MADmC;AAEnCC,IAAAA,KAFmC;AAGnCC,IAAAA,UAHmC;AAInCC,IAAAA,SAJmC;AAKnCC,IAAAA;AALmC,GAYlC;AACD,QAAM;AAAEC,IAAAA,UAAF;AAAcC,IAAAA,kBAAd;AAAkCC,IAAAA,UAAlC;AAA8CC,IAAAA;AAA9C,MACJ,gCAAgBN,UAAhB,CADF;AAEA,QAAM,CAACO,KAAD,EAAQC,QAAR,IAAoBC,KAAK,CAACC,QAAN,CAA8B,IAA9B,CAA1B;AACA,QAAMC,SAAS,GAAG,kCAAkB;AAAEb,IAAAA;AAAF,GAAlB,CAAlB;AACA,QAAMc,WAAW,GAAG,+BAAe;AAAED,IAAAA;
|
|
1
|
+
{"version":3,"sources":["inputUtils.ts"],"names":["useDateInput","locale","value","validRange","inputMode","onChange","isDisabled","isWithinValidRange","validStart","validEnd","error","setError","React","useState","formatter","inputFormat","formattedValue","format","onChangeText","date","dayIndex","indexOf","monthIndex","yearIndex","day","Number","slice","year","month","isNaN","finalDate","Date","errors","filter","n","join"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;;;;;AAEe,SAASA,YAAT,OAYZ;AAAA,MAZkC;AACnCC,IAAAA,MADmC;AAEnCC,I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,QAAQ,CAAC,IAAD,CAAR;;AACA,QAAIP,SAAS,KAAK,KAAlB,EAAyB;AACvBC,MAAAA,QAAQ,CAACyB,SAAD,CAAR;AACD,KAFD,MAEO;AACLzB,MAAAA,QAAQ,CAACyB,SAAD,CAAR;AACD;AACF,GApED;;AAqEA,SAAO;AACLzB,IAAAA,QADK;AAELK,IAAAA,KAFK;AAGLM,IAAAA,cAHK;AAILE,IAAAA,YAJK;AAKLH,IAAAA;AALK,GAAP;AAOD","sourcesContent":["import { useInputFormat, useInputFormatter, useRangeChecker } from './dateUtils'\nimport * as React from 'react'\nimport type { ValidRangeType } from './Calendar'\nimport { getTranslation } from 'react-native-paper-dates'\n\nexport default function useDateInput({\n locale,\n value,\n validRange,\n inputMode,\n onChange,\n}: {\n onChange: (d: Date) => void\n locale: undefined | string\n value: Date | undefined\n validRange: ValidRangeType | undefined\n inputMode: 'start' | 'end'\n}) {\n const { isDisabled, isWithinValidRange, validStart, validEnd } =\n useRangeChecker(validRange)\n const [error, setError] = React.useState<null | string>(null)\n const formatter = useInputFormatter({ locale })\n const inputFormat = useInputFormat({ formatter, locale })\n const formattedValue = formatter.format(value)\n const onChangeText = (date: string) => {\n const dayIndex = inputFormat.indexOf('DD')\n const monthIndex = inputFormat.indexOf('MM')\n const yearIndex =\n locale === 'pt'\n ? inputFormat.indexOf('AAAA')\n : inputFormat.indexOf('YYYY')\n\n const day = Number(date.slice(dayIndex, dayIndex + 2))\n const year = Number(date.slice(yearIndex, yearIndex + 4))\n const month = Number(date.slice(monthIndex, monthIndex + 2))\n\n if (Number.isNaN(day) || Number.isNaN(year) || Number.isNaN(month)) {\n setError(\n getTranslation(\n locale,\n 'notAccordingToDateFormat',\n () => 'notAccordingToDateFormat'\n )(inputFormat)\n )\n return\n }\n\n const finalDate =\n inputMode === 'end'\n ? new Date(year, month - 1, day, 23, 59, 59)\n : new Date(year, month - 1, day)\n\n if (isDisabled(finalDate)) {\n setError(getTranslation(locale, 'dateIsDisabled'))\n return\n }\n if (!isWithinValidRange(finalDate)) {\n let errors =\n validStart && validEnd\n ? [\n `${getTranslation(\n locale,\n 'mustBeBetween',\n () => 'mustBeBetween'\n )(formatter.format(validStart), formatter.format(validEnd))}`,\n ]\n : [\n validStart\n ? getTranslation(\n locale,\n 'mustBeHigherThan',\n () => 'mustBeHigherThan'\n )(formatter.format(validStart))\n : '',\n validEnd\n ? getTranslation(\n locale,\n 'mustBeLowerThan',\n () => 'mustBeLowerThan'\n )(formatter.format(validEnd))\n : '',\n ]\n setError(errors.filter((n) => n).join(' '))\n return\n }\n\n setError(null)\n if (inputMode === 'end') {\n onChange(finalDate)\n } else {\n onChange(finalDate)\n }\n }\n return {\n onChange,\n error,\n formattedValue,\n onChangeText,\n inputFormat,\n }\n}\n"]}
|
|
@@ -169,13 +169,14 @@ export function getEndOfDay(d) {
|
|
|
169
169
|
}
|
|
170
170
|
export function useInputFormat(_ref7) {
|
|
171
171
|
let {
|
|
172
|
-
formatter
|
|
172
|
+
formatter,
|
|
173
|
+
locale
|
|
173
174
|
} = _ref7;
|
|
174
175
|
return React.useMemo(() => {
|
|
175
176
|
// TODO: something cleaner and more universal?
|
|
176
177
|
const inputDate = formatter.format(new Date(2020, 10 - 1, 1));
|
|
177
|
-
return inputDate.replace('2020', 'YYYY').replace('10', 'MM').replace('01', 'DD');
|
|
178
|
-
}, [formatter]);
|
|
178
|
+
return inputDate.replace('2020', locale === 'pt' ? 'AAAA' : 'YYYY').replace('10', 'MM').replace('01', 'DD');
|
|
179
|
+
}, [formatter, locale]);
|
|
179
180
|
}
|
|
180
181
|
export function differenceInMonths(firstDate, secondDate) {
|
|
181
182
|
let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["dateUtils.tsx"],"names":["React","useLatest","showWeekDay","dayIndex","disableWeekDays","some","di","dateToUnix","d","Math","round","getTime","addMonths","date","count","n","getDate","n2","Date","setDate","setMonth","getMonth","min","getDaysInMonth","year","getFullYear","month","isLeapYear","getFirstDayOfMonth","getDay","useRangeChecker","validRange","validStart","startDate","validEnd","endDate","startUnix","getStartOfDay","undefined","endUnix","getEndOfDay","validDisabledDatesRef","disabledDates","isWithinValidRange","useCallback","day","isDateWithinOptionalRange","isDisabled","current","disabledDate","areDatesOnSameDay","a","b","isDateBetween","dateUnix","daySize","estimatedMonthHeight","startAtIndex","totalMonths","beginOffset","gridCounts","Array","getGridCount","index","cHeight","monthDate","getRealIndex","h","getGridCountForDate","daysInMonth","dayOfWeek","ceil","getInitialIndex","today","months","differenceInMonths","useInputFormatter","locale","useMemo","Intl","DateTimeFormat","useInputFormat","formatter","inputDate","format","replace","firstDate","secondDate","diffMonths"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AACA,SAASC,SAAT,QAA0B,UAA1B;AAKA,OAAO,SAASC,WAAT,CACLC,QADK,EAELC,eAFK,EAGI;AACT,SAAO,EAAEA,eAAe,IAAIA,eAAe,CAACC,IAAhB,CAAsBC,EAAD,IAAQA,EAAE,KAAKH,QAApC,CAArB,CAAP;AACD;AAED,OAAO,SAASI,UAAT,CAAoBC,CAApB,EAAqC;AAC1C,SAAOC,IAAI,CAACC,KAAL,CAAWF,CAAC,CAACG,OAAF,KAAc,IAAzB,CAAP;AACD;AAED,OAAO,SAASC,SAAT,CAAmBC,IAAnB,EAA+BC,KAA/B,EAA8C;AACnD,MAAIC,CAAC,GAAGF,IAAI,CAACG,OAAL,EAAR;AACA,MAAIC,EAAE,GAAG,IAAIC,IAAJ,CAASL,IAAI,CAACF,OAAL,EAAT,CAAT;AACAM,EAAAA,EAAE,CAACE,OAAH,CAAW,CAAX;AACAF,EAAAA,EAAE,CAACG,QAAH,CAAYH,EAAE,CAACI,QAAH,KAAgBP,KAA5B;AACAG,EAAAA,EAAE,CAACE,OAAH,CACEV,IAAI,CAACa,GAAL,CACEP,CADF,EAEEQ,cAAc,CAAC;AAAEC,IAAAA,IAAI,EAAEP,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AwD,CAAxD,EAA2D,CAA3D,CAAP;AACD;AACD,OAAO,SAASwB,WAAT,CAAqBhC,CAArB,EAAoC;AACzC,SAAO,IAAIU,IAAJ,CAASV,CAAC,CAACiB,WAAF,EAAT,EAA0BjB,CAAC,CAACa,QAAF,EAA1B,EAAwCb,CAAC,CAACQ,OAAF,EAAxC,EAAqD,EAArD,EAAyD,EAAzD,EAA6D,EAA7D,CAAP;AACD;AACD,OAAO,SAASiE,cAAT,QAIJ;AAAA,MAJ4B;AAC7BC,IAAAA;AAD6B,GAI5B;AACD,SAAOlF,KAAK,CAAC8E,OAAN,CAAc,MAAM;AACzB;AACA,UAAMK,SAAS,GAAGD,SAAS,CAACE,MAAV,CAAiB,IAAIlE,IAAJ,CAAS,IAAT,EAAe,KAAK,CAApB,EAAuB,CAAvB,CAAjB,CAAlB;AACA,WAAOiE,SAAS,CACbE,OADI,CACI,MADJ,EACY,MADZ,EAEJA,OAFI,CAEI,IAFJ,EAEU,IAFV,EAGJA,OAHI,CAGI,IAHJ,EAGU,IAHV,CAAP;AAID,GAPM,EAOJ,CAACH,SAAD,CAPI,CAAP;AAQD;AAED,OAAO,SAASP,kBAAT,CAA4BW,SAA5B,EAA6CC,UAA7C,EAA+D;AACpE,MAAIC,UAAU,GAAG,CAACD,UAAU,CAAC9D,WAAX,KAA2B6D,SAAS,CAAC7D,WAAV,EAA5B,IAAuD,EAAxE;AACA+D,EAAAA,UAAU,IAAIF,SAAS,CAACjE,QAAV,EAAd;AACAmE,EAAAA,UAAU,IAAID,UAAU,CAAClE,QAAX,EAAd;AACA,SAAOmE,UAAP;AACD","sourcesContent":["import * as React from 'react'\nimport { useLatest } from '../utils'\nimport type { ValidRangeType } from './Calendar'\n\nexport type DisableWeekDaysType = number[]\n\nexport function showWeekDay(\n dayIndex: number,\n disableWeekDays?: DisableWeekDaysType\n): boolean {\n return !(disableWeekDays && disableWeekDays.some((di) => di === dayIndex))\n}\n\nexport function dateToUnix(d: Date): number {\n return Math.round(d.getTime() / 1000)\n}\n\nexport function addMonths(date: Date, count: number) {\n let n = date.getDate()\n let n2 = new Date(date.getTime())\n n2.setDate(1)\n n2.setMonth(n2.getMonth() + count)\n n2.setDate(\n Math.min(\n n,\n getDaysInMonth({ year: n2.getFullYear(), month: n2.getMonth() })\n )\n )\n\n return n2\n}\n\n// https://stackoverflow.com/a/1185068/2508481\n// pass in any date as parameter anyDateInMonth based on dayjs\nexport function getDaysInMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return [\n 31,\n isLeapYear({ year }) ? 29 : 28,\n 31,\n 30,\n 31,\n 30,\n 31,\n 31,\n 30,\n 31,\n 30,\n 31,\n ][month]\n}\n\nexport function getFirstDayOfMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return new Date(year, month, 1).getDay()\n}\n\nexport function useRangeChecker(validRange: ValidRangeType | undefined) {\n const validStart = validRange?.startDate\n const validEnd = validRange?.endDate\n const startUnix =\n validStart instanceof Date\n ? dateToUnix(getStartOfDay(validStart))\n : undefined\n\n const endUnix =\n validEnd instanceof Date ? dateToUnix(getEndOfDay(validEnd)) : undefined\n\n const validDisabledDatesRef = useLatest(validRange?.disabledDates)\n\n const isWithinValidRange = React.useCallback(\n (day: Date) => {\n return isDateWithinOptionalRange(day, {\n startUnix: startUnix,\n endUnix: endUnix,\n })\n },\n [startUnix, endUnix]\n )\n\n const isDisabled = React.useCallback(\n (day: Date) => {\n return validDisabledDatesRef.current\n ? validDisabledDatesRef.current.some((disabledDate) =>\n areDatesOnSameDay(disabledDate, day)\n )\n : false\n },\n [validDisabledDatesRef]\n )\n\n return { isDisabled, isWithinValidRange, validStart, validEnd }\n}\n\nexport function areDatesOnSameDay(a: Date, b?: Date | null | undefined) {\n if (!b) {\n return false\n }\n\n return (\n a.getFullYear() === b.getFullYear() &&\n a.getMonth() === b.getMonth() &&\n a.getDate() === b.getDate()\n )\n}\n\nexport function isDateBetween(\n date: Date,\n {\n startDate,\n endDate,\n }: {\n startDate?: Date | null | undefined\n endDate?: Date | null | undefined\n }\n): boolean {\n if (!startDate || !endDate) {\n return false\n }\n return date <= endDate && date >= startDate\n}\n\n/**\n * Check if a date is within an optional range.\n *\n * If the range doesn't exist, it defaults to `true`.\n */\nexport function isDateWithinOptionalRange(\n date: Date,\n {\n startUnix,\n endUnix,\n }: { startUnix: number | undefined; endUnix: number | undefined }\n) {\n const dateUnix = dateToUnix(date)\n // if startUnix is provided and date is before start\n if (startUnix && dateUnix < startUnix) {\n return false\n }\n\n // if endUnix is provided and date is after end\n if (endUnix && dateUnix > endUnix) {\n return false\n }\n\n return true\n}\n\nexport function isLeapYear({ year }: { year: number }) {\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0\n}\n\nexport const daySize = 46\nexport const estimatedMonthHeight = 360\nexport const startAtIndex = 1200\nexport const totalMonths = startAtIndex * 2\nexport const beginOffset = estimatedMonthHeight * startAtIndex\nexport const gridCounts = new Array<number | undefined>(totalMonths)\n\nexport function getGridCount(index: number) {\n const cHeight = gridCounts[index]\n if (cHeight) {\n return cHeight\n }\n const monthDate = addMonths(new Date(), getRealIndex(index))\n const h = getGridCountForDate(monthDate)\n gridCounts[index] = h\n return h\n}\n\nexport function getGridCountForDate(date: Date) {\n const year = date.getFullYear()\n const month = date.getMonth()\n const daysInMonth = getDaysInMonth({ year, month })\n const dayOfWeek = getFirstDayOfMonth({ year, month })\n return Math.ceil((daysInMonth + dayOfWeek) / 7)\n}\n\nexport function getRealIndex(index: number) {\n return index - startAtIndex\n}\n\nexport function getInitialIndex(date: Date | undefined) {\n if (!date) {\n return startAtIndex\n }\n\n const today = new Date()\n const months = differenceInMonths(today, date)\n\n return startAtIndex + months\n}\n\nexport function useInputFormatter({ locale }: { locale: string | undefined }) {\n return React.useMemo(() => {\n return new Intl.DateTimeFormat(locale, {\n month: '2-digit',\n day: '2-digit',\n year: 'numeric',\n })\n }, [locale])\n}\nexport function getStartOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0)\n}\nexport function getEndOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)\n}\nexport function useInputFormat({\n formatter,\n}: {\n formatter: Intl.DateTimeFormat\n}) {\n return React.useMemo(() => {\n // TODO: something cleaner and more universal?\n const inputDate = formatter.format(new Date(2020, 10 - 1, 1))\n return inputDate\n .replace('2020', 'YYYY')\n .replace('10', 'MM')\n .replace('01', 'DD')\n }, [formatter])\n}\n\nexport function differenceInMonths(firstDate: Date, secondDate: Date) {\n let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12\n diffMonths -= firstDate.getMonth()\n diffMonths += secondDate.getMonth()\n return diffMonths\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["dateUtils.tsx"],"names":["React","useLatest","showWeekDay","dayIndex","disableWeekDays","some","di","dateToUnix","d","Math","round","getTime","addMonths","date","count","n","getDate","n2","Date","setDate","setMonth","getMonth","min","getDaysInMonth","year","getFullYear","month","isLeapYear","getFirstDayOfMonth","getDay","useRangeChecker","validRange","validStart","startDate","validEnd","endDate","startUnix","getStartOfDay","undefined","endUnix","getEndOfDay","validDisabledDatesRef","disabledDates","isWithinValidRange","useCallback","day","isDateWithinOptionalRange","isDisabled","current","disabledDate","areDatesOnSameDay","a","b","isDateBetween","dateUnix","daySize","estimatedMonthHeight","startAtIndex","totalMonths","beginOffset","gridCounts","Array","getGridCount","index","cHeight","monthDate","getRealIndex","h","getGridCountForDate","daysInMonth","dayOfWeek","ceil","getInitialIndex","today","months","differenceInMonths","useInputFormatter","locale","useMemo","Intl","DateTimeFormat","useInputFormat","formatter","inputDate","format","replace","firstDate","secondDate","diffMonths"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AACA,SAASC,SAAT,QAA0B,UAA1B;AAKA,OAAO,SAASC,WAAT,CACLC,QADK,EAELC,eAFK,EAGI;AACT,SAAO,EAAEA,eAAe,IAAIA,eAAe,CAACC,IAAhB,CAAsB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,OAFI,CAEI,IAFJ,EAEU,IAFV,EAGJA,OAHI,CAGI,IAHJ,EAGU,IAHV,CAAP;AAID,GAPM,EAOJ,CAACH,SAAD,EAAYL,MAAZ,CAPI,CAAP;AAQD;AAED,OAAO,SAASF,kBAAT,CAA4BW,SAA5B,EAA6CC,UAA7C,EAA+D;AACpE,MAAIC,UAAU,GAAG,CAACD,UAAU,CAAC9D,WAAX,KAA2B6D,SAAS,CAAC7D,WAAV,EAA5B,IAAuD,EAAxE;AACA+D,EAAAA,UAAU,IAAIF,SAAS,CAACjE,QAAV,EAAd;AACAmE,EAAAA,UAAU,IAAID,UAAU,CAAClE,QAAX,EAAd;AACA,SAAOmE,UAAP;AACD","sourcesContent":["import * as React from 'react'\nimport { useLatest } from '../utils'\nimport type { ValidRangeType } from './Calendar'\n\nexport type DisableWeekDaysType = number[]\n\nexport function showWeekDay(\n dayIndex: number,\n disableWeekDays?: DisableWeekDaysType\n): boolean {\n return !(disableWeekDays && disableWeekDays.some((di) => di === dayIndex))\n}\n\nexport function dateToUnix(d: Date): number {\n return Math.round(d.getTime() / 1000)\n}\n\nexport function addMonths(date: Date, count: number) {\n let n = date.getDate()\n let n2 = new Date(date.getTime())\n n2.setDate(1)\n n2.setMonth(n2.getMonth() + count)\n n2.setDate(\n Math.min(\n n,\n getDaysInMonth({ year: n2.getFullYear(), month: n2.getMonth() })\n )\n )\n\n return n2\n}\n\n// https://stackoverflow.com/a/1185068/2508481\n// pass in any date as parameter anyDateInMonth based on dayjs\nexport function getDaysInMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return [\n 31,\n isLeapYear({ year }) ? 29 : 28,\n 31,\n 30,\n 31,\n 30,\n 31,\n 31,\n 30,\n 31,\n 30,\n 31,\n ][month]\n}\n\nexport function getFirstDayOfMonth({\n year,\n month,\n}: {\n year: number\n month: number\n}): number {\n return new Date(year, month, 1).getDay()\n}\n\nexport function useRangeChecker(validRange: ValidRangeType | undefined) {\n const validStart = validRange?.startDate\n const validEnd = validRange?.endDate\n const startUnix =\n validStart instanceof Date\n ? dateToUnix(getStartOfDay(validStart))\n : undefined\n\n const endUnix =\n validEnd instanceof Date ? dateToUnix(getEndOfDay(validEnd)) : undefined\n\n const validDisabledDatesRef = useLatest(validRange?.disabledDates)\n\n const isWithinValidRange = React.useCallback(\n (day: Date) => {\n return isDateWithinOptionalRange(day, {\n startUnix: startUnix,\n endUnix: endUnix,\n })\n },\n [startUnix, endUnix]\n )\n\n const isDisabled = React.useCallback(\n (day: Date) => {\n return validDisabledDatesRef.current\n ? validDisabledDatesRef.current.some((disabledDate) =>\n areDatesOnSameDay(disabledDate, day)\n )\n : false\n },\n [validDisabledDatesRef]\n )\n\n return { isDisabled, isWithinValidRange, validStart, validEnd }\n}\n\nexport function areDatesOnSameDay(a: Date, b?: Date | null | undefined) {\n if (!b) {\n return false\n }\n\n return (\n a.getFullYear() === b.getFullYear() &&\n a.getMonth() === b.getMonth() &&\n a.getDate() === b.getDate()\n )\n}\n\nexport function isDateBetween(\n date: Date,\n {\n startDate,\n endDate,\n }: {\n startDate?: Date | null | undefined\n endDate?: Date | null | undefined\n }\n): boolean {\n if (!startDate || !endDate) {\n return false\n }\n return date <= endDate && date >= startDate\n}\n\n/**\n * Check if a date is within an optional range.\n *\n * If the range doesn't exist, it defaults to `true`.\n */\nexport function isDateWithinOptionalRange(\n date: Date,\n {\n startUnix,\n endUnix,\n }: { startUnix: number | undefined; endUnix: number | undefined }\n) {\n const dateUnix = dateToUnix(date)\n // if startUnix is provided and date is before start\n if (startUnix && dateUnix < startUnix) {\n return false\n }\n\n // if endUnix is provided and date is after end\n if (endUnix && dateUnix > endUnix) {\n return false\n }\n\n return true\n}\n\nexport function isLeapYear({ year }: { year: number }) {\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0\n}\n\nexport const daySize = 46\nexport const estimatedMonthHeight = 360\nexport const startAtIndex = 1200\nexport const totalMonths = startAtIndex * 2\nexport const beginOffset = estimatedMonthHeight * startAtIndex\nexport const gridCounts = new Array<number | undefined>(totalMonths)\n\nexport function getGridCount(index: number) {\n const cHeight = gridCounts[index]\n if (cHeight) {\n return cHeight\n }\n const monthDate = addMonths(new Date(), getRealIndex(index))\n const h = getGridCountForDate(monthDate)\n gridCounts[index] = h\n return h\n}\n\nexport function getGridCountForDate(date: Date) {\n const year = date.getFullYear()\n const month = date.getMonth()\n const daysInMonth = getDaysInMonth({ year, month })\n const dayOfWeek = getFirstDayOfMonth({ year, month })\n return Math.ceil((daysInMonth + dayOfWeek) / 7)\n}\n\nexport function getRealIndex(index: number) {\n return index - startAtIndex\n}\n\nexport function getInitialIndex(date: Date | undefined) {\n if (!date) {\n return startAtIndex\n }\n\n const today = new Date()\n const months = differenceInMonths(today, date)\n\n return startAtIndex + months\n}\n\nexport function useInputFormatter({ locale }: { locale: string | undefined }) {\n return React.useMemo(() => {\n return new Intl.DateTimeFormat(locale, {\n month: '2-digit',\n day: '2-digit',\n year: 'numeric',\n })\n }, [locale])\n}\nexport function getStartOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0)\n}\nexport function getEndOfDay(d: Date): Date {\n return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)\n}\nexport function useInputFormat({\n formatter,\n locale,\n}: {\n formatter: Intl.DateTimeFormat\n locale: string | undefined\n}) {\n return React.useMemo(() => {\n // TODO: something cleaner and more universal?\n const inputDate = formatter.format(new Date(2020, 10 - 1, 1))\n return inputDate\n .replace('2020', locale === 'pt' ? 'AAAA' : 'YYYY')\n .replace('10', 'MM')\n .replace('01', 'DD')\n }, [formatter, locale])\n}\n\nexport function differenceInMonths(firstDate: Date, secondDate: Date) {\n let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12\n diffMonths -= firstDate.getMonth()\n diffMonths += secondDate.getMonth()\n return diffMonths\n}\n"]}
|
|
@@ -20,14 +20,15 @@ export default function useDateInput(_ref) {
|
|
|
20
20
|
locale
|
|
21
21
|
});
|
|
22
22
|
const inputFormat = useInputFormat({
|
|
23
|
-
formatter
|
|
23
|
+
formatter,
|
|
24
|
+
locale
|
|
24
25
|
});
|
|
25
26
|
const formattedValue = formatter.format(value);
|
|
26
27
|
|
|
27
28
|
const onChangeText = date => {
|
|
28
29
|
const dayIndex = inputFormat.indexOf('DD');
|
|
29
30
|
const monthIndex = inputFormat.indexOf('MM');
|
|
30
|
-
const yearIndex = inputFormat.indexOf('YYYY');
|
|
31
|
+
const yearIndex = locale === 'pt' ? inputFormat.indexOf('AAAA') : inputFormat.indexOf('YYYY');
|
|
31
32
|
const day = Number(date.slice(dayIndex, dayIndex + 2));
|
|
32
33
|
const year = Number(date.slice(yearIndex, yearIndex + 4));
|
|
33
34
|
const month = Number(date.slice(monthIndex, monthIndex + 2));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["inputUtils.ts"],"names":["useInputFormat","useInputFormatter","useRangeChecker","React","getTranslation","useDateInput","locale","value","validRange","inputMode","onChange","isDisabled","isWithinValidRange","validStart","validEnd","error","setError","useState","formatter","inputFormat","formattedValue","format","onChangeText","date","dayIndex","indexOf","monthIndex","yearIndex","day","Number","slice","year","month","isNaN","finalDate","Date","errors","filter","n","join"],"mappings":"AAAA,SAASA,cAAT,EAAyBC,iBAAzB,EAA4CC,eAA5C,QAAmE,aAAnE;AACA,OAAO,KAAKC,KAAZ,MAAuB,OAAvB;AAEA,SAASC,cAAT,QAA+B,0BAA/B;AAEA,eAAe,SAASC,YAAT,OAYZ;AAAA,MAZkC;AACnCC,IAAAA,MADmC;AAEnCC,IAAAA,KAFmC;AAGnCC,IAAAA,UAHmC;AAInCC,IAAAA,SAJmC;AAKnCC,IAAAA;AALmC,GAYlC;AACD,QAAM;AAAEC,IAAAA,UAAF;AAAcC,IAAAA,kBAAd;AAAkCC,IAAAA,UAAlC;AAA8CC,IAAAA;AAA9C,MACJZ,eAAe,CAACM,UAAD,CADjB;AAEA,QAAM,CAACO,KAAD,EAAQC,QAAR,IAAoBb,KAAK,CAACc,QAAN,CAA8B,IAA9B,CAA1B;AACA,QAAMC,SAAS,GAAGjB,iBAAiB,CAAC;AAAEK,IAAAA;AAAF,GAAD,CAAnC;AACA,QAAMa,WAAW,GAAGnB,cAAc,CAAC;AAAEkB,IAAAA;
|
|
1
|
+
{"version":3,"sources":["inputUtils.ts"],"names":["useInputFormat","useInputFormatter","useRangeChecker","React","getTranslation","useDateInput","locale","value","validRange","inputMode","onChange","isDisabled","isWithinValidRange","validStart","validEnd","error","setError","useState","formatter","inputFormat","formattedValue","format","onChangeText","date","dayIndex","indexOf","monthIndex","yearIndex","day","Number","slice","year","month","isNaN","finalDate","Date","errors","filter","n","join"],"mappings":"AAAA,SAASA,cAAT,EAAyBC,iBAAzB,EAA4CC,eAA5C,QAAmE,aAAnE;AACA,OAAO,KAAKC,KAAZ,MAAuB,OAAvB;AAEA,SAASC,cAAT,QAA+B,0BAA/B;AAEA,eAAe,SAASC,YAAT,OAYZ;AAAA,MAZkC;AACnCC,IAAAA,MADmC;AAEnCC,IAAAA,KAFmC;AAGnCC,IAAAA,UAHmC;AAInCC,IAAAA,SAJmC;AAKnCC,IAAAA;AALmC,GAYlC;AACD,QAAM;AAAEC,IAAAA,UAAF;AAAcC,IAAAA,kBAAd;AAAkCC,IAAAA,UAAlC;AAA8CC,IAAAA;AAA9C,MACJZ,eAAe,CAACM,UAAD,CADjB;AAEA,QAAM,CAACO,KAAD,EAAQC,QAAR,IAAoBb,KAAK,CAACc,QAAN,CAA8B,IAA9B,CAA1B;AACA,QAAMC,SAAS,GAAGjB,iBAAiB,CAAC;AAAEK,IAAAA;AAAF,GAAD,CAAnC;AACA,QAAMa,WAAW,GAAGnB,cAAc,CAAC;AAAEkB,IAAAA,SAAF;AAAaZ,IAAAA;AAAb,GAAD,CAAlC;AACA,QAAMc,cAAc,GAAGF,SAAS,CAACG,MAAV,CAAiBd,KAAjB,CAAvB;;AACA,QAAMe,YAAY,GAAIC,IAAD,IAAkB;AAC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AR;AACA;AACD;;AACD,QAAI,CAACM,kBAAkB,CAACsB,SAAD,CAAvB,EAAoC;AAClC,UAAIE,MAAM,GACRvB,UAAU,IAAIC,QAAd,GACI,CACG,GAAEV,cAAc,CACfE,MADe,EAEf,eAFe,EAGf,MAAM,eAHS,CAAd,CAIDY,SAAS,CAACG,MAAV,CAAiBR,UAAjB,CAJC,EAI6BK,SAAS,CAACG,MAAV,CAAiBP,QAAjB,CAJ7B,CAIyD,EAL9D,CADJ,GAQI,CACED,UAAU,GACNT,cAAc,CACZE,MADY,EAEZ,kBAFY,EAGZ,MAAM,kBAHM,CAAd,CAIEY,SAAS,CAACG,MAAV,CAAiBR,UAAjB,CAJF,CADM,GAMN,EAPN,EAQEC,QAAQ,GACJV,cAAc,CACZE,MADY,EAEZ,iBAFY,EAGZ,MAAM,iBAHM,CAAd,CAIEY,SAAS,CAACG,MAAV,CAAiBP,QAAjB,CAJF,CADI,GAMJ,EAdN,CATN;AAyBAE,MAAAA,QAAQ,CAACoB,MAAM,CAACC,MAAP,CAAeC,CAAD,IAAOA,CAArB,EAAwBC,IAAxB,CAA6B,GAA7B,CAAD,CAAR;AACA;AACD;;AAEDvB,IAAAA,QAAQ,CAAC,IAAD,CAAR;;AACA,QAAIP,SAAS,KAAK,KAAlB,EAAyB;AACvBC,MAAAA,QAAQ,CAACwB,SAAD,CAAR;AACD,KAFD,MAEO;AACLxB,MAAAA,QAAQ,CAACwB,SAAD,CAAR;AACD;AACF,GApED;;AAqEA,SAAO;AACLxB,IAAAA,QADK;AAELK,IAAAA,KAFK;AAGLK,IAAAA,cAHK;AAILE,IAAAA,YAJK;AAKLH,IAAAA;AALK,GAAP;AAOD","sourcesContent":["import { useInputFormat, useInputFormatter, useRangeChecker } from './dateUtils'\nimport * as React from 'react'\nimport type { ValidRangeType } from './Calendar'\nimport { getTranslation } from 'react-native-paper-dates'\n\nexport default function useDateInput({\n locale,\n value,\n validRange,\n inputMode,\n onChange,\n}: {\n onChange: (d: Date) => void\n locale: undefined | string\n value: Date | undefined\n validRange: ValidRangeType | undefined\n inputMode: 'start' | 'end'\n}) {\n const { isDisabled, isWithinValidRange, validStart, validEnd } =\n useRangeChecker(validRange)\n const [error, setError] = React.useState<null | string>(null)\n const formatter = useInputFormatter({ locale })\n const inputFormat = useInputFormat({ formatter, locale })\n const formattedValue = formatter.format(value)\n const onChangeText = (date: string) => {\n const dayIndex = inputFormat.indexOf('DD')\n const monthIndex = inputFormat.indexOf('MM')\n const yearIndex =\n locale === 'pt'\n ? inputFormat.indexOf('AAAA')\n : inputFormat.indexOf('YYYY')\n\n const day = Number(date.slice(dayIndex, dayIndex + 2))\n const year = Number(date.slice(yearIndex, yearIndex + 4))\n const month = Number(date.slice(monthIndex, monthIndex + 2))\n\n if (Number.isNaN(day) || Number.isNaN(year) || Number.isNaN(month)) {\n setError(\n getTranslation(\n locale,\n 'notAccordingToDateFormat',\n () => 'notAccordingToDateFormat'\n )(inputFormat)\n )\n return\n }\n\n const finalDate =\n inputMode === 'end'\n ? new Date(year, month - 1, day, 23, 59, 59)\n : new Date(year, month - 1, day)\n\n if (isDisabled(finalDate)) {\n setError(getTranslation(locale, 'dateIsDisabled'))\n return\n }\n if (!isWithinValidRange(finalDate)) {\n let errors =\n validStart && validEnd\n ? [\n `${getTranslation(\n locale,\n 'mustBeBetween',\n () => 'mustBeBetween'\n )(formatter.format(validStart), formatter.format(validEnd))}`,\n ]\n : [\n validStart\n ? getTranslation(\n locale,\n 'mustBeHigherThan',\n () => 'mustBeHigherThan'\n )(formatter.format(validStart))\n : '',\n validEnd\n ? getTranslation(\n locale,\n 'mustBeLowerThan',\n () => 'mustBeLowerThan'\n )(formatter.format(validEnd))\n : '',\n ]\n setError(errors.filter((n) => n).join(' '))\n return\n }\n\n setError(null)\n if (inputMode === 'end') {\n onChange(finalDate)\n } else {\n onChange(finalDate)\n }\n }\n return {\n onChange,\n error,\n formattedValue,\n onChangeText,\n inputFormat,\n }\n}\n"]}
|
|
@@ -49,7 +49,8 @@ export declare function useInputFormatter({ locale }: {
|
|
|
49
49
|
}): Intl.DateTimeFormat;
|
|
50
50
|
export declare function getStartOfDay(d: Date): Date;
|
|
51
51
|
export declare function getEndOfDay(d: Date): Date;
|
|
52
|
-
export declare function useInputFormat({ formatter, }: {
|
|
52
|
+
export declare function useInputFormat({ formatter, locale, }: {
|
|
53
53
|
formatter: Intl.DateTimeFormat;
|
|
54
|
+
locale: string | undefined;
|
|
54
55
|
}): string;
|
|
55
56
|
export declare function differenceInMonths(firstDate: Date, secondDate: Date): number;
|
package/package.json
CHANGED
package/src/Date/dateUtils.tsx
CHANGED
|
@@ -218,17 +218,19 @@ export function getEndOfDay(d: Date): Date {
|
|
|
218
218
|
}
|
|
219
219
|
export function useInputFormat({
|
|
220
220
|
formatter,
|
|
221
|
+
locale,
|
|
221
222
|
}: {
|
|
222
223
|
formatter: Intl.DateTimeFormat
|
|
224
|
+
locale: string | undefined
|
|
223
225
|
}) {
|
|
224
226
|
return React.useMemo(() => {
|
|
225
227
|
// TODO: something cleaner and more universal?
|
|
226
228
|
const inputDate = formatter.format(new Date(2020, 10 - 1, 1))
|
|
227
229
|
return inputDate
|
|
228
|
-
.replace('2020', 'YYYY')
|
|
230
|
+
.replace('2020', locale === 'pt' ? 'AAAA' : 'YYYY')
|
|
229
231
|
.replace('10', 'MM')
|
|
230
232
|
.replace('01', 'DD')
|
|
231
|
-
}, [formatter])
|
|
233
|
+
}, [formatter, locale])
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
export function differenceInMonths(firstDate: Date, secondDate: Date) {
|
package/src/Date/inputUtils.ts
CHANGED
|
@@ -20,12 +20,15 @@ export default function useDateInput({
|
|
|
20
20
|
useRangeChecker(validRange)
|
|
21
21
|
const [error, setError] = React.useState<null | string>(null)
|
|
22
22
|
const formatter = useInputFormatter({ locale })
|
|
23
|
-
const inputFormat = useInputFormat({ formatter })
|
|
23
|
+
const inputFormat = useInputFormat({ formatter, locale })
|
|
24
24
|
const formattedValue = formatter.format(value)
|
|
25
25
|
const onChangeText = (date: string) => {
|
|
26
26
|
const dayIndex = inputFormat.indexOf('DD')
|
|
27
27
|
const monthIndex = inputFormat.indexOf('MM')
|
|
28
|
-
const yearIndex =
|
|
28
|
+
const yearIndex =
|
|
29
|
+
locale === 'pt'
|
|
30
|
+
? inputFormat.indexOf('AAAA')
|
|
31
|
+
: inputFormat.indexOf('YYYY')
|
|
29
32
|
|
|
30
33
|
const day = Number(date.slice(dayIndex, dayIndex + 2))
|
|
31
34
|
const year = Number(date.slice(yearIndex, yearIndex + 4))
|