Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit e5cf7fb

Browse files
committed
Refactor Messages.vue with composition api
1 parent e4aed5c commit e5cf7fb

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/components/Messages.vue

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<template>
22
<div class="flex flex-column col col-9">
3-
<main class="chat flex flex-column flex-1 clear">
3+
<main ref="chatPane" class="chat flex flex-column flex-1 clear">
44
<SingleMessage
55
v-for="message in messages"
66
v-cloak
77
:key="message._id"
88
:message="message"
99
/>
1010
</main>
11-
<ComposeMessage :create-message="createMessage" />
11+
12+
<ComposeMessage />
1213
</div>
1314
</template>
1415

1516
<script>
17+
import { ref, watch, onMounted } from '@vue/composition-api'
1618
import ComposeMessage from './Composer.vue'
1719
import SingleMessage from './Message.vue'
1820
@@ -23,31 +25,32 @@ export default {
2325
SingleMessage
2426
},
2527
props: {
26-
// eslint-disable-next-line vue/require-default-prop
27-
messages: Array,
28-
// eslint-disable-next-line vue/require-default-prop
29-
findMessages: Function,
30-
// eslint-disable-next-line vue/require-default-prop
31-
createMessage: Function
32-
},
33-
data() {
34-
return {
35-
// TODO: Fix the placeholder
36-
placeholder: 'PLACEHOLDER'
37-
}
38-
},
39-
watch: {
40-
messages() {
41-
this.messages.length && this.scrollToBottom()
28+
messages: {
29+
type: Array,
30+
required: true
4231
}
4332
},
44-
methods: {
45-
scrollToBottom() {
46-
this.$nextTick(() => {
47-
const node = this.$el.getElementsByClassName('chat')[0]
48-
node.scrollTop = node.scrollHeight
33+
setup(props, context) {
34+
const placeholder = ref('PLACEHOLDER')
35+
36+
// Chat Pane
37+
const chatPane = ref(null)
38+
function scrollToBottom() {
39+
context.root.$nextTick(() => {
40+
chatPane.value.scrollTop = chatPane.value.scrollHeight
4941
})
5042
}
43+
onMounted(() => {
44+
watch(
45+
() => props.messages.length,
46+
() => scrollToBottom()
47+
)
48+
})
49+
50+
return {
51+
placeholder,
52+
chatPane
53+
}
5154
}
5255
}
5356
</script>

0 commit comments

Comments
 (0)