wujingjing
2024-10-29 a6e4b1d05df37a87413677f4b827bce2af58b7b9
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
<template>
    <el-select
        class="w-32"
        :style="{ width: selectWidth }"
        v-model="selectValue"
        @change="changeValue"
        :disabled="disabled"
        :placeholder="data.title"
    >
        <el-option v-for="item in selectList" :key="item.value" :value="item.value" :label="item.title"></el-option>
    </el-select>
</template>
 
<script setup lang="ts">
import { ref, type PropType, computed } from 'vue';
import { getTextWidth } from '/@/utils/util';
import { StepParam } from '../types';
import { DAY_STEP_LIST, HOUR_STEP_LIST, IS_DAY_LIST } from './constants';
 
const props = defineProps({
    data: {
        type: Object as PropType<StepParam>,
    },
    disabled: {
        type: Boolean,
        default: false,
    },
});
 
const getSelectList = (defaultStepValue: string) => {
    if (IS_DAY_LIST.includes(defaultStepValue)) {
        return DAY_STEP_LIST;
    } else {
        return HOUR_STEP_LIST;
    }
};
 
const selectList = getSelectList(props.data?.origin?.step_value);
// const fontSize = ref('14px');
 
const emit = defineEmits(['change']);
const SELECT_OFFSET = 47;
const selectWidth = computed(() => {
    if (props.data?.list?.length > 0) {
        // 以最大字长为宽度
        const widthList = props.data.list.map((item) =>
            getTextWidth(item.title, {
                size:'14px',
            })
        );
        const maxWidth = Math.max(...widthList);
        const realWidth = maxWidth + SELECT_OFFSET;
        return realWidth + 'px';
    } else {
        return 0;
    }
});
 
const selectValue = defineModel({
    type: String,
});
 
const changeValue = (val) => {
    emit('change', val);
};
</script>
<style scoped lang="scss">
:deep(.el-input) {
    font-size: 14px;
}
</style>