index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.filter(value);
  146. // limit max decimal length
  147. if (isDef(this.data.decimalLength) && formatted.indexOf('.') !== -1) {
  148. const pair = formatted.split('.');
  149. formatted = `${pair[0]}.${pair[1].slice(0, this.data.decimalLength)}`;
  150. }
  151. this.emitChange(formatted);
  152. },
  153. emitChange(value) {
  154. if (!this.data.asyncChange) {
  155. this.setData({ currentValue: value });
  156. }
  157. this.$emit('change', value);
  158. },
  159. onChange() {
  160. const { type } = this;
  161. if (this.isDisabled(type)) {
  162. this.$emit('overlimit', type);
  163. return;
  164. }
  165. const diff = type === 'minus' ? -this.data.step : +this.data.step;
  166. const value = this.format(add(+this.data.currentValue, diff));
  167. this.emitChange(value);
  168. this.$emit(type);
  169. },
  170. longPressStep() {
  171. this.longPressTimer = setTimeout(() => {
  172. this.onChange();
  173. this.longPressStep();
  174. }, LONG_PRESS_INTERVAL);
  175. },
  176. onTap(event) {
  177. const { type } = event.currentTarget.dataset;
  178. this.type = type;
  179. this.onChange();
  180. },
  181. onTouchStart(event) {
  182. if (!this.data.longPress) {
  183. return;
  184. }
  185. clearTimeout(this.longPressTimer);
  186. const { type } = event.currentTarget.dataset;
  187. this.type = type;
  188. this.isLongPress = false;
  189. this.longPressTimer = setTimeout(() => {
  190. this.isLongPress = true;
  191. this.onChange();
  192. this.longPressStep();
  193. }, LONG_PRESS_START_TIME);
  194. },
  195. onTouchEnd() {
  196. if (!this.data.longPress) {
  197. return;
  198. }
  199. clearTimeout(this.longPressTimer);
  200. },
  201. },
  202. });
  203. export default global['__wxComponents']['vant/stepper/index']
  204. </script>
  205. <style platform="mp-weixin">
  206. @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)}
  207. </style>