index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { use } from '../utils';
  3. import Button from '../button';
  4. import Field from '../field';
  5. import Toast from '../toast';
  6. import Dialog from '../dialog';
  7. import { isMobile } from '../utils/validate/mobile';
  8. var _use = use('contact-edit'),
  9. sfc = _use[0],
  10. bem = _use[1],
  11. t = _use[2];
  12. var defaultContact = {
  13. tel: '',
  14. name: ''
  15. };
  16. export default sfc({
  17. props: {
  18. isEdit: Boolean,
  19. isSaving: Boolean,
  20. isDeleting: Boolean,
  21. contactInfo: {
  22. type: Object,
  23. "default": function _default() {
  24. return _extends({}, defaultContact);
  25. }
  26. },
  27. telValidator: {
  28. type: Function,
  29. "default": isMobile
  30. }
  31. },
  32. data: function data() {
  33. return {
  34. data: _extends({}, defaultContact, this.contactInfo),
  35. errorInfo: {
  36. name: false,
  37. tel: false
  38. }
  39. };
  40. },
  41. watch: {
  42. contactInfo: function contactInfo(val) {
  43. this.data = _extends({}, defaultContact, val);
  44. }
  45. },
  46. methods: {
  47. onFocus: function onFocus(key) {
  48. this.errorInfo[key] = false;
  49. },
  50. getErrorMessageByKey: function getErrorMessageByKey(key) {
  51. var value = this.data[key].trim();
  52. switch (key) {
  53. case 'name':
  54. return value ? '' : t('nameEmpty');
  55. case 'tel':
  56. return this.telValidator(value) ? '' : t('telInvalid');
  57. }
  58. },
  59. onSave: function onSave() {
  60. var _this = this;
  61. var isValid = ['name', 'tel'].every(function (item) {
  62. var msg = _this.getErrorMessageByKey(item);
  63. if (msg) {
  64. _this.errorInfo[item] = true;
  65. Toast(msg);
  66. }
  67. return !msg;
  68. });
  69. if (isValid && !this.isSaving) {
  70. this.$emit('save', this.data);
  71. }
  72. },
  73. onDelete: function onDelete() {
  74. var _this2 = this;
  75. Dialog.confirm({
  76. message: t('confirmDelete')
  77. }).then(function () {
  78. _this2.$emit('delete', _this2.data);
  79. });
  80. }
  81. },
  82. render: function render(h) {
  83. var _this3 = this;
  84. var data = this.data,
  85. errorInfo = this.errorInfo;
  86. var onFocus = function onFocus(name) {
  87. return function () {
  88. return _this3.onFocus(name);
  89. };
  90. };
  91. return h("div", {
  92. "class": bem()
  93. }, [h(Field, {
  94. "attrs": {
  95. "clearable": true,
  96. "maxlength": "30",
  97. "label": t('name'),
  98. "placeholder": t('nameEmpty'),
  99. "error": errorInfo.name
  100. },
  101. "on": {
  102. "focus": onFocus('name')
  103. },
  104. "model": {
  105. value: data.name,
  106. callback: function callback($$v) {
  107. data.name = $$v;
  108. }
  109. }
  110. }), h(Field, {
  111. "attrs": {
  112. "clearable": true,
  113. "type": "tel",
  114. "label": t('tel'),
  115. "placeholder": t('telEmpty'),
  116. "error": errorInfo.tel
  117. },
  118. "on": {
  119. "focus": onFocus('tel')
  120. },
  121. "model": {
  122. value: data.tel,
  123. callback: function callback($$v) {
  124. data.tel = $$v;
  125. }
  126. }
  127. }), h("div", {
  128. "class": bem('buttons')
  129. }, [h(Button, {
  130. "attrs": {
  131. "block": true,
  132. "type": "danger",
  133. "text": t('save'),
  134. "loading": this.isSaving
  135. },
  136. "on": {
  137. "click": this.onSave
  138. }
  139. }), this.isEdit && h(Button, {
  140. "attrs": {
  141. "block": true,
  142. "text": t('delete'),
  143. "loading": this.isDeleting
  144. },
  145. "on": {
  146. "click": this.onDelete
  147. }
  148. })])]);
  149. }
  150. });