Skip to content

fix(DateField): fix pasting string from clipboard with duplicate spaces #183

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

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/components/DateField/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,19 @@
return /z$/i.test(str) || /[+-]\d\d:\d\d$/.test(str);
}

/**
* Trims leading and trailing spaces from a string and replaces multiple consecutive spaces with a single space.
*
* @param str - The input string to process.
* @returns The processed string with trimmed spaces and single spaces between words.
*/
function trimExtraSpaces(str: string) {
return str.trim().replace(/\s+/g, ' ');
}

export function parseDateFromString(str: string, format: string, timeZone?: string): DateTime {
let date = parseDate({input: str, format, timeZone});
const input = typeof str === 'string' ? trimExtraSpaces(str) : str;
let date = parseDate({input, format, timeZone});
if (date.isValid()) {
if (timeZone && !isDateStringWithTimeZone(str)) {
const time = parseDate({input: str, format});
Expand Down Expand Up @@ -692,8 +703,8 @@
if (!isEditableSection(s)) {
continue;
}
const dateUnitIndex = dateUnits.indexOf(s.type as any);

Check warning on line 706 in src/components/DateField/utils.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
const timeUnitIndex = timeUnits.indexOf(s.type as any);

Check warning on line 707 in src/components/DateField/utils.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
availableUnits[s.type] = true;
hasDate ||= dateUnitIndex !== -1;
hasTime ||= timeUnitIndex !== -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ export function useRangeDateFieldState(props: RangeDateFieldStateOptions): Range

function setValueFromString(str: string) {
const list = str.split(delimiter);
const start = parseDateFromString(list?.[0]?.trim(), format, timeZone);
const end = parseDateFromString(list?.[1]?.trim(), format, timeZone);
const start = parseDateFromString(list?.[0], format, timeZone);
const end = parseDateFromString(list?.[1], format, timeZone);
const range = {start, end};
if (range.start.isValid() && range.end.isValid()) {
handleUpdateRange(range);
Expand Down
Loading