index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { VantComponent } from '../common/component';
  2. import { canIUseModel } from '../common/version';
  3. VantComponent({
  4. field: true,
  5. classes: ['field-class', 'input-class', 'cancel-class'],
  6. props: {
  7. value: {
  8. type: String,
  9. value: '',
  10. },
  11. label: String,
  12. focus: Boolean,
  13. error: Boolean,
  14. disabled: Boolean,
  15. readonly: Boolean,
  16. inputAlign: String,
  17. showAction: Boolean,
  18. useActionSlot: Boolean,
  19. useLeftIconSlot: Boolean,
  20. useRightIconSlot: Boolean,
  21. leftIcon: {
  22. type: String,
  23. value: 'search',
  24. },
  25. rightIcon: String,
  26. placeholder: String,
  27. placeholderStyle: String,
  28. actionText: {
  29. type: String,
  30. value: '取消',
  31. },
  32. background: {
  33. type: String,
  34. value: '#ffffff',
  35. },
  36. maxlength: {
  37. type: Number,
  38. value: -1,
  39. },
  40. shape: {
  41. type: String,
  42. value: 'square',
  43. },
  44. clearable: {
  45. type: Boolean,
  46. value: true,
  47. },
  48. clearTrigger: {
  49. type: String,
  50. value: 'focus',
  51. },
  52. clearIcon: {
  53. type: String,
  54. value: 'clear',
  55. },
  56. cursorSpacing: {
  57. type: Number,
  58. value: 0,
  59. },
  60. },
  61. methods: {
  62. onChange(event) {
  63. if (canIUseModel()) {
  64. this.setData({ value: event.detail });
  65. }
  66. this.$emit('change', event.detail);
  67. },
  68. onCancel() {
  69. /**
  70. * 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
  71. * https://github.com/youzan/vant-weapp/issues/1768
  72. */
  73. setTimeout(() => {
  74. if (canIUseModel()) {
  75. this.setData({ value: '' });
  76. }
  77. this.$emit('cancel');
  78. this.$emit('change', '');
  79. }, 200);
  80. },
  81. onSearch(event) {
  82. this.$emit('search', event.detail);
  83. },
  84. onFocus(event) {
  85. this.$emit('focus', event.detail);
  86. },
  87. onBlur(event) {
  88. this.$emit('blur', event.detail);
  89. },
  90. onClear(event) {
  91. this.$emit('clear', event.detail);
  92. },
  93. onClickInput(event) {
  94. this.$emit('click-input', event.detail);
  95. },
  96. },
  97. });