Skip to content

Commit 9d69bd4

Browse files
feat: add more translations
1 parent 592ed34 commit 9d69bd4

File tree

9 files changed

+148
-109
lines changed

9 files changed

+148
-109
lines changed

example/src/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ function App() {
120120
? overlay(3, theme.colors.surface)
121121
: (theme.colors.surface as any)
122122

123-
const locale = 'nl'
123+
const pastDate = new Date(new Date().setDate(new Date().getDate() - 5))
124+
const futureDate = new Date(new Date().setDate(new Date().getDate() + 5))
125+
126+
const locale = 'en'
124127
return (
125128
<>
126129
<ScrollView
@@ -323,10 +326,12 @@ function App() {
323326
onDismiss={onDismissSingle}
324327
date={date}
325328
onConfirm={onChangeSingle}
326-
// validRange={{
327-
// startDate: new Date(2021, 1, 2), // optional
328-
// endDate: new Date(), // optional
329-
// }}
329+
validRange={{
330+
startDate: pastDate,
331+
disabledDates: [futureDate],
332+
// startDate: new Date(2021, 1, 2), // optional
333+
// endDate: new Date(), // optional
334+
}}
330335
// saveLabel="Save" // optional
331336
// label="Select date" // optional
332337
// animationType="slide" // optional, default is 'slide' on ios/android and 'none' on web

src/Date/Calendar.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
areDatesOnSameDay,
77
dateToUnix,
88
DisableWeekDaysType,
9+
getEndOfDay,
910
getInitialIndex,
1011
} from './dateUtils'
1112

@@ -129,7 +130,7 @@ function Calendar(
129130
(d: Date) => {
130131
if (mode === 'single') {
131132
;(onChangeRef.current as SingleChange)({
132-
date: dateMode === 'start' ? d : getEndDate(d),
133+
date: dateMode === 'start' ? d : getEndOfDay(d),
133134
})
134135
} else if (mode === 'range') {
135136
const sd = startDateRef.current
@@ -140,7 +141,7 @@ function Calendar(
140141
}
141142
;(onChangeRef.current as RangeChange)({
142143
startDate: isStart ? d : sd,
143-
endDate: !isStart ? getEndDate(d) : undefined,
144+
endDate: !isStart ? getEndOfDay(d) : undefined,
144145
})
145146
} else if (mode === 'multiple') {
146147
datesRef.current = datesRef.current || []
@@ -211,10 +212,6 @@ function Calendar(
211212
)
212213
}
213214

214-
function getEndDate(d: Date) {
215-
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)
216-
}
217-
218215
const styles = StyleSheet.create({
219216
root: { flex: 1 },
220217
viewPager: { flex: 1 },

src/Date/DatePickerInput.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,18 @@ function DatePickerInput(
4848
const onInnerConfirm = React.useCallback(
4949
({ date }) => {
5050
setVisible(false)
51-
if (date) {
52-
onChangeRef.current(date)
53-
}
51+
onChangeRef.current(date)
5452
},
5553
[setVisible, onChangeRef]
5654
)
5755

5856
return (
59-
<>
57+
<View>
6058
<View style={styles.root}>
6159
<TextInputWithMask
6260
{...rest}
6361
ref={ref}
64-
label={
65-
withDateFormatInLabel ? `${label || ''} (${inputFormat})` : label
66-
}
62+
label={getLabel({ label, inputFormat, withDateFormatInLabel })}
6763
value={formattedValue}
6864
keyboardType={'number-pad'}
6965
placeholder={inputFormat}
@@ -85,6 +81,7 @@ function DatePickerInput(
8581
<HelperText type="error" visible={!!error}>
8682
{error}
8783
</HelperText>
84+
8885
{withModal ? (
8986
<DatePickerModal
9087
date={value}
@@ -96,10 +93,25 @@ function DatePickerInput(
9693
dateMode={inputMode}
9794
/>
9895
) : null}
99-
</>
96+
</View>
10097
)
10198
}
10299

100+
function getLabel({
101+
withDateFormatInLabel,
102+
inputFormat,
103+
label,
104+
}: {
105+
withDateFormatInLabel: boolean
106+
inputFormat: string
107+
label: string | undefined
108+
}) {
109+
if (withDateFormatInLabel) {
110+
return label ? `${label} (${inputFormat})` : inputFormat
111+
}
112+
return label || ''
113+
}
114+
103115
const styles = StyleSheet.create({
104116
root: {
105117
flexDirection: 'row',

src/Date/Month.tsx

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {
1818
startAtIndex,
1919
beginOffset,
2020
estimatedMonthHeight,
21-
isDateWithinOptionalRange,
22-
dateToUnix,
21+
useRangeChecker,
2322
} from './dateUtils'
2423
import { getCalendarHeaderHeight } from './CalendarHeader'
2524
import type {
@@ -91,39 +90,7 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
9190
const textColorOnPrimary = useTextColorOnPrimary()
9291
const realIndex = getRealIndex(index)
9392
const isHorizontal = scrollMode === 'horizontal'
94-
95-
const validRangeStart =
96-
validRange?.startDate instanceof Date
97-
? dateToUnix(
98-
new Date(
99-
validRange?.startDate.getFullYear(),
100-
validRange?.startDate.getMonth(),
101-
validRange?.startDate.getDate(),
102-
0,
103-
0,
104-
0
105-
)
106-
)
107-
: undefined
108-
109-
const validRangeEnd =
110-
validRange?.endDate instanceof Date
111-
? dateToUnix(
112-
new Date(
113-
validRange?.endDate.getFullYear(),
114-
validRange?.endDate.getMonth(),
115-
validRange?.endDate.getDate(),
116-
23,
117-
59,
118-
59
119-
)
120-
)
121-
: undefined
122-
123-
const validDisabledDates = validRange?.disabledDates
124-
? validRange?.disabledDates
125-
: undefined
126-
93+
const { isDisabled, isWithinValidRange } = useRangeChecker(validRange)
12794
const { monthName, month, year } = React.useMemo(() => {
12895
const md = addMonths(new Date(), realIndex)
12996
const y = md.getFullYear()
@@ -155,24 +122,15 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
155122
const isToday = areDatesOnSameDay(day, today)
156123

157124
let inRange = false
158-
let disabled = false
125+
let disabled = isDisabled(day)
159126
let selected = false
160-
let inDisabledDates = false
127+
161128
let leftCrop = dayOfMonth === 1
162129
let rightCrop = dayOfMonth === daysInMonth
163130

164131
const isFirstDayOfMonth = dayOfMonth === 1
165132
const isLastDayOfMonth = dayOfMonth === daysInMonth
166133

167-
inDisabledDates = validDisabledDates
168-
? validDisabledDates.some((disabledDate) =>
169-
areDatesOnSameDay(disabledDate, day)
170-
)
171-
: false
172-
if (inDisabledDates) {
173-
disabled = true
174-
}
175-
176134
if (mode === 'range') {
177135
const selectedStartDay = areDatesOnSameDay(day, startDate)
178136
const selectedEndDay = areDatesOnSameDay(day, endDate)
@@ -241,18 +199,13 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
241199
selected = false
242200
}
243201
}
244-
245-
//
246202
} else if (mode === 'single') {
247203
selected = areDatesOnSameDay(day, date)
248204
}
249-
//
250-
const isWithinOptionalValidRange = isDateWithinOptionalRange(day, {
251-
startUnix: validRangeStart,
252-
endUnix: validRangeEnd,
253-
})
254205

255-
if (inRange && !inDisabledDates) {
206+
const isWithinOptionalValidRange = isWithinValidRange(day)
207+
208+
if (inRange && !disabled) {
256209
disabled = false
257210
}
258211

@@ -282,14 +235,13 @@ function Month(props: MonthSingleProps | MonthRangeProps | MonthMultiProps) {
282235
year,
283236
month,
284237
index,
238+
isDisabled,
239+
mode,
240+
isWithinValidRange,
285241
startDate,
286242
endDate,
287-
date,
288243
dates,
289-
validRangeStart,
290-
validRangeEnd,
291-
validDisabledDates,
292-
mode,
244+
date,
293245
])
294246

295247
return (

src/Date/dateUtils.tsx

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as React from 'react'
2+
import { useLatest } from '../utils'
3+
import type { ValidRangeType } from './Calendar'
24

35
export type DisableWeekDaysType = number[]
46

@@ -63,15 +65,42 @@ export function getFirstDayOfMonth({
6365
return new Date(year, month, 1).getDay()
6466
}
6567

66-
// export function getLastDayOfMonth({
67-
// year,
68-
// month,
69-
// }: {
70-
// year: number
71-
// month: number
72-
// }): number {
73-
// return new Date(year, month, getDaysInMonth({ year, month })).getDay()
74-
// }
68+
export function useRangeChecker(validRange: ValidRangeType | undefined) {
69+
const validStart = validRange?.startDate
70+
const validEnd = validRange?.endDate
71+
const startUnix =
72+
validStart instanceof Date
73+
? dateToUnix(getStartOfDay(validStart))
74+
: undefined
75+
76+
const endUnix =
77+
validEnd instanceof Date ? dateToUnix(getEndOfDay(validEnd)) : undefined
78+
79+
const validDisabledDatesRef = useLatest(validRange?.disabledDates)
80+
81+
const isWithinValidRange = React.useCallback(
82+
(day: Date) => {
83+
return isDateWithinOptionalRange(day, {
84+
startUnix: startUnix,
85+
endUnix: endUnix,
86+
})
87+
},
88+
[startUnix, endUnix]
89+
)
90+
91+
const isDisabled = React.useCallback(
92+
(day: Date) => {
93+
return validDisabledDatesRef.current
94+
? validDisabledDatesRef.current.some((disabledDate) =>
95+
areDatesOnSameDay(disabledDate, day)
96+
)
97+
: false
98+
},
99+
[validDisabledDatesRef]
100+
)
101+
102+
return { isDisabled, isWithinValidRange, validStart, validEnd }
103+
}
75104

76105
export function areDatesOnSameDay(a: Date, b?: Date | null | undefined) {
77106
if (!b) {
@@ -181,7 +210,12 @@ export function useInputFormatter({ locale }: { locale: string | undefined }) {
181210
})
182211
}, [locale])
183212
}
184-
213+
export function getStartOfDay(d: Date): Date {
214+
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0)
215+
}
216+
export function getEndOfDay(d: Date): Date {
217+
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)
218+
}
185219
export function useInputFormat({
186220
formatter,
187221
}: {

src/Date/inputUtils.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import {
2-
dateToUnix,
3-
isDateWithinOptionalRange,
4-
useInputFormat,
5-
useInputFormatter,
6-
} from './dateUtils'
1+
import { useInputFormat, useInputFormatter, useRangeChecker } from './dateUtils'
72
import * as React from 'react'
83
import type { ValidRangeType } from './Calendar'
4+
import { getTranslation } from 'react-native-paper-dates'
95

106
export default function useDateInput({
117
locale,
@@ -20,6 +16,8 @@ export default function useDateInput({
2016
validRange: ValidRangeType | undefined
2117
inputMode: 'start' | 'end'
2218
}) {
19+
const { isDisabled, isWithinValidRange, validStart, validEnd } =
20+
useRangeChecker(validRange)
2321
const [error, setError] = React.useState<null | string>(null)
2422
const formatter = useInputFormatter({ locale })
2523
const inputFormat = useInputFormat({ formatter })
@@ -34,7 +32,13 @@ export default function useDateInput({
3432
const month = Number(date.slice(monthIndex, monthIndex + 2))
3533

3634
if (Number.isNaN(day) || Number.isNaN(year) || Number.isNaN(month)) {
37-
setError(inputFormat)
35+
setError(
36+
getTranslation(
37+
locale,
38+
'notAccordingToDateFormat',
39+
() => 'notAccordingToDateFormat'
40+
)(inputFormat)
41+
)
3842
return
3943
}
4044

@@ -43,20 +47,31 @@ export default function useDateInput({
4347
? new Date(year, month - 1, day, 23, 59, 59)
4448
: new Date(year, month - 1, day)
4549

46-
const validStart = validRange?.startDate
47-
const validEnd = validRange?.endDate
48-
if (
49-
!isDateWithinOptionalRange(finalDate, {
50-
startUnix: validStart ? dateToUnix(validStart) : undefined,
51-
endUnix: validEnd ? dateToUnix(validEnd) : undefined,
52-
})
53-
) {
50+
if (isDisabled(finalDate)) {
51+
setError(getTranslation(locale, 'dateIsDisabled'))
52+
return
53+
}
54+
if (!isWithinValidRange(finalDate)) {
5455
let errors =
5556
validStart && validEnd
56-
? [`${formatter.format(validStart)} - ${formatter.format(validEnd)}`]
57+
? [
58+
`${getTranslation(locale, 'mustBeBetween')} ${formatter.format(
59+
validStart
60+
)} - ${formatter.format(validEnd)}`,
61+
]
5762
: [
58-
validStart ? `> ${formatter.format(validStart)}` : '',
59-
validEnd ? `< ${formatter.format(validEnd)}` : '',
63+
validStart
64+
? `${getTranslation(
65+
locale,
66+
'mustBeHigherThan'
67+
)} ${formatter.format(validStart)}`
68+
: '',
69+
validEnd
70+
? `${getTranslation(
71+
locale,
72+
'mustBeLowerThan'
73+
)} ${formatter.format(validEnd)}`
74+
: '',
6075
]
6176
setError(errors.filter((n) => n).join(' '))
6277
return

0 commit comments

Comments
 (0)