index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // components/w-datetime-picker/index.ts
  2. /**
  3. * 格式化时长
  4. * @param num 时长 可以传负
  5. */
  6. function formatTime(num: number) {
  7. const date = new Date();
  8. date.setFullYear(date.getFullYear() + num);
  9. return date.getTime();
  10. }
  11. /**
  12. * 时长填充
  13. * @param value 值
  14. * @param maxLength 长度
  15. * @param fillText 填充内容
  16. * @returns 填充值
  17. */
  18. function padStartString(value: any, maxLength = 2, fillText = "0") {
  19. const text = value + "";
  20. return text.padStart(maxLength, fillText);
  21. }
  22. Component({
  23. options: {
  24. multipleSlots: true,
  25. styleIsolation: "shared",
  26. },
  27. /**
  28. * 组件的属性列表
  29. */
  30. properties: {
  31. value: {
  32. type: Number as any,
  33. value: null,
  34. observer: "_value",
  35. },
  36. /** 是否显示 */
  37. show: {
  38. type: Boolean,
  39. value: false,
  40. observer: "_show",
  41. },
  42. /** 类型,可选值为 date time year-month 不建议动态修改 */
  43. type: {
  44. type: String,
  45. value: "datetime",
  46. },
  47. zIndex: {
  48. type: Number,
  49. value: 100,
  50. },
  51. minDate: {
  52. type: Number,
  53. value: formatTime(-20),
  54. },
  55. maxDate: {
  56. type: Number,
  57. value: formatTime(20),
  58. },
  59. /**
  60. * 可见的选项个数
  61. */
  62. visibleItemCount: {
  63. type: Number,
  64. value: 6,
  65. },
  66. itemHeight: {
  67. type: Number,
  68. value: 46,
  69. },
  70. },
  71. /**
  72. * 组件的初始数据
  73. */
  74. data: {
  75. innerShow: false,
  76. // showAfterLeave: false,
  77. innerValue: null as any, // 单列时为string, 多列为array
  78. // defaultIndex: 0, // 默认选中的index 单行显示的
  79. // innerLoading: false,
  80. formatter(type: string, value: any) {
  81. if (type === "year") {
  82. return `${value}年`;
  83. }
  84. if (type === "month") {
  85. return `${value}月`;
  86. }
  87. if (type === "day") {
  88. return `${value}日`;
  89. }
  90. if (type === "hour") {
  91. return `${value}时`;
  92. }
  93. if (type === "minute") {
  94. return `${value}分`;
  95. }
  96. return value;
  97. },
  98. },
  99. lifetimes: {
  100. attached() {
  101. if (this.data.type !== "time" && !this.data.value) {
  102. this.setData({
  103. innerValue: Date.now(),
  104. });
  105. }
  106. },
  107. },
  108. /**
  109. * 组件的方法列表
  110. */
  111. methods: {
  112. onSubmit(event: any) {
  113. const date = new Date(event.detail);
  114. const year = date.getFullYear();
  115. const month = date.getMonth() + 1;
  116. const day = date.getDate();
  117. const hour = date.getHours();
  118. const minute = date.getMinutes();
  119. const detail = {
  120. chName: "",
  121. value: event.detail,
  122. };
  123. const type = this.data.type;
  124. if (type === "datetime") {
  125. detail.chName =
  126. year +
  127. "年" +
  128. padStartString(month) +
  129. "月" +
  130. padStartString(day) +
  131. "日 " +
  132. padStartString(hour) +
  133. "时" +
  134. padStartString(minute) +
  135. "分";
  136. } else if (type === "date") {
  137. detail.chName =
  138. year +
  139. "年" +
  140. padStartString(month) +
  141. "月" +
  142. padStartString(day) +
  143. "日";
  144. } else if (type === "year-month") {
  145. detail.chName = year + "年" + padStartString(month) + "月";
  146. } else if (type === "time") {
  147. detail.chName =
  148. padStartString(hour) + "分" + padStartString(minute) + "秒";
  149. }
  150. this.setData({
  151. value: detail.value,
  152. });
  153. this.triggerEvent("confirm", detail);
  154. this.triggerEvent("input", detail.value);
  155. },
  156. onClose() {
  157. this.triggerEvent("cancel");
  158. },
  159. // onAfterLeave() {
  160. // this.setData({
  161. // showAfterLeave: true,
  162. // });
  163. // },
  164. // onBeforeEnter() {
  165. // this.setData({
  166. // showAfterLeave: false,
  167. // });
  168. // },
  169. _show() {
  170. // 单行显示 需要设置默认选中的index
  171. this.setData({
  172. innerShow: this.data.show,
  173. });
  174. },
  175. _value() {
  176. if (!this.data.value) return;
  177. this.setData({
  178. innerValue: this.data.value,
  179. });
  180. },
  181. },
  182. });