ComboxFoundation.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { strings } from './constants';
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import { isValidDate } from '../datePicker/_utils/index';
  4. import isNullOrUndefined from '../utils/isNullOrUndefined';
  5. const HOUR = 1000 * 60 * 60;
  6. const DAY = 24 * HOUR;
  7. // TODO: move to utils
  8. export const formatOption = (option: number, disabledOptions: number[]) => {
  9. let value = `${option}`;
  10. if (option < 10) {
  11. value = `0${option}`;
  12. }
  13. let disabled = false;
  14. if (disabledOptions && disabledOptions.indexOf(option) >= 0) {
  15. disabled = true;
  16. }
  17. return {
  18. value,
  19. disabled,
  20. };
  21. };
  22. function generateOptions(length: number, disabledOptions: number[], hideDisabledOptions: boolean, step = 1) {
  23. const arr = [];
  24. for (let value = 0; value < length; value += step) {
  25. if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
  26. arr.push(value);
  27. }
  28. }
  29. return arr;
  30. }
  31. class ComboboxFoundation extends BaseFoundation<DefaultAdapter> {
  32. constructor(adapter: DefaultAdapter) {
  33. super({ ...adapter });
  34. }
  35. isAM() {
  36. return this.getProp('isAM');
  37. }
  38. initData() {
  39. const {
  40. timeStampValue,
  41. hourStep,
  42. disabledMinutes,
  43. disabledSeconds,
  44. hideDisabledOptions,
  45. minuteStep,
  46. secondStep,
  47. } = this.getProps();
  48. const format = this.getValidFormat();
  49. const dateTime = this.getDisplayDateFromTimeStamp(timeStampValue);
  50. const disabledHourOptions = this.disabledHours();
  51. const disabledMinuteOptions = disabledMinutes(dateTime ? dateTime.getHours() : null);
  52. const disabledSecondOptions = disabledSeconds(
  53. dateTime ? dateTime.getHours() : null,
  54. dateTime ? dateTime.getMinutes() : null
  55. );
  56. const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions, hourStep);
  57. const minuteOptions = generateOptions(60, disabledMinuteOptions, hideDisabledOptions, minuteStep);
  58. const secondOptions = generateOptions(60, disabledSecondOptions, hideDisabledOptions, secondStep);
  59. return {
  60. showHour: Boolean(format.match(/HH|hh|H|h/g)),
  61. showMinute: Boolean(format.match(/mm/g)),
  62. showSecond: Boolean(format.match(/ss/g)),
  63. hourOptions,
  64. minuteOptions,
  65. secondOptions,
  66. };
  67. }
  68. getPosition() {
  69. const position = this.getProp('position');
  70. const type = this.getProp('type') || strings.DEFAULT_TYPE;
  71. return position || strings.DEFAULT_POSITION[type];
  72. }
  73. getDefaultFormatIfNeed() {
  74. if (this._isInProps('format')) {
  75. return this.getProp('format');
  76. } else if (this.getProp('use12Hours')) {
  77. return strings.DEFAULT_FROMAT_A;
  78. } else {
  79. return strings.DEFAULT_FORMAT;
  80. }
  81. }
  82. disabledHours(): number[] {
  83. const { use12Hours, disabledHours } = this.getProps();
  84. let disabledOptions = disabledHours && disabledHours();
  85. if (use12Hours && Array.isArray(disabledOptions)) {
  86. if (this.isAM()) {
  87. disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h));
  88. } else {
  89. disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12));
  90. }
  91. }
  92. return disabledOptions;
  93. }
  94. getValidFormat(format?: string): string {
  95. let _format = isNullOrUndefined(format) ? this.getProp('format') : format;
  96. _format = this.getDefaultFormatIfNeed();
  97. _format = typeof _format === 'string' ? _format : strings.DEFAULT_FORMAT;
  98. // if (use12Hours) {
  99. // format = format.replace(/H/g, 'h');
  100. // if (!/(\s+)a/i.test(format)) {
  101. // format += ' a';
  102. // } else {
  103. // format = format.replace(/(\s+)A/i, '$1a');
  104. // }
  105. // }
  106. return _format;
  107. }
  108. /**
  109. * from 13-bit timestamp -> get display date
  110. * by combobox use
  111. */
  112. getDisplayDateFromTimeStamp(timeStamp: Date | string) {
  113. let date;
  114. if (timeStamp) {
  115. date = new Date(timeStamp);
  116. }
  117. if (!timeStamp || !isValidDate(date)) {
  118. return this.createDateDefault();
  119. }
  120. return date;
  121. }
  122. /**
  123. * create a date at 00:00:00
  124. */
  125. createDateDefault() {
  126. return new Date(parseInt(String(Date.now() / DAY), 10) * DAY - 8 * HOUR);
  127. }
  128. }
  129. export default ComboboxFoundation;