Field.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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]" @change="onInputChange()" 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]" @change="onSelectChange()">
  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. onChange: Function
  44. },
  45. mounted() {
  46. },
  47. methods: {
  48. addEntry() {
  49. let emptyEntry = {};
  50. this.fieldTypes.forEach((fieldType) => {
  51. if (fieldType.type === "text" || fieldType.type === "select") emptyEntry[fieldType.fieldTypeId] = "";
  52. else if (fieldType.type === "checkbox") emptyEntry[fieldType.fieldTypeId] = false;
  53. });
  54. this.entries.push(emptyEntry);
  55. this.onChange();
  56. },
  57. removeEntry(index) {
  58. this.entries.splice(index, 1);
  59. this.onChange();
  60. },
  61. toggleCheckbox(entryIndex, fieldTypeId) {
  62. this.entries[entryIndex][fieldTypeId] = !this.entries[entryIndex][fieldTypeId];
  63. this.onChange();
  64. },
  65. selectAutosuggest(entryIndex, fieldTypeId, autosuggestItem) {
  66. this.entries[entryIndex][fieldTypeId] = autosuggestItem;
  67. },
  68. blurInput(entryIndex, fieldTypeId) {
  69. this.focusedInput = "";
  70. },
  71. focusInput(entryIndex, fieldTypeId) {
  72. this.focusedInput = `${entryIndex}.${fieldTypeId}`;
  73. },
  74. keydownInput(entryIndex, fieldTypeId) {
  75. },
  76. focusAutosuggestContainer(entryIndex, fieldTypeId) {
  77. this.autosuggestHover = `${entryIndex}.${fieldTypeId}`;
  78. },
  79. blurAutosuggestContainer() {
  80. this.autosuggestHover = "";
  81. },
  82. onSelectChange() {
  83. this.onChange();
  84. },
  85. onInputChange() {
  86. this.onChange();
  87. },
  88. filter(autosuggest, value) {
  89. return autosuggest.filter(autosuggestItem => autosuggestItem.toLowerCase().startsWith(value.toLowerCase())).sort();
  90. }
  91. }
  92. };
  93. </script>
  94. <style lang="scss" scoped>
  95. .control {
  96. width: 100%;
  97. margin-bottom: 16px;
  98. label {
  99. display: block;
  100. margin-bottom: 4px;
  101. }
  102. .control-row {
  103. height: 32px;
  104. display: flex;
  105. grid-column-gap: 12px;
  106. }
  107. .control-row:not(:last-of-type) {
  108. margin-bottom: 8px;
  109. }
  110. .control-col {
  111. display: flex;
  112. position: relative;
  113. button:last-of-type {
  114. border-radius: 0 5px 5px 0;
  115. }
  116. > input:not(:last-child) {
  117. border-right: none;
  118. border-top-right-radius: 0;
  119. border-bottom-right-radius: 0;
  120. }
  121. .autosuggest-container {
  122. position: absolute;
  123. top: 31px;
  124. width: 100%;
  125. border-radius: 5px 5px 5px 5px;
  126. border: solid .5px #464646;
  127. z-index: 10;
  128. .autosuggest-item {
  129. padding: 8px;
  130. cursor: pointer;
  131. background-color: white;
  132. &:hover, &:focus {
  133. background-color: lightgray;
  134. }
  135. }
  136. }
  137. }
  138. input, select {
  139. border: solid .5px #464646;
  140. border-radius: 5px;
  141. padding: 4px;
  142. }
  143. input {
  144. width: 100%;
  145. }
  146. button {
  147. width: 32px;
  148. height: 32px;
  149. border: none;
  150. font-size: 24px;
  151. background-color: green;
  152. color: white;
  153. cursor: pointer;
  154. }
  155. button:hover, button:focus {
  156. background-color: darkgreen;
  157. }
  158. }
  159. .fill-remaining {
  160. width: 100%;
  161. width: -moz-available; /* WebKit-based browsers will ignore this. */
  162. width: -webkit-fill-available; /* Mozilla-based browsers will ignore this. */
  163. width: fill-available;
  164. }
  165. .checkbox {
  166. height: 32px;
  167. width: 32px;
  168. background-color: white;
  169. border: .5px #464646 solid;
  170. border-radius: 3px;
  171. cursor: pointer;
  172. position: relative;
  173. box-sizing: border-box;
  174. }
  175. .checkbox.checked::after {
  176. content: "";
  177. width: 26px;
  178. height: 26px;
  179. left: 2px;
  180. top: 2px;
  181. display: inline-block;
  182. position: absolute;
  183. border-radius: 3px;
  184. background-color: #69B862;
  185. }
  186. </style>