input.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 } 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-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-error-msg`,
  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);
  40. },
  41. };
  42. this.methods = {
  43. updateValue(value) {
  44. const { maxcharacter, maxlength } = this.properties;
  45. if (maxcharacter && maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  46. const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
  47. this.setData({
  48. value: characters,
  49. count: length,
  50. });
  51. }
  52. else if (maxlength > 0 && !Number.isNaN(maxlength)) {
  53. const { length, characters } = getCharacterLength('maxlength', value, maxlength);
  54. this.setData({
  55. value: characters,
  56. count: length,
  57. });
  58. }
  59. else {
  60. this.setData({
  61. value,
  62. count: value ? String(value).length : 0,
  63. });
  64. }
  65. },
  66. onInput(e) {
  67. const { value, cursor, keyCode } = e.detail;
  68. this.updateValue(value);
  69. this.triggerEvent('change', { value: this.data.value, cursor, keyCode });
  70. },
  71. onFocus(e) {
  72. this.triggerEvent('focus', e.detail);
  73. },
  74. onBlur(e) {
  75. this.triggerEvent('blur', e.detail);
  76. },
  77. onConfirm(e) {
  78. this.triggerEvent('enter', e.detail);
  79. },
  80. clearInput(e) {
  81. this.triggerEvent('clear', e.detail);
  82. this.setData({ value: '' });
  83. },
  84. onKeyboardHeightChange(e) {
  85. this.triggerEvent('keyboardheightchange', e.detail);
  86. },
  87. };
  88. }
  89. };
  90. Input = __decorate([
  91. wxComponent()
  92. ], Input);
  93. export default Input;