index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // components/w-picker/index.ts
  2. // 组件不支持简单的双向绑定,需要使用input和confirm事件来实现。
  3. Component({
  4. options: {
  5. multipleSlots: true,
  6. styleIsolation: "shared",
  7. },
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. value: {
  13. type: [String, Number, Array] as any,
  14. value: "",
  15. observer: "_value",
  16. },
  17. /** 是否显示 */
  18. show: {
  19. type: Boolean,
  20. value: false,
  21. observer: "_show",
  22. },
  23. /**
  24. * 列数据
  25. * @example 单列 [{ text: a, value: 1 }, { text: b, value: 2 }]
  26. * @example 多列 [{ values: [{ text: a, value: 1 }, { text: b, value: 2 }], defaultIndex: 0 }, { values: [{ text: a, value: 1 }, { text: b, value: 2 }], defaultIndex: 0 }]
  27. */
  28. columns: {
  29. type: Array as any,
  30. value: [],
  31. observer: "_columns",
  32. },
  33. zIndex: {
  34. type: Number,
  35. value: 100
  36. },
  37. /**
  38. * 可见的选项个数
  39. */
  40. visibleItemCount: {
  41. type: Number,
  42. value: 6,
  43. },
  44. itemHeight: {
  45. type: Number,
  46. value: 46,
  47. },
  48. placeholder: {
  49. type: String,
  50. value: '请输入搜索关键词'
  51. }
  52. },
  53. /**
  54. * 组件的初始数据
  55. */
  56. data: {
  57. isMulti: false, // 是否多列
  58. innerShow: false,
  59. showAfterLeave: false,
  60. innerValue: "" as any, // 单列时为string, 多列为array
  61. defaultIndex: 0, // 默认选中的index 单行显示的
  62. },
  63. /**
  64. * 组件的方法列表
  65. */
  66. methods: {
  67. onSubmit(event: any) {
  68. const detail = event.detail || {};
  69. if(this.data.isMulti) {
  70. const values: any = [];
  71. detail.value.forEach((item: any) => {
  72. values.push(item.value)
  73. })
  74. this.setData({
  75. show: false,
  76. value: values,
  77. });
  78. this.triggerEvent("confirm", detail);
  79. this.triggerEvent("input", values);
  80. } else {
  81. this.setData({
  82. show: false,
  83. value: detail.value.value,
  84. });
  85. this.triggerEvent("confirm", detail);
  86. this.triggerEvent("input", detail.value.value);
  87. }
  88. },
  89. onChange(event: any) {
  90. const detail = event.detail.value;
  91. this.triggerEvent("change", detail);
  92. },
  93. onClose() {
  94. this.triggerEvent("cancel");
  95. },
  96. onAfterLeave() {
  97. this.setData({
  98. showAfterLeave: true,
  99. });
  100. },
  101. onBeforeEnter() {
  102. this.setData({
  103. showAfterLeave: false,
  104. });
  105. },
  106. _show() {
  107. // 单行显示 需要设置默认选中的index
  108. let defaultIndex = 0;
  109. let columns = [...this.data.columns];
  110. if (this.data.show) {
  111. if (!this.data.isMulti) {
  112. // 单行显示
  113. this.data.columns.forEach((item: any, index: number) => {
  114. if (item.value === this.data.value) {
  115. defaultIndex = index;
  116. }
  117. });
  118. } else {
  119. // 多行显示
  120. columns.forEach((item: any, index: number) => {
  121. item.values.forEach((value: any, valueIndex: number) => {
  122. if (value.value === this.data.value[index]) {
  123. item.defaultIndex = valueIndex;
  124. }
  125. });
  126. });
  127. }
  128. }
  129. this.setData(
  130. {
  131. defaultIndex,
  132. columns,
  133. },
  134. () => {
  135. this.setData({
  136. innerShow: this.data.show,
  137. });
  138. }
  139. );
  140. },
  141. _value() {
  142. // console.log(this.data, "value");
  143. this.setData({
  144. innerValue: this.data.value,
  145. });
  146. },
  147. _columns() {
  148. // 判断是多行还是单行
  149. if (this.data.columns[0] && this.data.columns[0].values) {
  150. this.setData({
  151. isMulti: true,
  152. });
  153. } else {
  154. this.setData({
  155. isMulti: false,
  156. });
  157. }
  158. }
  159. },
  160. });