refac: defindComponent => vue3 setup
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<v-multiselect
|
<Multiselect
|
||||||
v-model="selectedCity"
|
v-model="selectedCity"
|
||||||
:allow-empty="false"
|
:allow-empty="false"
|
||||||
:options="citiesForFind"
|
:options="citiesForFind"
|
||||||
@@ -16,30 +16,31 @@
|
|||||||
:maxHeight="400"
|
:maxHeight="400"
|
||||||
:searchable="searchable"
|
:searchable="searchable"
|
||||||
>
|
>
|
||||||
<template #option="props"
|
<template #option="prop"
|
||||||
><span
|
><span
|
||||||
:style="{
|
:style="{
|
||||||
color: props.option.notAvailable ? 'red' : undefined,
|
color: prop.option.notAvailable ? 'red' : undefined,
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
{{ props.option.title }}
|
{{ prop.option.title }}
|
||||||
<span v-if="props.option.info">({{ props.option.info }})</span>
|
<span v-if="prop.option.info">({{ prop.option.info }})</span>
|
||||||
{{ props.option.notAvailable ? "(불가)" : undefined }}</span
|
{{ prop.option.notAvailable ? "(불가)" : undefined }}</span
|
||||||
>
|
>
|
||||||
</template>
|
</template>
|
||||||
<template #singleLabel="props">
|
<template #singleLabel="prop">
|
||||||
<span
|
<span
|
||||||
:style="{
|
:style="{
|
||||||
color: props.option.notAvailable ? 'red' : undefined,
|
color: prop.option.notAvailable ? 'red' : undefined,
|
||||||
}"
|
}"
|
||||||
>{{ props.option.simpleName }} {{ props.option.notAvailable ? "(불가)" : undefined }}</span
|
>{{ prop.option.simpleName }} {{ prop.option.notAvailable ? "(불가)" : undefined }}</span
|
||||||
>
|
>
|
||||||
</template>
|
</template>
|
||||||
</v-multiselect>
|
</Multiselect>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { Multiselect } from "vue-multiselect";
|
||||||
import { convertSearch초성 } from "@/util/convertSearch초성";
|
import { convertSearch초성 } from "@/util/convertSearch초성";
|
||||||
import { defineComponent, type PropType } from "vue";
|
import { onMounted, ref, watch, type PropType } from "vue";
|
||||||
|
|
||||||
type SelectedCity = {
|
type SelectedCity = {
|
||||||
value: number;
|
value: number;
|
||||||
@@ -50,55 +51,63 @@ type SelectedCity = {
|
|||||||
notAvailable?: boolean;
|
notAvailable?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default defineComponent({
|
const props = defineProps({
|
||||||
props: {
|
modelValue: {
|
||||||
modelValue: {
|
type: Number,
|
||||||
type: Number,
|
required: true,
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
cities: {
|
|
||||||
type: Map as PropType<Map<number, { name: string; info?: string }>>,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
searchable: {
|
|
||||||
type: Boolean,
|
|
||||||
required: false,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
emits: ["update:modelValue"],
|
cities: {
|
||||||
data() {
|
type: Map as PropType<Map<number, { name: string; info?: string }>>,
|
||||||
const citiesForFind = [];
|
required: true,
|
||||||
const targets = new Map<number, SelectedCity>();
|
|
||||||
let selectedCity;
|
|
||||||
for (const [value, { name, info }] of this.cities.entries()) {
|
|
||||||
const obj: SelectedCity = {
|
|
||||||
value,
|
|
||||||
title: name,
|
|
||||||
info: info,
|
|
||||||
simpleName: name,
|
|
||||||
searchText: convertSearch초성(name).join("|"),
|
|
||||||
};
|
|
||||||
if (value == this.modelValue) {
|
|
||||||
selectedCity = obj;
|
|
||||||
}
|
|
||||||
citiesForFind.push(obj);
|
|
||||||
targets.set(value, obj);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
selectedCity,
|
|
||||||
citiesForFind,
|
|
||||||
targets,
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
watch: {
|
searchable: {
|
||||||
modelValue(val: number) {
|
type: Boolean,
|
||||||
const target = this.targets.get(val);
|
required: false,
|
||||||
this.selectedCity = target;
|
default: true,
|
||||||
},
|
|
||||||
selectedCity(val: SelectedCity) {
|
|
||||||
this.$emit("update:modelValue", val.value);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(event: "update:modelValue", value: number): void;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const citiesForFind = ref<SelectedCity[]>([]);
|
||||||
|
const targets = new Map<number, SelectedCity>();
|
||||||
|
const selectedCity = ref<SelectedCity>();
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
citiesForFind.value = [];
|
||||||
|
targets.clear();
|
||||||
|
|
||||||
|
for (const [value, { name, info }] of props.cities.entries()) {
|
||||||
|
const obj: SelectedCity = {
|
||||||
|
value,
|
||||||
|
title: name,
|
||||||
|
info: info,
|
||||||
|
simpleName: name,
|
||||||
|
searchText: convertSearch초성(name).join("|"),
|
||||||
|
};
|
||||||
|
if (value == props.modelValue) {
|
||||||
|
selectedCity.value = obj;
|
||||||
|
}
|
||||||
|
citiesForFind.value.push(obj);
|
||||||
|
targets.set(value, obj);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.modelValue,
|
||||||
|
(value) => {
|
||||||
|
if (targets.has(value)) {
|
||||||
|
selectedCity.value = targets.get(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(selectedCity, (value) => {
|
||||||
|
if (!value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit("update:modelValue", value.value);
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<v-multiselect
|
<Multiselect
|
||||||
v-model="selectedColor"
|
v-model="selectedColor"
|
||||||
:allow-empty="false"
|
:allow-empty="false"
|
||||||
:options="forFind"
|
:options="forFind"
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
:maxHeight="400"
|
:maxHeight="400"
|
||||||
:searchable="false"
|
:searchable="false"
|
||||||
>
|
>
|
||||||
<template #option="props">
|
<template #option="prop">
|
||||||
<div
|
<div
|
||||||
:class="`sam-color-${props.option.title.slice(1)}`"
|
:class="`sam-color-${prop.option.title.slice(1)}`"
|
||||||
:style="{
|
:style="{
|
||||||
margin: '-0.375rem -0.75rem',
|
margin: '-0.375rem -0.75rem',
|
||||||
}"
|
}"
|
||||||
@@ -29,13 +29,13 @@
|
|||||||
padding: '0.545rem 0.75rem',
|
padding: '0.545rem 0.75rem',
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
{{ props.option.title }}
|
{{ prop.option.title }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<template #singleLabel="props">
|
<template #singleLabel="prop">
|
||||||
<div
|
<div
|
||||||
:class="`sam-color-${props.option.title.slice(1)}`"
|
:class="`sam-color-${prop.option.title.slice(1)}`"
|
||||||
:style="{
|
:style="{
|
||||||
margin: '-0.25rem -0.75rem',
|
margin: '-0.25rem -0.75rem',
|
||||||
}"
|
}"
|
||||||
@@ -47,61 +47,67 @@
|
|||||||
borderRadius: '0.25rem',
|
borderRadius: '0.25rem',
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
{{ props.option.title }}
|
{{ prop.option.title }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</v-multiselect>
|
</Multiselect>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script setup lang="ts">
|
||||||
import { unwrap } from "@/util/unwrap";
|
import { Multiselect } from "vue-multiselect";
|
||||||
import { defineComponent, type PropType } from "vue";
|
import { onMounted, ref, watch, type PropType } from "vue";
|
||||||
|
|
||||||
type SelectedColor = {
|
type SelectedColor = {
|
||||||
value: number;
|
value: number;
|
||||||
title: string;
|
title: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default defineComponent({
|
const props = defineProps({
|
||||||
props: {
|
modelValue: {
|
||||||
modelValue: {
|
type: Number,
|
||||||
type: Number,
|
required: true,
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
colors: {
|
|
||||||
type: Array as PropType<string[]>,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
emits: ["update:modelValue"],
|
colors: {
|
||||||
data() {
|
type: Array as PropType<string[]>,
|
||||||
const forFind = [];
|
required: true,
|
||||||
const targets = new Map<number, SelectedColor>();
|
|
||||||
for (const [value, title] of this.colors.entries()) {
|
|
||||||
const obj: SelectedColor = {
|
|
||||||
value,
|
|
||||||
title,
|
|
||||||
};
|
|
||||||
forFind.push(obj);
|
|
||||||
targets.set(value, obj);
|
|
||||||
}
|
|
||||||
let selectedColor = forFind[0];
|
|
||||||
|
|
||||||
return {
|
|
||||||
selectedColor,
|
|
||||||
searchMode: false,
|
|
||||||
forFind,
|
|
||||||
targets,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
watch: {
|
|
||||||
modelValue(val: number) {
|
|
||||||
const target = unwrap(this.targets.get(val));
|
|
||||||
this.selectedColor = target;
|
|
||||||
},
|
|
||||||
selectedColor(val: SelectedColor) {
|
|
||||||
this.$emit("update:modelValue", val.value);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(event: "update:modelValue", value: number): void;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const forFind = ref<SelectedColor[]>([]);
|
||||||
|
const targets = ref(new Map<number, SelectedColor>());
|
||||||
|
const selectedColor = ref<SelectedColor>();
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.modelValue,
|
||||||
|
(value) => {
|
||||||
|
selectedColor.value = targets.value.get(value);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
watch(selectedColor, (value) => {
|
||||||
|
if (!value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit("update:modelValue", value.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
forFind.value = [];
|
||||||
|
targets.value = new Map();
|
||||||
|
for (const [value, title] of props.colors.entries()) {
|
||||||
|
const obj: SelectedColor = {
|
||||||
|
value,
|
||||||
|
title,
|
||||||
|
};
|
||||||
|
forFind.value.push(obj);
|
||||||
|
targets.value.set(value, obj);
|
||||||
|
}
|
||||||
|
selectedColor.value = forFind.value[0];
|
||||||
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user