2023-01-04 12:56:10 +03:00
|
|
|
<script setup lang="ts">
|
|
|
|
import ISO6391 from 'iso-639-1'
|
|
|
|
import Fuse from 'fuse.js'
|
|
|
|
|
|
|
|
let { modelValue } = $defineModel<{
|
2023-01-04 13:21:18 +03:00
|
|
|
modelValue: string
|
2023-01-04 12:56:10 +03:00
|
|
|
}>()
|
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
|
|
|
|
const languageKeyword = $ref('')
|
|
|
|
|
|
|
|
const languageList: {
|
2023-01-04 13:21:18 +03:00
|
|
|
code: string
|
2023-01-04 12:56:10 +03:00
|
|
|
nativeName: string
|
2023-01-04 13:21:18 +03:00
|
|
|
name: string
|
|
|
|
}[] = ISO6391.getAllCodes().map(code => ({
|
2023-01-04 12:56:10 +03:00
|
|
|
code,
|
|
|
|
nativeName: ISO6391.getNativeName(code),
|
|
|
|
name: ISO6391.getName(code),
|
2023-01-04 13:21:18 +03:00
|
|
|
}))
|
2023-01-04 12:56:10 +03:00
|
|
|
|
|
|
|
const fuse = new Fuse(languageList, {
|
|
|
|
keys: ['code', 'nativeName', 'name'],
|
|
|
|
shouldSort: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
const languages = $computed(() =>
|
|
|
|
languageKeyword.trim()
|
|
|
|
? fuse.search(languageKeyword).map(r => r.item)
|
|
|
|
: [...languageList].sort(({ code: a }, { code: b }) => {
|
2023-01-04 13:21:18 +03:00
|
|
|
return a === modelValue ? -1 : b === modelValue ? 1 : a.localeCompare(b)
|
2023-01-04 12:56:10 +03:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2023-01-04 13:21:18 +03:00
|
|
|
function chooseLanguage(language: string) {
|
2023-01-04 12:56:10 +03:00
|
|
|
modelValue = language
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<input
|
|
|
|
v-model="languageKeyword"
|
|
|
|
:placeholder="t('language.search')"
|
|
|
|
p2 mb2 border-rounded w-full bg-transparent
|
|
|
|
outline-none border="~ base"
|
|
|
|
>
|
|
|
|
<div max-h-40vh overflow-auto>
|
|
|
|
<CommonDropdownItem
|
|
|
|
v-for="{ code, nativeName, name } in languages"
|
|
|
|
:key="code"
|
2023-01-04 13:21:18 +03:00
|
|
|
:text="nativeName"
|
|
|
|
:description="name"
|
|
|
|
:checked="code === modelValue"
|
2023-01-04 12:56:10 +03:00
|
|
|
@click="chooseLanguage(code)"
|
2023-01-04 13:21:18 +03:00
|
|
|
/>
|
2023-01-04 12:56:10 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|