index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <uni-shadow-root class="vant-field-index"><van-cell :size="size" :icon="leftIcon" :center="center" :border="border" :is-link="isLink" :required="required" :clickable="clickable" :title-width="titleWidth" title-style="margin-right: 12px;" :custom-style="customStyle" :arrow-direction="arrowDirection" custom-class="van-field" title-class="label-class">
  3. <slot name="left-icon" slot="icon"></slot>
  4. <view v-if="label" :class="utils.bem('field__label', { disabled })" slot="title">
  5. {{ label }}
  6. </view>
  7. <slot v-else name="label" slot="title"></slot>
  8. <view :class="utils.bem('field__body', [type])">
  9. <textarea v-if="type === 'textarea'" :class="'input-class '+(utils.bem('field__input', [inputAlign, type, { disabled, error }]))" :fixed="fixed" :focus="focus" :cursor="cursor" :value="innerValue" :auto-focus="autoFocus" :disabled="disabled || readonly" :maxlength="maxlength" :placeholder="placeholder" :placeholder-style="placeholderStyle" :placeholder-class="utils.bem('field__placeholder', { error, disabled })" :auto-height="(!!autosize)" :style="computed.inputStyle(autosize)" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" :show-confirm-bar="showConfirmBar" :hold-keyboard="holdKeyboard" :selection-end="selectionEnd" :selection-start="selectionStart" :disable-default-padding="disableDefaultPadding" @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm" @linechange="onLineChange" @keyboardheightchange="onKeyboardHeightChange">
  10. </textarea>
  11. <input v-else :class="'input-class '+(utils.bem('field__input', [inputAlign, { disabled, error }]))" :type="type" :focus="focus" :cursor="cursor" :value="innerValue" :auto-focus="autoFocus" :disabled="disabled || readonly" :maxlength="maxlength" :placeholder="placeholder" :placeholder-style="placeholderStyle" :placeholder-class="utils.bem('field__placeholder', { error })" :confirm-type="confirmType" :confirm-hold="confirmHold" :hold-keyboard="holdKeyboard" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" :selection-end="selectionEnd" :selection-start="selectionStart" :password="password || type === 'password'" @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm" @keyboardheightchange="onKeyboardHeightChange"></input>
  12. <van-icon v-if="showClear" name="clear" class="van-field__clear-root van-field__icon-root" @touchstart.native.stop.prevent="onClear"></van-icon>
  13. <view class="van-field__icon-container" @click="onClickIcon">
  14. <van-icon v-if="rightIcon || icon" :name="rightIcon || icon" :class="'van-field__icon-root '+(iconClass)" custom-class="right-icon-class"></van-icon>
  15. <slot name="right-icon"></slot>
  16. <slot name="icon"></slot>
  17. </view>
  18. <view class="van-field__button">
  19. <slot name="button"></slot>
  20. </view>
  21. </view>
  22. <view v-if="showWordLimit && maxlength" class="van-field__word-limit">
  23. <view :class="utils.bem('field__word-num', { full: value.length >= maxlength })">{{ value.length }}</view>/{{ maxlength }}
  24. </view>
  25. <view v-if="errorMessage" :class="utils.bem('field__error-message', [errorMessageAlign, { disabled, error }])">
  26. {{ errorMessage }}
  27. </view>
  28. </van-cell></uni-shadow-root>
  29. </template>
  30. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  31. <script>
  32. import VanCell from '../cell/index.vue'
  33. import VanIcon from '../icon/index.vue'
  34. global['__wxVueOptions'] = {components:{'van-cell': VanCell,'van-icon': VanIcon}}
  35. global['__wxRoute'] = 'vant/field/index'
  36. import { VantComponent } from '../common/component';
  37. import { commonProps, inputProps, textareaProps } from './props';
  38. VantComponent({
  39. field: true,
  40. classes: ['input-class', 'right-icon-class', 'label-class'],
  41. props: Object.assign(
  42. Object.assign(
  43. Object.assign(Object.assign({}, commonProps), inputProps),
  44. textareaProps
  45. ),
  46. {
  47. size: String,
  48. icon: String,
  49. label: String,
  50. error: Boolean,
  51. center: Boolean,
  52. isLink: Boolean,
  53. leftIcon: String,
  54. rightIcon: String,
  55. autosize: [Boolean, Object],
  56. required: Boolean,
  57. iconClass: String,
  58. clickable: Boolean,
  59. inputAlign: String,
  60. customStyle: String,
  61. errorMessage: String,
  62. arrowDirection: String,
  63. showWordLimit: Boolean,
  64. errorMessageAlign: String,
  65. readonly: {
  66. type: Boolean,
  67. observer: 'setShowClear',
  68. },
  69. clearable: {
  70. type: Boolean,
  71. observer: 'setShowClear',
  72. },
  73. border: {
  74. type: Boolean,
  75. value: true,
  76. },
  77. titleWidth: {
  78. type: String,
  79. value: '6.2em',
  80. },
  81. }
  82. ),
  83. data: {
  84. focused: false,
  85. innerValue: '',
  86. showClear: false,
  87. },
  88. created() {
  89. this.value = this.data.value;
  90. this.setData({ innerValue: this.value });
  91. },
  92. methods: {
  93. onInput(event) {
  94. const { value = '' } = event.detail || {};
  95. this.value = value;
  96. this.setShowClear();
  97. this.emitChange();
  98. },
  99. onFocus(event) {
  100. this.focused = true;
  101. this.setShowClear();
  102. this.$emit('focus', event.detail);
  103. },
  104. onBlur(event) {
  105. this.focused = false;
  106. this.setShowClear();
  107. this.$emit('blur', event.detail);
  108. },
  109. onClickIcon() {
  110. this.$emit('click-icon');
  111. },
  112. onClear() {
  113. this.setData({ innerValue: '' });
  114. this.value = '';
  115. this.setShowClear();
  116. wx.nextTick(() => {
  117. this.emitChange();
  118. this.$emit('clear', '');
  119. });
  120. },
  121. onConfirm(event) {
  122. const { value = '' } = event.detail || {};
  123. this.value = value;
  124. this.setShowClear();
  125. this.$emit('confirm', value);
  126. },
  127. setValue(value) {
  128. this.value = value;
  129. this.setShowClear();
  130. if (value === '') {
  131. this.setData({ innerValue: '' });
  132. }
  133. this.emitChange();
  134. },
  135. onLineChange(event) {
  136. this.$emit('linechange', event.detail);
  137. },
  138. onKeyboardHeightChange(event) {
  139. this.$emit('keyboardheightchange', event.detail);
  140. },
  141. emitChange() {
  142. this.setData({ value: this.value });
  143. wx.nextTick(() => {
  144. this.$emit('input', this.value);
  145. this.$emit('change', this.value);
  146. });
  147. },
  148. setShowClear() {
  149. const { clearable, readonly } = this.data;
  150. const { focused, value } = this;
  151. this.setData({
  152. showClear: !!clearable && !!focused && !!value && !readonly,
  153. });
  154. },
  155. noop() {},
  156. },
  157. });
  158. export default global['__wxComponents']['vant/field/index']
  159. </script>
  160. <style platform="mp-weixin">
  161. @import '../common/index.css';.van-field{--cell-icon-size:16px;--cell-icon-size:var(--field-icon-size,16px)}.van-field__label{color:#646566;color:var(--field-label-color,#646566)}.van-field__label--disabled{color:#c8c9cc;color:var(--field-disabled-text-color,#c8c9cc)}.van-field__body{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center}.van-field__body--textarea{padding:3.6px 0;line-height:1.2em}.van-field__body--textarea,.van-field__input{box-sizing:border-box;min-height:24px;min-height:var(--cell-line-height,24px)}.van-field__input{position:relative;display:block;width:100%;margin:0;padding:0;line-height:inherit;text-align:left;background-color:initial;border:0;resize:none;color:#323233;color:var(--field-input-text-color,#323233);height:24px;height:var(--cell-line-height,24px)}.van-field__input--textarea{height:18px;height:var(--field-text-area-min-height,18px);min-height:18px;min-height:var(--field-text-area-min-height,18px)}.van-field__input--error{color:#ee0a24;color:var(--field-input-error-text-color,#ee0a24)}.van-field__input--disabled{background-color:initial;opacity:1;color:#c8c9cc;color:var(--field-input-disabled-text-color,#c8c9cc)}.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;pointer-events:none;color:#c8c9cc;color:var(--field-placeholder-text-color,#c8c9cc)}.van-field__placeholder--error{color:#ee0a24;color:var(--field-error-message-color,#ee0a24)}.van-field__icon-root{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center;min-height:24px;min-height:var(--cell-line-height,24px)}.van-field__clear-root,.van-field__icon-container{line-height:inherit;vertical-align:middle;padding:0 8px;padding:0 var(--padding-xs,8px);margin-right:-8px;margin-right:-var(--padding-xs,8px)}.van-field__button,.van-field__clear-root,.van-field__icon-container{-webkit-flex-shrink:0;flex-shrink:0}.van-field__clear-root{font-size:16px;font-size:var(--field-clear-icon-size,16px);color:#c8c9cc;color:var(--field-clear-icon-color,#c8c9cc)}.van-field__icon-container{font-size:16px;font-size:var(--field-icon-size,16px);color:#969799;color:var(--field-icon-container-color,#969799)}.van-field__icon-container:empty{display:none}.van-field__button{padding-left:8px;padding-left:var(--padding-xs,8px)}.van-field__button:empty{display:none}.van-field__error-message{text-align:left;font-size:12px;font-size:var(--field-error-message-text-font-size,12px);color:#ee0a24;color:var(--field-error-message-color,#ee0a24)}.van-field__error-message--center{text-align:center}.van-field__error-message--right{text-align:right}.van-field__word-limit{text-align:right;margin-top:4px;margin-top:var(--padding-base,4px);color:#646566;color:var(--field-word-limit-color,#646566);font-size:12px;font-size:var(--field-word-limit-font-size,12px);line-height:16px;line-height:var(--field-word-limit-line-height,16px)}.van-field__word-num{display:inline}.van-field__word-num--full{color:#ee0a24;color:var(--field-word-num-full-color,#ee0a24)}
  162. </style>