Browse Source

fix: timepicker under timezone and controll get the wrong time after select in panel in 1.3.8 date-fns-tz (#1617)

YannLynn 2 years ago
parent
commit
792c450394

+ 4 - 6
packages/semi-foundation/timePicker/foundation.ts

@@ -84,10 +84,8 @@ class TimePickerFoundation<P = Record<string, any>, S = Record<string, any>> ext
         return hDis || mDis || sDis;
     }
 
-    isValidTimeZone(timeZone?: string | number) {
-        const _timeZone = timeZone === undefined ? this.getProp('timeZone') : timeZone;
-
-        return ['string', 'number'].includes(typeof _timeZone) && _timeZone !== '';
+    isValidTimeZone(timeZone: string | number) {
+        return ['string', 'number'].includes(typeof timeZone) && timeZone !== '';
     }
 
     getDefaultFormatIfNeed(): string {
@@ -121,7 +119,7 @@ class TimePickerFoundation<P = Record<string, any>, S = Record<string, any>> ext
         (value as any[]).forEach(v => {
             const pv = parseToDate(v, formatToken, dateFnsLocale);
             if (!isNaN(pv.getTime())) {
-                parsedValues.push(this.isValidTimeZone() ? utcToZonedTime(pv, timeZone) : pv);
+                parsedValues.push(this.isValidTimeZone(timeZone) ? utcToZonedTime(pv, timeZone) : pv);
             }
         });
 
@@ -422,7 +420,7 @@ class TimePickerFoundation<P = Record<string, any>, S = Record<string, any>> ext
             _value = Array.isArray(_value) ? _value[0] : _value;
         }
 
-        if (this.isValidTimeZone() && _value) {
+        if (this.isValidTimeZone(timeZone) && _value) {
             const formatToken = this.getValidFormat();
             if (Array.isArray(_value)) {
                 _value = _value.map(v => zonedTimeToUtc(v, timeZone));

+ 13 - 0
packages/semi-ui/timePicker/_story/timepicker.stories.jsx

@@ -298,4 +298,17 @@ export const TimePickerWithOnChangeWithDateFirst = () => {
 
 TimePickerWithOnChangeWithDateFirst.story = {
   name: 'OnChangeWithDateFirst',
+};
+
+export const Fix1604 = () => {
+  const [value, setValue] = useState();
+  return (
+    <div>
+      <TimePicker value={value} onChange={(value) => {console.log('onChange', value); setValue(value);}} timeZone={0}/>
+    </div>
+  );
+};
+
+Fix1604.story = {
+  name: 'Fix Time Zone',
 };