-
zhangwei
2 天以前 6002efe19de5fbf0ebf4f5192f3d9088f7588439
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!-- 
// NumberRange组件,用于输入数字范围(支持前缀和后缀插槽,支持数字精度,支持限制取值范围),在数值、金额等的范围输入场景中使用
// 使用示例:
<template>
    <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
        <el-form-item label="订单金额">
            <number-range v-model="queryParams.amountRange">
                <template #prepend>
                    <span>范围</span>
                </template>
            </number-range>
        </el-form-item>
    </el-col>
</template>
<script lang="ts" setup>
import NumberRange from '/@/components/numberRange/index.vue';
</script>
-->
 
<template>
    <div class="number-range-container">
        <div :id="usePrepend ? 'prepend' : ''" :class="{ 'slot-default': slotStyle === 'default', 'slot-pend ': usePrepend }">
            <slot name="prepend">
                <!-- 前缀插槽 -->
            </slot>
        </div>
        <div
            class="number-range"
            :class="{
                'is-disabled': disabled,
                'is-focus': isFocus,
                'number-range-left-border-radius-0': usePrepend,
                'number-range-right-border-radius-0': useAppend,
            }"
        >
            <el-input-number
                :disabled="disabled"
                placeholder="最小值"
                @blur="handleBlur"
                @focus="handleFocus"
                @change="handleChangeMinValue"
                @update:modelValue="updateMinValue"
                v-model="minValue_"
                v-bind="$attrs"
                :controls="false"
            />
            <div class="to">
                <span>{{ to }}</span>
            </div>
            <el-input-number
                :disabled="disabled"
                placeholder="最大值"
                @blur="handleBlur"
                @focus="handleFocus"
                @change="handleChangeMaxValue"
                @update:modelValue="updateMaxValue"
                v-model="maxValue_"
                v-bind="$attrs"
                :controls="false"
            />
            <!-- 清除图标 -->
            <el-icon v-if="clearable && (minValue_ || maxValue_)" class="el-icon el-input__icon el-input__clear" @click="clearValues">
                <CircleClose />
            </el-icon>
        </div>
        <div :id="useAppend ? 'append' : ''" :class="{ 'slot-default': slotStyle === 'default', 'slot-pend ': useAppend }">
            <slot name="append">
                <!-- 后缀插槽 -->
            </slot>
        </div>
    </div>
</template>
<script lang="ts" setup name="numberRange">
import { computed, ref, useSlots } from 'vue';
import { CircleClose } from '@element-plus/icons-vue';
 
const props = defineProps({
    modelValue: {
        type: Array<Number>,
        default: () => [null, null], // 调用时使用v-model="[min,max]" 绑定
    },
    clearable: {
        type: Boolean,
        default: false,
    },
    minValue: {
        type: Number,
        default: null, // 调用时使用v-model:min-value="" 绑定多个v-model
    },
    maxValue: {
        type: Number,
        default: null, // 调用时使用v-model:max-value="" 绑定多个v-model
    },
    // 是否禁用
    disabled: {
        type: Boolean,
        default: false,
    },
    to: {
        type: String,
        default: '-',
    },
    // 精度参数 -保留小数位数
    precision: {
        type: Number,
        default: 0,
        validator(val: number) {
            return val >= 0 && val === parseInt(String(val), 10);
        },
    },
    // 限制取值范围
    valueRange: {
        type: Array,
        default: () => [],
        validator(val: []) {
            if (val && val.length > 0) {
                // @ts-ignore
                if (val.length !== 2) {
                    throw new Error('请传入长度为2的Number数组');
                }
                // @ts-ignore
                if (typeof val[0] !== 'number' || typeof val[1] !== 'number') {
                    throw new Error('取值范围只接受Number类型,请确认');
                }
                // @ts-ignore
                if (val[1] < val[0]) {
                    throw new Error('valueRange格式须为[最小值,最大值],请确认');
                }
            }
            return true;
        },
    },
    // 插槽样式
    slotStyle: {
        type: String, // default --异色背景 |  plain--无背景色
        default: 'plain',
    },
});
 
const emit = defineEmits(['update:modelValue', 'update:minValue', 'update:maxValue', 'change']);
 
const minValue_ = computed({
    get() {
        return props.minValue || props.modelValue[0] || null;
    },
    set(value) {
        if (value === null) {
            emit('update:minValue', null);
            emit('update:modelValue', [null, maxValue_.value]);
            return;
        }
        emit('update:minValue', value);
        emit('update:modelValue', [value, maxValue_.value]);
    },
});
 
const maxValue_ = computed({
    get() {
        return props.maxValue || props.modelValue[1] || null;
    },
    set(value) {
        if (value === null) {
            emit('update:maxValue', null);
            emit('update:modelValue', [minValue_.value, null]);
            return;
        }
        emit('update:maxValue', value);
        emit('update:modelValue', [minValue_.value, value]);
    },
});
 
// 清除值的方法
const clearValues = () => {
    minValue_.value = null;
    maxValue_.value = null;
    emit('update:modelValue', [null, null]);
};
 
const handleChangeMinValue = (value: number | null) => {
    // 非数字空返回null
    if (value === null || isNaN(value)) {
        emit('update:minValue', null);
        return;
    }
    // 初始化数字精度
    const newMinValue = parsePrecision(value, props.precision);
    // min > max 交换min max
    if (typeof newMinValue === 'number' && parseFloat(String(newMinValue)) > parseFloat(String(maxValue_.value))) {
        // 取值范围判定
        const { min, max } = decideValueRange(Number(maxValue_.value), newMinValue);
        // 更新绑定值
        updateValue(min, max);
    }
};
 
const handleChangeMaxValue = (value: number | null) => {
    // 非数字空返回null
    if (value === null || isNaN(value)) {
        emit('update:maxValue', null);
        return;
    }
    // 初始化数字精度
    const newMaxValue = parsePrecision(value, props.precision);
    // max < min 交换min max
    if (typeof newMaxValue === 'number' && parseFloat(String(newMaxValue)) < parseFloat(String(minValue_.value))) {
        // 取值范围判定
        const { min, max } = decideValueRange(newMaxValue, Number(minValue_.value));
        // 更新绑定值
        updateValue(min, max);
    }
};
 
const updateMinValue = (value: number | null) => {
    minValue_.value = value;
};
 
const updateMaxValue = (value: number | null) => {
    maxValue_.value = value;
};
 
// 更新数据
const updateValue = (min: number | null, max: number | null) => {
    emit('update:minValue', min);
    emit('update:maxValue', max);
    emit('update:modelValue', [min, max]);
    emit('change', { min, max });
};
 
// 取值范围判定
const decideValueRange = (min: number | null, max: number | null) => {
    if (min === null || max === null) {
        return { min, max };
    }
 
    if (props.valueRange && props.valueRange.length > 0) {
        // @ts-ignore
        min = min < props.valueRange[0] ? props.valueRange[0] : min > props.valueRange[1] ? props.valueRange[1] : min;
        // @ts-ignore
        max = max > props.valueRange[1] ? props.valueRange[1] : max;
    }
    return { min, max };
};
 
// input焦点事件
const isFocus = ref();
 
const handleFocus = () => {
    isFocus.value = true;
};
 
const handleBlur = () => {
    isFocus.value = false;
};
 
// 处理数字精度
const parsePrecision = (number: number | null, precision = 0) => {
    if (number === null) {
        return null;
    }
    return parseFloat(String(Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)));
};
 
// 判断插槽是否被使用
// 组件外部使用时插入了
// <template #插槽名 >
// </template>
// 无论template标签内是否插入了内容,均视为已使用该插槽
const slots = useSlots();
const usePrepend = computed(() => {
    // 前缀插槽
    return slots && slots.prepend ? true : false;
});
const useAppend = computed(() => {
    // 后缀插槽
    return slots && slots.append ? true : false;
});
</script>
<style lang="scss" scoped>
.number-range-container {
    position: relative;
    display: flex;
    height: 100%;
    .slot-pend {
        white-space: nowrap;
        color: var(--el-color-info);
        border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
    }
    #prepend {
        padding: 0 20px;
        box-shadow:
            1px 0 0 0 var(--el-input-border-color, var(--el-border-color)) inset,
            0 1px 0 0 var(--el-input-border-color, var(--el-border-color)) inset,
            0 -1px 0 0 var(--el-input-border-color, var(--el-border-color)) inset;
        border-right: 0;
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
    }
    #append {
        padding: 0 20px;
        box-shadow:
            0 1px 0 0 var(--el-input-border-color, var(--el-border-color)) inset,
            0 -1px 0 0 var(--el-input-border-color, var(--el-border-color)) inset,
            -1px 0 0 0 var(--el-input-border-color, var(--el-border-color)) inset;
        border-left: 0;
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
    }
    .slot-default {
        background-color: var(--el-fill-color-light);
    }
 
    .number-range-left-border-radius-0 {
        border-top-left-radius: 0 !important;
        border-bottom-left-radius: 0 !important;
    }
    .number-range-right-border-radius-0 {
        border-top-right-radius: 0 !important;
        border-bottom-right-radius: 0 !important;
    }
 
    .number-range {
        background-color: var(--el-bg-color) !important;
        box-shadow: 0 0 0 1px var(--el-input-border-color, var(--el-border-color)) inset;
        border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
        padding: 0 2px;
        display: flex;
        flex-direction: row;
        width: 100%;
        justify-content: center;
        align-items: center;
        color: var(--el-input-text-color, var(--el-text-color-regular));
        transition: var(--el-transition-box-shadow);
        transform: translate3d(0, 0, 0);
        overflow: hidden;
 
        .to {
            margin-top: 1px;
        }
    }
 
    .is-focus {
        transition: all 0.3s;
        box-shadow: 0 0 0 1px var(--el-color-primary) inset !important;
    }
    .is-disabled {
        background-color: var(--el-input-bg-color);
        color: var(--el-input-text-color, var(--el-text-color-regular));
        cursor: not-allowed;
        .to {
            height: calc(100% - 3px);
            background-color: var(--el-fill-color-light) !important;
        }
    }
}
 
.el-input__clear {
    cursor: pointer;
    color: var(--el-input-icon-color, var(--el-text-color-placeholder));
    margin-left: 10px;
    display: none;
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
}
 
.number-range:hover .el-input__clear {
    display: flex;
    align-items: center;
    color: var(--el-input-clear-hover-color);
}
 
:deep(.el-input) {
    border: none;
}
:deep(.el-input__wrapper) {
    margin: 0;
    padding: 0 15px;
    background-color: transparent;
    border: none !important;
    box-shadow: none !important;
    &.is-focus {
        border: none !important;
        box-shadow: none !important;
    }
}
 
:deep(.el-input),
:deep(.el-select),
:deep(.el-input-number) {
    width: 100%;
}
</style>