@popsure/dirty-swan 0.49.1 → 0.49.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,6 +37,8 @@ export interface DateSelectorProps {
37
37
  year?: string;
38
38
  yearFormat?: string;
39
39
  error?: string;
40
+ errorBeforeMinYear?: string;
41
+ errorAfterMaxYear?: string;
40
42
  };
41
43
  firstDayOfWeek?: number;
42
44
  inputProps?: (key: keyof CalendarDate) => Partial<DateSelectorInputProps>;
@@ -52,34 +54,37 @@ const defaultPlaceholders: DateSelectorProps["placeholders"] = {
52
54
  error: "Please enter a valid date"
53
55
  }
54
56
 
55
- type ErrorField = 'all' | 'day' | 'month' | 'year' | undefined;
57
+ type ErrorType = 'afterMax' | 'beforeMin' | 'default';
56
58
 
57
59
  const isDateValid = (
58
60
  date: string | undefined,
59
- yearBoundaries: DateSelectorProps["yearBoundaries"]
61
+ yearBoundaries: DateSelectorProps["yearBoundaries"],
62
+ dateObject: Partial<CalendarDate>,
60
63
  ): {
61
64
  isValid: boolean;
62
- field?: ErrorField;
65
+ errorType?: ErrorType;
63
66
  } => {
64
67
  const { min = 0, max = 0 } = yearBoundaries;
65
-
68
+
66
69
  if (!date) {
67
- return { isValid: false, field: 'all' };
70
+ return { isValid: false, errorType: 'default' };
68
71
  }
69
72
 
73
+ const isValidYear = dateObject?.year && String(dateObject?.year).length === 4;
74
+
70
75
  if (max && dayjs(date).isAfter(`${max}-01-01`, 'year')) {
71
- return { isValid: false, field: 'year' };
76
+ return { isValid: false, errorType: isValidYear ? 'afterMax' : 'default' };
72
77
  }
73
78
 
74
79
  if (min && dayjs(date).isBefore(`${min}-01-01`, 'year')) {
75
- return { isValid: false, field: 'year' };
80
+ return { isValid: false, errorType: isValidYear ? 'beforeMin' : 'default' };
76
81
  }
77
82
 
78
- const isDateValid = dayjs(date, COLLECTABLE_DATE_FORMAT, true).isValid();
83
+ const isValidDate = dayjs(date, COLLECTABLE_DATE_FORMAT, true).isValid();
79
84
 
80
85
  return {
81
- isValid: isDateValid,
82
- field: isDateValid ? undefined : 'all',
86
+ isValid: isValidDate,
87
+ errorType: 'default'
83
88
  };
84
89
  }
85
90
 
@@ -100,7 +105,7 @@ export const DateSelector = ({
100
105
 
101
106
  const itemsRef = useRef<HTMLInputElement[]>([]);
102
107
  const [isDirty, setIsDirty] = useState(false);
103
- const [hasError, setHasError] = useState<ErrorField>();
108
+ const [hasError, setHasError] = useState<ErrorType>();
104
109
  const [isCalendarOpen, setIsCalendarOpen] = useState(false);
105
110
  const [internalValue, setInternalValue] = useState<Partial<CalendarDate>>({});
106
111
 
@@ -108,9 +113,9 @@ export const DateSelector = ({
108
113
  const calendarDateValue = value ? isoStringtoCalendarDate(value) : undefined;
109
114
 
110
115
  if(value !== calendarDateValue && calendarDateValue?.day && calendarDateValue?.month && calendarDateValue?.year) {
111
- const { isValid, field } = isDateValid(value, yearBoundaries)
116
+ const { isValid, errorType } = isDateValid(value, yearBoundaries, calendarDateValue)
112
117
  setInternalValue(calendarDateValue)
113
- setHasError(isValid ? undefined : field);
118
+ setHasError(isValid ? undefined : errorType);
114
119
  setIsDirty(true);
115
120
  }
116
121
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -127,15 +132,19 @@ export const DateSelector = ({
127
132
  year: tempValue.year || 0,
128
133
  })
129
134
 
130
- const { isValid, field } = isDateValid(formattedDate, yearBoundaries);
131
-
132
- if (dayjs(formattedDate, COLLECTABLE_DATE_FORMAT, true).isValid()) {
135
+ const { isValid, errorType } = isDateValid(formattedDate, yearBoundaries, tempValue);
136
+ const isDateInValidFormat = dayjs(formattedDate, COLLECTABLE_DATE_FORMAT, true).isValid();
137
+
138
+ if (isDateInValidFormat) {
133
139
  setIsDirty(true);
134
140
  }
135
141
 
136
- setHasError(isValid ? undefined : field);
137
- onChange(isValid ? formattedDate : "");
142
+ setHasError(isValid ? undefined : errorType);
138
143
  setIsCalendarOpen(false);
144
+
145
+ if (isDateInValidFormat || isDirty) {
146
+ onChange(isValid ? formattedDate : "");
147
+ }
139
148
  };
140
149
 
141
150
  const handleOnKeyDown = (event: KeyboardEvent<HTMLInputElement>, index: number) => {
@@ -212,7 +221,7 @@ export const DateSelector = ({
212
221
  placeholder: placeholders?.[`${key}Format` as FormatPlaceholder] ?? "",
213
222
  labelInsideInput: true,
214
223
  value: internalValue[key] ?? '',
215
- error: (hasError && [key, 'all'].includes(hasError)) && isDirty,
224
+ error: hasError && isDirty,
216
225
  type: "text",
217
226
  inputMode: "numeric",
218
227
  ref: (el: HTMLInputElement) => { itemsRef.current[index] = el },
@@ -249,8 +258,18 @@ export const DateSelector = ({
249
258
  </div>
250
259
 
251
260
  {hasError && isDirty && (
252
- <p className="p-p--small tc-red-500 w100 mt8">
253
- {placeholders.error}
261
+ <p
262
+ className={classNames(
263
+ hasError && isDirty ? 'd-block' : 'd-none',
264
+ "p-p--small tc-red-500 w100 mt8"
265
+ )}
266
+ data-testid="date-error-message"
267
+ >
268
+ {{
269
+ default: placeholders.error,
270
+ afterMax: placeholders.errorAfterMaxYear || `Please choose a date before ${yearBoundaries.max + 1}`,
271
+ beforeMin: placeholders.errorBeforeMinYear || `Please choose a date after ${yearBoundaries.min - 1}`,
272
+ }[hasError || "default"]}
254
273
  </p>
255
274
  )}
256
275
  </div>