textarea.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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}-textarea`;
  13. let Textarea = class Textarea extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.options = {
  17. multipleSlots: true,
  18. };
  19. this.behaviors = ['wx://form-field'];
  20. this.externalClasses = [
  21. `${prefix}-class`,
  22. `${prefix}-class-textarea`,
  23. `${prefix}-class-label`,
  24. `${prefix}-class-indicator`,
  25. ];
  26. this.properties = props;
  27. this.data = {
  28. prefix,
  29. classPrefix: name,
  30. count: 0,
  31. };
  32. this.observers = {
  33. value(val) {
  34. this.updateCount(val !== null && val !== void 0 ? val : this.properties.defaultValue);
  35. },
  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.methods = {
  45. updateCount(val) {
  46. const { maxcharacter, maxlength } = this.properties;
  47. const { count } = this.calculateValue(val, maxcharacter, maxlength);
  48. this.setData({
  49. count,
  50. });
  51. },
  52. updateValue(val) {
  53. const { maxcharacter, maxlength } = this.properties;
  54. const { value, count } = this.calculateValue(val, maxcharacter, maxlength);
  55. this.setData({
  56. value,
  57. count,
  58. });
  59. },
  60. calculateValue(value, maxcharacter, maxlength) {
  61. const { allowInputOverMax } = this.properties;
  62. if (maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  63. const { length, characters } = getCharacterLength('maxcharacter', value, allowInputOverMax ? Infinity : maxcharacter);
  64. return {
  65. value: characters,
  66. count: length,
  67. };
  68. }
  69. if (maxlength > 0 && !Number.isNaN(maxlength)) {
  70. const { length, characters } = getCharacterLength('maxlength', value, allowInputOverMax ? Infinity : maxlength);
  71. return {
  72. value: characters,
  73. count: length,
  74. };
  75. }
  76. return {
  77. value,
  78. count: value ? String(value).length : 0,
  79. };
  80. },
  81. onInput(event) {
  82. const { value, cursor } = event.detail;
  83. this.updateValue(value);
  84. this.triggerEvent('change', { value: this.data.value, cursor });
  85. },
  86. onFocus(event) {
  87. this.triggerEvent('focus', Object.assign({}, event.detail));
  88. },
  89. onBlur(event) {
  90. this.triggerEvent('blur', Object.assign({}, event.detail));
  91. },
  92. onConfirm(event) {
  93. this.triggerEvent('enter', Object.assign({}, event.detail));
  94. },
  95. onLineChange(event) {
  96. this.triggerEvent('line-change', Object.assign({}, event.detail));
  97. },
  98. onKeyboardHeightChange(e) {
  99. this.triggerEvent('keyboardheightchange', e.detail);
  100. },
  101. };
  102. }
  103. };
  104. Textarea = __decorate([
  105. wxComponent()
  106. ], Textarea);
  107. export default Textarea;