index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <uni-shadow-root class="vant-cascader-index"><view v-if="showHeader" class="van-cascader__header">
  3. <slot name="title" v-if="useTitleSlot"></slot>
  4. <text class="van-cascader__title" v-else>{{ title }}</text>
  5. <van-icon v-if="closeable" :name="closeIcon" class="van-cascader__close-icon" @click.native="onClose"></van-icon>
  6. </view>
  7. <van-tabs :active="activeTab" custom-class="van-cascader__tabs" wrap-class="van-cascader__tabs-wrap" tab-class="van-cascader__tab" :color="activeColor" :border="false" :swipeable="swipeable" @click="onClickTab">
  8. <van-tab v-for="(tab,tabIndex) in (tabs)" :key="tab.tabIndex" :title="tab.selected ? tab.selected[textKey] : placeholder" style="width: 100%;" :title-style="(!tab.selected ? 'color: #969799;font-weight:normal;' : '')">
  9. <view class="van-cascader__options">
  10. <view v-for="(option,index) in (tab.options)" :key="option.index" :class="(option.className)+' '+(utils.optionClass(tab, valueKey, option))" :style="utils.optionStyle({ tab, valueKey, option, activeColor })" :data-option="option" :data-tab-index="tabIndex" @click="onSelect">
  11. <text>{{ option[textKey] }}</text>
  12. <van-icon v-if="utils.isSelected(tab, valueKey, option)" name="success" size="18"></van-icon>
  13. </view>
  14. </view>
  15. </van-tab>
  16. </van-tabs></uni-shadow-root>
  17. </template>
  18. <wxs src="./index.wxs" module="utils"></wxs>
  19. <script>
  20. import VanIcon from '../icon/index.vue'
  21. import VanTab from '../tab/index.vue'
  22. import VanTabs from '../tabs/index.vue'
  23. global['__wxVueOptions'] = {components:{'van-icon': VanIcon,'van-tab': VanTab,'van-tabs': VanTabs}}
  24. global['__wxRoute'] = 'vant/cascader/index'
  25. import { VantComponent } from '../common/component';
  26. var FieldName;
  27. (function (FieldName) {
  28. FieldName["TEXT"] = "text";
  29. FieldName["VALUE"] = "value";
  30. FieldName["CHILDREN"] = "children";
  31. })(FieldName || (FieldName = {}));
  32. const defaultFieldNames = {
  33. text: FieldName.TEXT,
  34. value: FieldName.VALUE,
  35. children: FieldName.CHILDREN,
  36. };
  37. VantComponent({
  38. props: {
  39. title: String,
  40. value: {
  41. type: String,
  42. },
  43. placeholder: {
  44. type: String,
  45. value: '请选择',
  46. },
  47. activeColor: {
  48. type: String,
  49. value: '#1989fa',
  50. },
  51. options: {
  52. type: Array,
  53. value: [],
  54. },
  55. swipeable: {
  56. type: Boolean,
  57. value: false,
  58. },
  59. closeable: {
  60. type: Boolean,
  61. value: true,
  62. },
  63. showHeader: {
  64. type: Boolean,
  65. value: true,
  66. },
  67. closeIcon: {
  68. type: String,
  69. value: 'cross',
  70. },
  71. fieldNames: {
  72. type: Object,
  73. value: defaultFieldNames,
  74. observer: 'updateFieldNames',
  75. },
  76. useTitleSlot: Boolean,
  77. },
  78. data: {
  79. tabs: [],
  80. activeTab: 0,
  81. textKey: FieldName.TEXT,
  82. valueKey: FieldName.VALUE,
  83. childrenKey: FieldName.CHILDREN,
  84. innerValue: '',
  85. },
  86. watch: {
  87. options() {
  88. this.updateTabs();
  89. },
  90. value(newVal) {
  91. this.updateValue(newVal);
  92. },
  93. },
  94. created() {
  95. this.updateTabs();
  96. },
  97. methods: {
  98. updateValue(val) {
  99. if (val !== undefined) {
  100. const values = this.data.tabs.map((tab) => tab.selected && tab.selected[this.data.valueKey]);
  101. if (values.indexOf(val) > -1) {
  102. return;
  103. }
  104. }
  105. this.innerValue = val;
  106. this.updateTabs();
  107. },
  108. updateFieldNames() {
  109. const { text = 'text', value = 'value', children = 'children', } = this.data.fieldNames || defaultFieldNames;
  110. this.setData({
  111. textKey: text,
  112. valueKey: value,
  113. childrenKey: children,
  114. });
  115. },
  116. getSelectedOptionsByValue(options, value) {
  117. for (let i = 0; i < options.length; i++) {
  118. const option = options[i];
  119. if (option[this.data.valueKey] === value) {
  120. return [option];
  121. }
  122. if (option[this.data.childrenKey]) {
  123. const selectedOptions = this.getSelectedOptionsByValue(option[this.data.childrenKey], value);
  124. if (selectedOptions) {
  125. return [option, ...selectedOptions];
  126. }
  127. }
  128. }
  129. },
  130. updateTabs() {
  131. const { options } = this.data;
  132. const { innerValue } = this;
  133. if (!options.length) {
  134. return;
  135. }
  136. if (innerValue !== undefined) {
  137. const selectedOptions = this.getSelectedOptionsByValue(options, innerValue);
  138. if (selectedOptions) {
  139. let optionsCursor = options;
  140. const tabs = selectedOptions.map((option) => {
  141. const tab = {
  142. options: optionsCursor,
  143. selected: option,
  144. };
  145. const next = optionsCursor.find((item) => item[this.data.valueKey] === option[this.data.valueKey]);
  146. if (next) {
  147. optionsCursor = next[this.data.childrenKey];
  148. }
  149. return tab;
  150. });
  151. if (optionsCursor) {
  152. tabs.push({
  153. options: optionsCursor,
  154. selected: null,
  155. });
  156. }
  157. this.setData({
  158. tabs,
  159. });
  160. wx.nextTick(() => {
  161. this.setData({
  162. activeTab: tabs.length - 1,
  163. });
  164. });
  165. return;
  166. }
  167. }
  168. this.setData({
  169. tabs: [
  170. {
  171. options,
  172. selected: null,
  173. },
  174. ],
  175. });
  176. },
  177. onClose() {
  178. this.$emit('close');
  179. },
  180. onClickTab(e) {
  181. const { index: tabIndex, title } = e.detail;
  182. this.$emit('click-tab', { title, tabIndex });
  183. this.setData({
  184. activeTab: tabIndex,
  185. });
  186. },
  187. // 选中
  188. onSelect(e) {
  189. const { option, tabIndex } = e.currentTarget.dataset;
  190. if (option && option.disabled) {
  191. return;
  192. }
  193. const { valueKey, childrenKey } = this.data;
  194. let { tabs } = this.data;
  195. tabs[tabIndex].selected = option;
  196. if (tabs.length > tabIndex + 1) {
  197. tabs = tabs.slice(0, tabIndex + 1);
  198. }
  199. if (option[childrenKey]) {
  200. const nextTab = {
  201. options: option[childrenKey],
  202. selected: null,
  203. };
  204. if (tabs[tabIndex + 1]) {
  205. tabs[tabIndex + 1] = nextTab;
  206. }
  207. else {
  208. tabs.push(nextTab);
  209. }
  210. wx.nextTick(() => {
  211. this.setData({
  212. activeTab: tabIndex + 1,
  213. });
  214. });
  215. }
  216. this.setData({
  217. tabs,
  218. });
  219. const selectedOptions = tabs.map((tab) => tab.selected).filter(Boolean);
  220. const value = option[valueKey];
  221. const params = {
  222. value,
  223. tabIndex,
  224. selectedOptions,
  225. };
  226. this.innerValue = value;
  227. this.$emit('change', params);
  228. if (!option[childrenKey]) {
  229. this.$emit('finish', params);
  230. }
  231. },
  232. },
  233. });
  234. export default global['__wxComponents']['vant/cascader/index']
  235. </script>
  236. <style platform="mp-weixin">
  237. @import '../common/index.css';.van-cascader__header{align-items:center;display:flex;height:48px;justify-content:space-between;padding:0 16px}.van-cascader__title{font-size:16px;font-weight:600;line-height:20px}.van-cascader__close-icon{color:#c8c9cc;font-size:22px;height:22px}.van-cascader__tabs-wrap{height:48px!important;padding:0 8px}.van-cascader__tab{color:#323233!important;flex:none!important;font-weight:600!important;padding:0 8px!important}.van-cascader__tab--unselected{color:#969799!important;font-weight:400!important}.van-cascader__option{align-items:center;cursor:pointer;display:flex;font-size:14px;justify-content:space-between;line-height:20px;padding:10px 16px}.van-cascader__option:active{background-color:#f2f3f5}.van-cascader__option--selected{color:#1989fa;font-weight:600}.van-cascader__option--disabled{color:#c8c9cc;cursor:not-allowed}.van-cascader__option--disabled:active{background-color:initial}.van-cascader__options{-webkit-overflow-scrolling:touch;box-sizing:border-box;height:384px;overflow-y:auto;padding-top:6px}
  238. </style>