forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormSelect.vue
More file actions
166 lines (149 loc) · 4.1 KB
/
Copy pathFormSelect.vue
File metadata and controls
166 lines (149 loc) · 4.1 KB
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
<script setup lang="ts">
import { computed, type ComputedRef, onMounted, watch } from "vue";
import Multiselect from "vue-multiselect";
import { useMultiselect } from "@/composables/useMultiselect";
import { uid } from "@/utils/utils";
type SelectValue = string | number | null;
const { ariaExpanded, onOpen, onClose } = useMultiselect();
interface SelectOption {
label: string;
value: SelectValue;
}
const props = withDefaults(
defineProps<{
id?: string;
multiple?: boolean;
optional?: boolean;
options: Array<[string, SelectValue]>;
value?: Array<SelectValue> | string | number;
}>(),
{
id: `form-select-${uid()}`,
multiple: false,
optional: false,
value: null,
}
);
const emit = defineEmits<{
(e: "input", value: SelectValue | Array<SelectValue>): void;
}>();
/**
* Determine dom wrapper class
*/
const cls: ComputedRef<string> = computed(() => {
return props.multiple ? "form-select-multiple" : "form-select-single";
});
/**
* Configure deselect label
*/
const deselectLabel: ComputedRef<string> = computed(() => {
return props.multiple ? "Click to remove" : "";
});
/**
* Translates input options for consumption by the
* select component into an array of objects
*/
const formattedOptions: ComputedRef<Array<SelectOption>> = computed(() => {
const result: Array<SelectOption> = props.options.map((option) => ({
label: option[0],
value: option[1],
}));
if (props.optional && !props.multiple) {
result.unshift({
label: "Nothing selected",
value: null,
});
}
return result;
});
/**
* Tracks if the select field has options
*/
const hasOptions: ComputedRef<Boolean> = computed(() => {
return formattedOptions.value.length > 0;
});
/**
* Provides initial value if necessary
*/
const initialValue: ComputedRef<SelectValue> = computed(() => {
if (props.value === null && !props.optional && hasOptions.value) {
const v = formattedOptions.value[0];
if (v) {
return v.value;
}
}
return null;
});
/**
* Configure selected label
*/
const selectedLabel: ComputedRef<string> = computed(() => {
return props.multiple ? "Selected" : "";
});
/**
* Tracks selected values
*/
const selectedValues: ComputedRef<Array<SelectValue>> = computed(() => {
return Array.isArray(props.value) ? props.value : [props.value];
});
/**
* Tracks current value and emits changes
*/
const currentValue = computed({
get: () => formattedOptions.value.filter((option: SelectOption) => selectedValues.value.includes(option.value)),
set: (val: Array<SelectOption> | SelectOption): void => {
if (Array.isArray(val)) {
if (val.length > 0) {
const values: Array<SelectValue> = val.map((v: SelectOption) => v.value);
emit("input", values);
} else {
emit("input", null);
}
} else {
emit("input", val ? val.value : null);
}
},
});
/**
* Ensures that an initial value is selected for non-optional inputs
*/
function setInitialValue(): void {
if (initialValue.value) {
emit("input", initialValue.value);
}
}
/**
* Watches changes in select options and adjusts initial value if necessary
*/
watch(
() => props.options,
() => setInitialValue()
);
/**
* Sets initial value if necessary
*/
onMounted(() => {
setInitialValue();
});
</script>
<template>
<Multiselect
v-if="hasOptions"
:id="id"
v-model="currentValue"
:allow-empty="true"
:class="['form-select', cls]"
:close-on-select="!multiple"
:deselect-label="deselectLabel"
:options="formattedOptions"
:multiple="multiple"
:selected-label="selectedLabel"
:aria-expanded="ariaExpanded"
placeholder="Select value"
select-label="Click to select"
track-by="value"
label="label"
@open="onOpen"
@close="onClose" />
<b-alert v-else v-localize variant="warning" show> No options available. </b-alert>
</template>