index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // components/w-search-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] as any,
  14. value: "",
  15. observer: "_value",
  16. },
  17. searchValue: {
  18. type: String,
  19. value: "",
  20. observer: "_searchValue",
  21. },
  22. /** 是否显示 */
  23. show: {
  24. type: Boolean,
  25. value: false,
  26. observer: "_show",
  27. },
  28. /**
  29. * 列数据
  30. * @example 单列 [{ text: a, value: 1 }, { text: b, value: 2 }]
  31. */
  32. columns: {
  33. type: Array as any,
  34. value: [],
  35. },
  36. /**
  37. * 可见的选项个数
  38. */
  39. visibleItemCount: {
  40. type: Number,
  41. value: 6,
  42. },
  43. itemHeight: {
  44. type: Number,
  45. value: 46,
  46. },
  47. searchLoading: {
  48. type: Boolean,
  49. value: false,
  50. observer: "_searchLoading",
  51. },
  52. placeholder: {
  53. type: String,
  54. value: "请输入搜索关键词",
  55. },
  56. },
  57. /**
  58. * 组件的初始数据
  59. */
  60. data: {
  61. innerShow: false,
  62. showAfterLeave: false,
  63. innerValue: "" as any, // 单列时为string, 多列为array
  64. defaultIndex: 0, // 默认选中的index 单行显示的
  65. innerLoading: false,
  66. searchName: "",
  67. },
  68. /**
  69. * 组件的方法列表
  70. */
  71. methods: {
  72. onSubmit() {
  73. this.setData({
  74. show: false,
  75. value: this.data.innerValue,
  76. });
  77. const columns = this.data.columns || [];
  78. let detail: any = {};
  79. if (this.data.innerValue) {
  80. columns.forEach((item: any) => {
  81. if (item.value === this.data.innerValue) {
  82. detail = item;
  83. }
  84. });
  85. }
  86. this.triggerEvent("confirm", detail);
  87. this.triggerEvent("input", this.data.innerValue);
  88. },
  89. onChange(event: any) {
  90. const detail = event.detail.value;
  91. this.setData({
  92. innerValue: detail.value,
  93. });
  94. this.triggerEvent("change", detail);
  95. },
  96. onClose() {
  97. this.triggerEvent("cancel");
  98. },
  99. onSearchChange(event: any) {
  100. this.triggerEvent("search-change", event.detail);
  101. },
  102. onSearch(event: any) {
  103. // 如果在搜索中,不允许搜索
  104. if(this.data.innerLoading) {
  105. return;
  106. }
  107. this.setData({
  108. searchValue: event.detail,
  109. });
  110. this.triggerEvent("search", event.detail);
  111. },
  112. onAfterLeave() {
  113. this.setData({
  114. showAfterLeave: true,
  115. });
  116. },
  117. onBeforeEnter() {
  118. this.setData({
  119. showAfterLeave: false,
  120. });
  121. },
  122. _show() {
  123. // 单行显示 需要设置默认选中的index
  124. let defaultIndex = 0;
  125. let innerValue = this.data.innerValue;
  126. if (this.data.show) {
  127. // 单行显示
  128. this.data.columns.forEach((item: any, index: number) => {
  129. if (item.value === this.data.value) {
  130. defaultIndex = index;
  131. }
  132. if (!innerValue) {
  133. innerValue = item.value;
  134. }
  135. });
  136. }
  137. this.setData(
  138. {
  139. defaultIndex,
  140. innerValue,
  141. },
  142. () => {
  143. this.setData({
  144. innerShow: this.data.show,
  145. });
  146. }
  147. );
  148. },
  149. _value() {
  150. this.setData({
  151. innerValue: this.data.value,
  152. });
  153. },
  154. _searchLoading() {
  155. this.setData({
  156. innerLoading: this.data.searchLoading,
  157. });
  158. },
  159. _searchValue() {
  160. this.setData({
  161. searchName: this.data.searchValue,
  162. });
  163. },
  164. },
  165. });