Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

メンバー入力コンポーネント #33

Merged
merged 12 commits into from
Nov 5, 2022
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"pinia": "^2.0.22",
"ress": "^5.0.2",
"vue": "^3.2.39",
"vue-router": "^4.1.5"
"vue-router": "^4.1.5",
"vue-select": "^4.0.0-beta.5"
},
"devDependencies": {
"@iconify/json": "^2.1.106",
Expand Down
4 changes: 4 additions & 0 deletions public/icons/account.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 158 additions & 0 deletions src/components/UI/MemberInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<script lang="ts" setup>
import { storeToRefs } from 'pinia'
import vSelect from 'vue-select'
import 'vue-select/dist/vue-select.css'

import { computed, nextTick, onUnmounted, ref } from 'vue'
import { User } from '/@/lib/apis'
import { useUserStore } from '/@/store/user'
import UserIcon from './UserIcon.vue'

const store = useUserStore()
const { users } = storeToRefs(store)
store.fetchUsers()

interface Props {
modelValue: User[]
}

const props = defineProps<Props>()

const emit = defineEmits<{
(e: 'update:modelValue', value: User[]): void
}>()

const value = computed({
get: () => props.modelValue,
set: v => emit('update:modelValue', v)
})

const limit = ref(10)
const search = ref('')

const filtered = computed(
() => users.value?.filter(user => user.name.includes(search.value)) ?? []
)
const options = computed(() => filtered.value.slice(0, limit.value))
const hasNextPage = computed(() => filtered.value.length > options.value.length)

const onSearch = (v: string) => {
search.value = v
}

const infiniteScroll = async (entries: IntersectionObserverEntry[]) => {
const entry = entries[0]
if (entry?.isIntersecting) {
const ul = (entry.target as HTMLUListElement).offsetParent
const scrollTop = ul?.scrollTop ?? 0
limit.value += 10

await nextTick()
if (ul !== null) {
ul.scrollTop = scrollTop
}
}
}

const footerRef = ref<HTMLUListElement>()
const observer = new IntersectionObserver(infiniteScroll)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
onUnmounted(() => {
observer.disconnect()
})

const onOpen = async () => {
if (!hasNextPage.value) return

await nextTick()
if (footerRef.value !== undefined) {
observer.observe(footerRef.value)
}
}
const onClose = () => {
observer.disconnect()
}
</script>

<template>
<v-select
v-model="value"
:options="options"
placeholder="メンバー"
label="name"
:class="$style.select"
class="select"
multiple
:close-on-select="false"
deselect-from-dropdown
@open="onOpen"
@close="onClose"
@search="onSearch"
>
<template #selected-option-container="{ option }">
<p class="vs__selected">{{ option.name }},</p>
</template>
<template #option="{ name }">
<div :class="$style.item">
<user-icon :user-id="name" />
<p>{{ name }}</p>
</div>
</template>
<template #list-footer>
<li v-if="hasNextPage" ref="footerRef">Loading...</li>
</template>
<template #no-options>
<p>ユーザーが見つかりませんでした</p>
</template>
</v-select>
</template>

<style lang="scss" module>
.select {
--vs-dropdown-option-padding: 4px 8px;
--vs-search-input-placeholder-color: #{$color-secondary};
--vs-border-color: #{$color-secondary};
--vs-dropdown-option--active-bg: #{$color-background-dim};
--vs-dropdown-option--active-color: #{$color-text};
--vs-dropdown-option--deselect-bg: #{$color-background-dim};
--vs-dropdown-option--deselect-color: #{$color-text};
--vs-selected-border-style: none;
--vs-selected-bg: transparent;
--vs-selected-color: #{$color-text};
--vs-controls-color: #{$color-secondary};
&:focus-within {
--vs-border-color: #{$color-primary};
}
}

.item {
display: flex;
align-items: center;
gap: 0.5rem;
line-height: 2rem;
&[aria-selected='true'] {
background-color: $color-background-dim;
}
}

.select :global(.vs__selected-options) {
&::before {
content: '';
display: block;
width: 24px;
height: 24px;
background-color: $color-secondary;
mask: url('/icons/account.svg') no-repeat center center;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アイコンをiconifyから読みたかったけどいい感じにする方法がわからず/icons/account.svgを追加しました。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://icon-sets.iconify.design/mdi/account/
の画像のところにそれっぽいURLがありそうですがどうですか?:eyes:
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここだけ https://api.iconify.design にリクエストが飛ぶ形になってしまうので迷ってます。 他のiconifyのsvgは、プラグインで埋め込まれた状態になってます。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSSでハックする感じだと今の状態が一番よさそうです

margin-top: 4px;
margin-left: 4px;
}
&:focus-within::before {
background-color: $color-primary;
}
}
.select :global(.vs__dropdown-option--selected) {
background-color: $color-background-dim;
&:hover {
filter: brightness(0.95);
}
}
</style>
28 changes: 28 additions & 0 deletions src/components/UI/UserIcon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts" setup>
import { computed } from 'vue'
interface Props {
userId: string
size?: number
}
const props = withDefaults(defineProps<Props>(), { size: 24 })
const src = computed(
() =>
// TODO: userIdから画像を読み込む `https://q.trap.jp/api/v3/public/icon/${props.userId}`
`https://q.trap.jp/api/v3/public/icon/toshi00`
)

const styles = computed(() => ({
height: `${props.size}px`,
width: `${props.size}px`
}))
</script>

<template>
<img :class="$style.icon" :src="src" :style="styles" />
</template>

<style lang="scss" module>
.icon {
border-radius: 50%;
}
</style>
23 changes: 23 additions & 0 deletions src/store/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'

import apis, { User } from '/@/lib/apis'

export const useUserStore = defineStore('user', () => {
const users = ref<User[] | null>(null)

const fetchUsers = async () => {
if (users.value !== null) {
return users.value
}

const res = await apis.getUsers()
users.value = res.data
return res.data
}

return {
users,
fetchUsers
}
})
1 change: 1 addition & 0 deletions src/types/vue-select.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'vue-select'