index.js 3.9 KB

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