Skip to content

Commit

Permalink
Refactor date normalization in FormDateInput to improve date validati…
Browse files Browse the repository at this point in the history
…on and formatting
  • Loading branch information
b-l-i-n-d committed Jan 30, 2025
1 parent 5a21049 commit bd1952a
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions assets/react/v3/shared/components/fields/FormDateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ const createFormatters = (wpLocale: string): Partial<Formatters> => {
};
};

const normalizeDateString = (date: string): string => {
const parts = date.split('-');
if (parts.length !== 3) {
return date;
}

const year = parts[0];
const month = parts[1].padStart(2, '0');
const day = parts[2].padStart(2, '0');

return `${year}-${month}-${day}`;
};

const FormDateInput = ({
label,
field,
Expand All @@ -72,8 +59,8 @@ const FormDateInput = ({
}: FormDateInputProps) => {
const inputRef = useRef<HTMLInputElement>(null);
const [isOpen, setIsOpen] = useState(false);
const parsedISODate = parseISO(normalizeDateString(field.value));
const isValidDate = isValid(new Date(field.value));
const parsedISODate = isValidDate ? parseISO(format(new Date(field.value), DateFormats.yearMonthDay)) : new Date();
const fieldValue = isValidDate ? format(parsedISODate, dateFormat) : '';

const { triggerRef, position, popoverRef } = usePortalPopover<HTMLDivElement, HTMLDivElement>({
Expand Down

0 comments on commit bd1952a

Please sign in to comment.