index.js 3.9 KB

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