Skip to content

Commit 79dca8b

Browse files
committed
function calculation reverted
1 parent 86a500a commit 79dca8b

File tree

2 files changed

+71
-366
lines changed

2 files changed

+71
-366
lines changed

client/packages/lowcoder/src/comps/comps/dateComp/dateComp.tsx

Lines changed: 44 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import _, { noop } from "lodash";
22
import dayjs from "dayjs";
33
import utc from 'dayjs/plugin/utc';
4-
import customParseFormat from 'dayjs/plugin/customParseFormat';
5-
import timezone from 'dayjs/plugin/timezone';
64
import { RecordConstructorToComp, RecordConstructorToView } from "lowcoder-core";
75
import {
86
BoolCodeControl,
@@ -56,8 +54,6 @@ import { timeZoneOptions } from "./timeZone";
5654

5755

5856
dayjs.extend(utc);
59-
dayjs.extend(timezone);
60-
dayjs.extend(customParseFormat);
6157

6258

6359
const EventOptions = [changeEvent, focusEvent, blurEvent] as const;
@@ -473,57 +469,21 @@ export const DatePickerComp = withExposingConfigs(datePickerControl, [
473469
depsConfig({
474470
name: "value",
475471
desc: trans("export.datePickerValueDesc"),
476-
depKeys: ["value", "showTime", "timeZone", "userTimeZone"],
472+
depKeys: ["value", "showTime"],
477473
func: (input) => {
478-
479-
let mom = null;
480-
for (const format of DateParser) {
481-
if (dayjs.utc(input.value, format).isValid()) {
482-
mom = dayjs.utc(input.value, format);
483-
break;
484-
}
485-
}
486-
487-
if (!input.showTime && mom?.hour() === 0 && mom?.minute() === 0 && mom?.second() === 0) {
488-
mom = mom?.hour(12); // Default to noon to avoid day shift
489-
}
490-
491-
if (mom?.isValid()) {
492-
const tz = input.timeZone === 'UserChoice' ? input.userTimeZone : input.timeZone || 'UTC';
493-
const formattedDate = mom.tz(tz).format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT);
494-
return formattedDate;
495-
}
496-
497-
return null;
474+
const mom = Boolean(input.value) ? dayjs(input.value, DateParser) : null;
475+
return mom?.isValid() ? mom.format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT) : null;
498476
},
499477
}),
500-
501478
depsConfig({
502479
name: "formattedValue",
503480
desc: trans("export.datePickerFormattedValueDesc"),
504-
depKeys: ["value", "format", "timeZone", "userTimeZone"],
505-
481+
depKeys: ["value", "format"],
506482
func: (input) => {
507-
let mom = null;
508-
for (const format of DateParser) {
509-
if (dayjs.utc(input.value, format).isValid()) {
510-
mom = dayjs.utc(input.value, format);
511-
break;
512-
}
513-
}
514-
if (!input.showTime && mom?.hour() === 0 && mom?.minute() === 0 && mom?.second() === 0) {
515-
mom = mom?.hour(12); // Default to noon to avoid timezone-related day shifts
516-
}
517-
if (mom?.isValid()) {
518-
const tz = input.timeZone === 'UserChoice' ? input.userTimeZone : input.timeZone || 'UTC';
519-
const formattedTime = mom.tz(tz).format(input.format);
520-
return formattedTime;
521-
}
522-
return '';
483+
const mom = Boolean(input.value) ? dayjs(input.value, DateParser) : null;
484+
return mom?.isValid() ? mom.format(input.format) : "";
523485
},
524486
}),
525-
526-
527487
depsConfig({
528488
name: "timestamp",
529489
desc: trans("export.datePickerTimestampDesc"),
@@ -560,235 +520,89 @@ export let DateRangeComp = withExposingConfigs(dateRangeControl, [
560520
depsConfig({
561521
name: "start",
562522
desc: trans("export.dateRangeStartDesc"),
563-
depKeys: ["start", "showTime", "timeZone", "userRangeTimeZone"],
523+
depKeys: ["start", "showTime"],
564524
func: (input) => {
565-
let mom = null;
566-
for (const format of DateParser) {
567-
if (dayjs.utc(input.start, format).isValid()) {
568-
mom = dayjs.utc(input.start, format);
569-
break;
570-
}
571-
}
572-
if (!input.showTime && mom?.hour() === 0 && mom?.minute() === 0 && mom?.second() === 0) {
573-
mom = mom?.hour(12);
574-
}
575-
576-
if (mom?.isValid()) {
577-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
578-
const formattedStart = mom.tz(tz).format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT);
579-
return formattedStart;
580-
}
581-
return null;
525+
const mom = Boolean(input.start) ? dayjs(input.start, DateParser): null;
526+
return mom?.isValid() ? mom.format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT) : null;
582527
},
583528
}),
584-
585529
depsConfig({
586530
name: "end",
587531
desc: trans("export.dateRangeEndDesc"),
588-
depKeys: ["end", "showTime", "timeZone", "userRangeTimeZone"],
589-
532+
depKeys: ["end", "showTime"],
590533
func: (input) => {
591-
let mom = null;
592-
for (const format of DateParser) {
593-
if (dayjs.utc(input.end, format).isValid()) {
594-
mom = dayjs.utc(input.end, format);
595-
break;
596-
}
597-
}
598-
if (!input.showTime && mom?.hour() === 0 && mom?.minute() === 0 && mom?.second() === 0) {
599-
mom = mom?.hour(12);
600-
}
601-
602-
if (mom?.isValid()) {
603-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
604-
const formattedEnd = mom.tz(tz).format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT);
605-
return formattedEnd;
606-
}
607-
return null;
534+
const mom = Boolean(input.end) ? dayjs(input.end, DateParser): null;
535+
return mom?.isValid() ? mom.format(input.showTime ? DATE_TIME_FORMAT : DATE_FORMAT) : null;
608536
},
609537
}),
610-
611538
depsConfig({
612539
name: "startTimestamp",
613540
desc: trans("export.dateRangeStartTimestampDesc"),
614-
depKeys: ["start", "timeZone", "userRangeTimeZone"],
541+
depKeys: ["start"],
615542
func: (input) => {
616-
617-
let mom = null;
618-
for (const format of DateParser) {
619-
if (dayjs.utc(input.start, format).isValid()) {
620-
mom = dayjs.utc(input.start, format);
621-
break;
622-
}
623-
}
624-
if (mom?.isValid()) {
625-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
626-
return mom.tz(tz).unix();
627-
}
628-
return "";
543+
const mom = Boolean(input.start) ? dayjs(input.start, DateParser) : null;
544+
return mom?.isValid() ? mom.unix() : "";
629545
},
630546
}),
631-
632547
depsConfig({
633548
name: "endTimestamp",
634549
desc: trans("export.dateRangeEndTimestampDesc"),
635-
depKeys: ["end", "timeZone", "userRangeTimeZone"],
550+
depKeys: ["end"],
636551
func: (input) => {
637-
638-
let mom = null;
639-
for (const format of DateParser) {
640-
if (dayjs.utc(input.end, format).isValid()) {
641-
mom = dayjs.utc(input.end, format);
642-
break;
643-
}
644-
}
645-
if (mom?.isValid()) {
646-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
647-
return mom.tz(tz).unix();
648-
}
649-
return "";
552+
const mom = Boolean(input.end) ? dayjs(input.end, DateParser) : null;
553+
return mom?.isValid() ? mom.unix() : "";
650554
},
651555
}),
652-
653556
depsConfig({
654557
name: "formattedValue",
655558
desc: trans("export.dateRangeFormattedValueDesc"),
656-
depKeys: ["start", "end", "format", "timeZone", "userRangeTimeZone"],
559+
depKeys: ["start", "end", "format"],
657560
func: (input) => {
658-
let start = null;
659-
let end = null;
660-
661-
for (const format of DateParser) {
662-
if (dayjs.utc(input.start, format).isValid()) {
663-
start = dayjs.utc(input.start, format);
664-
break;
665-
}
666-
}
667-
for (const format of DateParser) {
668-
if (dayjs.utc(input.end, format).isValid()) {
669-
end = dayjs.utc(input.end, format);
670-
break;
671-
}
672-
}
673-
674-
//When the time is 00:00:00 and you convert it to a timezone behind UTC (e.g., UTC-5), the date can shift to the previous day
675-
if (!input.showTime && start?.hour() === 0 && start?.minute() === 0 && start?.second() === 0) {
676-
start = start?.hour(12);
677-
}
678-
679-
if (!input.showTime && end?.hour() === 0 && end?.minute() === 0 && end?.second() === 0) {
680-
end = end?.hour(12);
681-
}
682-
683-
if (start?.isValid() || end?.isValid()) {
684-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
685-
686-
const formattedStart = start?.isValid() ? start.tz(tz).format(input.format) : '';
687-
const formattedEnd = end?.isValid() ? end.tz(tz).format(input.format) : '';
688-
const formattedValue = [formattedStart, formattedEnd].filter(Boolean).join(" - ");
689-
return formattedValue;
690-
}
691-
return '';
561+
const start = Boolean(input.start) ? dayjs(input.start, DateParser): null;
562+
const end = Boolean(input.end) ? dayjs(input.end, DateParser): null;
563+
return [
564+
start?.isValid() && start.format(input.format),
565+
end?.isValid() && end.format(input.format),
566+
]
567+
.filter((item) => item)
568+
.join(" - ");
692569
},
693570
}),
694-
695-
696571
depsConfig({
697572
name: "formattedStartValue",
698573
desc: trans("export.dateRangeFormattedStartValueDesc"),
699-
depKeys: ["start", "format", "timeZone", "userRangeTimeZone"],
574+
depKeys: ["start", "format"],
700575
func: (input) => {
701-
let start = null;
702-
// Loop through DateParser to find a valid format
703-
for (const format of DateParser) {
704-
if (dayjs.utc(input.start, format).isValid()) {
705-
start = dayjs.utc(input.start, format);
706-
break;
707-
}
708-
}
709-
710-
if (!input.showTime && start?.hour() === 0 && start?.minute() === 0 && start?.second() === 0) {
711-
start = start?.hour(12);
712-
}
713-
714-
if (start?.isValid()) {
715-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
716-
const formattedStart = start.tz(tz).format(input.format);
717-
return formattedStart;
718-
}
719-
return '';
576+
const start = Boolean(input.start) ? dayjs(input.start, DateParser): null;
577+
return start?.isValid() && start.format(input.format);
720578
},
721579
}),
722-
723580
depsConfig({
724581
name: "formattedEndValue",
725582
desc: trans("export.dateRangeFormattedEndValueDesc"),
726-
depKeys: ["end", "format", "timeZone", "userRangeTimeZone"],
583+
depKeys: ["end", "format"],
727584
func: (input) => {
728-
let end = null;
729-
// Loop through DateParser to find a valid format
730-
for (const format of DateParser) {
731-
if (dayjs.utc(input.end, format).isValid()) {
732-
end = dayjs.utc(input.end, format);
733-
break;
734-
}
735-
}
736-
737-
if (!input.showTime && end?.hour() === 0 && end?.minute() === 0 && end?.second() === 0) {
738-
end = end?.hour(12);
739-
}
740-
741-
if (end?.isValid()) {
742-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
743-
const formattedEnd = end.tz(tz).format(input.format);
744-
return formattedEnd;
745-
}
746-
return '';
585+
const end = Boolean(input.end) ? dayjs(input.end, DateParser): null;
586+
return end?.isValid() && end.format(input.format);
747587
},
748588
}),
749-
750-
751589
depsConfig({
752590
name: "invalid",
753591
desc: trans("export.invalidDesc"),
754-
depKeys: ["start", "end", "required", "minTime", "maxTime", "minDate", "maxDate", "customRule", "timeZone", "userRangeTimeZone"],
755-
func: (input) => {
756-
const tz = input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
757-
let startDate = null;
758-
let endDate = null;
759-
760-
for (const format of DateParser) {
761-
if (dayjs.utc(input.start, format).isValid()) {
762-
startDate = dayjs.utc(input.start, format).tz(tz);
763-
break;
764-
}
765-
}
766-
for (const format of DateParser) {
767-
if (dayjs.utc(input.end, format).isValid()) {
768-
endDate = dayjs.utc(input.end, format).tz(tz);
769-
break;
770-
}
771-
}
772-
773-
const startInvalid = startDate && (!startDate.isValid() || (input.minDate && startDate.isBefore(dayjs(input.minDate).tz(tz))) || (input.maxDate && startDate.isAfter(dayjs(input.maxDate).tz(tz))));
774-
const endInvalid = endDate && (!endDate.isValid() || (input.minDate && endDate.isBefore(dayjs(input.minDate).tz(tz))) || (input.maxDate && endDate.isAfter(dayjs(input.maxDate).tz(tz))));
775-
776-
return startInvalid || endInvalid;
777-
},
778-
}),
779-
780-
depsConfig({
781-
name: "timeZone",
782-
desc: trans("export.timeZoneDesc"),
783-
depKeys: ["timeZone", "userRangeTimeZone"],
784-
func: (input) => {
785-
return input.timeZone === 'UserChoice' ? input.userRangeTimeZone : input.timeZone || 'UTC';
786-
},
592+
depKeys: ["start", "end", "required", "minTime", "maxTime", "minDate", "maxDate", "customRule"],
593+
func: (input) =>
594+
validate({
595+
...input,
596+
value: { value: input.start },
597+
}).validateStatus !== "success" ||
598+
validate({
599+
...input,
600+
value: { value: input.end },
601+
}).validateStatus !== "success",
787602
}),
788603
...CommonNameConfig,
789604
]);
790605

791-
792606
DateRangeComp = withMethodExposing(DateRangeComp, [
793607
...dateRefMethods,
794608
{
@@ -830,4 +644,4 @@ DateRangeComp = withMethodExposing(DateRangeComp, [
830644
comp.children.end.getView().onChange(data.end);
831645
},
832646
},
833-
]);
647+
]);

0 commit comments

Comments
 (0)