wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
<template>
    <picker
        class="cl-select__wrap"
        :mode="mode"
        :value="index"
        :range="options"
        :range-key="rangeKey"
        :disabled="disabled"
        :end="end"
        :start="start"
        :fields="fields"
        @change="onChange"
        @columnchange="onColumnChange"
        @cancel="onCancel"
    >
        <slot :label="text" :value="index">
            <cl-select-inner
                :height="height"
                :placeholder="placeholder"
                :disabled="disabled"
                :border="border"
                :round="round"
                :backgroundColor="backgroundColor"
                :borderRadius="borderRadius"
                :arrowIcon="arrowIcon"
                :padding="padding"
                :text="text"
            />
        </slot>
    </picker>
</template>
 
<script lang="ts">
import { computed, defineComponent, ref, watch, type PropType } from "vue";
import { parseRpx } from "/@/cool/utils";
import { isArray, isEmpty } from "lodash-es";
import { Props } from "../cl-select-inner/config";
 
export default defineComponent({
    name: "cl-select",
 
    props: {
        // 绑定值
        modelValue: null,
        // 模式
        mode: {
            type: String as PropType<"selector" | "multiSelector" | "region" | "time" | "date">,
            default: "selector",
        },
        // 选项列表
        options: {
            type: Array as PropType<{ label: string; value: any }[]>,
            default: () => [],
        },
        labelKey: {
            type: String,
            default: "label",
        },
        valueKey: {
            type: String,
            default: "value",
        },
        separator: {
            type: String,
            default: "/",
        },
        fields: {
            type: String as PropType<"year" | "month" | "day">,
            default: "day",
        },
        // 开始时间
        start: String,
        // 结束时间
        end: String,
        // 默认选中第一个
        defaultFirstOption: {
            type: Boolean,
            default: true,
        },
        // 设置选项列表时是否解析值
        setOptionsIsParseValue: Boolean,
        ...Props,
    },
 
    emits: ["update:modelValue", "confirm", "change", "column-change", "cancel"],
 
    setup(props, { emit }) {
        // 选中下标
        const index = ref<any>();
 
        // 文本
        const text = ref("");
 
        // key
        const rangeKey = computed(() => (props.mode == "region" ? "" : props.labelKey));
 
        // 解析值
        function parse(val: any) {
            // 取下标
            index.value = (() => {
                switch (props.mode) {
                    // 返回下标
                    case "selector":
                        return props.options.findIndex(
                            (e: any) => String(e[props.valueKey]) == String(val),
                        );
                    // 返回数组
                    case "multiSelector":
                        return (isArray(val) ? val : [val]).map((v, i) => {
                            //@ts-ignore
                            return props.options[i].findIndex(
                                (e: any) => String(e[props.valueKey]) == String(v),
                            );
                        });
                    default:
                        return val;
                }
            })();
 
            // 取文本值
            text.value = (() => {
                switch (props.mode) {
                    case "selector":
                        return props.options[index.value]
                            ? //@ts-ignore
                              props.options[index.value][props.labelKey]
                            : "";
                    case "multiSelector":
                        return index.value
                            .filter((v: any) => v >= 0)
                            .map(
                                (v: any, i: number) =>
                                    //@ts-ignore
                                    props.options[i][v][props.labelKey],
                            )
                            .join(props.separator);
                    case "region":
                        console.warn("请使用 cl-select-region 代替");
                    default:
                        return index.value;
                }
            })();
        }
 
        // 监听值
        function onChange({ detail }: any) {
            if (detail.value < 0 || detail.value === undefined) {
                return false;
            }
 
            // 返回的完整数据
            let data: any = null;
 
            // 返回的唯一数据
            let value: any = null;
 
            switch (props.mode) {
                case "selector":
                    data = props.options[detail.value];
                    value = data ? data[props.valueKey] : null;
                    break;
                case "multiSelector":
                    data = detail.value
                        .map((v: any) => (v < 0 ? 0 : v))
                        .map(
                            (v: any, i: number) =>
                                //@ts-ignore
                                props.options[i][v],
                        );
                    value = data.map((e: any) => e[props.valueKey]);
                    break;
                default:
                    value = detail.value;
            }
 
            emit("update:modelValue", value);
            emit("confirm", data);
            emit("change", value);
        }
 
        // 监听列
        function onColumnChange({ detail }: any) {
            index.value = index.value.map((v: any, i: number) =>
                i < detail.column ? v : i === detail.column ? detail.value : 0,
            );
 
            emit("column-change", { ...detail, selects: index.value });
        }
 
        // 取消
        function onCancel() {
            emit("cancel");
        }
 
        watch(() => props.modelValue, parse, {
            immediate: true,
        });
 
        watch(
            () => props.options,
            (arr: any[]) => {
                // 避免重复设置 options 异常问题
                if (!props.setOptionsIsParseValue) {
                    parse(props.modelValue);
                }
 
                // 为空时,默认返回列表第一个
                if (!isEmpty(arr) && props.defaultFirstOption) {
                    if (
                        index.value === undefined ||
                        index.value < 0 ||
                        index.value === "" ||
                        index.value === null
                    ) {
                        emit("update:modelValue", arr[0][props.valueKey]);
                    }
                }
            },
            {
                immediate: true,
            },
        );
 
        return {
            index,
            text,
            rangeKey,
            parse,
            onChange,
            onColumnChange,
            onCancel,
            parseRpx,
        };
    },
});
</script>