index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import _mergeJSXProps2 from "@vue/babel-helper-vue-jsx-merge-props";
  2. import _mergeJSXProps from "@vue/babel-helper-vue-jsx-merge-props";
  3. import { createNamespace, isDef, addUnit } from '../utils';
  4. import { resetScroll } from '../utils/dom/reset-scroll';
  5. import { preventDefault } from '../utils/dom/event';
  6. import { addNumber, formatNumber as _formatNumber } from '../utils/format/number';
  7. import { isNaN } from '../utils/validate/number';
  8. import { FieldMixin } from '../mixins/field';
  9. var _createNamespace = createNamespace('stepper'),
  10. createComponent = _createNamespace[0],
  11. bem = _createNamespace[1];
  12. var LONG_PRESS_START_TIME = 600;
  13. var LONG_PRESS_INTERVAL = 200;
  14. function equal(value1, value2) {
  15. return String(value1) === String(value2);
  16. }
  17. export default createComponent({
  18. mixins: [FieldMixin],
  19. props: {
  20. value: null,
  21. theme: String,
  22. integer: Boolean,
  23. disabled: Boolean,
  24. allowEmpty: Boolean,
  25. inputWidth: [Number, String],
  26. buttonSize: [Number, String],
  27. asyncChange: Boolean,
  28. placeholder: String,
  29. disablePlus: Boolean,
  30. disableMinus: Boolean,
  31. disableInput: Boolean,
  32. decimalLength: [Number, String],
  33. name: {
  34. type: [Number, String],
  35. default: ''
  36. },
  37. min: {
  38. type: [Number, String],
  39. default: 1
  40. },
  41. max: {
  42. type: [Number, String],
  43. default: Infinity
  44. },
  45. step: {
  46. type: [Number, String],
  47. default: 1
  48. },
  49. defaultValue: {
  50. type: [Number, String],
  51. default: 1
  52. },
  53. showPlus: {
  54. type: Boolean,
  55. default: true
  56. },
  57. showMinus: {
  58. type: Boolean,
  59. default: true
  60. },
  61. showInput: {
  62. type: Boolean,
  63. default: true
  64. },
  65. longPress: {
  66. type: Boolean,
  67. default: true
  68. }
  69. },
  70. data: function data() {
  71. var _this$value;
  72. var defaultValue = (_this$value = this.value) != null ? _this$value : this.defaultValue;
  73. var value = this.format(defaultValue);
  74. if (!equal(value, this.value)) {
  75. this.$emit('input', value);
  76. }
  77. return {
  78. currentValue: value
  79. };
  80. },
  81. computed: {
  82. minusDisabled: function minusDisabled() {
  83. return this.disabled || this.disableMinus || this.currentValue <= +this.min;
  84. },
  85. plusDisabled: function plusDisabled() {
  86. return this.disabled || this.disablePlus || this.currentValue >= +this.max;
  87. },
  88. inputStyle: function inputStyle() {
  89. var style = {};
  90. if (this.inputWidth) {
  91. style.width = addUnit(this.inputWidth);
  92. }
  93. if (this.buttonSize) {
  94. style.height = addUnit(this.buttonSize);
  95. }
  96. return style;
  97. },
  98. buttonStyle: function buttonStyle() {
  99. if (this.buttonSize) {
  100. var size = addUnit(this.buttonSize);
  101. return {
  102. width: size,
  103. height: size
  104. };
  105. }
  106. }
  107. },
  108. watch: {
  109. max: 'check',
  110. min: 'check',
  111. integer: 'check',
  112. decimalLength: 'check',
  113. value: function value(val) {
  114. if (!equal(val, this.currentValue)) {
  115. this.currentValue = this.format(val);
  116. }
  117. },
  118. currentValue: function currentValue(val) {
  119. this.$emit('input', val);
  120. this.$emit('change', val, {
  121. name: this.name
  122. });
  123. }
  124. },
  125. methods: {
  126. check: function check() {
  127. var val = this.format(this.currentValue);
  128. if (!equal(val, this.currentValue)) {
  129. this.currentValue = val;
  130. }
  131. },
  132. // formatNumber illegal characters
  133. formatNumber: function formatNumber(value) {
  134. return _formatNumber(String(value), !this.integer);
  135. },
  136. format: function format(value) {
  137. if (this.allowEmpty && value === '') {
  138. return value;
  139. }
  140. value = this.formatNumber(value); // format range
  141. value = value === '' ? 0 : +value;
  142. value = isNaN(value) ? this.min : value;
  143. value = Math.max(Math.min(this.max, value), this.min); // format decimal
  144. if (isDef(this.decimalLength)) {
  145. value = value.toFixed(this.decimalLength);
  146. }
  147. return value;
  148. },
  149. onInput: function onInput(event) {
  150. var value = event.target.value;
  151. var formatted = this.formatNumber(value); // limit max decimal length
  152. if (isDef(this.decimalLength) && formatted.indexOf('.') !== -1) {
  153. var pair = formatted.split('.');
  154. formatted = pair[0] + "." + pair[1].slice(0, this.decimalLength);
  155. }
  156. if (!equal(value, formatted)) {
  157. event.target.value = formatted;
  158. } // prefer number type
  159. if (formatted === String(+formatted)) {
  160. formatted = +formatted;
  161. }
  162. this.emitChange(formatted);
  163. },
  164. emitChange: function emitChange(value) {
  165. if (this.asyncChange) {
  166. this.$emit('input', value);
  167. this.$emit('change', value, {
  168. name: this.name
  169. });
  170. } else {
  171. this.currentValue = value;
  172. }
  173. },
  174. onChange: function onChange() {
  175. var type = this.type;
  176. if (this[type + "Disabled"]) {
  177. this.$emit('overlimit', type);
  178. return;
  179. }
  180. var diff = type === 'minus' ? -this.step : +this.step;
  181. var value = this.format(addNumber(+this.currentValue, diff));
  182. this.emitChange(value);
  183. this.$emit(type);
  184. },
  185. onFocus: function onFocus(event) {
  186. // readonly not work in legacy mobile safari
  187. if (this.disableInput && this.$refs.input) {
  188. this.$refs.input.blur();
  189. } else {
  190. this.$emit('focus', event);
  191. }
  192. },
  193. onBlur: function onBlur(event) {
  194. var value = this.format(event.target.value);
  195. event.target.value = value;
  196. this.emitChange(value);
  197. this.$emit('blur', event);
  198. resetScroll();
  199. },
  200. longPressStep: function longPressStep() {
  201. var _this = this;
  202. this.longPressTimer = setTimeout(function () {
  203. _this.onChange();
  204. _this.longPressStep(_this.type);
  205. }, LONG_PRESS_INTERVAL);
  206. },
  207. onTouchStart: function onTouchStart() {
  208. var _this2 = this;
  209. if (!this.longPress) {
  210. return;
  211. }
  212. clearTimeout(this.longPressTimer);
  213. this.isLongPress = false;
  214. this.longPressTimer = setTimeout(function () {
  215. _this2.isLongPress = true;
  216. _this2.onChange();
  217. _this2.longPressStep();
  218. }, LONG_PRESS_START_TIME);
  219. },
  220. onTouchEnd: function onTouchEnd(event) {
  221. if (!this.longPress) {
  222. return;
  223. }
  224. clearTimeout(this.longPressTimer);
  225. if (this.isLongPress) {
  226. preventDefault(event);
  227. }
  228. },
  229. onMousedown: function onMousedown(event) {
  230. // fix mobile safari page scroll down issue
  231. // see: https://github.com/vant-ui/vant/issues/7690
  232. if (this.disableInput) {
  233. event.preventDefault();
  234. }
  235. }
  236. },
  237. render: function render() {
  238. var _this3 = this;
  239. var h = arguments[0];
  240. var createListeners = function createListeners(type) {
  241. return {
  242. on: {
  243. click: function click(e) {
  244. // disable double tap scrolling on mobile safari
  245. e.preventDefault();
  246. _this3.type = type;
  247. _this3.onChange();
  248. },
  249. touchstart: function touchstart() {
  250. _this3.type = type;
  251. _this3.onTouchStart();
  252. },
  253. touchend: _this3.onTouchEnd,
  254. touchcancel: _this3.onTouchEnd
  255. }
  256. };
  257. };
  258. return h("div", {
  259. "class": bem([this.theme])
  260. }, [h("button", _mergeJSXProps([{
  261. "directives": [{
  262. name: "show",
  263. value: this.showMinus
  264. }],
  265. "attrs": {
  266. "type": "button"
  267. },
  268. "style": this.buttonStyle,
  269. "class": bem('minus', {
  270. disabled: this.minusDisabled
  271. })
  272. }, createListeners('minus')])), h("input", {
  273. "directives": [{
  274. name: "show",
  275. value: this.showInput
  276. }],
  277. "ref": "input",
  278. "attrs": {
  279. "type": this.integer ? 'tel' : 'text',
  280. "role": "spinbutton",
  281. "disabled": this.disabled,
  282. "readonly": this.disableInput,
  283. "inputmode": this.integer ? 'numeric' : 'decimal',
  284. "placeholder": this.placeholder,
  285. "aria-valuemax": this.max,
  286. "aria-valuemin": this.min,
  287. "aria-valuenow": this.currentValue
  288. },
  289. "class": bem('input'),
  290. "domProps": {
  291. "value": this.currentValue
  292. },
  293. "style": this.inputStyle,
  294. "on": {
  295. "input": this.onInput,
  296. "focus": this.onFocus,
  297. "blur": this.onBlur,
  298. "mousedown": this.onMousedown
  299. }
  300. }), h("button", _mergeJSXProps2([{
  301. "directives": [{
  302. name: "show",
  303. value: this.showPlus
  304. }],
  305. "attrs": {
  306. "type": "button"
  307. },
  308. "style": this.buttonStyle,
  309. "class": bem('plus', {
  310. disabled: this.plusDisabled
  311. })
  312. }, createListeners('plus')]))]);
  313. }
  314. });