textarea.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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);
  35. },
  36. };
  37. this.lifetimes = {
  38. ready() {
  39. const { value } = this.properties;
  40. this.updateValue(value == null ? '' : value);
  41. },
  42. };
  43. this.methods = {
  44. updateCount(val) {
  45. const { maxcharacter, maxlength } = this.properties;
  46. const { count } = this.calculateValue(val, maxcharacter, maxlength);
  47. this.setData({
  48. count,
  49. });
  50. },
  51. updateValue(val) {
  52. const { maxcharacter, maxlength } = this.properties;
  53. const { value, count } = this.calculateValue(val, maxcharacter, maxlength);
  54. this.setData({
  55. value,
  56. count,
  57. });
  58. },
  59. calculateValue(value, maxcharacter, maxlength) {
  60. if (maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  61. const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
  62. return {
  63. value: characters,
  64. count: length,
  65. };
  66. }
  67. if (maxlength > 0 && !Number.isNaN(maxlength)) {
  68. const { length, characters } = getCharacterLength('maxlength', value, maxlength);
  69. return {
  70. value: characters,
  71. count: length,
  72. };
  73. }
  74. return {
  75. value,
  76. count: value ? String(value).length : 0,
  77. };
  78. },
  79. onInput(event) {
  80. const { value } = event.detail;
  81. this.updateValue(value);
  82. this.triggerEvent('change', { value: this.data.value });
  83. },
  84. onFocus(event) {
  85. this.triggerEvent('focus', Object.assign({}, event.detail));
  86. },
  87. onBlur(event) {
  88. this.triggerEvent('blur', Object.assign({}, event.detail));
  89. },
  90. onConfirm(event) {
  91. this.triggerEvent('enter', Object.assign({}, event.detail));
  92. },
  93. onLineChange(event) {
  94. this.triggerEvent('lineChange', Object.assign({}, event.detail));
  95. },
  96. onKeyboardHeightChange(e) {
  97. this.triggerEvent('keyboardheightchange', e.detail);
  98. },
  99. };
  100. }
  101. };
  102. Textarea = __decorate([
  103. wxComponent()
  104. ], Textarea);
  105. export default Textarea;