Skip to content

Commit 525da9c

Browse files
author
Hector Arce De Las Heras
committed
Resolved onBlur execution error and improved accessibility
This commit addresses several issues: 1. Fixed an error that occurred during the execution of the onBlur event. 2. Enhanced error handling to distinguish between normal and ranged errors. 3. Rectified an issue with empty aria labels in SVGs within buttons, improving accessibility. 4. Corrected the event name for the onBlur event in the InputDate component, ensuring accurate event handling.
1 parent 8c0fda6 commit 525da9c

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export enum InternalErrorType {
22
INVALID_EMAIL = 'invalid_email',
33
INVALID_DATE = 'invalid_date',
4+
INVALID_DATE_RANGE = 'invalid_date_range',
45
INVALID_MIN_LENGTH = 'invalid_min_length',
56
INVALID_OPTION = 'INVALID_OPTION',
67
}

src/components/inputDate/hooks/useInputDate.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ export const useInputDate = ({
173173

174174
if (dateRangeFormatted && dateRangeFormatted?.length >= 2) {
175175
if (isValidDateRange) {
176-
removeInternalError(InternalErrorType.INVALID_DATE);
176+
removeInternalError(InternalErrorType.INVALID_DATE_RANGE);
177177
} else {
178-
addInternalError(InternalErrorType.INVALID_DATE);
178+
addInternalError(InternalErrorType.INVALID_DATE_RANGE);
179179
}
180180
}
181181
handleChangeDate(
@@ -258,13 +258,48 @@ export const useInputDate = ({
258258
const hasError = internalErrors.length > 0;
259259

260260
props.onDateError?.(hasError);
261-
onBlur?.(event);
261+
262+
let adaptEvent;
263+
const dateValue = event.target.value;
264+
if (props.hasRange) {
265+
const hasSeparator = props.dateSeparator && dateValue.includes(props.dateSeparator);
266+
const secondDateExists = dateValue.length > props.format.length;
267+
const dates = hasSeparator
268+
? dateValue.split(props.dateSeparator as string)
269+
: dateValue.split(' ');
270+
271+
const firstDate = formatDateToNative(dates[0], props.format);
272+
const secondDate = secondDateExists ? formatDateToNative(dates[1], props.format) : '';
273+
274+
const firstValueAsNumber = firstDate.length && new Date(firstDate).getTime();
275+
const secondValueAsNumber = secondDate.length && new Date(secondDate).getTime();
276+
277+
const firstValueAsDate = firstDate.length && new Date(firstDate);
278+
const secondValueAsDate = secondDate.length && new Date(secondDate);
279+
280+
adaptEvent = {
281+
target: {
282+
value: `${firstDate} ${secondDate}`,
283+
valueAsNumber: `${firstValueAsNumber} ${secondValueAsNumber}`.trim(),
284+
valueAsDate: `${firstValueAsDate} - ${secondValueAsDate}`,
285+
},
286+
};
287+
} else {
288+
adaptEvent = setDate(dateValue, props.format);
289+
}
290+
onBlur?.(adaptEvent);
262291
};
263292

264293
// validate formatted dates
265294
// eslint-disable-next-line complexity
266295
const handleChangeInternalValidate: ChangeEventHandler<HTMLInputElement> = event => {
267296
const dateValue = event?.target?.value;
297+
// reset the internal errors for new verify
298+
if (internalErrors.includes(InternalErrorType.INVALID_DATE)) {
299+
removeInternalError(InternalErrorType.INVALID_DATE);
300+
} else if (internalErrors.includes(InternalErrorType.INVALID_DATE_RANGE)) {
301+
removeInternalError(InternalErrorType.INVALID_DATE_RANGE);
302+
}
268303

269304
// date range
270305
if (dateValue?.length === props.format?.length * 2 + 1) {

0 commit comments

Comments
 (0)