index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { VantComponent } from '../common/component';
  2. import { commonProps, inputProps, textareaProps } from './props';
  3. VantComponent({
  4. field: true,
  5. classes: ['input-class', 'right-icon-class', 'label-class'],
  6. props: Object.assign(
  7. Object.assign(
  8. Object.assign(Object.assign({}, commonProps), inputProps),
  9. textareaProps
  10. ),
  11. {
  12. size: String,
  13. icon: String,
  14. label: String,
  15. error: Boolean,
  16. center: Boolean,
  17. isLink: Boolean,
  18. leftIcon: String,
  19. rightIcon: String,
  20. autosize: [Boolean, Object],
  21. required: Boolean,
  22. iconClass: String,
  23. clickable: Boolean,
  24. inputAlign: String,
  25. customStyle: String,
  26. errorMessage: String,
  27. arrowDirection: String,
  28. showWordLimit: Boolean,
  29. errorMessageAlign: String,
  30. readonly: {
  31. type: Boolean,
  32. observer: 'setShowClear',
  33. },
  34. clearable: {
  35. type: Boolean,
  36. observer: 'setShowClear',
  37. },
  38. border: {
  39. type: Boolean,
  40. value: true,
  41. },
  42. titleWidth: {
  43. type: String,
  44. value: '6.2em',
  45. },
  46. }
  47. ),
  48. data: {
  49. focused: false,
  50. innerValue: '',
  51. showClear: false,
  52. },
  53. created() {
  54. this.value = this.data.value;
  55. this.setData({ innerValue: this.value });
  56. },
  57. methods: {
  58. onInput(event) {
  59. const { value = '' } = event.detail || {};
  60. this.value = value;
  61. this.setShowClear();
  62. this.emitChange();
  63. },
  64. onFocus(event) {
  65. this.focused = true;
  66. this.setShowClear();
  67. this.$emit('focus', event.detail);
  68. },
  69. onBlur(event) {
  70. this.focused = false;
  71. this.setShowClear();
  72. this.$emit('blur', event.detail);
  73. },
  74. onClickIcon() {
  75. this.$emit('click-icon');
  76. },
  77. onClear() {
  78. this.setData({ innerValue: '' });
  79. this.value = '';
  80. this.setShowClear();
  81. wx.nextTick(() => {
  82. this.emitChange();
  83. this.$emit('clear', '');
  84. });
  85. },
  86. onConfirm(event) {
  87. const { value = '' } = event.detail || {};
  88. this.value = value;
  89. this.setShowClear();
  90. this.$emit('confirm', value);
  91. },
  92. setValue(value) {
  93. this.value = value;
  94. this.setShowClear();
  95. if (value === '') {
  96. this.setData({ innerValue: '' });
  97. }
  98. this.emitChange();
  99. },
  100. onLineChange(event) {
  101. this.$emit('linechange', event.detail);
  102. },
  103. onKeyboardHeightChange(event) {
  104. this.$emit('keyboardheightchange', event.detail);
  105. },
  106. emitChange() {
  107. this.setData({ value: this.value });
  108. wx.nextTick(() => {
  109. this.$emit('input', this.value);
  110. this.$emit('change', this.value);
  111. });
  112. },
  113. setShowClear() {
  114. const { clearable, readonly } = this.data;
  115. const { focused, value } = this;
  116. this.setData({
  117. showClear: !!clearable && !!focused && !!value && !readonly,
  118. });
  119. },
  120. noop() {},
  121. },
  122. });