input.js 4.3 KB

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