From 2952d41ef3866cc790ad0aad52dde91390df2744 Mon Sep 17 00:00:00 2001 From: Leonardo Victor Date: Sat, 30 Mar 2024 02:34:03 -0300 Subject: [PATCH 1/3] fix date validation and input value assign --- code/ui/blocks/src/controls/Date.tsx | 8 +++++++- code/ui/blocks/src/controls/types.ts | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/code/ui/blocks/src/controls/Date.tsx b/code/ui/blocks/src/controls/Date.tsx index 53eedf56c50c..f044ddb4f077 100644 --- a/code/ui/blocks/src/controls/Date.tsx +++ b/code/ui/blocks/src/controls/Date.tsx @@ -22,6 +22,7 @@ export const parseTime = (value: string) => { }; export const formatDate = (value: Date | number) => { + if (!value) return ''; const date = new Date(value); const year = `000${date.getFullYear()}`.slice(-4); const month = `0${date.getMonth() + 1}`.slice(-2); @@ -30,6 +31,7 @@ export const formatDate = (value: Date | number) => { }; export const formatTime = (value: Date | number) => { + if (!value) return ''; const date = new Date(value); const hours = `0${date.getHours()}`.slice(-2); const minutes = `0${date.getMinutes()}`.slice(-2); @@ -83,7 +85,9 @@ export const DateControl: FC = ({ name, value, onChange, onFocus, onB }, [value]); const onDateChange = (e: ChangeEvent) => { - const parsed = parseDate(e.target.value); + const inputValue = e.target.value; + if (!inputValue) return onChange(); + const parsed = parseDate(inputValue); const result = new Date(value); result.setFullYear(parsed.getFullYear(), parsed.getMonth(), parsed.getDate()); const time = result.getTime(); @@ -92,6 +96,8 @@ export const DateControl: FC = ({ name, value, onChange, onFocus, onB }; const onTimeChange = (e: ChangeEvent) => { + const inputValue = e.target.value; + if (!inputValue) return onChange(); const parsed = parseTime(e.target.value); const result = new Date(value); result.setHours(parsed.getHours()); diff --git a/code/ui/blocks/src/controls/types.ts b/code/ui/blocks/src/controls/types.ts index 26460ff6184c..4da8dc198977 100644 --- a/code/ui/blocks/src/controls/types.ts +++ b/code/ui/blocks/src/controls/types.ts @@ -5,7 +5,7 @@ export interface ControlProps { value?: T; defaultValue?: T; argType?: ArgType; - onChange: (value: T) => T | void; + onChange: (value?: T) => T | void; onFocus?: (evt: any) => void; onBlur?: (evt: any) => void; } From 895010aeade7632e5b3a2c6b4bf5d88a7f14b88d Mon Sep 17 00:00:00 2001 From: Leonardo Victor Date: Sat, 30 Mar 2024 02:34:03 -0300 Subject: [PATCH 2/3] fix date validation and input value assign --- code/ui/blocks/src/controls/Date.tsx | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/code/ui/blocks/src/controls/Date.tsx b/code/ui/blocks/src/controls/Date.tsx index f044ddb4f077..4331b7053dcf 100644 --- a/code/ui/blocks/src/controls/Date.tsx +++ b/code/ui/blocks/src/controls/Date.tsx @@ -76,18 +76,17 @@ export const DateControl: FC = ({ name, value, onChange, onFocus, onB useEffect(() => { if (valid !== false) { if (dateRef && dateRef.current) { - dateRef.current.value = formatDate(value); + dateRef.current.value = value ? formatDate(value) : ''; } if (timeRef && timeRef.current) { - timeRef.current.value = formatTime(value); + timeRef.current.value = value ? formatTime(value) : ''; } } }, [value]); const onDateChange = (e: ChangeEvent) => { - const inputValue = e.target.value; - if (!inputValue) return onChange(); - const parsed = parseDate(inputValue); + if (!e.target.value) return onChange(); + const parsed = parseDate(e.target.value); const result = new Date(value); result.setFullYear(parsed.getFullYear(), parsed.getMonth(), parsed.getDate()); const time = result.getTime(); @@ -96,8 +95,7 @@ export const DateControl: FC = ({ name, value, onChange, onFocus, onB }; const onTimeChange = (e: ChangeEvent) => { - const inputValue = e.target.value; - if (!inputValue) return onChange(); + if (!e.target.value) return onChange(); const parsed = parseTime(e.target.value); const result = new Date(value); result.setHours(parsed.getHours()); From f02d94e6b069ee4bc8705e63decb60d83ce910d6 Mon Sep 17 00:00:00 2001 From: Leonardo Victor Date: Sat, 30 Mar 2024 17:12:41 -0300 Subject: [PATCH 3/3] remove empty string check --- code/ui/blocks/src/controls/Date.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/ui/blocks/src/controls/Date.tsx b/code/ui/blocks/src/controls/Date.tsx index 4331b7053dcf..938fc8c8c33b 100644 --- a/code/ui/blocks/src/controls/Date.tsx +++ b/code/ui/blocks/src/controls/Date.tsx @@ -22,7 +22,6 @@ export const parseTime = (value: string) => { }; export const formatDate = (value: Date | number) => { - if (!value) return ''; const date = new Date(value); const year = `000${date.getFullYear()}`.slice(-4); const month = `0${date.getMonth() + 1}`.slice(-2); @@ -31,7 +30,6 @@ export const formatDate = (value: Date | number) => { }; export const formatTime = (value: Date | number) => { - if (!value) return ''; const date = new Date(value); const hours = `0${date.getHours()}`.slice(-2); const minutes = `0${date.getMinutes()}`.slice(-2);