index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { nextTick } from '../common/utils';
  2. import { VantComponent } from '../common/component';
  3. import { commonProps, inputProps, textareaProps } from './props';
  4. VantComponent({
  5. field: true,
  6. classes: ['input-class', 'right-icon-class', 'label-class'],
  7. props: Object.assign(Object.assign(Object.assign(Object.assign({}, commonProps), inputProps), textareaProps), { size: String, icon: String, label: String, error: Boolean, center: Boolean, isLink: Boolean, leftIcon: String, rightIcon: String, autosize: null, required: Boolean, iconClass: String, clickable: Boolean, inputAlign: String, customStyle: String, errorMessage: String, arrowDirection: String, showWordLimit: Boolean, errorMessageAlign: String, readonly: {
  8. type: Boolean,
  9. observer: 'setShowClear',
  10. }, clearable: {
  11. type: Boolean,
  12. observer: 'setShowClear',
  13. }, clearTrigger: {
  14. type: String,
  15. value: 'focus',
  16. }, border: {
  17. type: Boolean,
  18. value: true,
  19. }, titleWidth: {
  20. type: String,
  21. value: '6.2em',
  22. }, clearIcon: {
  23. type: String,
  24. value: 'clear',
  25. }, extraEventParams: {
  26. type: Boolean,
  27. value: false,
  28. } }),
  29. data: {
  30. focused: false,
  31. innerValue: '',
  32. showClear: false,
  33. },
  34. watch: {
  35. value(value) {
  36. if (value !== this.value) {
  37. this.setData({ innerValue: value });
  38. this.value = value;
  39. this.setShowClear();
  40. }
  41. },
  42. clearTrigger() {
  43. this.setShowClear();
  44. },
  45. },
  46. created() {
  47. this.value = this.data.value;
  48. this.setData({ innerValue: this.value });
  49. },
  50. methods: {
  51. formatValue(value) {
  52. const { maxlength } = this.data;
  53. if (maxlength !== -1 && value.length > maxlength) {
  54. return value.slice(0, maxlength);
  55. }
  56. return value;
  57. },
  58. onInput(event) {
  59. const { value = '' } = event.detail || {};
  60. const formatValue = this.formatValue(value);
  61. this.value = formatValue;
  62. this.setShowClear();
  63. return this.emitChange(Object.assign(Object.assign({}, event.detail), { value: formatValue }));
  64. },
  65. onFocus(event) {
  66. this.focused = true;
  67. this.setShowClear();
  68. this.$emit('focus', event.detail);
  69. },
  70. onBlur(event) {
  71. this.focused = false;
  72. this.setShowClear();
  73. this.$emit('blur', event.detail);
  74. },
  75. onClickIcon() {
  76. this.$emit('click-icon');
  77. },
  78. onClickInput(event) {
  79. this.$emit('click-input', event.detail);
  80. },
  81. onClear() {
  82. this.setData({ innerValue: '' });
  83. this.value = '';
  84. this.setShowClear();
  85. nextTick(() => {
  86. this.emitChange({ value: '' });
  87. this.$emit('clear', '');
  88. });
  89. },
  90. onConfirm(event) {
  91. const { value = '' } = event.detail || {};
  92. this.value = value;
  93. this.setShowClear();
  94. this.$emit('confirm', value);
  95. },
  96. setValue(value) {
  97. this.value = value;
  98. this.setShowClear();
  99. if (value === '') {
  100. this.setData({ innerValue: '' });
  101. }
  102. this.emitChange({ value });
  103. },
  104. onLineChange(event) {
  105. this.$emit('linechange', event.detail);
  106. },
  107. onKeyboardHeightChange(event) {
  108. this.$emit('keyboardheightchange', event.detail);
  109. },
  110. onBindNicknameReview(event) {
  111. this.$emit('nicknamereview', event.detail);
  112. },
  113. emitChange(detail) {
  114. const { extraEventParams } = this.data;
  115. this.setData({ value: detail.value });
  116. let result;
  117. const data = extraEventParams
  118. ? Object.assign(Object.assign({}, detail), { callback: (data) => {
  119. result = data;
  120. } }) : detail.value;
  121. this.$emit('input', data);
  122. this.$emit('change', data);
  123. return result;
  124. },
  125. setShowClear() {
  126. const { clearable, readonly, clearTrigger } = this.data;
  127. const { focused, value } = this;
  128. let showClear = false;
  129. if (clearable && !readonly) {
  130. const hasValue = !!value;
  131. const trigger = clearTrigger === 'always' || (clearTrigger === 'focus' && focused);
  132. showClear = hasValue && trigger;
  133. }
  134. this.setView({ showClear });
  135. },
  136. noop() { },
  137. },
  138. });