ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, onMounted, reactive, toRefs, unref, watch } from 'vue';
 
import { useAttrs, useMessage } from '@jnpf/hooks';
import { AButton, ScrollContainer } from '@jnpf/ui';
import { ModalClose, ModalFooter } from '@jnpf/ui/modal';
 
import { $t } from '@vben/locales';
 
import { globalShareState } from '@vben-core/shared/global-state';
 
import { useDebounceFn } from '@vueuse/core';
import { Modal as AModal, Checkbox, Form, Input, Radio, Select, TabPane, Tabs, Tag } from 'ant-design-vue';
import { cloneDeep, pick } from 'lodash-es';
 
import { groupSelectProps } from './props';
 
interface State {
  activeKey: string;
  allList: any[];
  cacheData: any[];
  innerValue: any[] | string | undefined;
  keyword: string;
  loading: boolean;
  options: any[];
  selectedData: any[];
  selectedIds: any[];
  treeData: any[];
  visible: boolean;
}
 
defineOptions({ inheritAttrs: false, name: 'JnpfGroupSelect' });
 
const props = defineProps(groupSelectProps);
const emit = defineEmits(['update:value', 'change', 'labelChange']);
const attrs: any = useAttrs({ excludeDefaultKeys: false });
const { getGroupByCondition } = globalShareState.getApi();
const { useOrganizeStore } = globalShareState.getStores();
const organizeStore = useOrganizeStore();
const formItemContext = Form.useInjectFormItemContext();
const { createMessage } = useMessage();
const state = reactive<State>({
  activeKey: '1',
  allList: [],
  cacheData: [],
  innerValue: '',
  keyword: '',
  loading: false,
  options: [],
  selectedData: [],
  selectedIds: [],
  treeData: [],
  visible: false,
});
const { activeKey, innerValue, keyword, loading, options, selectedData, selectedIds, treeData, visible } = toRefs(state);
const debounceHandleSearch = useDebounceFn(handleSearch, 300);
 
const getSelectBindValue: any = computed(() => ({
  ...pick(props, ['placeholder', 'disabled', 'size', 'allowClear']),
  class: unref(attrs).class ? `w-full ${unref(attrs).class}` : 'w-full',
  fieldNames: { label: 'fullName', value: 'id' },
  mode: props.multiple ? 'multiple' : '',
  open: false,
  showArrow: true,
  showSearch: false,
  style: Reflect.has(unref(attrs), 'style') ? unref(attrs).style : {},
}));
 
watch(
  () => unref(props.value),
  () => {
    setValue();
  },
  { immediate: true },
);
watch(
  () => state.allList,
  () => {
    setValue();
  },
  { deep: true },
);
watch(
  () => state.selectedData,
  (val) => {
    state.selectedIds = val.map((o) => o.id);
  },
  { deep: true },
);
 
function setValue() {
  if (!props.value || props.value.length === 0) {
    state.innerValue = props.multiple ? [] : undefined;
    state.options = [];
    state.selectedData = [];
    emit('labelChange', '');
    return;
  }
  const ids = props.multiple ? (props.value as any[]) : [props.value];
  const selectedList: any[] = [];
  for (const id of ids) {
    inner: for (let j = 0; j < state.allList.length; j++) {
      if (id === state.allList[j].id) {
        selectedList.push(state.allList[j]);
        break inner;
      }
    }
  }
  const innerIds = selectedList.map((o) => o.id);
  state.innerValue = props.multiple ? innerIds : innerIds[0];
  state.options = cloneDeep(selectedList);
  state.selectedData = cloneDeep(selectedList);
  const labels = state.selectedData.map((o) => o.fullName).join(',');
  emit('labelChange', labels);
}
function onChange(_val, option) {
  state.selectedData = option ?? [];
  handleSubmit();
}
function onTagClose(i) {
  removeData(i);
  handleSubmit();
}
function openSelectModal() {
  if (props.disabled) return;
  state.visible = true;
  state.keyword = '';
  state.treeData = [];
  initData();
  setValue();
}
function handleCancel() {
  state.visible = false;
}
function handleSearch(e) {
  const value = e.target.value;
  state.treeData = state.cacheData.filter((o) => o.fullName.includes(value));
}
function handleSelect(data) {
  if (data?.disabled) return;
  const index = state.selectedData.findIndex((o) => o.id === data.id);
  if (index !== -1) return state.selectedData.splice(index, 1);
  props.multiple ? state.selectedData.push(data) : (state.selectedData = [data]);
}
function removeAll() {
  state.selectedData = [];
}
function removeData(index: number) {
  state.selectedData.splice(index, 1);
}
function handleSubmit() {
  const ids = state.selectedData.map((o) => o.id);
  if (props.required && !ids.length) {
    createMessage.warning('至少选择一条数据');
    return;
  }
  state.options = state.selectedData;
  state.innerValue = props.multiple ? ids : ids[0];
  if (props.multiple) {
    emit('update:value', ids);
    emit('change', ids, state.options);
  } else {
    emit('update:value', ids[0] || '');
    emit('change', ids[0] || '', state.options[0]);
  }
  formItemContext.onFieldChange();
  handleCancel();
}
async function initData() {
  state.loading = true;
  state.allList = await organizeStore.getGroupList();
  if (props.selectType === 'all') {
    state.treeData = state.allList;
    state.cacheData = state.allList;
    state.loading = false;
  } else {
    if (!props.ableIds?.length) {
      state.treeData = [];
      state.cacheData = [];
      state.loading = false;
      return;
    }
    const query = { ids: props.ableIds };
    getGroupByCondition(query).then((res) => {
      state.treeData = res.data || [];
      state.cacheData = res.data || [];
      state.loading = false;
    });
  }
}
 
onMounted(async () => {
  state.allList = await organizeStore.getGroupList();
});
</script>
 
<template>
  <div v-if="buttonType === 'button'" :class="[$attrs.class]" class="select-tag-list">
    <AButton pre-icon="icon-ym icon-ym-btn-add" :type="buttonShowType" @click="openSelectModal">{{ modalTitle }}</AButton>
    <div class="tags" v-if="showSelectedList">
      <Tag v-for="(item, i) in options" :key="item.id" :closable="!disabled" class="!mt-[10px]" @close="onTagClose(i)">{{ item.fullName }}</Tag>
    </div>
  </div>
  <Select v-bind="getSelectBindValue" v-else v-model:value="innerValue" :options="options" @change="onChange" @click="openSelectModal" />
  <AModal v-model:open="visible" :keyboard="false" :mask-closable="false" title="选择用户组" :width="800" centered class="common-select-modal">
    <template #closeIcon>
      <ModalClose :can-fullscreen="false" @cancel="handleCancel" />
    </template>
    <template #footer>
      <ModalFooter @cancel="handleCancel" @ok="handleSubmit">
        <template #insertFooter v-if="multiple">
          <div class="float-left">
            <span class="mr-[10px]">{{ $t('component.jnpf.common.selected') }}({{ selectedData.length }})</span>
            <span class="remove-all-btn" @click="removeAll">{{ $t('component.jnpf.common.clearAll') }}</span>
          </div>
        </template>
      </ModalFooter>
    </template>
    <div class="common-select-modal-main">
      <div class="selected-pane">
        <Tag v-for="(item, i) in selectedData" :key="item.id" :closable="true" class="selected-pane-tag" @close="removeData(i)">
          {{ item.fullName }}
        </Tag>
      </div>
      <div class="select-pane">
        <div class="select-pane-tool">
          <Tabs v-model:active-key="activeKey" :tab-bar-gutter="20" class="select-pane-tool-tabs">
            <TabPane key="1" tab="用户组" />
            <template #rightExtra>
              <Input v-model:value="keyword" :placeholder="$t('common.enterKeyword')" allow-clear @change="debounceHandleSearch" />
            </template>
          </Tabs>
          <div class="select-pane-main">
            <ScrollContainer class="select-pane-list" v-loading="loading">
              <div v-for="(item, i) in treeData" :key="i" class="select-pane-list__item" @click="handleSelect(item)">
                <div class="select-pane-list__item-title">
                  <i :class="item.icon" v-if="item.icon" class="mr-[6px]"></i>
                  <span :title="item.fullName">{{ item.fullName }}</span>
                </div>
                <div class="select-pane-list__item-action">
                  <Checkbox :checked="selectedIds.includes(item.id)" v-if="multiple" />
                  <Radio :checked="selectedIds.includes(item.id)" v-else />
                </div>
              </div>
              <jnpf-empty v-if="treeData.length === 0" />
            </ScrollContainer>
          </div>
        </div>
      </div>
    </div>
  </AModal>
</template>