123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <template>
- <view class="tui-numberbox">
- <view class="tui-num__icon__box" :style="{background:iconBgColor,borderRadius:radius}" @tap="reduce"
- :class="[disabled || min >= inputValue ? 'tui-disabled' : '']">
- <text class="tui-numbox-icon tui-num__icon-reduce"
- :style="{ color: iconColor, fontSize: iconSize + 'rpx',lineHeight:iconSize + 'rpx' }"></text>
- </view>
- <input type="number" v-model="inputValue" :disabled="disabled" @blur="blur" class="tui-num-input"
- :style="{ color: color, fontSize: size + 'rpx', background: backgroundColor, height: height + 'rpx', minHeight: height + 'rpx', width: width + 'rpx' }" />
- <view class="tui-num__icon__box" :style="{background:iconBgColor,borderRadius:radius}" @tap="plus"
- :class="[disabled || inputValue >= max ? 'tui-disabled' : '']">
- <text class="tui-numbox-icon tui-num__icon-plus"
- :style="{ color: iconColor, fontSize: iconSize + 'rpx',lineHeight:iconSize + 'rpx' }"></text>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'tuiNumberbox',
- emits: ['change'],
- props: {
- value: {
- type: [Number, String],
- default: 1
- },
-
- min: {
- type: Number,
- default: 1
- },
-
- max: {
- type: Number,
- default: 99
- },
-
- step: {
- type: Number,
- default: 1
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
- iconBgColor: {
- type: String,
- default: 'transparent'
- },
- radius:{
- type: String,
- default: '50%'
- },
-
- iconSize: {
- type: Number,
- default: 22
- },
- iconColor: {
- type: String,
- default: '#666666'
- },
-
- height: {
- type: Number,
- default: 42
- },
-
- width: {
- type: Number,
- default: 80
- },
- size: {
- type: Number,
- default: 28
- },
-
- backgroundColor: {
- type: String,
- default: '#F5F5F5'
- },
-
- color: {
- type: String,
- default: '#333'
- },
-
- index: {
- type: [Number, String],
- default: 0
- },
-
- custom: {
- type: [Number, String],
- default: 0
- }
- },
- created() {
- this.inputValue = +this.value;
- },
- data() {
- return {
- inputValue: 0
- };
- },
- watch: {
- value(val) {
- this.inputValue = +val;
- }
- },
- methods: {
- getScale(val, step) {
- let scale = 1;
- let scaleVal = 1;
-
- if (!Number.isInteger(step)) {
- scale = Math.pow(10, (step + '').split('.')[1].length);
- }
-
- if (!Number.isInteger(val)) {
- scaleVal = Math.pow(10, (val + '').split('.')[1].length);
- }
- return Math.max(scale, scaleVal);
- },
- calcNum: function(type) {
- if (this.disabled || (this.inputValue == this.min && type === 'reduce') || (this.inputValue == this
- .max && type === 'plus')) {
- return;
- }
- const scale = this.getScale(Number(this.inputValue), Number(this.step));
- let num = Number(this.inputValue) * scale;
- let step = this.step * scale;
- if (type === 'reduce') {
- num -= step;
- } else if (type === 'plus') {
- num += step;
- }
- let value = Number((num / scale).toFixed(String(scale).length - 1));
- if (value < this.min) {
- value = this.min;
- } else if (value > this.max) {
- value = this.max;
- }
- this.handleChange(value, type);
- },
- plus: function() {
- this.calcNum('plus');
- },
- reduce: function() {
- this.calcNum('reduce');
- },
- blur: function(e) {
- let value = e.detail.value;
- if (value) {
- if (~value.indexOf('.') && Number.isInteger(this.step) && Number.isInteger(Number(value))) {
- value = value.split('.')[0];
- }
- value = Number(value);
- if (value > this.max) {
- value = this.max;
- } else if (value < this.min) {
- value = this.min;
- }
- } else {
- value = this.min;
- }
- if ((value == this.value && value != this.inputValue) || !e.detail.value) {
- this.inputValue = value;
- }
- this.handleChange(value, 'blur');
- },
- handleChange(value, type) {
- if (this.disabled) return;
- this.$emit('change', {
- value: Number(value),
- type: type,
- index: this.index,
- custom: this.custom
- });
- }
- }
- };
- </script>
- <style scoped>
- @font-face {
- font-family: 'numberbox';
- src: url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAASQAA0AAAAABtwAAAAAAA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O2Eu5vSbcsKZxgIV3CKJ+Eq9ZVwjfwqXMcVPoQb1L+EmxjjV7iFa2WpDOFhMEFgnEFjig3jAjEcLJIyBtahOfRmEsxMTzd6ETubOBso71dilwMeaDnngCntPbdmvkon/mDLgdSYbh4FS7YpjS4idCgbXyyc1d2oc7D9nu22tNi/a4E1x+xRDWzU/D3bM9JIbAyvkJI18jK3pBJTj2hrrPG7ZynW814IiU68y/SIx5o0dTr3bmniwOLn8owcfbS5kj33qBw+Y1kIeb/dTsQgil2GP5PYcRkAAAB42mNgYoAALjDJyIAOWMGiTIxMjMwiWZmJQJRXVQoigTgjMd9QGIsgAFDsEBsAAAAAAAAB//8AAgABAAAADAAAABYAAAACAAEAAwAEAAEABAAAAAIAAAAAeNpjYGBgZACCq0vUOUD0TWsFORgNADPBBE4AAA==) format('woff');
- font-weight: normal;
- font-style: normal;
- }
- .tui-num__icon__box {
- display: flex;
- align-items: center;
- justify-content: center;
- text-align: center;
- padding: 12rpx;
-
- cursor: pointer;
-
- }
- .tui-numbox-icon {
- font-family: 'numberbox' !important;
- font-style: normal;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale;
- }
- .tui-num__icon-reduce:before {
- content: '\e691';
- }
- .tui-num__icon-plus:before {
- content: '\e605';
- }
- .tui-numberbox {
-
- display: inline-flex;
-
- align-items: center;
- }
- .tui-num-input {
- text-align: center;
- margin: 0 6rpx;
- font-weight: 400;
- padding: 0;
- }
- .tui-disabled {
- opacity: .5;
-
- cursor: not-allowed;
-
- }
- </style>
|