index.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <uni-shadow-root class="vant-dist-stepper-index"><view :class="(utils.bem('stepper', [theme]))+' custom-class'">
  3. <view v-if="showMinus" data-type="minus" :style="computed.buttonStyle({ buttonSize })" :class="'minus-class '+(utils.bem('stepper__minus', { disabled: disabled || disableMinus || currentValue <= min }))" hover-class="van-stepper__minus--hover" hover-stay-time="70" @click="onTap" @touchstart="onTouchStart" @touchend="onTouchEnd">
  4. <slot name="minus"></slot>
  5. </view>
  6. <input :type="integer ? 'number' : 'digit'" :class="'input-class '+(utils.bem('stepper__input', { disabled: disabled || disableInput }))" :style="computed.inputStyle({ buttonSize, inputWidth })" :value="currentValue" :focus="focus" :disabled="disabled || disableInput" @input="onInput" @focus="onFocus" @blur="onBlur"></input>
  7. <view v-if="showPlus" data-type="plus" :style="computed.buttonStyle({ buttonSize })" :class="'plus-class '+(utils.bem('stepper__plus', { disabled: disabled || disablePlus || currentValue >= max }))" hover-class="van-stepper__plus--hover" hover-stay-time="70" @click="onTap" @touchstart="onTouchStart" @touchend="onTouchEnd">
  8. <slot name="plus"></slot>
  9. </view>
  10. </view></uni-shadow-root>
  11. </template>
  12. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  13. <script>
  14. global['__wxRoute'] = 'vant/dist/stepper/index'
  15. import { VantComponent } from '../common/component';
  16. import { isDef } from '../common/validator';
  17. const LONG_PRESS_START_TIME = 600;
  18. const LONG_PRESS_INTERVAL = 200;
  19. // add num and avoid float number
  20. function add(num1, num2) {
  21. const cardinal = Math.pow(10, 10);
  22. return Math.round((num1 + num2) * cardinal) / cardinal;
  23. }
  24. function equal(value1, value2) {
  25. return String(value1) === String(value2);
  26. }
  27. VantComponent({
  28. field: true,
  29. classes: ['input-class', 'plus-class', 'minus-class'],
  30. props: {
  31. value: {
  32. type: null,
  33. observer: 'observeValue',
  34. },
  35. integer: {
  36. type: Boolean,
  37. observer: 'check',
  38. },
  39. disabled: Boolean,
  40. inputWidth: String,
  41. buttonSize: String,
  42. asyncChange: Boolean,
  43. disableInput: Boolean,
  44. decimalLength: {
  45. type: Number,
  46. value: null,
  47. observer: 'check',
  48. },
  49. min: {
  50. type: null,
  51. value: 1,
  52. observer: 'check',
  53. },
  54. max: {
  55. type: null,
  56. value: Number.MAX_SAFE_INTEGER,
  57. observer: 'check',
  58. },
  59. step: {
  60. type: null,
  61. value: 1,
  62. },
  63. showPlus: {
  64. type: Boolean,
  65. value: true,
  66. },
  67. showMinus: {
  68. type: Boolean,
  69. value: true,
  70. },
  71. disablePlus: Boolean,
  72. disableMinus: Boolean,
  73. longPress: {
  74. type: Boolean,
  75. value: true,
  76. },
  77. theme: String,
  78. },
  79. data: {
  80. currentValue: '',
  81. },
  82. created() {
  83. this.setData({
  84. currentValue: this.format(this.data.value),
  85. });
  86. },
  87. methods: {
  88. observeValue() {
  89. const { value, currentValue } = this.data;
  90. if (!equal(value, currentValue)) {
  91. this.setData({ currentValue: this.format(value) });
  92. }
  93. },
  94. check() {
  95. const val = this.format(this.data.currentValue);
  96. if (!equal(val, this.data.currentValue)) {
  97. this.setData({ currentValue: val });
  98. }
  99. },
  100. isDisabled(type) {
  101. const { disabled, disablePlus, disableMinus, currentValue, max, min, } = this.data;
  102. if (type === 'plus') {
  103. return disabled || disablePlus || currentValue >= max;
  104. }
  105. return disabled || disableMinus || currentValue <= min;
  106. },
  107. onFocus(event) {
  108. this.$emit('focus', event.detail);
  109. },
  110. onBlur(event) {
  111. const value = this.format(event.detail.value);
  112. this.emitChange(value);
  113. this.$emit('blur', Object.assign(Object.assign({}, event.detail), { value }));
  114. },
  115. // filter illegal characters
  116. filter(value) {
  117. value = String(value).replace(/[^0-9.-]/g, '');
  118. if (this.data.integer && value.indexOf('.') !== -1) {
  119. value = value.split('.')[0];
  120. }
  121. return value;
  122. },
  123. // limit value range
  124. format(value) {
  125. value = this.filter(value);
  126. // format range
  127. value = value === '' ? 0 : +value;
  128. value = Math.max(Math.min(this.data.max, value), this.data.min);
  129. // format decimal
  130. if (isDef(this.data.decimalLength)) {
  131. value = value.toFixed(this.data.decimalLength);
  132. }
  133. return value;
  134. },
  135. onInput(event) {
  136. const { value = '' } = event.detail || {};
  137. // allow input to be empty
  138. if (value === '') {
  139. return;
  140. }
  141. let formatted = this.filter(value);
  142. // limit max decimal length
  143. if (isDef(this.data.decimalLength) && formatted.indexOf('.') !== -1) {
  144. const pair = formatted.split('.');
  145. formatted = `${pair[0]}.${pair[1].slice(0, this.data.decimalLength)}`;
  146. }
  147. this.emitChange(formatted);
  148. },
  149. emitChange(value) {
  150. if (!this.data.asyncChange) {
  151. this.setData({ currentValue: value });
  152. }
  153. this.$emit('change', value);
  154. },
  155. onChange() {
  156. const { type } = this;
  157. if (this.isDisabled(type)) {
  158. this.$emit('overlimit', type);
  159. return;
  160. }
  161. const diff = type === 'minus' ? -this.data.step : +this.data.step;
  162. const value = this.format(add(+this.data.currentValue, diff));
  163. this.emitChange(value);
  164. this.$emit(type);
  165. },
  166. longPressStep() {
  167. this.longPressTimer = setTimeout(() => {
  168. this.onChange();
  169. this.longPressStep();
  170. }, LONG_PRESS_INTERVAL);
  171. },
  172. onTap(event) {
  173. const { type } = event.currentTarget.dataset;
  174. this.type = type;
  175. this.onChange();
  176. },
  177. onTouchStart(event) {
  178. if (!this.data.longPress) {
  179. return;
  180. }
  181. clearTimeout(this.longPressTimer);
  182. const { type } = event.currentTarget.dataset;
  183. this.type = type;
  184. this.isLongPress = false;
  185. this.longPressTimer = setTimeout(() => {
  186. this.isLongPress = true;
  187. this.onChange();
  188. this.longPressStep();
  189. }, LONG_PRESS_START_TIME);
  190. },
  191. onTouchEnd() {
  192. if (!this.data.longPress) {
  193. return;
  194. }
  195. clearTimeout(this.longPressTimer);
  196. },
  197. },
  198. });
  199. export default global['__wxComponents']['vant/dist/stepper/index']
  200. </script>
  201. <style platform="mp-weixin">
  202. @import '../common/index.css';.van-stepper{font-size:0}.van-stepper__minus,.van-stepper__plus{position:relative;display:inline-block;box-sizing:border-box;margin:1px;vertical-align:middle;border:0;background-color:#f2f3f5;background-color:var(--stepper-background-color,#f2f3f5);color:#323233;color:var(--stepper-button-icon-color,#323233);width:28px;width:var(--stepper-input-height,28px);height:28px;height:var(--stepper-input-height,28px);padding:4px;padding:var(--padding-base,4px)}.van-stepper__minus:before,.van-stepper__plus:before{width:9px;height:1px}.van-stepper__minus:after,.van-stepper__plus:after{width:1px;height:9px}.van-stepper__minus:empty.van-stepper__minus:after,.van-stepper__minus:empty.van-stepper__minus:before,.van-stepper__minus:empty.van-stepper__plus:after,.van-stepper__minus:empty.van-stepper__plus:before,.van-stepper__plus:empty.van-stepper__minus:after,.van-stepper__plus:empty.van-stepper__minus:before,.van-stepper__plus:empty.van-stepper__plus:after,.van-stepper__plus:empty.van-stepper__plus:before{position:absolute;top:0;right:0;bottom:0;left:0;margin:auto;background-color:currentColor;content:""}.van-stepper__minus--hover,.van-stepper__plus--hover{background-color:#e8e8e8;background-color:var(--stepper-active-color,#e8e8e8)}.van-stepper__minus--disabled,.van-stepper__plus--disabled{color:#c8c9cc;color:var(--stepper-button-disabled-icon-color,#c8c9cc)}.van-stepper__minus--disabled,.van-stepper__minus--disabled.van-stepper__minus--hover,.van-stepper__minus--disabled.van-stepper__plus--hover,.van-stepper__plus--disabled,.van-stepper__plus--disabled.van-stepper__minus--hover,.van-stepper__plus--disabled.van-stepper__plus--hover{background-color:#f7f8fa;background-color:var(--stepper-button-disabled-color,#f7f8fa)}.van-stepper__minus{border-radius:4px 0 0 4px;border-radius:var(--stepper-border-radius,4px) 0 0 var(--stepper-border-radius,4px)}.van-stepper__minus:after{display:none}.van-stepper__plus{border-radius:0 4px 4px 0;border-radius:0 var(--stepper-border-radius,4px) var(--stepper-border-radius,4px) 0}.van-stepper--round .van-stepper__input{background-color:initial!important}.van-stepper--round .van-stepper__minus,.van-stepper--round .van-stepper__plus{border-radius:100%}.van-stepper--round .van-stepper__minus:active,.van-stepper--round .van-stepper__plus:active{opacity:.7}.van-stepper--round .van-stepper__minus--disabled,.van-stepper--round .van-stepper__minus--disabled:active,.van-stepper--round .van-stepper__plus--disabled,.van-stepper--round .van-stepper__plus--disabled:active{opacity:.3}.van-stepper--round .van-stepper__plus{color:#fff;background-color:#ee0a24}.van-stepper--round .van-stepper__minus{color:#ee0a24;background-color:#fff;border:1px solid #ee0a24}.van-stepper__input{display:inline-block;box-sizing:border-box;min-height:0;margin:1px;padding:1px;text-align:center;vertical-align:middle;border:0;border-width:1px 0;border-radius:0;-webkit-appearance:none;font-size:14px;font-size:var(--stepper-input-font-size,14px);color:#323233;color:var(--stepper-input-text-color,#323233);background-color:#f2f3f5;background-color:var(--stepper-background-color,#f2f3f5);width:32px;width:var(--stepper-input-width,32px);height:28px;height:var(--stepper-input-height,28px)}.van-stepper__input--disabled{color:#c8c9cc;color:var(--stepper-input-disabled-text-color,#c8c9cc);background-color:#f2f3f5;background-color:var(--stepper-input-disabled-background-color,#f2f3f5)}
  203. </style>