You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateRangePicker.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import classNames from 'classnames';
  21. import { max, min } from 'date-fns';
  22. import * as React from 'react';
  23. import { PopupZLevel } from '../../helpers';
  24. import { InputSizeKeys } from '../../types';
  25. import { LightLabel } from '../Text';
  26. import { DatePicker } from './DatePicker';
  27. interface DateRange {
  28. from?: Date;
  29. to?: Date;
  30. }
  31. interface Props {
  32. alignEndDateCalandarRight?: boolean;
  33. className?: string;
  34. clearButtonLabel: string;
  35. fromLabel: string;
  36. inputSize?: InputSizeKeys;
  37. maxDate?: Date;
  38. minDate?: Date;
  39. onChange: (date: DateRange) => void;
  40. separatorText?: string;
  41. toLabel: string;
  42. value?: DateRange;
  43. valueFormatter?: (date?: Date) => string;
  44. zLevel?: PopupZLevel;
  45. }
  46. export class DateRangePicker extends React.PureComponent<Props> {
  47. toDateInput?: HTMLInputElement | null;
  48. get from() {
  49. return this.props.value?.from;
  50. }
  51. get to() {
  52. return this.props.value?.to;
  53. }
  54. handleFromChange = (from: Date | undefined) => {
  55. this.props.onChange({ from, to: this.to });
  56. // use `setTimeout` to work around the immediate closing of the `toDateInput`
  57. setTimeout(() => {
  58. if (from && !this.to && this.toDateInput) {
  59. this.toDateInput.focus();
  60. }
  61. }, 0);
  62. };
  63. handleToChange = (to: Date | undefined) => {
  64. this.props.onChange({ from: this.from, to });
  65. };
  66. render() {
  67. const {
  68. alignEndDateCalandarRight,
  69. clearButtonLabel,
  70. fromLabel,
  71. inputSize = 'full',
  72. minDate,
  73. maxDate,
  74. separatorText,
  75. toLabel,
  76. valueFormatter,
  77. zLevel,
  78. } = this.props;
  79. return (
  80. <div className={classNames('sw-flex sw-items-center', this.props.className)}>
  81. <DatePicker
  82. clearButtonLabel={clearButtonLabel}
  83. currentMonth={this.to}
  84. data-test="from"
  85. highlightTo={this.to}
  86. id="date-from"
  87. maxDate={maxDate && this.to ? min([maxDate, this.to]) : maxDate ?? this.to}
  88. minDate={minDate}
  89. onChange={this.handleFromChange}
  90. placeholder={fromLabel}
  91. size={inputSize}
  92. value={this.from}
  93. valueFormatter={valueFormatter}
  94. zLevel={zLevel}
  95. />
  96. <LightLabel className="sw-mx-2">{separatorText ?? '–'}</LightLabel>
  97. <DatePicker
  98. alignRight={alignEndDateCalandarRight}
  99. clearButtonLabel={clearButtonLabel}
  100. currentMonth={this.from}
  101. data-test="to"
  102. highlightFrom={this.from}
  103. id="date-to"
  104. inputRef={(element: HTMLInputElement | null) => {
  105. this.toDateInput = element;
  106. }}
  107. maxDate={maxDate}
  108. minDate={minDate && this.from ? max([minDate, this.from]) : minDate ?? this.from}
  109. onChange={this.handleToChange}
  110. placeholder={toLabel}
  111. size={inputSize}
  112. value={this.to}
  113. valueFormatter={valueFormatter}
  114. zLevel={zLevel}
  115. />
  116. </div>
  117. );
  118. }
  119. }