@veristone/nuxt-v-app 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Composable for managing data table state
3
+ * Handles sorting, filtering, pagination, and selection
4
+ */
5
+ export function useDataTable(options = {}) {
6
+ const {
7
+ defaultPageSize = 10,
8
+ defaultSortKey = '',
9
+ defaultSortDirection = 'asc'
10
+ } = options
11
+
12
+ // Pagination
13
+ const page = ref(1)
14
+ const pageSize = ref(defaultPageSize)
15
+ const total = ref(0)
16
+
17
+ const totalPages = computed(() => Math.ceil(total.value / pageSize.value))
18
+ const startIndex = computed(() => (page.value - 1) * pageSize.value)
19
+ const endIndex = computed(() => Math.min(startIndex.value + pageSize.value, total.value))
20
+
21
+ // Sorting
22
+ const sortKey = ref(defaultSortKey)
23
+ const sortDirection = ref(defaultSortDirection)
24
+
25
+ function toggleSort(key) {
26
+ if (sortKey.value === key) {
27
+ sortDirection.value = sortDirection.value === 'asc' ? 'desc' : 'asc'
28
+ } else {
29
+ sortKey.value = key
30
+ sortDirection.value = 'asc'
31
+ }
32
+ page.value = 1 // Reset to first page on sort change
33
+ }
34
+
35
+ function setSort(key, direction = 'asc') {
36
+ sortKey.value = key
37
+ sortDirection.value = direction
38
+ page.value = 1
39
+ }
40
+
41
+ function clearSort() {
42
+ sortKey.value = ''
43
+ sortDirection.value = 'asc'
44
+ }
45
+
46
+ // Filtering
47
+ const filters = ref({})
48
+ const searchQuery = ref('')
49
+
50
+ function setFilter(key, value) {
51
+ filters.value[key] = value
52
+ page.value = 1 // Reset to first page on filter change
53
+ }
54
+
55
+ function removeFilter(key) {
56
+ delete filters.value[key]
57
+ page.value = 1
58
+ }
59
+
60
+ function clearFilters() {
61
+ filters.value = {}
62
+ searchQuery.value = ''
63
+ page.value = 1
64
+ }
65
+
66
+ const activeFilters = computed(() => {
67
+ return Object.entries(filters.value)
68
+ .filter(([_, value]) => value !== undefined && value !== null && value !== '')
69
+ .map(([key, value]) => ({ key, value, label: key }))
70
+ })
71
+
72
+ // Selection
73
+ const selectedRows = ref([])
74
+
75
+ function selectRow(row, rowKey = 'id') {
76
+ const index = selectedRows.value.findIndex(r => r[rowKey] === row[rowKey])
77
+ if (index < 0) {
78
+ selectedRows.value.push(row)
79
+ }
80
+ }
81
+
82
+ function deselectRow(row, rowKey = 'id') {
83
+ const index = selectedRows.value.findIndex(r => r[rowKey] === row[rowKey])
84
+ if (index >= 0) {
85
+ selectedRows.value.splice(index, 1)
86
+ }
87
+ }
88
+
89
+ function toggleRowSelection(row, rowKey = 'id') {
90
+ const index = selectedRows.value.findIndex(r => r[rowKey] === row[rowKey])
91
+ if (index >= 0) {
92
+ selectedRows.value.splice(index, 1)
93
+ } else {
94
+ selectedRows.value.push(row)
95
+ }
96
+ }
97
+
98
+ function selectAll(rows) {
99
+ selectedRows.value = [...rows]
100
+ }
101
+
102
+ function deselectAll() {
103
+ selectedRows.value = []
104
+ }
105
+
106
+ function isRowSelected(row, rowKey = 'id') {
107
+ return selectedRows.value.some(r => r[rowKey] === row[rowKey])
108
+ }
109
+
110
+ // Query parameters for API
111
+ const queryParams = computed(() => ({
112
+ page: page.value,
113
+ limit: pageSize.value,
114
+ sort: sortKey.value ? `${sortDirection.value === 'desc' ? '-' : ''}${sortKey.value}` : undefined,
115
+ search: searchQuery.value || undefined,
116
+ ...filters.value
117
+ }))
118
+
119
+ // Reset all state
120
+ function reset() {
121
+ page.value = 1
122
+ pageSize.value = defaultPageSize
123
+ sortKey.value = defaultSortKey
124
+ sortDirection.value = defaultSortDirection
125
+ filters.value = {}
126
+ searchQuery.value = ''
127
+ selectedRows.value = []
128
+ }
129
+
130
+ return {
131
+ // Pagination
132
+ page,
133
+ pageSize,
134
+ total,
135
+ totalPages,
136
+ startIndex,
137
+ endIndex,
138
+
139
+ // Sorting
140
+ sortKey,
141
+ sortDirection,
142
+ toggleSort,
143
+ setSort,
144
+ clearSort,
145
+
146
+ // Filtering
147
+ filters,
148
+ searchQuery,
149
+ activeFilters,
150
+ setFilter,
151
+ removeFilter,
152
+ clearFilters,
153
+
154
+ // Selection
155
+ selectedRows,
156
+ selectRow,
157
+ deselectRow,
158
+ toggleRowSelection,
159
+ selectAll,
160
+ deselectAll,
161
+ isRowSelected,
162
+
163
+ // Query
164
+ queryParams,
165
+
166
+ // Reset
167
+ reset
168
+ }
169
+ }