-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(date-input): modifying validation error containing date #3694
Changes from 5 commits
242a253
c6d9919
8ddc7cc
b3a971a
fadddd6
8ac47e4
7838f48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nextui-org/date-input": patch | ||
--- | ||
|
||
The Date mentioned in the Error message of the date-picker is not according to the Locale. This PR adds an layer to the validationError to ensure correct formatting date in error messages. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
export default { | ||
"ar-AE": { | ||
minValidationMessage: "يجب أن تكون القيمة {date} أو بعد ذلك", | ||
maxValidationMessage: "يجب أن تكون القيمة {date} أو قبل ذلك", | ||
}, | ||
"bg-BG": { | ||
minValidationMessage: "Стойността трябва да бъде {date} или по-късно", | ||
maxValidationMessage: "Стойността трябва да бъде {date} или по-рано", | ||
}, | ||
"cs-CZ": { | ||
minValidationMessage: "Hodnota musí být {date} nebo později", | ||
maxValidationMessage: "Hodnota musí být {date} nebo dříve", | ||
}, | ||
"da-DK": { | ||
minValidationMessage: "Værdien skal være {date} eller senere", | ||
maxValidationMessage: "Værdien skal være {date} eller tidligere", | ||
}, | ||
"de-DE": { | ||
minValidationMessage: "Der Wert muss {date} oder später sein", | ||
maxValidationMessage: "Der Wert muss {date} oder früher sein", | ||
}, | ||
"el-GR": { | ||
minValidationMessage: "Η τιμή πρέπει να είναι {date} ή αργότερα", | ||
maxValidationMessage: "Η τιμή πρέπει να είναι {date} ή νωρίτερα", | ||
}, | ||
"en-US": { | ||
minValidationMessage: "Value must be {date} or later", | ||
maxValidationMessage: "Value must be {date} or earlier", | ||
}, | ||
"es-ES": { | ||
minValidationMessage: "El valor debe ser {date} o posterior", | ||
maxValidationMessage: "El valor debe ser {date} o anterior", | ||
}, | ||
"et-EE": { | ||
minValidationMessage: "Väärtus peab olema {date} või hiljem", | ||
maxValidationMessage: "Väärtus peab olema {date} või varem", | ||
}, | ||
"fi-FI": { | ||
minValidationMessage: "Arvon on oltava {date} tai myöhemmin", | ||
maxValidationMessage: "Arvon on oltava {date} tai aikaisemmin", | ||
}, | ||
"fr-FR": { | ||
minValidationMessage: "La valeur doit être {date} ou ultérieure", | ||
maxValidationMessage: "La valeur doit être {date} ou antérieure", | ||
}, | ||
"he-IL": { | ||
minValidationMessage: "הערך חייב להיות {date} או מאוחר יותר", | ||
maxValidationMessage: "הערך חייב להיות {date} או קודם", | ||
}, | ||
"hr-HR": { | ||
minValidationMessage: "Vrijednost mora biti {date} ili kasnije", | ||
maxValidationMessage: "Vrijednost mora biti {date} ili ranije", | ||
}, | ||
"hu-HU": { | ||
minValidationMessage: "Az értéknek {date} vagy későbbi kell lennie", | ||
maxValidationMessage: "Az értéknek {date} vagy korábbi kell lennie", | ||
}, | ||
"it-IT": { | ||
minValidationMessage: "Il valore deve essere {date} o successivo", | ||
maxValidationMessage: "Il valore deve essere {date} o precedente", | ||
}, | ||
"ja-JP": { | ||
minValidationMessage: "値は{date}以降でなければなりません", | ||
maxValidationMessage: "{date}以前でなければなりません", | ||
}, | ||
"ko-KR": { | ||
minValidationMessage: "{date} 이후여야 합니다", | ||
maxValidationMessage: "{date} 이전이어야 합니다", | ||
}, | ||
"lt-LT": { | ||
minValidationMessage: "Vertė turi būti {date} arba vėliau", | ||
maxValidationMessage: "Vertė turi būti {date} arba ankstesnė", | ||
}, | ||
"lv-LV": { | ||
minValidationMessage: "Vērtībai jābūt {date} vai vēlāk", | ||
maxValidationMessage: "Vērtībai jābūt {date} vai agrāk", | ||
}, | ||
"nb-NO": { | ||
minValidationMessage: "Verdien må være {date} eller senere", | ||
maxValidationMessage: "Verdien må være {date} eller tidligere", | ||
}, | ||
"nl-NL": { | ||
minValidationMessage: "De waarde moet {date} of later zijn", | ||
maxValidationMessage: "De waarde moet {date} of eerder zijn", | ||
}, | ||
"pl-PL": { | ||
minValidationMessage: "Wartość musi być {date} lub późniejsza", | ||
maxValidationMessage: "Wartość musi być {date} lub wcześniejsza", | ||
}, | ||
"pt-BR": { | ||
minValidationMessage: "O valor deve ser {date} ou posterior", | ||
maxValidationMessage: "O valor deve ser {date} ou anterior", | ||
}, | ||
"pt-PT": { | ||
minValidationMessage: "O valor deve ser {date} ou mais tarde", | ||
maxValidationMessage: "O valor deve ser {date} ou antes", | ||
}, | ||
"ro-RO": { | ||
minValidationMessage: "Valoarea trebuie să fie {date} sau mai târziu", | ||
maxValidationMessage: "Valoarea trebuie să fie {date} sau mai devreme", | ||
}, | ||
"ru-RU": { | ||
minValidationMessage: "Значение должно быть {date} или позже", | ||
maxValidationMessage: "Значение должно быть {date} или раньше", | ||
}, | ||
"sk-SK": { | ||
minValidationMessage: "Hodnota musí byť {date} alebo neskôr", | ||
maxValidationMessage: "Hodnota musí byť {date} alebo skôr", | ||
}, | ||
"sl-SI": { | ||
minValidationMessage: "Vrednost mora biti {date} ali kasneje", | ||
maxValidationMessage: "Vrednost mora biti {date} ali prej", | ||
}, | ||
"sr-SP": { | ||
minValidationMessage: "Vrednost mora biti {date} ili kasnije", | ||
maxValidationMessage: "Vrednost mora biti {date} ili ranije", | ||
}, | ||
"sv-SE": { | ||
minValidationMessage: "Värdet måste vara {date} eller senare", | ||
maxValidationMessage: "Värdet måste vara {date} eller tidigare", | ||
}, | ||
"tr-TR": { | ||
minValidationMessage: "Değer {date} veya sonrasında olmalıdır", | ||
maxValidationMessage: "Değer {date} veya daha önce olmalıdır", | ||
}, | ||
"uk-UA": { | ||
minValidationMessage: "Значення повинно бути {date} або пізніше", | ||
maxValidationMessage: "Значення повинно бути {date} або раніше", | ||
}, | ||
"zh-CN": { | ||
minValidationMessage: "值必须为 {date} 或更晚", | ||
maxValidationMessage: "值必须为 {date} 或更早", | ||
}, | ||
"zh-TW": { | ||
minValidationMessage: "值必須為 {date} 或更晚", | ||
maxValidationMessage: "值必須為 {date} 或更早", | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import type {ReactRef} from "@nextui-org/react-utils"; | |
import type {DOMAttributes, GroupDOMAttributes} from "@react-types/shared"; | ||
import type {DateInputGroupProps} from "./date-input-group"; | ||
|
||
import {useLocale} from "@react-aria/i18n"; | ||
import {useLocale, useLocalizedStringFormatter} from "@react-aria/i18n"; | ||
import {createCalendar, CalendarDate, DateFormatter} from "@internationalized/date"; | ||
import {mergeProps} from "@react-aria/utils"; | ||
import {PropGetter, useProviderContext} from "@nextui-org/system"; | ||
|
@@ -18,6 +18,8 @@ import {objectToDeps, clsx, dataAttr, getGregorianYearOffset} from "@nextui-org/ | |
import {dateInput, cn} from "@nextui-org/theme"; | ||
import {useMemo} from "react"; | ||
|
||
import intlMessages from "../intl/messages"; | ||
|
||
type NextUIBaseProps<T extends DateValue> = Omit< | ||
HTMLNextUIProps<"div">, | ||
keyof AriaDateFieldProps<T> | "onChange" | ||
|
@@ -184,6 +186,48 @@ export function useDateInput<T extends DateValue>(originalProps: UseDateInputPro | |
isInvalid: ariaIsInvalid, | ||
} = useAriaDateField({...originalProps, label, validationBehavior, inputRef}, state, domRef); | ||
|
||
const stringFormatter = useLocalizedStringFormatter(intlMessages) as any; | ||
|
||
if (props.minValue != undefined && validationDetails.rangeUnderflow) { | ||
const indexInValidationErrors: number = | ||
Number(validationDetails.badInput) + | ||
Number(validationDetails.customError) + | ||
Number(validationDetails.patternMismatch); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to cater There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need But in case of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I know where did you get this info? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I assumed that the errors in the Also, while scrapping through RA code, I understood that if ValidationError has It is clear that if exists in ValidationError, the The reason why the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the following changes in 7838f48
|
||
const minValueDate = new Date( | ||
minValue.year, | ||
minValue.month - 1, | ||
minValue.day, | ||
).toLocaleDateString(locale); | ||
const timeZone = | ||
state.segments.filter((segment) => segment.type === "timeZoneName")[0]?.text ?? ""; | ||
const rangeUnderflow = stringFormatter | ||
.format("minValidationMessage") | ||
.replace("{date}", `${minValueDate} ${timeZone}`); | ||
|
||
validationErrors.splice(indexInValidationErrors, 1, rangeUnderflow); | ||
} | ||
|
||
if (props.maxValue != undefined && validationDetails.rangeOverflow) { | ||
const indexInValidationErrors: number = | ||
Number(validationDetails.badInput) + | ||
Number(validationDetails.customError) + | ||
Number(validationDetails.patternMismatch) + | ||
Number(validationDetails.rangeUnderflow); | ||
const maxValueDate = new Date( | ||
maxValue.year, | ||
maxValue.month - 1, | ||
maxValue.day, | ||
).toLocaleDateString(locale); | ||
const timeZone = | ||
state.segments.filter((segment) => segment.type === "timeZoneName")[0]?.text ?? ""; | ||
|
||
const rangeOverflow = stringFormatter | ||
.format("maxValidationMessage") | ||
.replace("{date}", `${maxValueDate} ${timeZone}`); | ||
|
||
validationErrors.splice(indexInValidationErrors, 1, rangeOverflow); | ||
} | ||
|
||
const baseStyles = clsx(classNames?.base, className); | ||
|
||
const isInvalid = isInvalidProp || ariaIsInvalid; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think
as any
is not required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in: 8ac47e4