index.vue 10 KB

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