闫增涛
2024-02-23 fdfb3ca757ecd6c396632ed276ff354671d3a7e5
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
Component({
  properties: {
    show: {
      type: Boolean,
      observer(show) {
        if (!show) return;
        this.updateDivisions();
      },
    },
    title: {
      type: String,
      value: '',
    },
    value: {
      type: String,
      value: '',
      observer() {
        if (!this.data.show) return;
        this.updateDivisions();
      },
    },
    pickerOptions: {
      type: Array,
      value: [],
      observer() {
        if (!this.data.show) return;
        this.updateDivisions();
      },
    },
    headerVisible: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    pickerValue: [],
  },
  methods: {
    updateDivisions() {
      const { pickerOptions, value } = this.data;
      const index = (pickerOptions || []).findIndex(
        (item) => item.code === value,
      );
 
      setTimeout(() => {
        this.setData({ pickerValue: index >= 0 ? [index] : [0] });
      }, 0);
    },
 
    getAreaByIndex(indexes) {
      const { pickerOptions } = this.data;
      return pickerOptions[indexes.toString()];
    },
 
    onChange(e) {
      const currentValue = e.detail.value;
      const target = this.getAreaByIndex(currentValue);
      if (target === null) return;
 
      this.setData({ pickerValue: currentValue });
      this.triggerEvent('change', { value: target.code, target: target });
    },
 
    onConfirm() {
      const target = this.getAreaByIndex(this.data.pickerValue);
      this.triggerEvent('confirm', { value: target?.code, target });
    },
 
    onClose() {
      this.triggerEvent('close');
    },
  },
});