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.

109 lines
2.5 KiB

  1. <script lang="ts">
  2. import { allOffsets, pastOffsets, CustomDateRange } from "../utils/date-range";
  3. import type { DateRange } from "../utils/date-range";
  4. import { formatFormTime } from "../utils/time";
  5. export let noFuture: boolean = false;
  6. export let value: DateRange;
  7. export let styled: boolean = false;
  8. export let label: string = "Time";
  9. export let disabled: boolean = false;
  10. let rangeOptions: DateRange[];
  11. let selected = value.name;
  12. let [customMin, customMax] = (value || pastOffsets[0]).calculate(new Date()).map(d => formatFormTime(d));
  13. $: rangeOptions = noFuture ? pastOffsets : allOffsets;
  14. $: {
  15. if (selected === "Specific Dates") {
  16. value = new CustomDateRange(new Date(customMin), new Date(customMax))
  17. } else {
  18. value = rangeOptions.find(v => v.name === selected);
  19. if (value != null) {
  20. let [from, to] = value.calculate(new Date()).map(d => formatFormTime(d));
  21. customMin = from;
  22. customMax = to;
  23. } else {
  24. selected = rangeOptions[0].name;
  25. value = rangeOptions[0];
  26. }
  27. }
  28. }
  29. </script>
  30. <div class:styled>
  31. <div>
  32. <label for="timeRange">{label}</label>
  33. <select name="timeRange" disabled={disabled} bind:value={selected}>
  34. {#each rangeOptions as option (option.name)}
  35. <option value={option.name}>{option.name}</option>
  36. {/each}
  37. <option value="Specific Dates">Specific Dates</option>
  38. </select>
  39. </div>
  40. {#if value.name === "Specific Dates"}
  41. <div>
  42. <label for="startTime">Start Time</label>
  43. <input disabled={disabled} name="startTime" type="datetime-local" bind:value={customMin} />
  44. </div>
  45. <div>
  46. <label for="endTime">End Time</label>
  47. <input disabled={disabled} name="endTime" type="datetime-local" bind:value={customMax} />
  48. </div>
  49. {/if}
  50. </div>
  51. <style>
  52. div.styled {
  53. display: flex;
  54. flex-direction: row;
  55. width: 100%;
  56. }
  57. div.styled > div:first {
  58. width: 20%;
  59. }
  60. div.styled > div {
  61. padding: 0 0.5em;
  62. margin: 0 auto;
  63. width: 35%;
  64. }
  65. div.styled label {
  66. line-height: 1.5em;
  67. }
  68. div.styled select, div.styled input {
  69. width: 100%;
  70. margin-bottom: 0.5em;
  71. padding: 0.35em;
  72. background: #222;
  73. color: #777;
  74. border: none;
  75. outline: none;
  76. resize: vertical;
  77. }
  78. div.styled select {
  79. padding: 0.46em 0.5ch;
  80. }
  81. div.styled input:focus, div.styled select:focus {
  82. background: #191919;
  83. color: #CCC;
  84. border: none;
  85. outline: none;
  86. }
  87. @media screen and (max-width: 900px) {
  88. div.styled {
  89. flex-direction: column;
  90. margin-bottom: 1em;
  91. }
  92. div.styled > div {
  93. width: 90%;
  94. }
  95. }
  96. </style>