mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 22:50:51 +08:00
feat: add mobile dropdown page and search
This commit is contained in:
@@ -58,7 +58,10 @@
|
||||
|
||||
<script>
|
||||
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { hatch } from 'ldrs'
|
||||
import { isMobile } from '../utils/screen'
|
||||
import { registerDropdownStore, removeDropdownStore } from '../utils/mobileDropdown'
|
||||
hatch.register()
|
||||
|
||||
export default {
|
||||
@@ -76,7 +79,10 @@ export default {
|
||||
},
|
||||
emits: ['update:modelValue'],
|
||||
setup(props, { emit }) {
|
||||
const router = useRouter()
|
||||
const storeId = Math.random().toString(36).substring(2)
|
||||
const open = ref(false)
|
||||
const model = ref(props.modelValue)
|
||||
const search = ref('')
|
||||
const setSearch = (val) => {
|
||||
search.value = val
|
||||
@@ -87,7 +93,18 @@ export default {
|
||||
const wrapper = ref(null)
|
||||
|
||||
const toggle = () => {
|
||||
open.value = !open.value
|
||||
if (isMobile.value) {
|
||||
registerDropdownStore(storeId, {
|
||||
value: model,
|
||||
multiple: props.multiple,
|
||||
fetchOptions: props.fetchOptions,
|
||||
remote: props.remote,
|
||||
showSearch: props.showSearch
|
||||
})
|
||||
router.push(`/mobile-dropdown/${storeId}`)
|
||||
} else {
|
||||
open.value = !open.value
|
||||
}
|
||||
}
|
||||
|
||||
const close = () => {
|
||||
@@ -96,16 +113,16 @@ export default {
|
||||
|
||||
const select = id => {
|
||||
if (props.multiple) {
|
||||
const arr = Array.isArray(props.modelValue) ? [...props.modelValue] : []
|
||||
const arr = Array.isArray(model.value) ? [...model.value] : []
|
||||
const idx = arr.indexOf(id)
|
||||
if (idx > -1) {
|
||||
arr.splice(idx, 1)
|
||||
} else {
|
||||
arr.push(id)
|
||||
}
|
||||
emit('update:modelValue', arr)
|
||||
model.value = arr
|
||||
} else {
|
||||
emit('update:modelValue', id)
|
||||
model.value = id
|
||||
close()
|
||||
}
|
||||
search.value = ''
|
||||
@@ -145,6 +162,18 @@ export default {
|
||||
}
|
||||
}
|
||||
)
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
val => {
|
||||
model.value = val
|
||||
}
|
||||
)
|
||||
watch(
|
||||
() => model.value,
|
||||
val => {
|
||||
emit('update:modelValue', val)
|
||||
}
|
||||
)
|
||||
|
||||
watch(open, async val => {
|
||||
if (val) {
|
||||
@@ -163,21 +192,26 @@ export default {
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('click', clickOutside)
|
||||
if (!isMobile.value) {
|
||||
document.addEventListener('click', clickOutside)
|
||||
}
|
||||
if (!props.remote) {
|
||||
loadOptions()
|
||||
}
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('click', clickOutside)
|
||||
if (!isMobile.value) {
|
||||
document.removeEventListener('click', clickOutside)
|
||||
}
|
||||
removeDropdownStore(storeId)
|
||||
})
|
||||
|
||||
const selectedLabels = computed(() => {
|
||||
if (props.multiple) {
|
||||
return options.value.filter(o => (props.modelValue || []).includes(o.id))
|
||||
return options.value.filter(o => (model.value || []).includes(o.id))
|
||||
}
|
||||
const match = options.value.find(o => o.id === props.modelValue)
|
||||
const match = options.value.find(o => o.id === model.value)
|
||||
return match ? [match] : []
|
||||
})
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
</div>
|
||||
|
||||
<div v-if="isLogin" class="header-content-right">
|
||||
<button v-if="isMobile" class="mobile-search-btn" @click="openMobileSearch">
|
||||
<i class="fas fa-search"></i>
|
||||
</button>
|
||||
<DropdownMenu ref="userMenu" :items="headerMenuItems">
|
||||
<template #trigger>
|
||||
<div class="avatar-container">
|
||||
@@ -24,6 +27,9 @@
|
||||
</div>
|
||||
|
||||
<div v-else class="header-content-right">
|
||||
<button v-if="isMobile" class="mobile-search-btn" @click="openMobileSearch">
|
||||
<i class="fas fa-search"></i>
|
||||
</button>
|
||||
<div class="header-content-item-main" @click="goToLogin">登录</div>
|
||||
<div class="header-content-item-secondary" @click="goToSignup">注册</div>
|
||||
</div>
|
||||
@@ -33,8 +39,11 @@
|
||||
|
||||
<script>
|
||||
import { authState, clearToken, loadCurrentUser } from '../utils/auth'
|
||||
import { watch } from 'vue'
|
||||
import { watch, ref } from 'vue'
|
||||
import DropdownMenu from './DropdownMenu.vue'
|
||||
import { isMobile } from '../utils/screen'
|
||||
import { registerDropdownStore } from '../utils/mobileDropdown'
|
||||
import { API_BASE_URL } from '../main'
|
||||
|
||||
export default {
|
||||
name: 'HeaderComponent',
|
||||
@@ -54,6 +63,9 @@ export default {
|
||||
isLogin() {
|
||||
return authState.loggedIn
|
||||
},
|
||||
isMobile() {
|
||||
return isMobile.value
|
||||
},
|
||||
headerMenuItems() {
|
||||
return [
|
||||
{ text: '设置', onClick: this.goToSettings },
|
||||
@@ -120,6 +132,23 @@ export default {
|
||||
goToLogout() {
|
||||
clearToken()
|
||||
this.$router.push('/login')
|
||||
},
|
||||
async openMobileSearch() {
|
||||
const id = Math.random().toString(36).substring(2)
|
||||
registerDropdownStore(id, {
|
||||
value: ref(null),
|
||||
multiple: false,
|
||||
remote: true,
|
||||
showSearch: true,
|
||||
async fetchOptions(kw) {
|
||||
if (!kw) return []
|
||||
const res = await fetch(`${API_BASE_URL}/api/search/global?keyword=${encodeURIComponent(kw)}`)
|
||||
if (!res.ok) return []
|
||||
const data = await res.json()
|
||||
return data.map(r => ({ id: r.id, name: r.text }))
|
||||
}
|
||||
})
|
||||
this.$router.push(`/mobile-dropdown/${id}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,6 +229,20 @@ export default {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.mobile-search-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: inherit;
|
||||
font-size: 20px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
@media (min-width: 769px) {
|
||||
.mobile-search-btn {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.avatar-container {
|
||||
position: relative;
|
||||
display: flex;
|
||||
|
||||
Reference in New Issue
Block a user