Field.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 :ref="`input:${entryIndex}:${fieldType.fieldTypeId}`" 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)" @keyup.ctrl.exact.59="fillInput(entryIndex, fieldType.fieldTypeId, 'date')" @keyup.ctrl.shift.exact.59="fillInput(entryIndex, fieldType.fieldTypeId, 'time')" />
  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. fillInput(entryIndex, fieldTypeId, fillType) {
  92. let input = this.$refs[`input:${entryIndex}:${fieldTypeId}`][0];
  93. const cursorPoint = input.selectionStart;
  94. const currentValue = this.entries[entryIndex][fieldTypeId];
  95. const firstPart = currentValue.substring(0, cursorPoint);
  96. const lastPart = currentValue.substring(cursorPoint, currentValue.length);
  97. let fillValue = "";
  98. const currentDate = new Date();
  99. switch(fillType) {
  100. case "date":
  101. const year = currentDate.getFullYear();
  102. const month = `0${(currentDate.getMonth() + 1)}`.slice(-2);
  103. const day = `0${currentDate.getDate()}`.slice(-2);
  104. fillValue = `${year}-${month}-${day}`;
  105. break;
  106. case "time":
  107. const hour = `0${currentDate.getHours()}`.slice(-2);
  108. const minute = `0${currentDate.getMinutes()}`.slice(-2);
  109. fillValue = `${hour}:${minute}`;
  110. break;
  111. }
  112. const newValue = `${firstPart}${fillValue}${lastPart}`;
  113. this.entries[entryIndex][fieldTypeId] = newValue;
  114. const newCaretPosition = firstPart.length + fillValue.length;
  115. this.$nextTick(() => {
  116. input.focus();
  117. input.setSelectionRange(newCaretPosition, newCaretPosition);
  118. });
  119. }
  120. }
  121. };
  122. </script>
  123. <style lang="scss" scoped>
  124. .control {
  125. width: 100%;
  126. margin-bottom: 16px;
  127. label {
  128. display: block;
  129. margin-bottom: 4px;
  130. }
  131. .control-row {
  132. height: 32px;
  133. display: flex;
  134. grid-column-gap: 12px;
  135. }
  136. .control-row:not(:last-of-type) {
  137. margin-bottom: 8px;
  138. }
  139. .control-col {
  140. display: flex;
  141. position: relative;
  142. button:last-of-type {
  143. border-radius: 0 5px 5px 0;
  144. }
  145. > input:not(:last-child) {
  146. border-right: none;
  147. border-top-right-radius: 0;
  148. border-bottom-right-radius: 0;
  149. }
  150. .autosuggest-container {
  151. position: absolute;
  152. top: 31px;
  153. width: 100%;
  154. border-radius: 5px 5px 5px 5px;
  155. border: solid .5px #464646;
  156. z-index: 10;
  157. .autosuggest-item {
  158. padding: 8px;
  159. cursor: pointer;
  160. background-color: white;
  161. &:hover, &:focus {
  162. background-color: lightgray;
  163. }
  164. }
  165. }
  166. }
  167. input, select {
  168. border: solid .5px #464646;
  169. border-radius: 5px;
  170. padding: 4px;
  171. }
  172. input {
  173. width: 100%;
  174. }
  175. button {
  176. width: 32px;
  177. height: 32px;
  178. border: none;
  179. font-size: 24px;
  180. background-color: green;
  181. color: white;
  182. cursor: pointer;
  183. }
  184. button:hover, button:focus {
  185. background-color: darkgreen;
  186. }
  187. }
  188. .fill-remaining {
  189. width: 100%;
  190. width: -moz-available; /* WebKit-based browsers will ignore this. */
  191. width: -webkit-fill-available; /* Mozilla-based browsers will ignore this. */
  192. width: fill-available;
  193. }
  194. .checkbox {
  195. height: 32px;
  196. width: 32px;
  197. background-color: white;
  198. border: .5px #464646 solid;
  199. border-radius: 3px;
  200. cursor: pointer;
  201. position: relative;
  202. box-sizing: border-box;
  203. }
  204. .checkbox.checked::after {
  205. content: "";
  206. width: 26px;
  207. height: 26px;
  208. left: 2px;
  209. top: 2px;
  210. display: inline-block;
  211. position: absolute;
  212. border-radius: 3px;
  213. background-color: #69B862;
  214. }
  215. </style>