1
+ <template >
2
+ <div
3
+ v-if =" field.type === 'matrix'"
4
+ class =" border-b py-2 px-4"
5
+ >
6
+ <h3 class =" font-semibold block text-lg" >
7
+ Matrix
8
+ </h3 >
9
+ <p class =" text-gray-400 mb-3 text-xs" >
10
+ Advanced options for matrix.
11
+ </p >
12
+
13
+ <div class =" grid grid-cols-2 gap-4" >
14
+ <div class =" " >
15
+ <div v-for =" row, i in field.rows" class =" flex items-center space-x-2" :key =" row+i" >
16
+ <text-input
17
+ name =" rows"
18
+ class =" mb-0"
19
+ v-model =" field.rows[i]"
20
+ />
21
+ <button @click =" removeMatrixRow(i)" >
22
+ <Icon name =" heroicons:trash" class =" text-gray-300 w-4 h-4" />
23
+ </button >
24
+ </div >
25
+ <button @click =" addMatrixRow" class =" space-x-1 flex items-center bg-gray-200 rounded text-xs p-1 px-2" >
26
+ <Icon name =" heroicons:plus" class =" w-4 h-4" />
27
+ <span >Add rows</span >
28
+ </button >
29
+ </div >
30
+ <div class =" " >
31
+ <div v-for =" column, i in field.columns" class =" flex items-center space-x-2" :key =" i" >
32
+ <text-input
33
+ class =" mb-0"
34
+ v-model =" field.columns[i]"
35
+ />
36
+ <button @click =" removeMatrixColumn(i)" >
37
+ <Icon name =" heroicons:trash" class =" text-gray-300 w-4 h-4" />
38
+ </button >
39
+ </div >
40
+ <button @click =" addMatrixColumn" class =" space-x-1 flex items-center bg-gray-200 rounded text-xs p-1 px-2" >
41
+ <Icon name =" heroicons:plus" class =" w-4 h-4" />
42
+ <span >Add column</span >
43
+ </button >
44
+ </div >
45
+ </div >
46
+ </div >
47
+ </template >
48
+
49
+ <script >
50
+ export default {
51
+ name: ' MatrixFieldOptions' ,
52
+ props: {
53
+ field: {
54
+ type: Object ,
55
+ required: false
56
+ },
57
+ },
58
+ computed: {
59
+ selectionData () {
60
+ return this .field .rows ? .reduce ((obj , row ) => {
61
+ obj[row] = ' ' ;
62
+ return obj;
63
+ }, {});
64
+ }
65
+ },
66
+
67
+ methods: {
68
+ addMatrixRow () {
69
+ this .field .rows .push (this .generateUniqueLabel (this .field .rows , ' Row' ))
70
+ this .field .selection_data = this .selectionData
71
+ },
72
+ removeMatrixRow (index ) {
73
+ this .field .rows .splice (index, 1 )
74
+ this .field .selection_data = this .selectionData
75
+ },
76
+
77
+ addMatrixColumn () {
78
+ this .field .columns .push (this .generateUniqueLabel (this .field .columns , null ))
79
+ this .field .selection_data = this .selectionData
80
+ },
81
+ removeMatrixColumn (index ) {
82
+ this .field .columns .splice (index, 1 )
83
+ this .field .selection_data = this .selectionData
84
+ },
85
+
86
+ generateUniqueLabel (array , prefix = null ) {
87
+ let uniqueNumber = 1 ; // Start checking from 1
88
+ let label = prefix ? ` ${ prefix} ${ uniqueNumber} ` : uniqueNumber
89
+ while (array .includes (label)) {
90
+ uniqueNumber++ ; // Increment if the number is found in the array
91
+ label = prefix ? ` ${ prefix} ${ uniqueNumber} ` : uniqueNumber
92
+ }
93
+ return label; // Return the first unique number found
94
+ }
95
+ }
96
+ }
97
+ < / script>
0 commit comments