-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnAccordion.vue
72 lines (71 loc) · 1.62 KB
/
AnAccordion.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
<div class="an-accordion">
<slot />
</div>
</template>
<script>
export default {
name: 'AnAccordion',
props: {
value: {
type: Number,
default: undefined
},
/** if set the active item is written to the route using the specified parameter name */
queryParam: {
type: String,
default: undefined
}
},
data() {
return {
internalValue: this.value
};
},
computed: {
items() {
return this.$children.filter(
child => child.$options.name === 'AnAccordionItem'
);
},
currentQuery() {
return this.queryParam ? this.$route.query[this.queryParam] : undefined;
}
},
watch: {
value(newValue) {
this.internalValue = newValue;
}
},
created() {
if (this.queryParam) {
// already in route
if (Object.hasOwnProperty.call(this.$route.query, this.queryParam)) {
this.changeActiveFromRoute();
}
// watch for changes in route
this.$watch('currentQuery', () => {
this.changeActiveFromRoute();
});
}
},
methods: {
changeActive(index) {
if (this.internalValue === index) return;
this.internalValue = index;
this.$emit('input', index);
if (this.queryParam) {
this.$router.push({
query: { ...this.$route.query, [this.queryParam]: index }
});
}
},
changeActiveFromRoute() {
const index = Number(this.$route.query[this.queryParam]);
if (!Number.isInteger(index) || this.internalValue === index) return;
this.internalValue = index;
this.$emit('input', index);
}
}
};
</script>