Browse Source

fix: Timepicker fill with wrong current time in start time under controlled mode (#1717)

Co-authored-by: pointhalo <[email protected]>
YannLynn 2 years ago
parent
commit
ce49ca8cc7

+ 7 - 7
packages/semi-foundation/timePicker/foundation.ts

@@ -213,14 +213,14 @@ class TimePickerFoundation<P = Record<string, any>, S = Record<string, any>> ext
                     )
                 );
             }
-            const inputValue = this.formatValue(dates);
-
-            this.setState({
-                value: dates,
-                invalid,
-                inputValue,
-            } as any);
         }
+        const inputValue = this.formatValue(dates);
+
+        this.setState({
+            value: dates,
+            invalid,
+            inputValue,
+        } as any);
     }
 
     handleFocus(e: any) {

+ 2 - 0
packages/semi-foundation/timePicker/utils/index.ts

@@ -25,6 +25,8 @@ export const parseToDate = (input: string | Date | number, formatToken = strings
         // console.log(curDate, formatToken);
 
         return curDate;
+    } else if (typeof input === 'undefined') {
+        return undefined;
     }
 
     return new Date();

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

@@ -311,4 +311,28 @@ export const Fix1604 = () => {
 
 Fix1604.story = {
   name: 'Fix Time Zone',
+};
+
+export const Fix1716 = () => {
+  const [v, setV] = useState([]);
+  const handleChange = (value) => {
+      console.log(value);
+      setV(value);
+  }
+  return (
+    <div>
+      <h4>非受控的timepicker,正常</h4>
+      <TimePicker type="timeRange" />
+      <h4>受控的timepicker,修改后正常</h4>
+      <TimePicker
+        type="timeRange"
+        value={v}
+        onChange={handleChange} 
+      /> 
+    </div>
+  );
+};
+
+Fix1716.story = {
+  name: 'Fix 1716',
 };