index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. activeTab: 0,
  176. });
  177. },
  178. onClose() {
  179. this.$emit('close');
  180. },
  181. onClickTab(e) {
  182. const { index: tabIndex, title } = e.detail;
  183. this.$emit('click-tab', { title, tabIndex });
  184. this.setData({
  185. activeTab: tabIndex,
  186. });
  187. },
  188. // 选中
  189. onSelect(e) {
  190. const { option, tabIndex } = e.currentTarget.dataset;
  191. if (option && option.disabled) {
  192. return;
  193. }
  194. const { valueKey, childrenKey } = this.data;
  195. let { tabs } = this.data;
  196. tabs[tabIndex].selected = option;
  197. if (tabs.length > tabIndex + 1) {
  198. tabs = tabs.slice(0, tabIndex + 1);
  199. }
  200. if (option[childrenKey]) {
  201. const nextTab = {
  202. options: option[childrenKey],
  203. selected: null,
  204. };
  205. if (tabs[tabIndex + 1]) {
  206. tabs[tabIndex + 1] = nextTab;
  207. }
  208. else {
  209. tabs.push(nextTab);
  210. }
  211. wx.nextTick(() => {
  212. this.setData({
  213. activeTab: tabIndex + 1,
  214. });
  215. });
  216. }
  217. this.setData({
  218. tabs,
  219. });
  220. const selectedOptions = tabs.map((tab) => tab.selected).filter(Boolean);
  221. const value = option[valueKey];
  222. const params = {
  223. value,
  224. tabIndex,
  225. selectedOptions,
  226. };
  227. this.innerValue = value;
  228. this.$emit('change', params);
  229. if (!option[childrenKey]) {
  230. this.$emit('finish', params);
  231. }
  232. },
  233. },
  234. });
  235. export default global['__wxComponents']['vant/cascader/index']
  236. </script>
  237. <style platform="mp-weixin">
  238. @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}
  239. </style>