index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <demo-section>
  3. <demo-block :title="$t('basicUsage')">
  4. <van-contact-card
  5. :type="cardType"
  6. :name="currentContact.name"
  7. :tel="currentContact.tel"
  8. @click="showList = true"
  9. />
  10. <van-popup
  11. v-model="showList"
  12. position="bottom"
  13. :lazy-render="false"
  14. >
  15. <van-contact-list
  16. v-model="chosenContactId"
  17. :list="list"
  18. @add="onAdd"
  19. @edit="onEdit"
  20. @select="onSelect"
  21. />
  22. </van-popup>
  23. <van-popup
  24. v-model="showEdit"
  25. position="bottom"
  26. :lazy-render="false"
  27. >
  28. <van-contact-edit
  29. :contact-info="editingContact"
  30. :is-edit="isEdit"
  31. @save="onSave"
  32. @delete="onDelete"
  33. />
  34. </van-popup>
  35. </demo-block>
  36. <demo-block :title="$t('uneditable')">
  37. <van-contact-card
  38. type="edit"
  39. :name="mockContact.name"
  40. :tel="mockContact.tel"
  41. :editable="false"
  42. />
  43. </demo-block>
  44. </demo-section>
  45. </template>
  46. <script>
  47. export default {
  48. i18n: {
  49. 'zh-CN': {
  50. name: '张三'
  51. },
  52. 'en-US': {
  53. name: 'John Snow'
  54. }
  55. },
  56. data() {
  57. return {
  58. chosenContactId: null,
  59. editingContact: {},
  60. showList: false,
  61. showEdit: false,
  62. isEdit: false,
  63. list: []
  64. };
  65. },
  66. computed: {
  67. mockContact() {
  68. return {
  69. name: this.$t('name'),
  70. tel: '13000000000',
  71. id: 0
  72. };
  73. },
  74. cardType() {
  75. return this.chosenContactId !== null ? 'edit' : 'add';
  76. },
  77. currentContact() {
  78. const id = this.chosenContactId;
  79. return id !== null ? this.list.filter(item => item.id === id)[0] : {};
  80. }
  81. },
  82. created() {
  83. this.list.push(this.mockContact);
  84. },
  85. methods: {
  86. onAdd() {
  87. this.editingContact = { id: this.list.length };
  88. this.isEdit = false;
  89. this.showEdit = true;
  90. },
  91. onEdit(item) {
  92. this.isEdit = true;
  93. this.showEdit = true;
  94. this.editingContact = item;
  95. },
  96. onSelect() {
  97. this.showList = false;
  98. },
  99. onSave(info) {
  100. this.showEdit = false;
  101. this.showList = false;
  102. if (this.isEdit) {
  103. this.list = this.list.map(item => (item.id === info.id ? info : item));
  104. } else {
  105. this.list.push(info);
  106. }
  107. this.chosenContactId = info.id;
  108. },
  109. onDelete(info) {
  110. this.showEdit = false;
  111. this.list = this.list.filter(item => item.id !== info.id);
  112. if (this.chosenContactId === info.id) {
  113. this.chosenContactId = null;
  114. }
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="less">
  120. .demo-contact-card {
  121. .van-popup {
  122. height: 100%;
  123. background-color: #f2f2f2;
  124. }
  125. }
  126. </style>