index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Component({
  2. options: {
  3. addGlobalClass: true
  4. },
  5. properties: {
  6. iconName: {
  7. type: String,
  8. value: "icon-rili1"
  9. },
  10. title: {
  11. type: String,
  12. },
  13. onChange: {
  14. type: Function,
  15. },
  16. fields: {
  17. type: String,
  18. value: "day", //有效值 year,month,day,表示选择器的粒度
  19. },
  20. end: {
  21. type: [String, Number]
  22. },
  23. start: {
  24. type: [String, Number]
  25. }
  26. },
  27. lifetimes: {
  28. attached: function () {
  29. const fields = this.properties.fields;
  30. let value = new Date();
  31. if (fields === "year") {
  32. value = value.getFullYear().toString();
  33. } else if (fields === "month") {
  34. const year = value.getFullYear();
  35. const month = (value.getMonth() + 1).toString().padStart(2, '0');
  36. value = `${year}-${month}`;
  37. } else if (fields === "day") {
  38. value = value.toISOString().split('T')[0];
  39. }
  40. this.setData({
  41. value
  42. });
  43. getApp().globalData.Language.getLanguagePackage(this);
  44. }
  45. },
  46. data: {
  47. value: new Date().toISOString().split('T')[0], // 默认值为今天,格式为 YYYY-MM-DD
  48. },
  49. methods: {
  50. onChange(e) {
  51. if (e.detail.value == this.data.value) return;
  52. this.setData({
  53. value: e.detail.value
  54. })
  55. this.triggerEvent("onChange", e.detail.value)
  56. },
  57. }
  58. })