Skip to content

Commit d0e199a

Browse files
committed
Introduce new date input props and update docs.
1 parent 149f55f commit d0e199a

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

docusaurus/docs/date-picker/input-date-picker.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,20 @@ The type of input needed for the the picker component.
6363
`Type: 'flat' | 'outlined'`
6464
See [react-native-paper text-input](https://callstack.github.io/react-native-paper/text-input.html#mode).
6565

66+
**withDateFormatInLabel**
67+
`Type: boolean | undefined`
68+
Flag indicating if the date format should be inside the components label.
69+
70+
**hasError**
71+
`Type: boolean | undefined`
72+
Flag indicating if the the component should display error styles.
73+
74+
**hideValidationErrors**
75+
`Type: boolean | undefined`
76+
Flag indicating if the the component should hide error styles along with the `helperText` component displaying the error message.
77+
78+
**onValidationError**
79+
`Type: Function | undefined`
80+
Callback used to return any error messages from the components validation.
81+
6682
* Other [react-native TextInput props](https://reactnative.dev/docs/textinput#props).*

src/Date/DatePickerInput.shared.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type DatePickerInputProps = {
1010
validRange?: ValidRangeType | undefined
1111
withModal?: boolean
1212
withDateFormatInLabel?: boolean
13+
hideValidationErrors?: boolean
14+
hasError?: boolean
15+
onValidationError?: ((error: string) => void) | undefined
1316
calendarIcon?: string
1417
saveLabel?: string
1518
} & Omit<

src/Date/DatePickerInputWithoutModal.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function DatePickerInputWithoutModal(
1616
validRange,
1717
inputMode,
1818
withDateFormatInLabel = true,
19+
hasError,
20+
hideValidationErrors,
21+
onValidationError,
1922
modal,
2023
inputButtons,
2124
saveLabel,
@@ -39,6 +42,7 @@ function DatePickerInputWithoutModal(
3942
validRange,
4043
inputMode,
4144
onChange,
45+
onValidationError,
4246
})
4347

4448
return (
@@ -56,16 +60,15 @@ function DatePickerInputWithoutModal(
5660
})}
5761
value={formattedValue}
5862
keyboardType={'number-pad'}
59-
placeholder={inputFormat}
6063
mask={inputFormat}
6164
onChangeText={onChangeText}
6265
keyboardAppearance={theme.dark ? 'dark' : 'default'}
63-
error={!!error}
66+
error={(!!error && !hideValidationErrors) || !!hasError}
6467
style={[styles.input, style]}
6568
/>
6669
{inputButtons}
6770
</View>
68-
{error ? (
71+
{error && !hideValidationErrors ? (
6972
<HelperText style={styles.helperText} type="error" visible={!!error}>
7073
{error}
7174
</HelperText>

src/Date/inputUtils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ export default function useDateInput({
99
validRange,
1010
inputMode,
1111
onChange,
12+
onValidationError,
1213
}: {
1314
onChange: (d: Date) => void
1415
locale: undefined | string
1516
value: Date | undefined
1617
validRange: ValidRangeType | undefined
1718
inputMode: 'start' | 'end'
19+
onValidationError?: ((error: string) => void) | undefined
1820
}) {
1921
const { isDisabled, isWithinValidRange, validStart, validEnd } =
2022
useRangeChecker(validRange)
@@ -35,13 +37,13 @@ export default function useDateInput({
3537
const month = Number(date.slice(monthIndex, monthIndex + 2))
3638

3739
if (Number.isNaN(day) || Number.isNaN(year) || Number.isNaN(month)) {
38-
setError(
39-
getTranslation(
40-
locale,
41-
'notAccordingToDateFormat',
42-
() => 'notAccordingToDateFormat'
43-
)(inputFormat)
44-
)
40+
const inputError = getTranslation(
41+
locale,
42+
'notAccordingToDateFormat',
43+
() => 'notAccordingToDateFormat'
44+
)(inputFormat)
45+
setError(inputError)
46+
onValidationError(inputError)
4547
return
4648
}
4749

@@ -51,7 +53,9 @@ export default function useDateInput({
5153
: new Date(year, month - 1, day)
5254

5355
if (isDisabled(finalDate)) {
54-
setError(getTranslation(locale, 'dateIsDisabled'))
56+
const inputError = getTranslation(locale, 'dateIsDisabled')
57+
setError(inputError)
58+
onValidationError(inputError)
5559
return
5660
}
5761
if (!isWithinValidRange(finalDate)) {
@@ -80,11 +84,14 @@ export default function useDateInput({
8084
)(formatter.format(validEnd))
8185
: '',
8286
]
87+
const inputError = errors.filter((n) => n).join(' ')
8388
setError(errors.filter((n) => n).join(' '))
89+
onValidationError(inputError)
8490
return
8591
}
8692

8793
setError(null)
94+
onValidationError(null)
8895
if (inputMode === 'end') {
8996
onChange(finalDate)
9097
} else {

0 commit comments

Comments
 (0)