Skip to content

Commit b06f0d1

Browse files
committed
refactor: use script setup for Collapse
1 parent 4d8f6db commit b06f0d1

File tree

10 files changed

+130
-174
lines changed

10 files changed

+130
-174
lines changed

docs/.vitepress/components/alert/auto-dismissing.vue

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@
1414
>
1515
</section>
1616
</template>
17-
<script>
18-
export default {
19-
data() {
20-
return {
21-
alerts: [],
22-
duration: 2000,
23-
};
24-
},
25-
methods: {
26-
addAutoDismissAlert() {
27-
this.alerts.push({ key: new Date().getTime() });
28-
},
29-
},
30-
};
17+
<script setup>
18+
import { reactive } from 'vue';
19+
20+
const alerts = reactive([]);
21+
const duration = 2000;
22+
23+
function addAutoDismissAlert() {
24+
alerts.push({ key: new Date().getTime() });
25+
}
3126
</script>

docs/.vitepress/components/alert/dismissible.vue

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
1818
>
1919
</section>
2020
</template>
21-
<script>
22-
export default {
23-
data() {
24-
return {
25-
show: true,
26-
alerts: [],
27-
};
28-
},
29-
methods: {
30-
addDismissibleAlert() {
31-
this.alerts.push({ key: new Date().getTime() });
32-
},
33-
},
34-
};
21+
<script setup>
22+
import { reactive, ref } from 'vue';
23+
24+
const show = ref(true);
25+
const alerts = reactive([]);
26+
27+
function addDismissibleAlert() {
28+
alerts.push({ key: new Date().getTime() });
29+
}
3530
</script>

docs/.vitepress/components/alert/with-collapse.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
</collapse>
1010
</section>
1111
</template>
12-
<script>
13-
export default {
14-
data() {
15-
return {
16-
show: true,
17-
};
18-
},
19-
};
12+
<script setup>
13+
import { ref } from 'vue';
14+
15+
const show = ref(true);
2016
</script>

docs/.vitepress/components/breadcrumbs/example.vue

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
<breadcrumbs :items="items" />
44
</div>
55
</template>
6-
<script>
7-
export default {
8-
data() {
9-
return {
10-
items: [
11-
{ text: 'Home', href: '#' },
12-
{ text: 'Library', href: '#' },
13-
{ text: 'Data', href: '#' },
14-
],
15-
};
16-
},
17-
};
6+
<script setup>
7+
import { ref } from 'vue';
8+
9+
const items = ref([
10+
{ text: 'Home', href: '#' },
11+
{ text: 'Library', href: '#' },
12+
{ text: 'Data', href: '#' },
13+
]);
1814
</script>

docs/.vitepress/components/breadcrumbs/router-link.vue

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
<breadcrumbs :items="items" />
44
</div>
55
</template>
6-
<script>
7-
export default {
8-
data() {
9-
return {
10-
items: [
11-
{ text: 'Home', to: '/', exact: true },
12-
{ text: 'Breadcrumbs', to: '/breadcrumbs' },
13-
],
14-
};
15-
},
16-
};
6+
<script setup>
7+
import { ref } from 'vue';
8+
9+
const items = ref([
10+
{ text: 'Home', to: '/', exact: true },
11+
{ text: 'Breadcrumbs', to: '/breadcrumbs' },
12+
]);
1713
</script>

docs/.vitepress/components/btn/checkbox.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@
1818
<alert>Selected: {{ model }}</alert>
1919
</section>
2020
</template>
21-
<script>
22-
export default {
23-
data() {
24-
return {
25-
model: ['1'],
26-
};
27-
},
28-
};
21+
<script setup>
22+
import { ref } from 'vue';
23+
24+
const model = ref(['1']);
2925
</script>

docs/.vitepress/components/btn/radio.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212
<alert>Selected: {{ model }}</alert>
1313
</section>
1414
</template>
15-
<script>
16-
export default {
17-
data() {
18-
return {
19-
model: '1',
20-
};
21-
},
22-
};
15+
<script setup>
16+
import { ref } from 'vue';
17+
18+
const model = ref('1');
2319
</script>

docs/.vitepress/components/collapse/accordion.vue

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,16 @@
3838
</div>
3939
</div>
4040
</template>
41-
<script>
42-
export default {
43-
data() {
44-
return {
45-
showAccordion: [true, false, false],
46-
};
47-
},
48-
methods: {
49-
toggleAccordion(index) {
50-
if (this.showAccordion[index]) {
51-
this.showAccordion[index] = false;
52-
} else {
53-
this.showAccordion = this.showAccordion.map((v, i) => i === index);
54-
}
55-
},
56-
},
57-
};
41+
<script setup>
42+
import { ref } from 'vue';
43+
44+
const showAccordion = ref([true, false, false]);
45+
46+
function toggleAccordion(index) {
47+
if (showAccordion.value[index]) {
48+
showAccordion.value[index] = false;
49+
} else {
50+
showAccordion.value = showAccordion.value.map((v, i) => i === index);
51+
}
52+
}
5853
</script>

docs/.vitepress/components/collapse/example.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
</collapse>
1010
</div>
1111
</template>
12-
<script>
13-
export default {
14-
data() {
15-
return {
16-
show: false,
17-
};
18-
},
19-
};
12+
<script setup>
13+
import { ref } from 'vue';
14+
15+
const show = ref(false);
2016
</script>
Lines changed: 69 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,80 @@
1-
<script lang="jsx">
1+
<script lang="jsx" setup>
22
import { addClass, removeClass } from '../../utils/dom.utils';
3+
import { onMounted, ref, watchEffect } from 'vue';
4+
5+
const props = defineProps({
6+
tag: { type: String, default: 'div' },
7+
modelValue: { type: Boolean, default: false },
8+
transition: { type: Number, default: 350 },
9+
});
10+
const emit = defineEmits(['show', 'shown', 'hide', 'hidden']);
311
412
const COLLAPSE = 'collapse';
513
const IN = 'in';
614
const COLLAPSING = 'collapsing';
15+
let timeoutId = 0;
16+
const element = ref(null);
717
8-
export default {
9-
props: {
10-
tag: {
11-
type: String,
12-
default: 'div',
13-
},
14-
modelValue: {
15-
type: Boolean,
16-
default: false,
17-
},
18-
transition: {
19-
type: Number,
20-
default: 350,
21-
},
22-
},
23-
emits: ['show', 'shown', 'hide', 'hidden'],
24-
data() {
25-
return {
26-
timeoutId: 0,
27-
};
28-
},
29-
watch: {
30-
modelValue(show) {
31-
this.toggle(show);
32-
},
33-
},
34-
mounted() {
35-
const el = this.$el;
36-
addClass(el, COLLAPSE);
37-
if (this.modelValue) {
18+
function toggle(show) {
19+
clearTimeout(timeoutId);
20+
const el = element.value;
21+
if (!el) {
22+
return;
23+
}
24+
if (show) {
25+
emit('show');
26+
removeClass(el, COLLAPSE);
27+
el.style.height = 'auto';
28+
const height = window.getComputedStyle(el).height;
29+
el.style.height = null;
30+
addClass(el, COLLAPSING);
31+
el.offsetHeight; // force repaint
32+
el.style.height = height;
33+
timeoutId = setTimeout(() => {
34+
removeClass(el, COLLAPSING);
35+
addClass(el, COLLAPSE);
3836
addClass(el, IN);
39-
}
40-
},
41-
methods: {
42-
toggle(show) {
43-
clearTimeout(this.timeoutId);
44-
const el = this.$el;
45-
if (show) {
46-
this.$emit('show');
47-
removeClass(el, COLLAPSE);
48-
el.style.height = 'auto';
49-
const height = window.getComputedStyle(el).height;
50-
el.style.height = null;
51-
addClass(el, COLLAPSING);
52-
el.offsetHeight; // force repaint
53-
el.style.height = height;
54-
this.timeoutId = setTimeout(() => {
55-
removeClass(el, COLLAPSING);
56-
addClass(el, COLLAPSE);
57-
addClass(el, IN);
58-
el.style.height = null;
59-
this.timeoutId = 0;
60-
this.$emit('shown');
61-
}, this.transition);
62-
} else {
63-
this.$emit('hide');
64-
el.style.height = window.getComputedStyle(el).height;
65-
removeClass(el, IN);
66-
removeClass(el, COLLAPSE);
67-
el.offsetHeight;
68-
el.style.height = null;
69-
addClass(el, COLLAPSING);
70-
this.timeoutId = setTimeout(() => {
71-
addClass(el, COLLAPSE);
72-
removeClass(el, COLLAPSING);
73-
el.style.height = null;
74-
this.timeoutId = 0;
75-
this.$emit('hidden');
76-
}, this.transition);
77-
}
78-
},
79-
},
37+
el.style.height = null;
38+
timeoutId = 0;
39+
emit('shown');
40+
}, props.transition);
41+
} else {
42+
emit('hide');
43+
el.style.height = window.getComputedStyle(el).height;
44+
removeClass(el, IN);
45+
removeClass(el, COLLAPSE);
46+
el.offsetHeight;
47+
el.style.height = null;
48+
addClass(el, COLLAPSING);
49+
timeoutId = setTimeout(() => {
50+
addClass(el, COLLAPSE);
51+
removeClass(el, COLLAPSING);
52+
el.style.height = null;
53+
timeoutId = 0;
54+
emit('hidden');
55+
}, props.transition);
56+
}
57+
}
58+
59+
watchEffect(() => {
60+
toggle(props.modelValue);
61+
});
62+
63+
onMounted(() => {
64+
addClass(element.value, COLLAPSE);
65+
if (props.modelValue) {
66+
addClass(element.value, IN);
67+
}
68+
});
69+
</script>
70+
71+
<script lang="jsx">
72+
import { defineComponent } from 'vue';
73+
74+
export default defineComponent({
8075
render() {
8176
const Tag = this.tag;
82-
return <Tag>{this.$slots.default?.()}</Tag>;
77+
return <Tag ref="element">{this.$slots.default?.()}</Tag>;
8378
},
84-
};
79+
});
8580
</script>

0 commit comments

Comments
 (0)