DatePickerView.ets 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { DateDialogOptions } from '../PickerDateTimeHelper'
  2. import { ToolsHelper } from '../ToolsHelper'
  3. @Component
  4. export struct DatePickerView {
  5. @Prop option: DateDialogOptions | undefined = undefined
  6. @Prop dialogTag: string | undefined = undefined
  7. @State showYear: boolean = false
  8. @State showMonth: boolean = false
  9. @State showDay: boolean = false
  10. //年份相关
  11. @State startYear: number = 1970
  12. @State endYear: number = 1970
  13. @State currentYear: number = 0
  14. @State years: string[] = []
  15. // 月份相关
  16. @State startMonth: number = 1
  17. @State endMonth: number = 12
  18. @State currentMonth: number = 0
  19. @State months: string[] = []
  20. // day相关
  21. @State startDay: number = 1
  22. @State endDay: number = 30
  23. @State currentDay: number = -1
  24. @State days: string[] = []
  25. aboutToAppear(): void {
  26. if (!this.option?.type) {
  27. this.option!.type = 'YYYY-MM-DD'
  28. }
  29. this.checkYear()
  30. this.checkMonth()
  31. this.checkDays()
  32. }
  33. checkYear() {
  34. this.showYear = this.option!.type!.indexOf('YYYY') > -1
  35. if (this.showYear) {
  36. if (this.option?.startDate) {
  37. this.startYear = Number.parseInt(this.option.startDate?.split('-')[0])
  38. }
  39. if (this.option?.endDate) {
  40. this.endYear = Number.parseInt(this.option.endDate?.split('-')[0])
  41. } else {
  42. this.endYear = new Date().getFullYear() + 10
  43. }
  44. this.years = []
  45. for (let index = this.startYear; index <= this.endYear; index++) {
  46. this.years.push(index.toString())
  47. }
  48. if (this.option?.currentDate) {
  49. const cy = this.option.currentDate?.split('-')[0]
  50. this.currentYear = this.years.indexOf(cy)
  51. } else {
  52. this.currentYear = this.years.indexOf(new Date().getFullYear().toString())
  53. }
  54. }
  55. }
  56. checkMonth() {
  57. this.showMonth = this.option!.type!.indexOf('MM') > -1
  58. if (this.showMonth) {
  59. if (this.option?.startDate) {
  60. this.startMonth = Number.parseInt(this.option.startDate?.split('-')[1])
  61. }
  62. if (this.option?.endDate) {
  63. this.endMonth = Number.parseInt(this.option.endDate?.split('-')[1])
  64. } else {
  65. this.endMonth = 12
  66. }
  67. if (this.endMonth > 12) {
  68. this.endMonth = 12
  69. }
  70. if (this.endMonth < 1) {
  71. this.endMonth = 1
  72. }
  73. this.months = []
  74. for (let index = this.startMonth; index <= this.endMonth; index++) {
  75. this.months.push(index.toString())
  76. }
  77. if (this.option?.currentDate) {
  78. const cy = this.option.currentDate?.split('-')[1]
  79. this.currentMonth = this.months.indexOf(cy)
  80. } else {
  81. this.currentMonth = this.months.indexOf((new Date().getMonth() + 1).toString())
  82. }
  83. }
  84. }
  85. checkDays() {
  86. this.showDay = this.option!.type!.indexOf('DD') > -1
  87. if (this.showDay) {
  88. if (this.option?.startDate) {
  89. this.startDay = Number.parseInt(this.option.startDate?.split('-')[2])
  90. }
  91. const d = new Date(this.currentYear, this.currentMonth + 1, 0).getDate()
  92. if (this.option?.endDate) {
  93. this.endDay = Number.parseInt(this.option.endDate?.split('-')[2])
  94. } else {
  95. this.endDay = d
  96. }
  97. if (this.endDay > d) {
  98. this.endDay = d
  99. }
  100. if (this.endDay < 1) {
  101. this.endDay = 1
  102. }
  103. this.days = []
  104. for (let index = this.startDay; index <= this.endDay; index++) {
  105. this.days.push(index.toString())
  106. }
  107. if (this.currentDay === -1) {
  108. if (this.option?.currentDate) {
  109. const cy = this.option.currentDate?.split('-')[2]
  110. this.currentDay = this.days.indexOf(cy)
  111. } else {
  112. this.currentDay = this.days.indexOf((new Date().getDate()).toString())
  113. }
  114. } else {
  115. if (this.currentDay > this.days.length - 1) {
  116. this.currentDay = this.days.length - 1
  117. }
  118. }
  119. }
  120. }
  121. build() {
  122. Column() {
  123. Row() {
  124. Text('取消').fontSize(17).fontColor('#333333').onClick(() => {
  125. this.option?.onCancel?.()
  126. ToolsHelper.closeCustomDialog(this.dialogTag!)
  127. })
  128. Text('确定').fontSize(17).fontColor('#18ABFB').onClick(() => {
  129. this.option?.onConfirm?.(this.option!.type!.replace('YYYY', this.years[this.currentYear])
  130. .replace('MM', this.months[this.currentMonth])
  131. .replace('DD', this.days[this.currentDay])
  132. )
  133. ToolsHelper.closeCustomDialog(this.dialogTag!)
  134. })
  135. }.alignItems(VerticalAlign.Center)
  136. .justifyContent(FlexAlign.SpaceBetween)
  137. .width('100%')
  138. .padding(15)
  139. Row() {
  140. if (this.startYear) {
  141. TextPicker({ range: this.years, selected: $$this.currentYear })
  142. .onChange((value: string | string[], index: number | number[]) => {
  143. this.checkDays()
  144. }).canLoop(false)
  145. }
  146. if (this.startMonth) {
  147. TextPicker({ range: this.months, selected: $$this.currentMonth })
  148. .onChange((value: string | string[], index: number | number[]) => {
  149. this.checkDays()
  150. }).canLoop(false)
  151. }
  152. if (this.startDay) {
  153. TextPicker({ range: this.days, selected: $$this.currentDay })
  154. }
  155. }.width('100%').justifyContent(FlexAlign.Center).alignItems(VerticalAlign.Center)
  156. }
  157. }
  158. }