index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <uni-shadow-root class="weapp-lib-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'] = 'weapp/lib/field/index'
  38. "use strict";
  39. var __assign = (this && this.__assign) || function () {
  40. __assign = Object.assign || function(t) {
  41. for (var s, i = 1, n = arguments.length; i < n; i++) {
  42. s = arguments[i];
  43. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  44. t[p] = s[p];
  45. }
  46. return t;
  47. };
  48. return __assign.apply(this, arguments);
  49. };
  50. Object.defineProperty(exports, "__esModule", { value: true });
  51. var utils_1 = require("../common/utils");
  52. var component_1 = require("../common/component");
  53. var props_1 = require("./props");
  54. (0, component_1.VantComponent)({
  55. field: true,
  56. classes: ['input-class', 'right-icon-class', 'label-class'],
  57. props: __assign(__assign(__assign(__assign({}, props_1.commonProps), props_1.inputProps), props_1.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: {
  58. type: Boolean,
  59. observer: 'setShowClear',
  60. }, clearable: {
  61. type: Boolean,
  62. observer: 'setShowClear',
  63. }, clearTrigger: {
  64. type: String,
  65. value: 'focus',
  66. }, border: {
  67. type: Boolean,
  68. value: true,
  69. }, titleWidth: {
  70. type: String,
  71. value: '6.2em',
  72. }, clearIcon: {
  73. type: String,
  74. value: 'clear',
  75. }, extraEventParams: {
  76. type: Boolean,
  77. value: false,
  78. } }),
  79. data: {
  80. focused: false,
  81. innerValue: '',
  82. showClear: false,
  83. },
  84. created: function () {
  85. this.value = this.data.value;
  86. this.setData({ innerValue: this.value });
  87. },
  88. methods: {
  89. formatValue: function (value) {
  90. var maxlength = this.data.maxlength;
  91. if (maxlength !== -1 && value.length > maxlength) {
  92. return value.slice(0, maxlength);
  93. }
  94. return value;
  95. },
  96. onInput: function (event) {
  97. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  98. var formatValue = this.formatValue(value);
  99. this.value = formatValue;
  100. this.setShowClear();
  101. return this.emitChange(__assign(__assign({}, event.detail), { value: formatValue }));
  102. },
  103. onFocus: function (event) {
  104. this.focused = true;
  105. this.setShowClear();
  106. this.$emit('focus', event.detail);
  107. },
  108. onBlur: function (event) {
  109. this.focused = false;
  110. this.setShowClear();
  111. this.$emit('blur', event.detail);
  112. },
  113. onClickIcon: function () {
  114. this.$emit('click-icon');
  115. },
  116. onClickInput: function (event) {
  117. this.$emit('click-input', event.detail);
  118. },
  119. onClear: function () {
  120. var _this = this;
  121. this.setData({ innerValue: '' });
  122. this.value = '';
  123. this.setShowClear();
  124. (0, utils_1.nextTick)(function () {
  125. _this.emitChange({ value: '' });
  126. _this.$emit('clear', '');
  127. });
  128. },
  129. onConfirm: function (event) {
  130. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  131. this.value = value;
  132. this.setShowClear();
  133. this.$emit('confirm', value);
  134. },
  135. setValue: function (value) {
  136. this.value = value;
  137. this.setShowClear();
  138. if (value === '') {
  139. this.setData({ innerValue: '' });
  140. }
  141. this.emitChange({ value: value });
  142. },
  143. onLineChange: function (event) {
  144. this.$emit('linechange', event.detail);
  145. },
  146. onKeyboardHeightChange: function (event) {
  147. this.$emit('keyboardheightchange', event.detail);
  148. },
  149. emitChange: function (detail) {
  150. var extraEventParams = this.data.extraEventParams;
  151. this.setData({ value: detail.value });
  152. var result;
  153. var data = extraEventParams
  154. ? __assign(__assign({}, detail), { callback: function (data) {
  155. result = data;
  156. } }) : detail.value;
  157. this.$emit('input', data);
  158. this.$emit('change', data);
  159. return result;
  160. },
  161. setShowClear: function () {
  162. var _a = this.data, clearable = _a.clearable, readonly = _a.readonly, clearTrigger = _a.clearTrigger;
  163. var _b = this, focused = _b.focused, value = _b.value;
  164. var showClear = false;
  165. if (clearable && !readonly) {
  166. var hasValue = !!value;
  167. var trigger = clearTrigger === 'always' || (clearTrigger === 'focus' && focused);
  168. showClear = hasValue && trigger;
  169. }
  170. this.setData({ showClear: showClear });
  171. },
  172. noop: function () { },
  173. },
  174. });
  175. export default global['__wxComponents']['weapp/lib/field/index']
  176. </script>
  177. <style platform="mp-weixin">
  178. @import '../common/index.css';.van-field{--cell-icon-size:var(--field-icon-size,16px)}.van-field__label{color:var(--field-label-color,#646566)}.van-field__label--disabled{color:var(--field-disabled-text-color,#c8c9cc)}.van-field__body{align-items:center;display:flex}.van-field__body--textarea{box-sizing:border-box;line-height:1.2em;min-height:var(--cell-line-height,24px);padding:3.6px 0}.van-field__control:empty+.van-field__control{display:block}.van-field__control{background-color:initial;border:0;box-sizing:border-box;color:var(--field-input-text-color,#323233);display:none;height:var(--cell-line-height,24px);line-height:inherit;margin:0;min-height:var(--cell-line-height,24px);padding:0;position:relative;resize:none;text-align:left;width:100%}.van-field__control:empty{display:none}.van-field__control--textarea{height:var(--field-text-area-min-height,18px);min-height:var(--field-text-area-min-height,18px)}.van-field__control--error{color:var(--field-input-error-text-color,#ee0a24)}.van-field__control--disabled{background-color:initial;color:var(--field-input-disabled-text-color,#c8c9cc);opacity:1}.van-field__control--center{text-align:center}.van-field__control--right{text-align:right}.van-field__control--custom{align-items:center;display:flex;min-height:var(--cell-line-height,24px)}.van-field__placeholder{color:var(--field-placeholder-text-color,#c8c9cc);left:0;pointer-events:none;position:absolute;right:0;top:0}.van-field__placeholder--error{color:var(--field-error-message-color,#ee0a24)}.van-field__icon-root{align-items:center;display:flex;min-height:var(--cell-line-height,24px)}.van-field__clear-root,.van-field__icon-container{line-height:inherit;margin-right:calc(var(--padding-xs, 8px)*-1);padding:0 var(--padding-xs,8px);vertical-align:middle}.van-field__button,.van-field__clear-root,.van-field__icon-container{flex-shrink:0}.van-field__clear-root{color:var(--field-clear-icon-color,#c8c9cc);font-size:var(--field-clear-icon-size,16px)}.van-field__icon-container{color:var(--field-icon-container-color,#969799);font-size:var(--field-icon-size,16px)}.van-field__icon-container:empty{display:none}.van-field__button{padding-left:var(--padding-xs,8px)}.van-field__button:empty{display:none}.van-field__error-message{color:var(--field-error-message-color,#ee0a24);font-size:var(--field-error-message-text-font-size,12px);text-align:left}.van-field__error-message--center{text-align:center}.van-field__error-message--right{text-align:right}.van-field__word-limit{color:var(--field-word-limit-color,#646566);font-size:var(--field-word-limit-font-size,12px);line-height:var(--field-word-limit-line-height,16px);margin-top:var(--padding-base,4px);text-align:right}.van-field__word-num{display:inline}.van-field__word-num--full{color:var(--field-word-num-full-color,#ee0a24)}
  179. </style>