index.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. field: true,
  4. classes: ['input-class'],
  5. props: {
  6. size: String,
  7. icon: String,
  8. label: String,
  9. error: Boolean,
  10. fixed: Boolean,
  11. focus: Boolean,
  12. center: Boolean,
  13. isLink: Boolean,
  14. leftIcon: String,
  15. disabled: Boolean,
  16. autosize: Boolean,
  17. readonly: Boolean,
  18. required: Boolean,
  19. iconClass: String,
  20. clearable: Boolean,
  21. inputAlign: String,
  22. customClass: String,
  23. confirmType: String,
  24. confirmHold: Boolean,
  25. errorMessage: String,
  26. placeholder: String,
  27. customStyle: String,
  28. useIconSlot: Boolean,
  29. useButtonSlot: Boolean,
  30. showConfirmBar: {
  31. type: Boolean,
  32. value: true
  33. },
  34. placeholderStyle: String,
  35. adjustPosition: {
  36. type: Boolean,
  37. value: true
  38. },
  39. cursorSpacing: {
  40. type: Number,
  41. value: 50
  42. },
  43. maxlength: {
  44. type: Number,
  45. value: -1
  46. },
  47. type: {
  48. type: String,
  49. value: 'text'
  50. },
  51. border: {
  52. type: Boolean,
  53. value: true
  54. },
  55. titleWidth: {
  56. type: String,
  57. value: '90px'
  58. }
  59. },
  60. data: {
  61. showClear: false
  62. },
  63. beforeCreate() {
  64. this.focused = false;
  65. },
  66. methods: {
  67. onInput(event) {
  68. const { value = '' } = event.detail || {};
  69. this.set({
  70. value,
  71. showClear: this.getShowClear(value)
  72. }, () => {
  73. this.emitChange(value);
  74. });
  75. },
  76. onFocus(event) {
  77. const { value = '', height = 0 } = event.detail || {};
  78. this.$emit('focus', { value, height });
  79. this.focused = true;
  80. this.blurFromClear = false;
  81. this.set({
  82. showClear: this.getShowClear()
  83. });
  84. },
  85. onBlur(event) {
  86. const { value = '', cursor = 0 } = event.detail || {};
  87. this.$emit('blur', { value, cursor });
  88. this.focused = false;
  89. const showClear = this.getShowClear();
  90. if (this.data.value === value) {
  91. this.set({
  92. showClear
  93. });
  94. }
  95. else if (!this.blurFromClear) {
  96. // fix: the handwritten keyboard does not trigger input change
  97. this.set({
  98. value,
  99. showClear
  100. }, () => {
  101. this.emitChange(value);
  102. });
  103. }
  104. },
  105. onClickIcon() {
  106. this.$emit('click-icon');
  107. },
  108. getShowClear(value) {
  109. value = value === undefined ? this.data.value : value;
  110. return (this.data.clearable && this.focused && value && !this.data.readonly);
  111. },
  112. onClear() {
  113. this.blurFromClear = true;
  114. this.set({
  115. value: '',
  116. showClear: this.getShowClear('')
  117. }, () => {
  118. this.emitChange('');
  119. this.$emit('clear', '');
  120. });
  121. },
  122. onConfirm() {
  123. this.$emit('confirm', this.data.value);
  124. },
  125. emitChange(value) {
  126. this.$emit('input', value);
  127. this.$emit('change', value);
  128. }
  129. }
  130. });