index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var utils_1 = require("../common/utils");
  15. var component_1 = require("../common/component");
  16. var props_1 = require("./props");
  17. (0, component_1.VantComponent)({
  18. field: true,
  19. classes: ['input-class', 'right-icon-class', 'label-class'],
  20. props: __assign(__assign(__assign(__assign({}, props_1.commonProps), props_1.inputProps), props_1.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: {
  21. type: Boolean,
  22. observer: 'setShowClear',
  23. }, clearable: {
  24. type: Boolean,
  25. observer: 'setShowClear',
  26. }, clearTrigger: {
  27. type: String,
  28. value: 'focus',
  29. }, border: {
  30. type: Boolean,
  31. value: true,
  32. }, titleWidth: {
  33. type: String,
  34. value: '6.2em',
  35. }, clearIcon: {
  36. type: String,
  37. value: 'clear',
  38. }, extraEventParams: {
  39. type: Boolean,
  40. value: false,
  41. } }),
  42. data: {
  43. focused: false,
  44. innerValue: '',
  45. showClear: false,
  46. },
  47. watch: {
  48. value: function (value) {
  49. if (value !== this.value) {
  50. this.setData({ innerValue: value });
  51. this.value = value;
  52. this.setShowClear();
  53. }
  54. },
  55. clearTrigger: function () {
  56. this.setShowClear();
  57. },
  58. },
  59. created: function () {
  60. this.value = this.data.value;
  61. this.setData({ innerValue: this.value });
  62. },
  63. methods: {
  64. formatValue: function (value) {
  65. var maxlength = this.data.maxlength;
  66. if (maxlength !== -1 && value.length > maxlength) {
  67. return value.slice(0, maxlength);
  68. }
  69. return value;
  70. },
  71. onInput: function (event) {
  72. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  73. var formatValue = this.formatValue(value);
  74. this.value = formatValue;
  75. this.setShowClear();
  76. return this.emitChange(__assign(__assign({}, event.detail), { value: formatValue }));
  77. },
  78. onFocus: function (event) {
  79. this.focused = true;
  80. this.setShowClear();
  81. this.$emit('focus', event.detail);
  82. },
  83. onBlur: function (event) {
  84. this.focused = false;
  85. this.setShowClear();
  86. this.$emit('blur', event.detail);
  87. },
  88. onClickIcon: function () {
  89. this.$emit('click-icon');
  90. },
  91. onClickInput: function (event) {
  92. this.$emit('click-input', event.detail);
  93. },
  94. onClear: function () {
  95. var _this = this;
  96. this.setData({ innerValue: '' });
  97. this.value = '';
  98. this.setShowClear();
  99. (0, utils_1.nextTick)(function () {
  100. _this.emitChange({ value: '' });
  101. _this.$emit('clear', '');
  102. });
  103. },
  104. onConfirm: function (event) {
  105. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  106. this.value = value;
  107. this.setShowClear();
  108. this.$emit('confirm', value);
  109. },
  110. setValue: function (value) {
  111. this.value = value;
  112. this.setShowClear();
  113. if (value === '') {
  114. this.setData({ innerValue: '' });
  115. }
  116. this.emitChange({ value: value });
  117. },
  118. onLineChange: function (event) {
  119. this.$emit('linechange', event.detail);
  120. },
  121. onKeyboardHeightChange: function (event) {
  122. this.$emit('keyboardheightchange', event.detail);
  123. },
  124. onBindNicknameReview: function (event) {
  125. this.$emit('nicknamereview', event.detail);
  126. },
  127. emitChange: function (detail) {
  128. var extraEventParams = this.data.extraEventParams;
  129. this.setData({ value: detail.value });
  130. var result;
  131. var data = extraEventParams
  132. ? __assign(__assign({}, detail), { callback: function (data) {
  133. result = data;
  134. } }) : detail.value;
  135. this.$emit('input', data);
  136. this.$emit('change', data);
  137. return result;
  138. },
  139. setShowClear: function () {
  140. var _a = this.data, clearable = _a.clearable, readonly = _a.readonly, clearTrigger = _a.clearTrigger;
  141. var _b = this, focused = _b.focused, value = _b.value;
  142. var showClear = false;
  143. if (clearable && !readonly) {
  144. var hasValue = !!value;
  145. var trigger = clearTrigger === 'always' || (clearTrigger === 'focus' && focused);
  146. showClear = hasValue && trigger;
  147. }
  148. this.setView({ showClear: showClear });
  149. },
  150. noop: function () { },
  151. },
  152. });