Skip to content

Commit dbf70f7

Browse files
committed
fix: location input fetching
1 parent 7ef4207 commit dbf70f7

File tree

8 files changed

+74
-61
lines changed

8 files changed

+74
-61
lines changed

packages/components-vue/src/components/Modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
const modalId = computed(() => {
178178
const seed = _.deburr(props.subtitle || props.title);
179179
180-
return `modal_${seed.replace(" ", "") || randomId}`;
180+
return `modal_${seed.replaceAll(" ", "") || randomId}`;
181181
});
182182
const saveButtonOptions = computed<iButtonConfig>(() => ({
183183
title: t("ok"),

packages/components-vue/src/components/base/Input.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
const inputId = computed(() => {
5858
const seed = _.deburr(props.id || props.name || props.placeholder || props.title);
5959
60-
return `input_${seed.replace(" ", "") || randomId}`;
60+
return `input_${seed.replaceAll(" ", "") || randomId}`;
6161
});
6262
6363
function handleInput(e: Event) {

packages/components-vue/src/components/base/Select.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<select
33
v-bind="{
44
...$attrs,
5-
..._.omit(props, 'modelValue'),
5+
..._.omit(props, ['modelValue', 'options']),
66
id: selectId,
77
name: name ?? selectId,
88
title,
@@ -66,7 +66,7 @@
6666
const selectId = computed(() => {
6767
const seed = _.deburr(props.id || props.name || props.placeholder || props.title);
6868
69-
return `select_${seed.replace(" ", "") || randomId}`;
69+
return `select_${seed.replaceAll(" ", "") || randomId}`;
7070
});
7171
7272
function handleInput(e: Event) {
@@ -83,8 +83,6 @@
8383
watch(
8484
selectOptions,
8585
(options) => {
86-
// reset model
87-
if (!options.length) emit("update:model-value", "");
8886
// set single option as value
8987
if (options.length === 1) emit("update:model-value", options[0].value);
9088
},

packages/components-vue/src/components/form/Input.vue

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
v-if="!defaultCountry || models[i].value.length === 1"
149149
v-model="models[i].value[0]"
150150
:options="countriesArr"
151+
name="country"
151152
:value="defaultCountry"
152153
icon="earth-americas"
153154
:theme="theme"
@@ -160,12 +161,8 @@
160161
>
161162
<SelectFilter
162163
v-model="models[i].value[1]"
163-
:options="
164-
statesArr ||
165-
(statesReq.loading || statesReq.content.length
166-
? statesReq.content.map(stateToOption)
167-
: [models[i].value[0] || defaultCountry])
168-
"
164+
:options="statesArr || statesReq.content.map(stateToOption)"
165+
name="state"
169166
icon="mountain-sun"
170167
:theme="theme"
171168
:disabled="!(models[i].value[0] || defaultCountry)"
@@ -174,11 +171,8 @@
174171
/>
175172
<SelectFilter
176173
v-model="models[i].value[2]"
177-
:options="
178-
citiesReq.loading || citiesReq.content.length
179-
? citiesReq.content.map(cityToOption)
180-
: [models[i].value[1]]
181-
"
174+
:options="citiesReq.content.map(cityToOption)"
175+
name="city"
182176
icon="city"
183177
:theme="theme"
184178
:disabled="!models[i].value[1]"
@@ -271,7 +265,7 @@
271265
</div>
272266
</template>
273267
<script setup lang="ts">
274-
import { computed } from "vue";
268+
import { computed, reactive } from "vue";
275269
import validator from "validator";
276270
import _ from "lodash";
277271
@@ -372,7 +366,7 @@
372366
const models = computed(() => {
373367
return props.modelValue.map((value, valueIndex) =>
374368
computed({
375-
get: () => value,
369+
get: () => (Array.isArray(value) ? reactive(value) : value),
376370
set: (newValue) => {
377371
emit("update:model-value", props.modelValue.toSpliced(valueIndex, 1, newValue));
378372
},

packages/components-vue/src/components/form/InputCountriesAPI.vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
<LoaderContentFetch
1212
v-slot="citiesReq"
1313
:theme="theme"
14-
:promise="
15-
!!(countryValue && model[0]) &&
16-
model[0] !== model[0] &&
17-
!!(statesReq.content.length || states) &&
18-
unHydrate(getStateCities)
19-
"
14+
:promise="!!model[1] && unHydrate(getStateCities)"
2015
:payload="[countryValue, model[1]]"
2116
:fallback="[]"
2217
unwrap

packages/components-vue/src/components/form/InputNValues.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<slot></slot>
77
</div>
88
<p v-else class="--txtColor-danger">
9-
{{ values.map((l) => t("form_requires_n_values", l)).join(" | ") }}
9+
{{ values.map((l) => t("form_requires_n_values", l)).join(" or ") }}
1010
</p>
1111
</template>
1212

packages/components-vue/src/components/loader/ContentFetch.vue

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,31 @@
108108
109109
// refetch on url or promise change
110110
watch(
111-
[() => props.url, () => props.promise],
112-
(newValues, oldValues) => {
113-
const sameUrl = newValues[0] === oldValues[0];
114-
const samePromise = newValues[1] === oldValues[1];
111+
() => props.url,
112+
(newUrl, oldUrl) => {
113+
// prevent muntiple requests
114+
if (newUrl === oldUrl) return;
115+
116+
// refresh
117+
if (!loading.value && !!newUrl) refresh();
118+
else if (props.fallback) content.value = props.fallback;
119+
},
120+
{ immediate: false }
121+
);
122+
watch(
123+
() => props.promise,
124+
(newPromise, oldPromise) => {
115125
/**
116126
* The same promise would trigger the watcher
117127
* We assume here that the same promise is provided
118128
*/
119-
const possibleSamePromise = newValues[1] && oldValues[1];
129+
const possibleSamePromise = !!newPromise && !!oldPromise;
120130
121131
// prevent muntiple requests
122-
if ((sameUrl && samePromise) || !!possibleSamePromise) return;
132+
if (newPromise === oldPromise || !!possibleSamePromise) return;
123133
124134
// refresh
125-
if (!loading.value && !!(newValues[1] || newValues[0])) refresh();
135+
if (!loading.value && !!newPromise) refresh();
126136
else if (props.fallback) content.value = props.fallback;
127137
},
128138
{ immediate: false }

packages/components-vue/src/components/select/Filter.vue

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
:theme="theme"
4848
:aria-label="t('select_restablish_field')"
4949
:title="t('select_restablish_field')"
50-
@click.prevent="model = ''"
50+
@click.prevent="resetModel"
5151
>
5252
<IconFa name="xmark" size="20" />
5353
</ActionLink>
@@ -56,7 +56,7 @@
5656

5757
<script setup lang="ts">
5858
import type { IconName } from "@fortawesome/fontawesome-common-types";
59-
import { computed, ref } from "vue";
59+
import { computed, ref, watch } from "vue";
6060
import _ from "lodash";
6161
6262
import type { iFormIconProps, iFormOption } from "@open-xamu-co/ui-common-types";
@@ -111,38 +111,24 @@
111111
const selectFilterName = computed(() => {
112112
const seed = _.deburr(props.id || props.name || props.placeholder || props.title);
113113
114-
return `select-filter_${seed.replace(" ", "") || randomId}`;
114+
return `select-filter_${seed.replaceAll(" ", "") || randomId}`;
115115
});
116-
const selectOptions = computed<iFormOption[]>(() => {
117-
return (props.options ?? []).map(toOption);
118-
});
119-
const textModel = ref<string | number>(props.modelValue || "");
116+
const selectOptions = computed<iFormOption[]>(() => (props.options ?? []).map(toOption));
117+
const textModel = ref<string | number>("");
120118
/**
121119
* Input model
122120
*/
123121
const model = computed({
124-
get: () => props.modelValue,
125-
set: (newModel = "") => {
126-
const deburr = (string: string) => _.deburr(string).toLowerCase();
122+
get: () => {
123+
return props.modelValue;
124+
},
125+
set: (newModel) => {
126+
emit("update:model-value", newModel);
127127
128128
// look for alias first
129-
const option = selectOptions.value.find(({ value, alias }) => {
130-
const match = alias ?? value;
131-
132-
if (typeof newModel === "string" && typeof match === "string") {
133-
if (deburr(match) === deburr(newModel)) return true;
134-
}
135-
136-
return match === newModel;
137-
});
138-
139-
if (option) {
140-
textModel.value = option.alias || option.value;
141-
emit("update:model-value", option.value);
142-
} else if (newModel === "") {
143-
textModel.value = newModel;
144-
emit("update:model-value", newModel);
145-
}
129+
const option = selectOptions.value.find(({ value }) => value === newModel);
130+
131+
if (option) textModel.value = option.alias || option.value;
146132
},
147133
});
148134
const isInvalid = computed<boolean>(() => {
@@ -151,15 +137,45 @@
151137
return (model.value && !option) || props.invalid;
152138
});
153139
140+
/**
141+
* Clears up input model
142+
*/
143+
function resetModel() {
144+
model.value = "";
145+
textModel.value = "";
146+
}
147+
154148
/**
155149
* Handle select input
156150
* @listenerOverride select filter requires specific event handling
157151
*/
158152
function handleInputChange(e: Event) {
159153
const { target } = e as Event & { target: HTMLSelectElement };
154+
const deburr = (v: string | number) => _.deburr(String(v)).toLowerCase();
155+
const newModel = deburr(target.value);
160156
161-
model.value = target.value;
157+
// look for alias first
158+
const option = selectOptions.value.find(({ value, alias }) => {
159+
const match = deburr(alias ?? value);
160+
161+
return match === newModel;
162+
});
163+
164+
if (option) model.value = option.value;
165+
else if (model.value) model.value = "";
162166
}
163167
168+
// lifecycle
169+
164170
if (isBrowser) supportsDatalist.value = !!HTMLDataListElement;
171+
172+
watch(
173+
selectOptions,
174+
(newOptions) => {
175+
const option = newOptions.find(({ value }) => value === props.modelValue);
176+
177+
if (option) textModel.value = option.alias || option.value;
178+
},
179+
{ immediate: true }
180+
);
165181
</script>

0 commit comments

Comments
 (0)