input.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { getCharacterLength, calcIcon, isDef } from '../common/utils';
  11. const { prefix } = config;
  12. const name = `${prefix}-input`;
  13. let Input = class Input extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.options = {
  17. multipleSlots: true,
  18. };
  19. this.externalClasses = [
  20. `${prefix}-class`,
  21. `${prefix}-class-prefix-icon`,
  22. `${prefix}-class-label`,
  23. `${prefix}-class-input`,
  24. `${prefix}-class-clearable`,
  25. `${prefix}-class-suffix`,
  26. `${prefix}-class-suffix-icon`,
  27. `${prefix}-class-tips`,
  28. ];
  29. this.behaviors = ['wx://form-field'];
  30. this.properties = props;
  31. this.data = {
  32. prefix,
  33. classPrefix: name,
  34. classBasePrefix: prefix,
  35. showClearIcon: true,
  36. };
  37. this.lifetimes = {
  38. ready() {
  39. var _a;
  40. const { value, defaultValue } = this.properties;
  41. this.updateValue((_a = value !== null && value !== void 0 ? value : defaultValue) !== null && _a !== void 0 ? _a : '');
  42. },
  43. };
  44. this.observers = {
  45. prefixIcon(v) {
  46. this.setData({
  47. _prefixIcon: calcIcon(v),
  48. });
  49. },
  50. suffixIcon(v) {
  51. this.setData({
  52. _suffixIcon: calcIcon(v),
  53. });
  54. },
  55. clearable(v) {
  56. this.setData({
  57. _clearIcon: calcIcon(v, 'close-circle-filled'),
  58. });
  59. },
  60. 'clearTrigger, clearable, disabled, readonly'() {
  61. this.updateClearIconVisible();
  62. },
  63. };
  64. this.methods = {
  65. updateValue(value) {
  66. const { allowInputOverMax, maxcharacter, maxlength } = this.properties;
  67. if (!allowInputOverMax && maxcharacter && maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  68. const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
  69. this.setData({
  70. value: characters,
  71. count: length,
  72. });
  73. }
  74. else if (!allowInputOverMax && maxlength && maxlength > 0 && !Number.isNaN(maxlength)) {
  75. const { length, characters } = getCharacterLength('maxlength', value, maxlength);
  76. this.setData({
  77. value: characters,
  78. count: length,
  79. });
  80. }
  81. else {
  82. this.setData({
  83. value,
  84. count: isDef(value) ? String(value).length : 0,
  85. });
  86. }
  87. },
  88. updateClearIconVisible(value = false) {
  89. const { clearTrigger, disabled, readonly } = this.properties;
  90. if (disabled || readonly) {
  91. this.setData({ showClearIcon: false });
  92. return;
  93. }
  94. this.setData({ showClearIcon: value || clearTrigger === 'always' });
  95. },
  96. onInput(e) {
  97. const { value, cursor, keyCode } = e.detail;
  98. this.updateValue(value);
  99. this.triggerEvent('change', { value: this.data.value, cursor, keyCode });
  100. },
  101. onFocus(e) {
  102. this.updateClearIconVisible(true);
  103. this.triggerEvent('focus', e.detail);
  104. },
  105. onBlur(e) {
  106. this.updateClearIconVisible();
  107. if (typeof this.properties.format === 'function') {
  108. const v = this.properties.format(e.detail.value);
  109. this.updateValue(v);
  110. this.triggerEvent('blur', { value: this.data.value, cursor: this.data.count });
  111. return;
  112. }
  113. this.triggerEvent('blur', e.detail);
  114. },
  115. onConfirm(e) {
  116. this.triggerEvent('enter', e.detail);
  117. },
  118. onSuffixClick() {
  119. this.triggerEvent('click', { trigger: 'suffix' });
  120. },
  121. onSuffixIconClick() {
  122. this.triggerEvent('click', { trigger: 'suffix-icon' });
  123. },
  124. clearInput(e) {
  125. this.triggerEvent('clear', e.detail);
  126. this.setData({ value: '' });
  127. },
  128. onKeyboardHeightChange(e) {
  129. this.triggerEvent('keyboardheightchange', e.detail);
  130. },
  131. onNickNameReview(e) {
  132. this.triggerEvent('nicknamereview', e.detail);
  133. },
  134. };
  135. }
  136. };
  137. Input = __decorate([
  138. wxComponent()
  139. ], Input);
  140. export default Input;