yearAndMonthFoundation.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. export interface YearAndMonthFoundationProps {
  3. currentYear?: number;
  4. currentMonth?: number;
  5. onSelect?: (obj: { currentMonth: number; currentYear: number }) => void;
  6. onBackToMain?: () => void;
  7. locale?: any;
  8. localeCode?: string;
  9. monthCycled?: boolean;
  10. yearCycled?: boolean;
  11. noBackBtn?: boolean;
  12. disabledDate?: (date: Date) => boolean;
  13. density?: string;
  14. }
  15. export interface YearAndMonthFoundationState {
  16. years: Array<{ value: number; year: number }>;
  17. months: Array<{ value: number; month: number }>;
  18. currentYear: number;
  19. currentMonth: number;
  20. }
  21. export interface YearAndMonthAdapter extends DefaultAdapter<YearAndMonthFoundationProps, YearAndMonthFoundationState> {
  22. setCurrentYear: (currentYear: number) => void;
  23. setCurrentMonth: (currentMonth: number) => void;
  24. notifySelectYear: (year: number) => void;
  25. notifySelectMonth: (month: number) => void;
  26. notifyBackToMain: () => void;
  27. }
  28. export interface MonthScrollItem {
  29. [k: string]: any;
  30. month: number;
  31. value: string;
  32. disabled: boolean;
  33. }
  34. export interface YearScrollItem {
  35. [k: string]: any;
  36. year: number;
  37. value: number;
  38. disabled: boolean;
  39. }
  40. export default class YearAndMonthFoundation extends BaseFoundation<YearAndMonthAdapter> {
  41. constructor(adapter: YearAndMonthAdapter) {
  42. super({ ...adapter });
  43. }
  44. // eslint-disable-next-line @typescript-eslint/no-empty-function
  45. init() {}
  46. // eslint-disable-next-line @typescript-eslint/no-empty-function
  47. destroy() {}
  48. selectYear(item: YearScrollItem) {
  49. const year = item.value;
  50. this._adapter.setCurrentYear(year);
  51. this._adapter.notifySelectYear(year);
  52. }
  53. selectMonth(item: MonthScrollItem) {
  54. const { month } = item;
  55. this._adapter.setCurrentMonth(month);
  56. this._adapter.notifySelectMonth(month);
  57. }
  58. backToMain() {
  59. this._adapter.notifyBackToMain();
  60. }
  61. }