Skip to content

Commit 0baf334

Browse files
committed
VfSmartSelect: fix invalid value loading; add isNotNullOrUndefined
1 parent 5950a77 commit 0baf334

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

demo/components/demo-vf-smart-select.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@
4242

4343
Selected value: {{ selectedDelayedOption2 ?? '-' }}
4444
</div>
45+
46+
<div>
47+
<VfSmartSelect
48+
v-model="selectedDelayedOption3"
49+
:options="delayedOptions"
50+
label-field="label"
51+
value-field="value"
52+
group-field="group"
53+
null-title="No selection"
54+
/>
55+
56+
Selected value: {{ selectedDelayedOption3 ?? '-' }}
57+
</div>
4558
</div>
4659
</template>
4760

@@ -75,7 +88,8 @@ const delayedOptions = ref<IOption[]>();
7588
const selectedInstantOption1 = ref<IOption | null>(null);
7689
const selectedInstantOption2 = ref<IOption | null>(null);
7790
const selectedDelayedOption1 = ref<IOption | null>(options[1]);
78-
const selectedDelayedOption2 = ref<string | null>(options[1].value);
91+
const selectedDelayedOption2 = ref<string | null>('2');
92+
const selectedDelayedOption3 = ref<string | null>('19'); // intentionally invalid value
7993
8094
function setDelayedOptions() {
8195
delayedOptions.value = cloneDeep(options);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@signal24/vue-foundation",
33
"type": "module",
4-
"version": "4.25.4",
4+
"version": "4.25.5",
55
"description": "Common components, directives, and helpers for Vue 3 apps",
66
"module": "./dist/vue-foundation.es.js",
77
"exports": {

src/components/vf-smart-select.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import { debounce, groupBy, isEqual, uniq } from 'lodash';
5454
import { computed, onMounted, ref, watch } from 'vue';
5555
56+
import { isNotNullOrUndefined } from '@/helpers';
57+
5658
import { escapeHtml } from '../helpers/string';
5759
import type { VfSmartSelectOptionDescriptor } from './vf-smart-select.types';
5860
@@ -321,7 +323,7 @@ onMounted(async () => {
321323
if (selectedOption.value !== props.modelValue) {
322324
emit(
323325
'update:modelValue',
324-
selectedOption.value !== null && effectiveValueExtractor.value !== null
326+
isNotNullOrUndefined(selectedOption.value) && effectiveValueExtractor.value !== null
325327
? effectiveValueExtractor.value(selectedOption.value)
326328
: selectedOption.value
327329
);
@@ -559,7 +561,7 @@ function handleValueChanged() {
559561
selectedOption.value = effectiveValueExtractor.value
560562
? allOptions.value.find(o => props.modelValue === effectiveValueExtractor.value!(o))
561563
: props.modelValue;
562-
selectedOptionTitle.value = selectedOption.value !== null ? effectiveSelectionFormatter.value(selectedOption.value) : null;
564+
selectedOptionTitle.value = isNotNullOrUndefined(selectedOption.value) ? effectiveSelectionFormatter.value(selectedOption.value) : null;
563565
searchText.value = selectedOptionTitle.value ?? '';
564566
} else {
565567
selectedOption.value = null;

src/helpers/object.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export function nullifyEmptyInputs<T extends Record<string, unknown>, K extends
1818
}
1919
return result;
2020
}
21+
22+
export function isNotNullOrUndefined<T>(value: T | null | undefined): value is T {
23+
return value !== null && value !== undefined;
24+
}

0 commit comments

Comments
 (0)