Skip to content

Commit 5ba19d3

Browse files
committed
[#28] create component to show sorting
1 parent 739ec78 commit 5ba19d3

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Meta, Story } from "@storybook/vue3";
2+
import SortWrap from "~/components/SortWrap/SortWrap.vue";
3+
import { SORT_ORDER } from "~/config/constants";
4+
import { action } from "@storybook/addon-actions";
5+
6+
export default {
7+
title: "Components/SortWrap",
8+
component: SortWrap,
9+
} as Meta<typeof SortWrap>;
10+
11+
const Template: Story = (args) => ({
12+
components: { SortWrap },
13+
methods: {
14+
action
15+
},
16+
setup() {
17+
return {
18+
args,
19+
SORT_ORDER,
20+
};
21+
},
22+
23+
template: `
24+
<div>
25+
<div>
26+
<sort-wrap
27+
@changeSort="(a) => action('Change Sort')(a)"
28+
>
29+
Default sort
30+
</sort-wrap>
31+
</div>
32+
<br>
33+
<div>
34+
<sort-wrap
35+
@changeSort="(a) => action('Change Sort')(a)"
36+
:sort="SORT_ORDER.ASC"
37+
>
38+
ASC sort
39+
</sort-wrap>
40+
</div>
41+
<br>
42+
<div>
43+
<sort-wrap
44+
@changeSort="(a) => action('Change Sort')(a)"
45+
:sort="SORT_ORDER.DESC"
46+
>
47+
DESC sort
48+
</sort-wrap>
49+
</div>
50+
</div>`,
51+
});
52+
53+
export const Default = Template.bind({});
54+
Default.args = {
55+
};

components/SortWrap/SortWrap.vue

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<template>
2+
<span class="sort-wrap" @click="changeSortOrder">
3+
<slot />
4+
5+
<span class="sort-wrap__markers">
6+
<span
7+
class="sort-wrap__marker sort-wrap__marker--asc"
8+
:class="{ 'sort-wrap__marker--active': sort === sortOrder.ASC }"
9+
></span>
10+
<span
11+
class="sort-wrap__marker sort-wrap__marker--desc"
12+
:class="{ 'sort-wrap__marker--active': sort === sortOrder.DESC }"
13+
></span>
14+
</span>
15+
</span>
16+
</template>
17+
18+
<script lang="ts">
19+
import { defineComponent } from "vue";
20+
import { SORT_ORDER } from "~/config/constants";
21+
22+
export default defineComponent({
23+
components: {},
24+
props: {
25+
sort: {
26+
type: String,
27+
default: SORT_ORDER.DEFAULT,
28+
},
29+
},
30+
emits: ["changeSort"],
31+
computed: {
32+
sortOrder() {
33+
return SORT_ORDER;
34+
},
35+
},
36+
methods: {
37+
changeSortOrder() {
38+
const sortOrderList = [
39+
SORT_ORDER.ASC,
40+
SORT_ORDER.DESC,
41+
SORT_ORDER.DEFAULT,
42+
];
43+
44+
const nextSortOrderIndex = sortOrderList.findIndex(
45+
(sortOrder) => sortOrder === this.sort
46+
);
47+
48+
const nextSortOrder =
49+
sortOrderList[nextSortOrderIndex + 1] || sortOrderList[0];
50+
51+
this.$emit("changeSort", nextSortOrder);
52+
},
53+
},
54+
});
55+
</script>
56+
57+
<style lang="scss" scoped>
58+
.sort-wrap {
59+
@apply cursor-pointer relative bg-transparent inline-flex items-center;
60+
}
61+
62+
.sort-wrap__markers {
63+
@apply flex items-center h-full flex-col text-gray-400 dark:text-gray-600 gap-y-1 ml-1;
64+
}
65+
66+
.sort-wrap__marker {
67+
@apply opacity-40 hover:opacity-100 w-0 h-0 border-l-[4px] border-l-transparent border-t-[6px] border-t-gray-500 border-r-[4px] border-r-transparent border-b-[6px] border-b-transparent;
68+
}
69+
70+
.sort-wrap__marker--asc {
71+
@apply border-b-[6px] border-b-gray-600 dark:border-b-white border-t-[0px];
72+
}
73+
74+
.sort-wrap__marker--desc {
75+
@apply border-t-[6px] border-t-gray-600 dark:border-t-white border-b-[0px];
76+
}
77+
78+
.sort-wrap__marker--active {
79+
@apply opacity-100;
80+
}
81+
</style>

config/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ export const LOCAL_STORAGE_KEYS = {
5151
CACHED_EVENTS: "cached_events",
5252
THEME: "theme",
5353
};
54+
55+
56+
export const SORT_ORDER = {
57+
ASC: 'asc',
58+
DESC: 'desc',
59+
DEFAULT: 'default',
60+
}

0 commit comments

Comments
 (0)