Field.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div class="control">
  3. <label for="name">{{ name }}</label>
  4. <button v-if="entries.length === 0" @click="addEntry()" type="button">+</button>
  5. <div class="control-row" v-for="(entry, entryIndex) in entries">
  6. <div class="control-col" v-for="(fieldType, fieldIndex) in fieldTypes" :class="{ 'fill-remaining': fieldType.fill }">
  7. <input name="name" type="text" v-if="fieldType.type === 'text'" v-model="entry[fieldType.fieldTypeId]" v-on:blur="blurInput(entryIndex, fieldType.fieldTypeId)" v-on:focus="focusInput(entryIndex, fieldType.fieldTypeId)" v-on:keydown="keydownInput(entryIndex, fieldType.fieldTypeId)" />
  8. <select name="name" v-if="fieldType.type === 'select'" class="fill-remaining" v-model="entry[fieldType.fieldTypeId]">
  9. <option v-for="option in fieldType.options" :value="option.value">{{option.text}}</option>
  10. </select>
  11. <div tabindex="0" v-on:keyup.enter="toggleCheckbox(entryIndex, fieldType.fieldTypeId)" v-on:keyup.space="toggleCheckbox(entryIndex, fieldType.fieldTypeId)" name="name" class="checkbox" v-if="fieldType.type === 'checkbox'" :class="{ checked: entry[fieldType.fieldTypeId] }" @click="toggleCheckbox(entryIndex, fieldType.fieldTypeId)"></div>
  12. <button v-if="fieldType.extraButtons" v-for="buttonInfo in fieldType.extraButtons" type="button" :class="[buttonInfo.style]">{{buttonInfo.icon}}</button>
  13. <button v-if="entryIndex + 1 === entries.length && entryIndex + 1 < maxEntries && fieldIndex + 1 === fieldTypes.length" @click="addEntry()" type="button">+</button>
  14. <button v-if="entries.length > minEntries && fieldIndex + 1 === fieldTypes.length" @click="removeEntry(entryIndex)" type="button">-</button>
  15. <div v-if="fieldType.autosuggestGroup && (focusedInput === `${entryIndex}.${fieldType.fieldTypeId}` || autosuggestHover === `${entryIndex}.${fieldType.fieldTypeId}`)" class="autosuggest-container" @mouseover="focusAutosuggestContainer(entryIndex, fieldType.fieldTypeId)" @mouseleave="blurAutosuggestContainer()">
  16. <div v-for="autosuggestItem in filter(autosuggest[fieldType.autosuggestGroup], entry[fieldType.fieldTypeId])" class="autosuggest-item" @click="selectAutosuggest(entryIndex, fieldType.fieldTypeId, autosuggestItem)">
  17. {{ autosuggestItem }}
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. function Field() {
  26. }
  27. export default {
  28. data: function() {
  29. return {
  30. entries: [...this.initialEntries],
  31. focusedInput: "",
  32. activeAutosuggestHover: "",
  33. autosuggestHover: ""
  34. };
  35. },
  36. props: {
  37. name: String,
  38. fieldTypes: Array,
  39. minEntries: Number,
  40. maxEntries: Number,
  41. initialEntries: Array,
  42. autosuggest: Object
  43. },
  44. mounted() {
  45. },
  46. methods: {
  47. addEntry() {
  48. let emptyEntry = {};
  49. this.fieldTypes.forEach((fieldType) => {
  50. if (fieldType.type === "text" || fieldType.type === "select") emptyEntry[fieldType.fieldTypeId] = "";
  51. else if (fieldType.type === "checkbox") emptyEntry[fieldType.fieldTypeId] = false;
  52. });
  53. this.entries.push(emptyEntry);
  54. },
  55. removeEntry(index) {
  56. this.entries.splice(index, 1);
  57. },
  58. toggleCheckbox(entryIndex, fieldTypeId) {
  59. this.entries[entryIndex][fieldTypeId] = !this.entries[entryIndex][fieldTypeId];
  60. },
  61. selectAutosuggest(entryIndex, fieldTypeId, autosuggestItem) {
  62. this.entries[entryIndex][fieldTypeId] = autosuggestItem;
  63. },
  64. blurInput(entryIndex, fieldTypeId) {
  65. this.focusedInput = "";
  66. },
  67. focusInput(entryIndex, fieldTypeId) {
  68. this.focusedInput = `${entryIndex}.${fieldTypeId}`;
  69. },
  70. keydownInput(entryIndex, fieldTypeId) {
  71. },
  72. focusAutosuggestContainer(entryIndex, fieldTypeId) {
  73. this.autosuggestHover = `${entryIndex}.${fieldTypeId}`;
  74. },
  75. blurAutosuggestContainer() {
  76. this.autosuggestHover = "";
  77. },
  78. filter(autosuggest, value) {
  79. return autosuggest.filter(autosuggestItem => autosuggestItem.toLowerCase().startsWith(value.toLowerCase())).sort();
  80. }
  81. }
  82. };
  83. </script>
  84. <style lang="scss" scoped>
  85. .control {
  86. width: 100%;
  87. margin-bottom: 16px;
  88. label {
  89. display: block;
  90. margin-bottom: 4px;
  91. }
  92. .control-row {
  93. height: 32px;
  94. display: flex;
  95. grid-column-gap: 12px;
  96. }
  97. .control-row:not(:last-of-type) {
  98. margin-bottom: 8px;
  99. }
  100. .control-col {
  101. display: flex;
  102. position: relative;
  103. button:last-of-type {
  104. border-radius: 0 5px 5px 0;
  105. }
  106. > input:not(:last-child) {
  107. border-right: none;
  108. border-top-right-radius: 0;
  109. border-bottom-right-radius: 0;
  110. }
  111. .autosuggest-container {
  112. position: absolute;
  113. top: 31px;
  114. width: 100%;
  115. border-radius: 5px 5px 5px 5px;
  116. border: solid .5px #464646;
  117. z-index: 10;
  118. .autosuggest-item {
  119. padding: 8px;
  120. cursor: pointer;
  121. background-color: white;
  122. &:hover, &:focus {
  123. background-color: lightgray;
  124. }
  125. }
  126. }
  127. }
  128. input, select {
  129. border: solid .5px #464646;
  130. border-radius: 5px;
  131. padding: 4px;
  132. }
  133. input {
  134. width: 100%;
  135. }
  136. button {
  137. width: 32px;
  138. height: 32px;
  139. border: none;
  140. font-size: 24px;
  141. background-color: green;
  142. color: white;
  143. cursor: pointer;
  144. }
  145. button:hover, button:focus {
  146. background-color: darkgreen;
  147. }
  148. }
  149. .fill-remaining {
  150. width: 100%;
  151. width: -moz-available; /* WebKit-based browsers will ignore this. */
  152. width: -webkit-fill-available; /* Mozilla-based browsers will ignore this. */
  153. width: fill-available;
  154. }
  155. .checkbox {
  156. height: 32px;
  157. width: 32px;
  158. background-color: white;
  159. border: .5px #464646 solid;
  160. border-radius: 3px;
  161. cursor: pointer;
  162. position: relative;
  163. box-sizing: border-box;
  164. }
  165. .checkbox.checked::after {
  166. content: "";
  167. width: 26px;
  168. height: 26px;
  169. left: 2px;
  170. top: 2px;
  171. display: inline-block;
  172. position: absolute;
  173. border-radius: 3px;
  174. background-color: #69B862;
  175. }
  176. </style>