index.vue 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <uni-shadow-root class="vant-field-index"><van-cell :icon="leftIcon" :title="label" :center="center" :border="border" :is-link="isLink" :required="required" :custom-style="customStyle" :title-width="titleWidth" custom-class="van-field" :size="size">
  3. <slot name="left-icon" slot="icon"></slot>
  4. <slot name="label" slot="title"></slot>
  5. <view :class="utils.bem('field__body', [type, system])">
  6. <textarea v-if="type === 'textarea'" :class="'input-class '+(utils.bem('field__input', [inputAlign, type, { disabled, error }]))" :fixed="fixed" :focus="focus" :value="value" :disabled="disabled || readonly" :maxlength="maxlength" :placeholder="placeholder" :placeholder-style="placeholderStyle" :placeholder-class="utils.bem('field__placeholder', { error })" :auto-height="autosize" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" :show-confirm-bar="showConfirmBar" :selection-end="selectionEnd" :selection-start="selectionStart" @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm">
  7. </textarea>
  8. <input v-else :class="'input-class '+(utils.bem('field__input', [inputAlign, { disabled, error }]))" :type="type" :focus="focus" :value="value" :disabled="disabled || readonly" :maxlength="maxlength" :placeholder="placeholder" :placeholder-style="placeholderStyle" :placeholder-class="utils.bem('field__placeholder', { error })" :confirm-type="confirmType" :confirm-hold="confirmHold" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" :selection-end="selectionEnd" :selection-start="selectionStart" :password="password || type === 'password'" @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm"></input>
  9. <van-icon v-if="clearable && focused && value && !readonly" size="16px" name="clear" class="van-field__clear-root van-field__icon-root" @touchstart.native="onClear"></van-icon>
  10. <view class="van-field__icon-container" @click="onClickIcon">
  11. <van-icon v-if="rightIcon || icon" size="16px" :name="rightIcon || icon" :class="'van-field__icon-root '+(iconClass)" custom-class="right-icon-class"></van-icon>
  12. <slot name="right-icon"></slot>
  13. <slot name="icon"></slot>
  14. </view>
  15. <view class="van-field__button">
  16. <slot name="button"></slot>
  17. </view>
  18. </view>
  19. <view v-if="errorMessage" :class="'van-field__error-message '+(utils.bem('field__error', [errorMessageAlign, { disabled, error }]))">
  20. {{ errorMessage }}
  21. </view>
  22. </van-cell></uni-shadow-root>
  23. </template>
  24. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  25. <script>
  26. import VanCell from '../cell/index.vue'
  27. import VanIcon from '../icon/index.vue'
  28. global['__wxVueOptions'] = {components:{'van-cell': VanCell,'van-icon': VanIcon}}
  29. global['__wxRoute'] = 'vant/field/index'
  30. import { VantComponent } from '../common/component';
  31. import { getSystemInfoSync } from '../common/utils';
  32. VantComponent({
  33. field: true,
  34. classes: ['input-class', 'right-icon-class'],
  35. props: {
  36. size: String,
  37. icon: String,
  38. label: String,
  39. error: Boolean,
  40. fixed: Boolean,
  41. focus: Boolean,
  42. center: Boolean,
  43. isLink: Boolean,
  44. leftIcon: String,
  45. rightIcon: String,
  46. disabled: Boolean,
  47. autosize: Boolean,
  48. readonly: Boolean,
  49. required: Boolean,
  50. password: Boolean,
  51. iconClass: String,
  52. clearable: Boolean,
  53. inputAlign: String,
  54. customStyle: String,
  55. confirmType: String,
  56. confirmHold: Boolean,
  57. errorMessage: String,
  58. placeholder: String,
  59. placeholderStyle: String,
  60. errorMessageAlign: String,
  61. selectionEnd: {
  62. type: Number,
  63. value: -1
  64. },
  65. selectionStart: {
  66. type: Number,
  67. value: -1
  68. },
  69. showConfirmBar: {
  70. type: Boolean,
  71. value: true
  72. },
  73. adjustPosition: {
  74. type: Boolean,
  75. value: true
  76. },
  77. cursorSpacing: {
  78. type: Number,
  79. value: 50
  80. },
  81. maxlength: {
  82. type: Number,
  83. value: -1
  84. },
  85. type: {
  86. type: String,
  87. value: 'text'
  88. },
  89. border: {
  90. type: Boolean,
  91. value: true
  92. },
  93. titleWidth: {
  94. type: String,
  95. value: '90px'
  96. }
  97. },
  98. data: {
  99. focused: false,
  100. system: getSystemInfoSync().system.split(' ').shift().toLowerCase()
  101. },
  102. methods: {
  103. onInput(event) {
  104. const { value = '' } = event.detail || {};
  105. this.set({ value }, () => {
  106. this.emitChange(value);
  107. });
  108. },
  109. onFocus(event) {
  110. this.set({ focused: true });
  111. this.$emit('focus', event.detail);
  112. },
  113. onBlur(event) {
  114. this.set({ focused: false });
  115. this.$emit('blur', event.detail);
  116. },
  117. onClickIcon() {
  118. this.$emit('click-icon');
  119. },
  120. onClear() {
  121. this.set({ value: '' }, () => {
  122. this.emitChange('');
  123. this.$emit('clear', '');
  124. });
  125. },
  126. onConfirm() {
  127. this.$emit('confirm', this.data.value);
  128. },
  129. emitChange(value) {
  130. this.$emit('input', value);
  131. this.$emit('change', value);
  132. }
  133. }
  134. });
  135. export default global['__wxComponents']['vant/field/index']
  136. </script>
  137. <style platform="mp-weixin">
  138. @import '../common/index.css';.van-field__body{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center}.van-field__body--textarea{min-height:24px;line-height:1.2em}.van-field__body--textarea.van-field__body--ios{margin-top:-4.5px}.van-field__input{position:relative;display:block;width:100%;height:24px;min-height:24px;padding:0;margin:0;line-height:inherit;color:#333;text-align:left;background-color:initial;border:0;box-sizing:border-box;resize:none}.van-field__input--textarea{height:18px;min-height:18px}.van-field__input--error{color:#f44}.van-field__input--disabled{color:#999;background-color:initial;opacity:1}.van-field__input--center{text-align:center}.van-field__input--right{text-align:right}.van-field__placeholder{position:absolute;top:0;right:0;left:0;color:#999;pointer-events:none}.van-field__placeholder--error{color:#f44}.van-field__icon-root{display:-webkit-flex;display:flex;min-height:24px;-webkit-align-items:center;align-items:center}.van-field__clear-root,.van-field__icon-container{padding:0 10px;margin-right:-10px;line-height:inherit;vertical-align:middle}.van-field__button,.van-field__clear-root,.van-field__icon-container{-webkit-flex-shrink:0;flex-shrink:0}.van-field__clear-root{color:#c9c9c9}.van-field__icon-container{color:#999}.van-field__icon-container:empty{display:none}.van-field__button{padding-left:10px}.van-field__button:empty{display:none}.van-field__error-message{font-size:12px;color:#f44;text-align:left}.van-field__error-message--center{text-align:center}.van-field__error-message--right{text-align:right}
  139. </style>