Browse Source

Merge pull request #211 from smiclea/fix-datepicker-change

Fix `DateTimePicker` event if date not changed
Dorin Paslaru 8 năm trước cách đây
mục cha
commit
601c148164

+ 12 - 7
src/components/molecules/DatetimePicker/index.jsx

@@ -150,18 +150,13 @@ class DatetimePicker extends React.Component<Props, State> {
     let path = DomUtils.getEventPath(e)
     let path = DomUtils.getEventPath(e)
 
 
     if (!this.itemMouseDown && !path.find(n => n.className === 'rdtPicker')) {
     if (!this.itemMouseDown && !path.find(n => n.className === 'rdtPicker')) {
-      if (this.state.date && this.state.showPicker) {
-        this.props.onChange(this.state.date.toDate())
-      }
+      this.dispatchChange()
       this.setState({ showPicker: false })
       this.setState({ showPicker: false })
     }
     }
   }
   }
 
 
   handleDropdownClick() {
   handleDropdownClick() {
-    if (this.state.showPicker && this.state.date) {
-      this.props.onChange(this.state.date.toDate())
-    }
-
+    this.dispatchChange()
     this.setState({ showPicker: !this.state.showPicker })
     this.setState({ showPicker: !this.state.showPicker })
   }
   }
 
 
@@ -174,6 +169,16 @@ class DatetimePicker extends React.Component<Props, State> {
     this.setState({ date })
     this.setState({ date })
   }
   }
 
 
+  dispatchChange() {
+    if (
+      this.state.date
+      && this.state.showPicker
+      && this.state.date.toDate().getTime() !== (this.props.value && this.props.value.getTime())
+    ) {
+      this.props.onChange(this.state.date.toDate())
+    }
+  }
+
   renderDateTimePicker(timezoneDate: ?moment$Moment) {
   renderDateTimePicker(timezoneDate: ?moment$Moment) {
     if (!this.state.showPicker) {
     if (!this.state.showPicker) {
       return null
       return null