2
0

index.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled, { injectGlobal } from 'styled-components'
  18. import Datetime from 'react-datetime'
  19. import moment from 'moment'
  20. import DropdownButton from '../../atoms/DropdownButton'
  21. import DomUtils from '../../../utils/DomUtils'
  22. import DateUtils from '../../../utils/DateUtils'
  23. import StyleProps from '../../styleUtils/StyleProps'
  24. import style from './style.js'
  25. require('moment/locale/en-gb')
  26. injectGlobal`${style}`
  27. const Wrapper = styled.div`
  28. position: relative;
  29. width: ${StyleProps.inputSizes.regular.width}px;
  30. `
  31. const DropdownButtonStyled = styled(DropdownButton)`
  32. font-size: 12px;
  33. `
  34. const DatetimeStyled = styled(Datetime)`
  35. position: absolute;
  36. right: -11px;
  37. top: 49px;
  38. z-index: 10;
  39. .rdtPicker {
  40. display: ${props => props.open ? 'block' : 'none'};
  41. }
  42. `
  43. type Props = {
  44. value: ?Date,
  45. onChange: (date: Date) => void,
  46. isValidDate: (currentDate: Date, selectedDate: Date) => boolean,
  47. timezone: 'utc' | 'local',
  48. useBold?: boolean,
  49. }
  50. type State = {
  51. showPicker: boolean,
  52. date: ?moment$Moment,
  53. }
  54. @observer
  55. class DatetimePicker extends React.Component<Props, State> {
  56. itemMouseDown: boolean
  57. constructor() {
  58. super()
  59. this.state = {
  60. showPicker: false,
  61. date: null,
  62. }
  63. const self: any = this
  64. self.handlePageClick = this.handlePageClick.bind(this)
  65. }
  66. componentWillMount() {
  67. if (this.props.value) {
  68. this.setState({ date: moment(this.props.value) })
  69. }
  70. }
  71. componentDidMount() {
  72. window.addEventListener('mousedown', this.handlePageClick, false)
  73. }
  74. componentWillUnmount() {
  75. window.removeEventListener('mousedown', this.handlePageClick, false)
  76. }
  77. isValidDate(currentDate: Date, selectedDate: Date): boolean {
  78. if (!this.props.isValidDate) {
  79. return true
  80. }
  81. return this.props.isValidDate(currentDate, selectedDate)
  82. }
  83. handlePageClick(e: Event) {
  84. let path = DomUtils.getEventPath(e)
  85. if (!this.itemMouseDown && !path.find(n => n.className === 'rdtPicker')) {
  86. if (this.state.date && this.state.showPicker) {
  87. this.props.onChange(this.state.date.toDate())
  88. }
  89. this.setState({ showPicker: false })
  90. }
  91. }
  92. handleDropdownClick() {
  93. if (this.state.showPicker && this.state.date) {
  94. this.props.onChange(this.state.date.toDate())
  95. }
  96. this.setState({ showPicker: !this.state.showPicker })
  97. }
  98. handleChange(newDate: Date) {
  99. let date = moment(newDate)
  100. if (this.props.timezone === 'utc') {
  101. date = DateUtils.getLocalTime(newDate)
  102. }
  103. this.setState({ date })
  104. }
  105. render() {
  106. let timezoneDate = this.state.date
  107. if (this.props.timezone === 'utc' && timezoneDate) {
  108. timezoneDate = DateUtils.getUtcTime(timezoneDate)
  109. }
  110. return (
  111. <Wrapper>
  112. <DropdownButtonStyled
  113. width={176}
  114. value={(timezoneDate && moment(timezoneDate).format('DD/MM/YYYY hh:mm A')) || '-'}
  115. centered
  116. useBold={this.props.useBold}
  117. onClick={() => { this.handleDropdownClick() }}
  118. onMouseDown={() => { this.itemMouseDown = true }}
  119. onMouseUp={() => { this.itemMouseDown = false }}
  120. />
  121. <DatetimeStyled
  122. input={false}
  123. value={timezoneDate}
  124. open={this.state.showPicker}
  125. onChange={date => { this.handleChange(date) }}
  126. dateFormat="DD/MM/YYYY"
  127. timeFormat="hh:mm A"
  128. locale="en-gb"
  129. isValidDate={(currentDate, selectedDate) => this.isValidDate(currentDate, selectedDate)}
  130. />
  131. </Wrapper>
  132. )
  133. }
  134. }
  135. export default DatetimePicker