| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- "use strict";
- exports.__esModule = true;
- exports["default"] = void 0;
- var _utils = require("../utils");
- var _use = (0, _utils.use)('stepper'),
- sfc = _use[0],
- bem = _use[1];
- var _default = sfc({
- props: {
- value: null,
- integer: Boolean,
- disabled: Boolean,
- inputWidth: String,
- asyncChange: Boolean,
- disableInput: Boolean,
- min: {
- type: [String, Number],
- "default": 1
- },
- max: {
- type: [String, Number],
- "default": Infinity
- },
- step: {
- type: [String, Number],
- "default": 1
- },
- defaultValue: {
- type: [String, Number],
- "default": 1
- }
- },
- data: function data() {
- var value = this.range((0, _utils.isDef)(this.value) ? this.value : this.defaultValue);
- if (value !== +this.value) {
- this.$emit('input', value);
- }
- return {
- currentValue: value
- };
- },
- computed: {
- minusDisabled: function minusDisabled() {
- return this.disabled || this.currentValue <= this.min;
- },
- plusDisabled: function plusDisabled() {
- return this.disabled || this.currentValue >= this.max;
- }
- },
- watch: {
- value: function value(val) {
- if (val !== this.currentValue) {
- this.currentValue = this.format(val);
- }
- },
- currentValue: function currentValue(val) {
- this.$emit('input', val);
- this.$emit('change', val);
- }
- },
- methods: {
- // filter illegal characters
- format: function format(value) {
- value = String(value).replace(/[^0-9.-]/g, '');
- return value === '' ? 0 : this.integer ? Math.floor(value) : +value;
- },
- // limit value range
- range: function range(value) {
- return Math.max(Math.min(this.max, this.format(value)), this.min);
- },
- onInput: function onInput(event) {
- var value = event.target.value;
- var formatted = this.format(value);
- if (!this.asyncChange) {
- if (+value !== formatted) {
- event.target.value = formatted;
- }
- this.currentValue = formatted;
- } else {
- event.target.value = this.currentValue;
- this.$emit('input', formatted);
- this.$emit('change', formatted);
- }
- },
- onChange: function onChange(type) {
- if (this[type + "Disabled"]) {
- this.$emit('overlimit', type);
- return;
- }
- var diff = type === 'minus' ? -this.step : +this.step;
- var value = Math.round((this.currentValue + diff) * 100) / 100;
- if (!this.asyncChange) {
- this.currentValue = this.range(value);
- } else {
- this.$emit('input', value);
- this.$emit('change', value);
- }
- this.$emit(type);
- },
- onFocus: function onFocus(event) {
- this.$emit('focus', event);
- },
- onBlur: function onBlur(event) {
- this.currentValue = this.range(this.currentValue);
- this.$emit('blur', event); // fix edge case when input is empty and min is 0
- if (this.currentValue === 0) {
- event.target.value = this.currentValue;
- }
- }
- },
- render: function render(h) {
- var _this = this;
- var onChange = function onChange(type) {
- return function () {
- _this.onChange(type);
- };
- };
- return h("div", {
- "class": bem()
- }, [h("button", {
- "class": bem('minus', {
- disabled: this.minusDisabled
- }),
- "on": {
- "click": onChange('minus')
- }
- }), h("input", {
- "attrs": {
- "type": "number",
- "disabled": this.disabled || this.disableInput
- },
- "class": bem('input'),
- "domProps": {
- "value": this.currentValue
- },
- "style": {
- width: this.inputWidth
- },
- "on": {
- "input": this.onInput,
- "focus": this.onFocus,
- "blur": this.onBlur
- }
- }), h("button", {
- "class": bem('plus', {
- disabled: this.plusDisabled
- }),
- "on": {
- "click": onChange('plus')
- }
- })]);
- }
- });
- exports["default"] = _default;
|