( value: DateValue | null, minValue: DateValue | null | undefined, maxValue: DateValue | null | undefined, isDateUnavailable: ((v: DateValue) => boolean) | undefined, options: FormatterOptions )
| 45 | } |
| 46 | |
| 47 | export function getValidationResult( |
| 48 | value: DateValue | null, |
| 49 | minValue: DateValue | null | undefined, |
| 50 | maxValue: DateValue | null | undefined, |
| 51 | isDateUnavailable: ((v: DateValue) => boolean) | undefined, |
| 52 | options: FormatterOptions |
| 53 | ): ValidationResult { |
| 54 | let rangeOverflow = value != null && maxValue != null && value.compare(maxValue) > 0; |
| 55 | let rangeUnderflow = value != null && minValue != null && value.compare(minValue) < 0; |
| 56 | let isUnavailable = (value != null && isDateUnavailable?.(value)) || false; |
| 57 | let isInvalid = rangeOverflow || rangeUnderflow || isUnavailable; |
| 58 | let errors: string[] = []; |
| 59 | |
| 60 | if (isInvalid) { |
| 61 | let locale = getLocale(); |
| 62 | let strings = |
| 63 | LocalizedStringDictionary.getGlobalDictionaryForPackage('@react-stately/datepicker') || |
| 64 | dictionary; |
| 65 | let formatter = new LocalizedStringFormatter(locale, strings); |
| 66 | let dateFormatter = new DateFormatter(locale, getFormatOptions({}, options)); |
| 67 | let timeZone = dateFormatter.resolvedOptions().timeZone; |
| 68 | |
| 69 | if (rangeUnderflow && minValue != null) { |
| 70 | errors.push( |
| 71 | formatter.format('rangeUnderflow', { |
| 72 | minValue: dateFormatter.format(minValue.toDate(timeZone)) |
| 73 | }) |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | if (rangeOverflow && maxValue != null) { |
| 78 | errors.push( |
| 79 | formatter.format('rangeOverflow', { |
| 80 | maxValue: dateFormatter.format(maxValue.toDate(timeZone)) |
| 81 | }) |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | if (isUnavailable) { |
| 86 | errors.push(formatter.format('unavailableDate')); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return { |
| 91 | isInvalid, |
| 92 | validationErrors: errors, |
| 93 | validationDetails: { |
| 94 | badInput: isUnavailable, |
| 95 | customError: false, |
| 96 | patternMismatch: false, |
| 97 | rangeOverflow, |
| 98 | rangeUnderflow, |
| 99 | stepMismatch: false, |
| 100 | tooLong: false, |
| 101 | tooShort: false, |
| 102 | typeMismatch: false, |
| 103 | valueMissing: false, |
| 104 | valid: !isInvalid |
no test coverage detected