index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { use } from '../utils';
  2. import Button from '../button';
  3. import Field from '../field';
  4. import Toast from '../toast';
  5. import Dialog from '../dialog';
  6. import { isMobile } from '../utils/validate/mobile';
  7. const [sfc, bem, t] = use('contact-edit');
  8. const defaultContact = {
  9. tel: '',
  10. name: ''
  11. };
  12. export default sfc({
  13. props: {
  14. isEdit: Boolean,
  15. isSaving: Boolean,
  16. isDeleting: Boolean,
  17. contactInfo: {
  18. type: Object,
  19. default: () => ({ ...defaultContact })
  20. },
  21. telValidator: {
  22. type: Function,
  23. default: isMobile
  24. }
  25. },
  26. data() {
  27. return {
  28. data: {
  29. ...defaultContact,
  30. ...this.contactInfo
  31. },
  32. errorInfo: {
  33. name: false,
  34. tel: false
  35. }
  36. };
  37. },
  38. watch: {
  39. contactInfo(val) {
  40. this.data = {
  41. ...defaultContact,
  42. ...val
  43. };
  44. }
  45. },
  46. methods: {
  47. onFocus(key) {
  48. this.errorInfo[key] = false;
  49. },
  50. getErrorMessageByKey(key) {
  51. const 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() {
  60. const isValid = ['name', 'tel'].every(item => {
  61. const msg = this.getErrorMessageByKey(item);
  62. if (msg) {
  63. this.errorInfo[item] = true;
  64. Toast(msg);
  65. }
  66. return !msg;
  67. });
  68. if (isValid && !this.isSaving) {
  69. this.$emit('save', this.data);
  70. }
  71. },
  72. onDelete() {
  73. Dialog.confirm({
  74. message: t('confirmDelete')
  75. }).then(() => {
  76. this.$emit('delete', this.data);
  77. });
  78. }
  79. },
  80. render(h) {
  81. const { data, errorInfo } = this;
  82. const onFocus = name => () => this.onFocus(name);
  83. return (
  84. <div class={bem()}>
  85. <Field
  86. vModel={data.name}
  87. clearable
  88. maxlength="30"
  89. label={t('name')}
  90. placeholder={t('nameEmpty')}
  91. error={errorInfo.name}
  92. onFocus={onFocus('name')}
  93. />
  94. <Field
  95. vModel={data.tel}
  96. clearable
  97. type="tel"
  98. label={t('tel')}
  99. placeholder={t('telEmpty')}
  100. error={errorInfo.tel}
  101. onFocus={onFocus('tel')}
  102. />
  103. <div class={bem('buttons')}>
  104. <Button
  105. block
  106. type="danger"
  107. text={t('save')}
  108. loading={this.isSaving}
  109. onClick={this.onSave}
  110. />
  111. {this.isEdit && (
  112. <Button block text={t('delete')} loading={this.isDeleting} onClick={this.onDelete} />
  113. )}
  114. </div>
  115. </div>
  116. );
  117. }
  118. });