-
Notifications
You must be signed in to change notification settings - Fork 770
/
Copy pathTable.vue
238 lines (222 loc) · 5.61 KB
/
Table.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<template>
<div v-if="tableFields?.length">
<div v-if="showLabel" class="text-gray-600 dark:text-gray-400 text-sm mb-1">
{{ df.label }}
</div>
<div :class="border ? 'border dark:border-gray-800 rounded-md' : ''">
<!-- Title Row -->
<Row
:ratio="ratio"
class="
border-b
dark:border-gray-800
px-2
text-gray-600
dark:text-gray-400
w-full
flex
items-center
"
>
<div class="flex items-center ps-2">#</div>
<div
v-for="df in tableFields"
:key="df.fieldname"
class="items-center flex px-2 h-row-mid"
:class="{
'ms-auto': isNumeric(df),
}"
:style="{
height: ``,
}"
>
{{ df.label }}
</div>
</Row>
<!-- Data Rows -->
<div
v-if="value"
class="overflow-auto custom-scroll custom-scroll-thumb1"
:style="{ 'max-height': maxHeight }"
>
<TableRow
v-for="(row, idx) of value"
ref="table-row"
:key="row.name"
:class="idx < value.length - 1 ? 'border-b dark:border-gray-800' : ''"
v-bind="{ row, tableFields, size, ratio, isNumeric }"
:read-only="isReadOnly"
:can-edit-row="canEditRow"
@remove="removeRow(row)"
@change="(field, value) => $emit('row-change', field, value, df)"
/>
</div>
<!-- Add Row and Row Count -->
<Row
v-if="!isReadOnly"
:ratio="ratio"
class="
text-gray-500
cursor-pointer
px-2
w-full
h-row-mid
flex
items-center
focus:outline-none focus:ring-1 focus:ring-blue-500
"
:class="value.length > 0 ? 'border-t dark:border-gray-800' : ''"
tabindex="0"
@click="addRow"
@keydown.enter="addRow"
@keydown.space.prevent="addRow"
@keydown.up.prevent="focusLastRow"
ref="addRowButton"
>
<div class="flex items-center ps-1">
<feather-icon name="plus" class="w-4 h-4 text-gray-500" />
</div>
<div
class="flex justify-between px-2"
:style="`grid-column: 2 / ${ratio.length + 1}`"
>
<p>
{{ t`Add Row` }}
</p>
<p
v-if="
value &&
maxRowsBeforeOverflow &&
value.length > maxRowsBeforeOverflow
"
class="text-end px-2"
>
{{ t`${value.length} rows` }}
</p>
</div>
</Row>
</div>
</div>
</template>
<script>
import Row from 'src/components/Row.vue';
import { fyo } from 'src/initFyo';
import { nextTick } from 'vue';
import Base from './Base.vue';
import TableRow from './TableRow.vue';
export default {
name: 'Table',
components: {
Row,
TableRow,
},
extends: Base,
props: {
value: { type: Array, default: () => [] },
showHeader: {
type: Boolean,
default: true,
},
maxRowsBeforeOverflow: {
type: Number,
default: 3,
},
border: {
type: Boolean,
default: false,
},
},
emits: ['editrow', 'row-change'],
data() {
return { maxHeight: '' };
},
computed: {
height() {
if (this.size === 'small') {
}
return 2;
},
canEditRow() {
return this.df.edit;
},
ratio() {
const ratio = [0.3].concat(this.tableFields.map(() => 1));
if (this.canEditRow) {
return ratio.concat(0.3);
}
return ratio;
},
tableFields() {
const fields = fyo.schemaMap[this.df.target].tableFields ?? [];
return fields.map((fieldname) => fyo.getField(this.df.target, fieldname));
},
},
watch: {
value() {
this.setMaxHeight();
},
},
mounted() {
if (fyo.store.isDevelopment) {
window.tab = this;
}
},
methods: {
focus() {},
async addRow() {
await this.doc.append(this.df.fieldname);
await nextTick();
this.scrollToRow(this.value.length - 1);
this.triggerChange(this.value);
this.$nextTick(() => {
const rows = this.$refs['table-row'];
if (rows && rows.length > 0) {
const lastRow = rows[rows.length - 1];
if (lastRow.focusFirstInput) {
lastRow.focusFirstInput();
}
}
});
},
focusLastRow() {
if (this.value.length === 0) return;
this.$nextTick(() => {
const rows = this.$refs['table-row'];
if (rows && rows.length > 0) {
const lastRow = rows[rows.length - 1];
if (lastRow.focusFirstInput) {
lastRow.focusFirstInput();
}
}
});
},
removeRow(row) {
this.doc.remove(this.df.fieldname, row.idx).then((s) => {
if (!s) {
return;
}
this.triggerChange(this.value);
});
},
scrollToRow(index) {
const row = this.$refs['table-row'][index];
row && row.$el.scrollIntoView({ block: 'nearest' });
},
setMaxHeight() {
if (this.maxRowsBeforeOverflow === 0) {
return (this.maxHeight = '');
}
const size = this?.value?.length ?? 0;
if (size === 0) {
return (this.maxHeight = '');
}
const rowHeight = this.$refs?.['table-row']?.[0]?.$el.offsetHeight;
if (rowHeight === undefined) {
return (this.maxHeight = '');
}
const maxHeight = rowHeight * Math.min(this.maxRowsBeforeOverflow, size);
return (this.maxHeight = `${maxHeight}px`);
},
},
};
</script>