Skip to content

Commit d4b8282

Browse files
Merge pull request #51 from chakrihacker/fix/date-picker-crash
Fixed the crash on input mode
2 parents b135125 + 21504d9 commit d4b8282

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

src/Date/CalendarEdit.tsx

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { CalendarDate, ModeType } from './Calendar'
1010
import { LocalState } from './DatePickerModalContent'
1111
import TextInputWithMask from '../TextInputMask'
12-
import { useTheme } from 'react-native-paper'
12+
import { Text, useTheme } from 'react-native-paper'
1313

1414
function CalendarEdit({
1515
mode,
@@ -129,6 +129,7 @@ function CalendarInputPure(
129129
ref: any
130130
) {
131131
const theme = useTheme()
132+
const [error, setError] = React.useState(false)
132133
const formatter = React.useMemo(() => {
133134
return new Intl.DateTimeFormat(locale, {
134135
month: '2-digit',
@@ -155,26 +156,46 @@ function CalendarInputPure(
155156
const day = Number(date.slice(dayIndex, dayIndex + 2))
156157
const year = Number(date.slice(yearIndex, yearIndex + 4))
157158
const month = Number(date.slice(monthIndex, monthIndex + 2))
158-
if (isEndDate) {
159-
onChange(new Date(year, month - 1, day, 23, 59, 59))
159+
if (!Number.isNaN(day) && !Number.isNaN(year) && !Number.isNaN(month)) {
160+
setError(false)
161+
if (isEndDate) {
162+
onChange(new Date(year, month - 1, day, 23, 59, 59))
163+
} else {
164+
onChange(new Date(year, month - 1, day))
165+
}
160166
} else {
161-
onChange(new Date(year, month - 1, day))
167+
setError(true)
162168
}
163169
}
164170
return (
165-
<TextInputWithMask
166-
ref={ref}
167-
value={formattedValue}
168-
style={styles.input}
169-
label={`${label} (${inputFormat})`}
170-
keyboardType={'number-pad'}
171-
placeholder={inputFormat}
172-
mask={inputFormat}
173-
onChangeText={onChangeText}
174-
returnKeyType={returnKeyType}
175-
onSubmitEditing={onSubmitEditing}
176-
keyboardAppearance={theme.dark ? 'dark' : 'default'}
177-
/>
171+
<View style={styles.inputContainer}>
172+
<TextInputWithMask
173+
ref={ref}
174+
value={formattedValue}
175+
style={styles.input}
176+
label={`${label} (${inputFormat})`}
177+
keyboardType={'number-pad'}
178+
placeholder={inputFormat}
179+
mask={inputFormat}
180+
onChangeText={onChangeText}
181+
returnKeyType={returnKeyType}
182+
onSubmitEditing={onSubmitEditing}
183+
keyboardAppearance={theme.dark ? 'dark' : 'default'}
184+
/>
185+
{error && (
186+
<Text
187+
style={[
188+
{
189+
color: theme.colors.error,
190+
},
191+
theme.fonts.medium,
192+
styles.errorText,
193+
]}
194+
>
195+
{inputFormat}
196+
</Text>
197+
)}
198+
</View>
178199
)
179200
}
180201

@@ -183,8 +204,13 @@ const CalendarInput = React.forwardRef(CalendarInputPure)
183204
const styles = StyleSheet.create({
184205
root: { padding: 12 },
185206
inner: { flexDirection: 'row' },
207+
inputContainer: { flex: 1 },
186208
input: { flex: 1 },
187209
separator: { width: 12 },
210+
errorText: {
211+
textAlign: 'right',
212+
marginTop: 12,
213+
},
188214
})
189215

190216
export default React.memo(CalendarEdit)

0 commit comments

Comments
 (0)