Skip to content

Commit 4dc9419

Browse files
committed
fix: paste string from clipboard with duplicate spaces
1 parent 42fb866 commit 4dc9419

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/components/DateField/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,19 @@ function isDateStringWithTimeZone(str: string) {
647647
return /z$/i.test(str) || /[+-]\d\d:\d\d$/.test(str);
648648
}
649649

650+
/**
651+
* Trims leading and trailing spaces from a string and replaces multiple consecutive spaces with a single space.
652+
*
653+
* @param str - The input string to process.
654+
* @returns The processed string with trimmed spaces and single spaces between words.
655+
*/
656+
function trimExtraSpaces(str: string) {
657+
return str.trim().replace(/\s+/g, ' ');
658+
}
659+
650660
export function parseDateFromString(str: string, format: string, timeZone?: string): DateTime {
651-
let date = parseDate({input: str, format, timeZone});
661+
const input = typeof str === 'string' ? trimExtraSpaces(str) : str;
662+
let date = parseDate({input, format, timeZone});
652663
if (date.isValid()) {
653664
if (timeZone && !isDateStringWithTimeZone(str)) {
654665
const time = parseDate({input: str, format});

src/components/RangeDateField/hooks/useRangeDateFieldState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ export function useRangeDateFieldState(props: RangeDateFieldStateOptions): Range
224224

225225
function setValueFromString(str: string) {
226226
const list = str.split(delimiter);
227-
const start = parseDateFromString(list?.[0]?.trim(), format, timeZone);
228-
const end = parseDateFromString(list?.[1]?.trim(), format, timeZone);
227+
const start = parseDateFromString(list?.[0], format, timeZone);
228+
const end = parseDateFromString(list?.[1], format, timeZone);
229229
const range = {start, end};
230230
if (range.start.isValid() && range.end.isValid()) {
231231
handleUpdateRange(range);

0 commit comments

Comments
 (0)