zhengyiming
2025-02-14 05357b9c587f64cd548e987643066b253cd9bfe0
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
<template>
  <view class="nut-menu-item" v-show="state.showWrapper" :style="style">
    <view
      v-show="state.showPopup"
      @click="handleClickOutside"
      class="nut-menu-item-placeholder-element"
      :style="placeholderElementStyle"
      :catch-move="parent.props.lockScroll"
    >
    </view>
    <nut-popup
      :style="{ position: 'absolute' }"
      :overlayStyle="{ position: 'absolute' }"
      v-bind="$attrs"
      v-model:visible="state.showPopup"
      :position="parent.props.direction === 'down' ? 'top' : 'bottom'"
      :duration="parent.props.duration"
      :destroy-on-close="false"
      :overlay="parent.props.overlay"
      :lockScroll="parent.props.lockScroll"
      @closed="handleClose"
      :close-on-click-overlay="parent.props.closeOnClickOverlay"
    >
      <scroll-view :scroll-y="true">
        <view class="nut-menu-item__content" :class="{ noPadding: !options?.length }">
          <view
            v-for="(option, index) in options"
            :key="index"
            class="nut-menu-item__option"
            :class="[{ active: optionIsActive(option) }]"
            :style="{ 'flex-basis': 100 / cols + '%' }"
            @click="onClick(option)"
          >
            <span
              class="nut-menu-item__span"
              v-if="optionIsActive(option)"
              :class="[optionIsActive(option) ? activeTitleClass : inactiveTitleClass]"
            >
              <slot name="icon">
                <Check v-bind="$attrs" :color="parent.props.activeColor"></Check>
              </slot>
            </span>
            <view
              :class="[optionIsActive(option) ? activeTitleClass : inactiveTitleClass]"
              :style="{ color: optionIsActive(option) ? parent.props.activeColor : '' }"
              >{{ option.text }}</view
            >
          </view>
        </view>
        <slot></slot>
      </scroll-view>
    </nut-popup>
  </view>
</template>
<script lang="ts">
import { MenuItemOption } from '@nutui/nutui-taro/dist/types/__VUE/menuitem/type';
import { Check } from '@nutui/icons-vue-taro';
import { isArray } from 'lodash';
 
export default defineComponent({
  name: 'bl-menu-item',
  props: {
    title: String,
    options: {
      type: Array as PropType<MenuItemOption[]>,
      default: () => [] as MenuItemOption[],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    modelValue: {
      type: [Array, String, Number],
    },
    cols: {
      type: Number,
      default: 1,
    },
    activeTitleClass: String,
    inactiveTitleClass: String,
    emptyValue: {
      type: [String, Number],
      default: null,
    },
    emptyTitle: {
      type: String,
      default: '',
    },
    enableCancelSelect: {
      type: Boolean,
      default: true,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    Check,
  },
  emits: ['update:modelValue', 'change', 'closePopup'],
  setup(props, { emit }) {
    const state = reactive({
      showPopup: false,
      showWrapper: false,
    });
 
    const useParent: any = () => {
      const parent = inject('menuParent', null);
      if (parent) {
        // 获取子组件自己的实例
        const instance = getCurrentInstance()!;
        const { link, removeLink } = parent;
        // @ts-ignore
        link(instance);
        onUnmounted(() => {
          // @ts-ignore
          removeLink(instance);
        });
        return { parent };
      }
    };
 
    const { parent } = useParent();
 
    const style = computed(() => {
      return parent.props.direction === 'down'
        ? { top: parent.offset.value + 'px' }
        : { bottom: parent.offset.value + 'px' };
    });
 
    const placeholderElementStyle = computed(() => {
      const heightStyle = { height: parent.offset.value + 'px' };
      if (parent.props.direction === 'down') {
        return { ...heightStyle, top: '0px' };
      } else {
        return { ...heightStyle, bottom: '0px' };
      }
    });
 
    const toggle = (show = !state.showPopup) => {
      if (show === state.showPopup) {
        return;
      }
      state.showPopup = show;
      if (show) {
        state.showWrapper = true;
      }
    };
 
    const renderTitle = () => {
      if (props.title) {
        return props.title;
      }
      if (isArray(props.modelValue)) {
        const selectedOptions = (props.options ?? [])
          .filter((option) => (props.modelValue as any).includes(option.value))
          .map((x) => x.text);
        return selectedOptions.length > 0 ? selectedOptions.join(',') : props.emptyTitle;
      }
      const match: any = props.options?.find((option: any) => option.value === props.modelValue);
      return match ? match.text : props.emptyTitle;
    };
 
    const onClick = (option: MenuItemOption) => {
      console.log(1);
 
      state.showPopup = false;
      if (props.multiple) {
        const modelValue = props.modelValue as Array<any>;
        const val = modelValue.includes(option.value)
          ? modelValue.filter((x) => x !== option.value)
          : [...modelValue, option.text];
        console.log('val: ', val);
 
        emit('update:modelValue', val);
        emit('change', val);
      } else {
        if (option.value !== props.modelValue) {
          emit('update:modelValue', option.value);
          emit('change', option.value);
        } else {
          if (props.enableCancelSelect) {
            emit('update:modelValue', props.emptyValue);
            emit('change', props.emptyValue);
          }
        }
      }
    };
 
    const handleClose = () => {
      state.showWrapper = false;
    };
 
    const handleClickOutside = () => {
      state.showPopup = false;
      emit('closePopup');
    };
 
    function optionIsActive(option: MenuItemOption) {
      return isArray(props.modelValue)
        ? props.modelValue.includes(option.value)
        : option.value === props.modelValue;
    }
 
    return {
      style,
      placeholderElementStyle,
      renderTitle,
      state,
      parent,
      toggle,
      onClick,
      handleClose,
      handleClickOutside,
      optionIsActive,
    };
  },
});
</script>