textarea.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = [`${prefix}-class`, `${prefix}-class-textarea`, `${prefix}-class-label`];
  21. this.properties = Object.assign(Object.assign({}, props), { cursorSpacing: {
  22. type: Number,
  23. value: 0,
  24. } });
  25. this.data = {
  26. prefix,
  27. classPrefix: name,
  28. count: 0,
  29. };
  30. this.lifetimes = {
  31. ready() {
  32. const { value } = this.properties;
  33. this.updateValue(value);
  34. },
  35. };
  36. this.methods = {
  37. updateValue(value) {
  38. const { maxcharacter, maxlength } = this.properties;
  39. if (maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
  40. const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
  41. this.setData({
  42. value: characters,
  43. count: length,
  44. });
  45. }
  46. else if (maxlength > 0 && !Number.isNaN(maxlength)) {
  47. const { length, characters } = getCharacterLength('maxlength', value, maxlength);
  48. this.setData({
  49. value: characters,
  50. count: length,
  51. });
  52. }
  53. else {
  54. this.setData({
  55. value,
  56. count: value ? String(value).length : 0,
  57. });
  58. }
  59. },
  60. onInput(event) {
  61. const { value } = event.detail;
  62. this.updateValue(value);
  63. this.triggerEvent('change', { value: this.data.value });
  64. },
  65. onFocus(event) {
  66. this.triggerEvent('focus', Object.assign({}, event.detail));
  67. },
  68. onBlur(event) {
  69. this.triggerEvent('blur', Object.assign({}, event.detail));
  70. },
  71. onConfirm(event) {
  72. this.triggerEvent('enter', Object.assign({}, event.detail));
  73. },
  74. onLineChange(event) {
  75. this.triggerEvent('lineChange', Object.assign({}, event.detail));
  76. },
  77. };
  78. }
  79. };
  80. Textarea = __decorate([
  81. wxComponent()
  82. ], Textarea);
  83. export default Textarea;