Skip to content

Commit 65b4d08

Browse files
committed
Update behavior municio select
1 parent 506007f commit 65b4d08

File tree

1 file changed

+87
-60
lines changed

1 file changed

+87
-60
lines changed

src/components/sub-select.js

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref, watch, computed, toRaw, onBeforeMount, onMounted, h, reactive, nextTick } from "vue/dist/vue.esm-bundler";
2-
import { NSelect, NFormItem, NDatePicker, NButton, NTooltip, NIcon, NSpin } from "naive-ui";
2+
import { NSelect, NFormItem, NDatePicker, NButton, NTooltip, NIcon, NSpin, NSpace } from "naive-ui";
33
import { useStore } from 'vuex';
44
import { computedVar } from "../utils";
55
import { biEraser } from "../icons.js";
@@ -11,7 +11,8 @@ export const subSelect = {
1111
NDatePicker,
1212
NButton,
1313
NIcon,
14-
NSpin
14+
NSpin,
15+
NSpace
1516
},
1617
props: {
1718
modal: {
@@ -20,6 +21,7 @@ export const subSelect = {
2021
},
2122
},
2223
setup (props) {
24+
const allCitiesValues = [];
2325
const store = useStore();
2426
const tab = computed(() => store.state.content.tab);
2527
const tabBy = computed(() => store.state.content.tabBy);
@@ -48,6 +50,7 @@ export const subSelect = {
4850
const selectRefsMap = reactive({});
4951
const resizeObserver = ref(null);
5052
const isLoadingCities = ref(false);
53+
const firstLoadCities = ref(true);
5154

5255
const activeSelectKey = ref(null);
5356
const formRef = ref(null);
@@ -92,17 +95,21 @@ export const subSelect = {
9295
setTimeout(() => {
9396
const allOptions = toRaw(citiesTemp.value);
9497
const selectLength = Array.isArray(cityTemp.value) ? cityTemp.value.length : null
98+
9599
if ((selectLength == allOptions.length) || uncheckAll) {
100+
city.value = [];
96101
cityTemp.value = [];
97102
handleShowUpdate(true, field);
98103
isLoadingCities.value = false;
99104
return;
100105
}
101106

102-
cityTemp.value = allOptions.map(option => option.value);
107+
city.value = allCitiesValues;
108+
cityTemp.value = allCitiesValues;
109+
103110
handleShowUpdate(true, field);
104111
isLoadingCities.value = false;
105-
}, 0)
112+
}, 0);
106113
}
107114

108115
const handleLocalsUpdateShow = (show, field) => {
@@ -176,31 +183,50 @@ export const subSelect = {
176183

177184
watch(
178185
() => store.state.content.form.local,
179-
async (loc) => {
180-
if (!loc.length) {
181-
city.value = [];
182-
citiesTemp.value = cities.value;
183-
cities.value.forEach(item => {
184-
item.disabled = false;
185-
item.disabledText = ""
186-
});
187-
} else {
188-
citiesTemp.value = cities.value.filter(city => loc.includes(city.uf));
189-
if (city.value?.length) {
190-
city.value = city.value.filter(itemA => citiesTemp.value.find(itemB => itemB.value === itemA));
191-
cityTemp.value = city.value;
192-
}
193-
disableStateCitiesSelector(cityTemp.value);
194-
}
186+
(loc) => {
195187
localTemp.value = loc;
196-
await showCitiesSelectUpdate();
188+
189+
isLoadingCities.value = true;
190+
191+
setTimeout(async () => {
192+
if (!loc.length) {
193+
citiesTemp.value = cities.value;
194+
cityTemp.value = [];
195+
city.value = [];
196+
} else {
197+
const rawCities = toRaw(cities.value);
198+
const locSet = new Set(loc);
199+
200+
citiesTemp.value = rawCities.filter(city => locSet.has(city.uf));
201+
202+
if (city.value?.length) {
203+
const citiesTempSet = new Set(citiesTemp.value.map(item => item.value));
204+
205+
const rawCityValue = toRaw(city.value);
206+
207+
city.value = rawCityValue.filter(itemA => citiesTempSet.has(itemA));
208+
cityTemp.value = city.value;
209+
}
210+
211+
disableStateCitiesSelector(cityTemp.value);
212+
}
213+
214+
await showCitiesSelectUpdate();
215+
isLoadingCities.value = false;
216+
}, 0);
197217
}
198218
);
199219

200220
watch(
201221
() => store.state.content.form.cities,
202222
(cities) => {
203-
citiesTemp.value = cities;
223+
if (firstLoadCities.value) {
224+
citiesTemp.value = cities;
225+
firstLoadCities.value = false;
226+
for (let i = 0; i < cities.length; i++) {
227+
allCitiesValues.push(cities[i].value);
228+
}
229+
}
204230
}
205231
)
206232

@@ -549,44 +575,45 @@ export const subSelect = {
549575
/>
550576
</n-form-item>
551577
<n-form-item label="Municípios" v-if="showCitiesSelect">
552-
<n-select
553-
:consistent-menu-width="false"
554-
:disabled="disableAll"
555-
:options="citiesTemp"
556-
:ref="el => (selectRefsMap['field8'] = el)"
557-
:style="styleWidth"
558-
@update:value="value => handleCitiesUpdateValue(value)"
559-
@update:show="show => handleShowUpdate(show, 'field8')"
560-
class="mct-select"
561-
clearable
562-
filterable
563-
max-tag-count="responsive"
564-
placeholder="Selecione Município"
565-
v-model:value="cityTemp"
566-
:multiple="true"
567-
:render-option="renderOption"
568-
:filter="customFilter"
569-
>
570-
<template #action v-if="tab === 'table'">
571-
<n-form-item label="Ação">
572-
<n-button :on-click="() => selectAllCities('field8')" size="small">
573-
{{ (cityTemp && cityTemp.length === citiesTemp.length ? 'Desmarcar' : 'Marcar') + ' todos' }}
574-
<n-spin v-if="isLoadingCities" size="tiny" :stroke-width="16" style="margin-left: 6px;" />
575-
</n-button>
576-
</n-form-item>
577-
</template>
578-
<template #action v-else-if="tab === 'chart' && (cityTemp && cityTemp.length)">
579-
<n-form-item label="Ação">
580-
<n-button
581-
:on-click="() => selectAllCities('field8', true)"
582-
size="small"
583-
>
584-
Desmarcar todos
585-
<n-spin v-if="isLoadingCities" size="tiny" :stroke-width="16" style="margin-left: 6px;" />
586-
</n-button>
587-
</n-form-item>
588-
</template>
589-
</n-select>
578+
<n-space vertical>
579+
<n-select
580+
:consistent-menu-width="true"
581+
:disabled="disableAll"
582+
:options="citiesTemp"
583+
:ref="el => (selectRefsMap['field8'] = el)"
584+
:style="styleWidth"
585+
@update:value="value => handleCitiesUpdateValue(value)"
586+
@update:show="show => handleShowUpdate(show, 'field8')"
587+
class="mct-select"
588+
clearable
589+
filterable
590+
max-tag-count="responsive"
591+
placeholder="Selecione Município"
592+
v-model:value="cityTemp"
593+
:multiple="true"
594+
:filter="customFilter"
595+
>
596+
<template #action v-if="tab === 'table'">
597+
<n-form-item label="Ação">
598+
<n-button :on-click="() => selectAllCities('field8')" size="small">
599+
{{ (cityTemp && cityTemp.length === citiesTemp.length ? 'Desmarcar' : 'Marcar') + ' todos' }}
600+
<n-spin v-show="isLoadingCities" size="tiny" :stroke-width="20" style="margin-left: 4px;" />
601+
</n-button>
602+
</n-form-item>
603+
</template>
604+
<template #action v-else-if="tab === 'chart' && (cityTemp && cityTemp.length)">
605+
<n-form-item label="Ação">
606+
<n-button
607+
:on-click="() => selectAllCities('field8', true)"
608+
size="small"
609+
>
610+
Desmarcar todos
611+
<n-spin v-if="isLoadingCities" size="tiny" :stroke-width="16" style="margin-left: 6px;" />
612+
</n-button>
613+
</n-form-item>
614+
</template>
615+
</n-select>
616+
</n-space>
590617
</n-form-item>
591618
</section>
592619
<n-form-item>

0 commit comments

Comments
 (0)