Skip to content

Commit f37cab0

Browse files
committed
composition api setup + items sort
1 parent 48eb022 commit f37cab0

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed

src/App.vue

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,35 @@
1414
</template>
1515

1616
<script>
17+
import { ref } from 'vue'
18+
1719
import Form from '@/components/Form'
1820
import List from '@/components/List'
21+
1922
export default {
2023
components: { Form, List },
21-
data() {
24+
setup() {
25+
const items = ref([
26+
{
27+
id: 1,
28+
body: 'hello vue 3',
29+
likes: 12,
30+
avatar: `https://avatars.dicebear.com/api/male/1.svg`,
31+
date: new Date(Date.now()).toLocaleString()
32+
},
33+
{
34+
id: 2,
35+
body: 'hello world',
36+
likes: 6,
37+
avatar: `https://avatars.dicebear.com/api/male/2.svg`,
38+
date: new Date(Date.now()).toLocaleString()
39+
}
40+
])
41+
const handleSubmit = item => items.value.push(item)
42+
2243
return {
23-
items: [
24-
{
25-
id: 1,
26-
body: 'hello vue 3',
27-
likes: 12,
28-
avatar: `https://avatars.dicebear.com/api/male/1.svg`,
29-
date: new Date(Date.now()).toLocaleString()
30-
},
31-
{
32-
id: 2,
33-
body: 'hello world',
34-
likes: 6,
35-
avatar: `https://avatars.dicebear.com/api/male/2.svg`,
36-
date: new Date(Date.now()).toLocaleString()
37-
}
38-
]
39-
}
40-
},
41-
methods: {
42-
handleSubmit(item) {
43-
this.items.push(item)
44+
items,
45+
handleSubmit
4446
}
4547
}
4648
}

src/components/Form.vue

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@
66
</template>
77

88
<script>
9+
import { ref } from 'vue'
10+
911
export default {
10-
data() {
11-
return {
12-
body: ''
13-
}
14-
},
15-
methods: {
16-
onSubmit() {
17-
this.$emit('onSubmit', {
12+
emits: ['onSubmit'],
13+
14+
setup(_, { emit }) {
15+
const body = ref('')
16+
17+
const onSubmit = () => {
18+
emit('onSubmit', {
1819
id: Math.round(Math.random() * 30),
1920
avatar: `https://avatars.dicebear.com/api/male/${Date.now()}.svg`,
20-
body: this.body,
21+
body: body.value,
2122
likes: 0,
2223
date: new Date(Date.now()).toLocaleString()
2324
})
24-
25-
// reset
26-
this.body = ''
25+
body.value = ''
2726
}
27+
28+
return { body, onSubmit }
2829
}
2930
}
3031
</script>

src/components/List.vue

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<template>
2+
<select v-model="sortBy">
3+
<option value="date">Sort by date</option>
4+
<option value="likes">Sort by like</option>
5+
</select>
26
<ul class="tweets__wrapper">
3-
<Item v-for="item in items" :key="item.id" :item="item" />
7+
<Item v-for="item in sorteredItems" :key="item.id" :item="item" />
48
</ul>
59
</template>
610

711
<script>
12+
import { ref, computed } from 'vue'
813
import Item from '@/components/Item.vue'
914
1015
export default {
@@ -14,6 +19,18 @@ export default {
1419
type: Array,
1520
reqired: true
1621
}
22+
},
23+
setup({ items }) {
24+
const sortBy = ref('date')
25+
26+
const sorteredItems = computed(() => {
27+
return items.sort((a, b) => {
28+
if (a[sortBy.value] > b[sortBy.value]) return -1
29+
if (a[sortBy.value] < b[sortBy.value]) return 1
30+
})
31+
})
32+
33+
return { sortBy, sorteredItems }
1734
}
1835
}
1936
</script>

0 commit comments

Comments
 (0)