index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div :class="classes">
  3. <template v-if="!swipeEdition">
  4. <general-shell
  5. v-for="(item, index) of dataArray"
  6. :key="index"
  7. :item="item"
  8. :longPress="longPress"
  9. @delIcon="clickDelIcon"
  10. @editIcon="clickEditIcon"
  11. @itemClick="clickContentItem"
  12. @swipeDel="clickSwipeDel"
  13. @longCopy="clickLongCopy"
  14. @longSet="clickLongSet"
  15. @longDel="clickLongDel"
  16. >
  17. <template v-slot:contentInfo v-if="longPress">
  18. <slot name="iteminfos"></slot>
  19. </template>
  20. <template v-slot:contentIcons v-if="longPress">
  21. <slot name="itemicon"></slot>
  22. </template>
  23. <template v-slot:contentAddrs v-if="longPress">
  24. <slot name="itemaddr"></slot>
  25. </template>
  26. <template v-slot:longpressAll v-if="longPress">
  27. <slot name="longpressbtns"></slot>
  28. </template>
  29. </general-shell>
  30. </template>
  31. <template v-if="swipeEdition">
  32. <swipe-shell
  33. v-for="(item, index) of dataArray"
  34. :key="index"
  35. :item="item"
  36. @delIcon="clickDelIcon"
  37. @editIcon="clickEditIcon"
  38. @itemClick="clickContentItem"
  39. @swipeDel="clickSwipeDel"
  40. >
  41. <template v-slot:contentInfo>
  42. <slot name="iteminfos"></slot>
  43. </template>
  44. <template v-slot:contentIcons>
  45. <slot name="itemicon"></slot>
  46. </template>
  47. <template v-slot:contentAddrs>
  48. <slot name="itemaddr"></slot>
  49. </template>
  50. <template v-slot:swiperightbtn>
  51. <slot name="swiperight"></slot>
  52. </template>
  53. </swipe-shell>
  54. </template>
  55. <div class="nut-address-list__bottom" v-if="showBottomButton" @click="addAddress">
  56. <nut-button block type="danger">{{ translate('addAddress') }}</nut-button>
  57. </div>
  58. </div>
  59. </template>
  60. <script lang="ts">
  61. import { reactive, onMounted, ref, watch, computed } from 'vue';
  62. import { createComponent } from '@/packages/utils/create';
  63. const { componentName, create, translate } = createComponent('address-list');
  64. import SwipeShell from './components/SwipeShell.vue';
  65. import GeneralShell from './components/GeneralShell.vue';
  66. import { floatData } from '@/packages/utils/util';
  67. import Button from '../button/index.vue';
  68. export default create({
  69. props: {
  70. data: {
  71. type: Array,
  72. default: []
  73. },
  74. longPress: {
  75. type: Boolean,
  76. default: false
  77. },
  78. swipeEdition: {
  79. type: Boolean,
  80. default: false
  81. },
  82. showBottomButton: {
  83. type: Boolean,
  84. default: true
  85. },
  86. dataMapOptions: {
  87. type: Object,
  88. default: {}
  89. }
  90. },
  91. components: {
  92. SwipeShell,
  93. GeneralShell,
  94. [Button.name]: Button
  95. },
  96. emits: ['delIcon', 'editIcon', 'itemClick', 'longCopy', 'longSet', 'longDel', 'swipeDel', 'add'],
  97. setup(props, { emit }) {
  98. const dataArray = ref([]) as any;
  99. const dataInfo = reactive({
  100. id: 2,
  101. addressName: '姓名',
  102. phone: '123****4567',
  103. defaultAddress: false,
  104. fullAddress: '北京市通州区测试测试测试测试测试测试测试测试测试'
  105. });
  106. const classes = computed(() => {
  107. const prefixCls = componentName;
  108. return {
  109. [prefixCls]: true
  110. };
  111. });
  112. //磨平参数差异
  113. const trowelData = () => {
  114. if (Object.keys(props.dataMapOptions).length > 0) {
  115. dataArray.value = props.data.map((item) => {
  116. return floatData(dataInfo, item, props.dataMapOptions);
  117. });
  118. }
  119. };
  120. watch(
  121. () => props.data,
  122. () => trowelData(),
  123. { deep: true }
  124. );
  125. const clickDelIcon = (event: Event, item: unknown) => {
  126. emit('delIcon', event, item);
  127. event.stopPropagation();
  128. };
  129. const clickEditIcon = (event: Event, item: unknown) => {
  130. emit('editIcon', event, item);
  131. event.stopPropagation();
  132. };
  133. const clickContentItem = (event: Event, item: unknown) => {
  134. emit('itemClick', event, item);
  135. event.stopPropagation();
  136. };
  137. const clickLongCopy = (event: Event, item: unknown) => {
  138. emit('longCopy', event, item);
  139. event.stopPropagation();
  140. };
  141. const clickLongSet = (event: Event, item: unknown) => {
  142. emit('longSet', event, item);
  143. event.stopPropagation();
  144. };
  145. const clickLongDel = (event: Event, item: unknown) => {
  146. emit('longDel', event, item);
  147. event.stopPropagation();
  148. };
  149. const clickSwipeDel = (event: Event, item: unknown) => {
  150. emit('swipeDel', event, item);
  151. event.stopPropagation();
  152. };
  153. const addAddress = (event: Event) => {
  154. emit('add', event);
  155. event.stopPropagation();
  156. };
  157. onMounted(() => {
  158. trowelData();
  159. });
  160. return {
  161. classes,
  162. clickDelIcon,
  163. clickEditIcon,
  164. clickContentItem,
  165. clickLongCopy,
  166. clickLongSet,
  167. clickLongDel,
  168. clickSwipeDel,
  169. addAddress,
  170. dataArray,
  171. translate
  172. };
  173. }
  174. });
  175. </script>