Skip to content

Commit d91abd8

Browse files
feat: add date picker input
1 parent c1dd7f0 commit d91abd8

File tree

16 files changed

+294
-226
lines changed

16 files changed

+294
-226
lines changed

README.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
- Smooth and fast cross platform Material Design **date** picker and **time** picker for ([react-native-paper](https://callstack.github.io/react-native-paper/))
1515
- Tested on Android, iOS and the web
16-
- Uses the native Date.Intl API's which work out of the box on the web / iOS (automatic day name, month translations without bundle size increase)
17-
- For Android Intl support please follow the [android-caveats guide](https://github.com/web-ridge/react-native-paper-dates#android-caveats)
16+
- Uses the native Date.Intl API's which work out of the box on the web / iOS an on Android with Hermes from RN version 0.66 (automatic day name, month translations without bundle size increase)
17+
- For RN below 0.66 see for Android Intl support the [android-caveats guide](https://github.com/web-ridge/react-native-paper-dates#android-caveats)
1818
- Simple API
1919
- Typesafe
2020
- Endless (virtual) scrolling
@@ -282,19 +282,14 @@ This is to prevent the need to press 2 times before save or close button in moda
282282
[React Native Issue: #10138](https://github.com/facebook/react-native/issues/10138)
283283

284284
## Android Caveats
285-
286-
287-
285+
We recommend Hermes with React Native >= 0.66 you won't need these polyfills at all!
286+
### Below React Native 0.66
288287
You will need to add a polyfill for the Intl API on Android if:
289288

290-
- You have [Hermes](https://github.com/facebook/hermes/issues/23) enabled
289+
- You have [Hermes](https://github.com/facebook/hermes/issues/23) enabled and are below React Native 0.66
291290
- You have [Hermes](https://github.com/facebook/hermes/issues/23) disabled and you want to support locales outside of en-US and you don't have the org.webkit:android-jsc-intl:+ variant enabled in your app/build.gradle
292291

293292

294-
*update*: [Hermes is planning on native Intl support in version 0.65!](https://github.com/facebook/hermes/issues/23#issuecomment-816126715) when that's released we won't need any polyfills anymore!
295-
296-
But for now.
297-
298293
Install polyfills with Yarn
299294

300295
```

example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { registerRootComponent } from 'expo'
22

3-
import App from './src/'
3+
import Index from './src'
44

55
// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
66
// It also ensures that whether you load the app in the Expo client or in a native build,
77
// the environment is set up appropriately
8-
registerRootComponent(App)
8+
registerRootComponent(Index)

example/src/App.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import {
2626
DatePickerModal,
2727
DatePickerModalContent,
2828
TimePickerModal,
29-
// @ts-ignore
29+
DatePickerInput,
30+
// @ts-ignore TODO: try to fix expo to work with local library
3031
} from 'react-native-paper-dates'
31-
import DatePickerInput from '../../src/Date/DatePickerInput'
3232

3333
function App() {
34-
// I18nManager.forceRTL(true);
3534
const theme = useTheme()
3635
const dateFormatter = new Intl.DateTimeFormat(undefined, {
3736
day: 'numeric',
@@ -43,6 +42,8 @@ function App() {
4342
minute: '2-digit',
4443
hour12: false,
4544
})
45+
const [inputDate, setInputDate] = React.useState<Date | undefined>(undefined)
46+
4647
const [date, setDate] = React.useState<Date | undefined>(undefined)
4748
const [dates, setDates] = React.useState<Date[] | undefined>()
4849
const [range, setRange] = React.useState<{
@@ -119,6 +120,7 @@ function App() {
119120
? overlay(3, theme.colors.surface)
120121
: (theme.colors.surface as any)
121122

123+
const locale = 'nl'
122124
return (
123125
<>
124126
<ScrollView
@@ -188,7 +190,12 @@ function App() {
188190
<Row>
189191
<Label>Input</Label>
190192
<Text>
191-
<DatePickerInput />
193+
<DatePickerInput
194+
locale={locale}
195+
value={inputDate}
196+
onChange={setInputDate}
197+
inputMode="start"
198+
/>
192199
</Text>
193200
</Row>
194201
<Row>
@@ -282,7 +289,7 @@ function App() {
282289
{customOpen ? (
283290
<View style={[StyleSheet.absoluteFill, styles.customModal]}>
284291
<DatePickerModalContent
285-
// locale={'en'} optional, default: automatic
292+
locale={locale}
286293
mode="range"
287294
onDismiss={onDismissCustom}
288295
startDate={range.startDate}
@@ -294,7 +301,7 @@ function App() {
294301
</Portal>
295302

296303
<DatePickerModal
297-
// locale={'en'} optional, default: automatic
304+
locale={locale}
298305
mode="range"
299306
visible={rangeOpen}
300307
onDismiss={onDismissRange}
@@ -310,7 +317,7 @@ function App() {
310317
/>
311318

312319
<DatePickerModal
313-
// locale={'en'} optional, default: automatic
320+
locale={locale}
314321
mode="single"
315322
visible={singleOpen}
316323
onDismiss={onDismissSingle}
@@ -326,7 +333,7 @@ function App() {
326333
/>
327334

328335
<DatePickerModal
329-
// locale={'en'} optional, default: automatic
336+
locale={locale}
330337
mode="multiple"
331338
visible={multiOpen}
332339
onDismiss={onDismissMulti}
@@ -343,7 +350,7 @@ function App() {
343350
/>
344351

345352
<TimePickerModal
346-
locale={'nl'} //optional, default: automatic
353+
locale={locale}
347354
visible={timeOpen}
348355
onDismiss={onDismissTime}
349356
onConfirm={onConfirmTime}
@@ -435,7 +442,7 @@ const styles = StyleSheet.create({
435442
marginTop: 24,
436443
padding: 24,
437444
alignSelf: 'center',
438-
flex: 1,
445+
// flex: 1,
439446
},
440447
contentInline: {
441448
padding: 0,

example/src/index.android.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

example/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
import './App'
1+
import {
2+
en,
3+
nl,
4+
registerTranslation,
5+
// @ts-ignore TODO: try to fix expo to work with local library
6+
} from 'react-native-paper-dates'
7+
registerTranslation('en', en)
8+
registerTranslation('nl', nl)
9+
10+
import App from './App'
11+
export default App

src/Date/Calendar.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as React from 'react'
22
import { StyleSheet, View } from 'react-native'
3-
43
import Swiper from './Swiper'
5-
64
import Month from './Month'
75
import {
86
areDatesOnSameDay,
@@ -37,6 +35,7 @@ export type BaseCalendarProps = {
3735
dates?: CalendarDates
3836
startDate?: CalendarDate
3937
endDate?: CalendarDate
38+
dateMode?: 'start' | 'end'
4039
}
4140

4241
export type CalendarDate = Date | undefined
@@ -47,15 +46,15 @@ export type RangeChange = (params: {
4746
endDate: CalendarDate
4847
}) => any
4948

50-
export type SingleChange = (params: { date: CalendarDate }) => any
49+
export type SingleChange = (params: { date: CalendarDate }) => void
5150

5251
export type MultiChange = (params: {
5352
dates: CalendarDates
5453
datePressed: Date
5554
change: 'added' | 'removed'
5655
}) => any
5756

58-
export type MultiConfirm = (params: { dates: Date[] }) => any
57+
export type MultiConfirm = (params: { dates: Date[] }) => void
5958

6059
export interface CalendarSingleProps extends BaseCalendarProps {
6160
mode: 'single'
@@ -89,6 +88,7 @@ function Calendar(
8988
disableWeekDays,
9089
dates,
9190
validRange,
91+
dateMode,
9292
} = props
9393

9494
const theme = useTheme()
@@ -128,7 +128,7 @@ function Calendar(
128128
(d: Date) => {
129129
if (mode === 'single') {
130130
;(onChangeRef.current as SingleChange)({
131-
date: d,
131+
date: dateMode === 'start' ? d : getEndDate(d),
132132
})
133133
} else if (mode === 'range') {
134134
const sd = startDateRef.current
@@ -139,9 +139,7 @@ function Calendar(
139139
}
140140
;(onChangeRef.current as RangeChange)({
141141
startDate: isStart ? d : sd,
142-
endDate: !isStart
143-
? new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)
144-
: undefined,
142+
endDate: !isStart ? getEndDate(d) : undefined,
145143
})
146144
} else if (mode === 'multiple') {
147145
datesRef.current = datesRef.current || []
@@ -159,10 +157,11 @@ function Calendar(
159157
})
160158
}
161159
},
162-
[mode, onChangeRef, startDateRef, endDateRef, datesRef]
160+
[mode, dateMode, onChangeRef, startDateRef, endDateRef, datesRef]
163161
)
164162

165163
const firstDate = startDate || date || dates?.[0]
164+
166165
return (
167166
<View style={styles.root}>
168167
<Swiper
@@ -211,6 +210,10 @@ function Calendar(
211210
)
212211
}
213212

213+
function getEndDate(d: Date) {
214+
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59)
215+
}
216+
214217
const styles = StyleSheet.create({
215218
root: { flex: 1 },
216219
viewPager: { flex: 1 },

0 commit comments

Comments
 (0)