input.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 } 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. };
  36. this.lifetimes = {
  37. ready() {
  38. const { value } = this.properties;
  39. this.updateValue(value == null ? '' : value);
  40. },
  41. };
  42. this.observers = {
  43. prefixIcon(v) {
  44. this.setData({
  45. _prefixIcon: calcIcon(v),
  46. });
  47. },
  48. suffixIcon(v) {
  49. this.setData({
  50. _suffixIcon: calcIcon(v),
  51. });
  52. },
  53. clearable(v) {
  54. this.setData({
  55. _clearIcon: calcIcon(v, 'close-circle-filled'),
  56. });
  57. },
  58. };
  59. this.methods = {
  60. updateValue(value) {
  61. const { maxcharacter, maxlength } = this.properties;
  62. if (maxcharacter && maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  63. const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
  64. this.setData({
  65. value: characters,
  66. count: length,
  67. });
  68. }
  69. else if (maxlength > 0 && !Number.isNaN(maxlength)) {
  70. const { length, characters } = getCharacterLength('maxlength', value, maxlength);
  71. this.setData({
  72. value: characters,
  73. count: length,
  74. });
  75. }
  76. else {
  77. this.setData({
  78. value,
  79. count: value ? String(value).length : 0,
  80. });
  81. }
  82. },
  83. onInput(e) {
  84. const { value, cursor, keyCode } = e.detail;
  85. this.updateValue(value);
  86. this.triggerEvent('change', { value: this.data.value, cursor, keyCode });
  87. },
  88. onFocus(e) {
  89. this.triggerEvent('focus', e.detail);
  90. },
  91. onBlur(e) {
  92. this.triggerEvent('blur', e.detail);
  93. },
  94. onConfirm(e) {
  95. this.triggerEvent('enter', e.detail);
  96. },
  97. onSuffixClick() {
  98. this.triggerEvent('click', { trigger: 'suffix' });
  99. },
  100. onSuffixIconClick() {
  101. this.triggerEvent('click', { trigger: 'suffix-icon' });
  102. },
  103. clearInput(e) {
  104. this.triggerEvent('clear', e.detail);
  105. this.setData({ value: '' });
  106. },
  107. onKeyboardHeightChange(e) {
  108. this.triggerEvent('keyboardheightchange', e.detail);
  109. },
  110. onNickNameReview(e) {
  111. this.triggerEvent('nicknamereview', e.detail);
  112. },
  113. };
  114. }
  115. };
  116. Input = __decorate([
  117. wxComponent()
  118. ], Input);
  119. export default Input;