index.vue 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <uni-shadow-root class="vant-dist-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">
  3. <slot name="left-icon" slot="icon"></slot>
  4. <view v-if="label" :class="'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. <view :class="utils.bem('field__control', [inputAlign, 'custom'])" @click="onClickInput">
  10. <slot name="input"></slot>
  11. </view>
  12. <include v-if="type === 'textarea'" src="./textarea.wxml"></include>
  13. <include v-else src="./input.wxml"></include>
  14. <van-icon v-if="showClear" :name="clearIcon" class="van-field__clear-root van-field__icon-root" @touchstart.native.stop.prevent="onClear"></van-icon>
  15. <view class="van-field__icon-container" @click="onClickIcon">
  16. <van-icon v-if="rightIcon || icon" :name="rightIcon || icon" :class="'van-field__icon-root '+(iconClass)" custom-class="right-icon-class"></van-icon>
  17. <slot name="right-icon"></slot>
  18. <slot name="icon"></slot>
  19. </view>
  20. <view class="van-field__button">
  21. <slot name="button"></slot>
  22. </view>
  23. </view>
  24. <view v-if="showWordLimit && maxlength" class="van-field__word-limit">
  25. <view :class="utils.bem('field__word-num', { full: value.length >= maxlength })">{{ value.length >= maxlength ? maxlength : value.length }}</view>/{{ maxlength }}
  26. </view>
  27. <view v-if="errorMessage" :class="utils.bem('field__error-message', [errorMessageAlign, { disabled, error }])">
  28. {{ errorMessage }}
  29. </view>
  30. </van-cell></uni-shadow-root>
  31. </template>
  32. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  33. <script>
  34. import VanCell from '../cell/index.vue'
  35. import VanIcon from '../icon/index.vue'
  36. global['__wxVueOptions'] = {components:{'van-cell': VanCell,'van-icon': VanIcon}}
  37. global['__wxRoute'] = 'vant/dist/field/index'
  38. import { nextTick } from '../common/utils';
  39. import { VantComponent } from '../common/component';
  40. import { commonProps, inputProps, textareaProps } from './props';
  41. VantComponent({
  42. field: true,
  43. classes: ['input-class', 'right-icon-class', 'label-class'],
  44. 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: {
  45. type: Boolean,
  46. observer: 'setShowClear',
  47. }, clearable: {
  48. type: Boolean,
  49. observer: 'setShowClear',
  50. }, clearTrigger: {
  51. type: String,
  52. value: 'focus',
  53. }, border: {
  54. type: Boolean,
  55. value: true,
  56. }, titleWidth: {
  57. type: String,
  58. value: '6.2em',
  59. }, clearIcon: {
  60. type: String,
  61. value: 'clear',
  62. } }),
  63. data: {
  64. focused: false,
  65. innerValue: '',
  66. showClear: false,
  67. },
  68. created() {
  69. this.value = this.data.value;
  70. this.setData({ innerValue: this.value });
  71. },
  72. methods: {
  73. onInput(event) {
  74. const { value = '' } = event.detail || {};
  75. this.value = value;
  76. this.setShowClear();
  77. this.emitChange();
  78. },
  79. onFocus(event) {
  80. this.focused = true;
  81. this.setShowClear();
  82. this.$emit('focus', event.detail);
  83. },
  84. onBlur(event) {
  85. this.focused = false;
  86. this.setShowClear();
  87. this.$emit('blur', event.detail);
  88. },
  89. onClickIcon() {
  90. this.$emit('click-icon');
  91. },
  92. onClickInput(event) {
  93. this.$emit('click-input', event.detail);
  94. },
  95. onClear() {
  96. this.setData({ innerValue: '' });
  97. this.value = '';
  98. this.setShowClear();
  99. nextTick(() => {
  100. this.emitChange();
  101. this.$emit('clear', '');
  102. });
  103. },
  104. onConfirm(event) {
  105. const { value = '' } = event.detail || {};
  106. this.value = value;
  107. this.setShowClear();
  108. this.$emit('confirm', value);
  109. },
  110. setValue(value) {
  111. this.value = value;
  112. this.setShowClear();
  113. if (value === '') {
  114. this.setData({ innerValue: '' });
  115. }
  116. this.emitChange();
  117. },
  118. onLineChange(event) {
  119. this.$emit('linechange', event.detail);
  120. },
  121. onKeyboardHeightChange(event) {
  122. this.$emit('keyboardheightchange', event.detail);
  123. },
  124. emitChange() {
  125. this.setData({ value: this.value });
  126. nextTick(() => {
  127. this.$emit('input', this.value);
  128. this.$emit('change', this.value);
  129. });
  130. },
  131. setShowClear() {
  132. const { clearable, readonly, clearTrigger } = this.data;
  133. const { focused, value } = this;
  134. let showClear = false;
  135. if (clearable && !readonly) {
  136. const hasValue = !!value;
  137. const trigger = clearTrigger === 'always' || (clearTrigger === 'focus' && focused);
  138. showClear = hasValue && trigger;
  139. }
  140. this.setData({ showClear });
  141. },
  142. noop() { },
  143. },
  144. });
  145. export default global['__wxComponents']['vant/dist/field/index']
  146. </script>
  147. <style platform="mp-weixin">
  148. @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:flex;align-items:center}.van-field__body--textarea{box-sizing:border-box;padding:3.6px 0;line-height:1.2em;min-height:24px;min-height:var(--cell-line-height,24px)}.van-field__control:empty+.van-field__control{display:block}.van-field__control{position:relative;display:none;box-sizing:border-box;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);min-height:24px;min-height:var(--cell-line-height,24px)}.van-field__control:empty{display:none}.van-field__control--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__control--error{color:#ee0a24;color:var(--field-input-error-text-color,#ee0a24)}.van-field__control--disabled{background-color:initial;opacity:1;color:#c8c9cc;color:var(--field-input-disabled-text-color,#c8c9cc)}.van-field__control--center{text-align:center}.van-field__control--right{text-align:right}.van-field__control--custom{display:flex;align-items:center;min-height:24px;min-height:var(--cell-line-height,24px)}.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:flex;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{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)}
  149. </style>