ChoiceCombobox.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <div
  3. ref="rootRef"
  4. class="choice-combobox"
  5. >
  6. <input
  7. :id="id"
  8. ref="searchInputRef"
  9. type="text"
  10. class="choice-combobox-input"
  11. role="combobox"
  12. autocomplete="off"
  13. :aria-expanded="isOpen"
  14. :aria-controls="listboxId"
  15. :aria-activedescendant="activeDescendantId"
  16. :placeholder="placeholderText"
  17. :value="query"
  18. :required="required"
  19. @focus="handleFocus"
  20. @input="handleSearchInput"
  21. @keydown="handleKeydown"
  22. @blur="handleBlur"
  23. >
  24. <input
  25. :name="name"
  26. type="hidden"
  27. :value="modelValue"
  28. >
  29. <ul
  30. v-if="isOpen && filteredChoices.length > 0"
  31. :id="listboxId"
  32. role="listbox"
  33. class="choice-combobox-list"
  34. >
  35. <li
  36. v-for="(choice, index) in filteredChoices"
  37. :id="listboxOptionId(index)"
  38. :key="choice.value"
  39. role="option"
  40. :aria-selected="choice.value === modelValue"
  41. :class="{
  42. highlighted: index === highlightedIndex,
  43. selected: choice.value === modelValue
  44. }"
  45. @mousedown.prevent="selectChoice(choice)"
  46. >
  47. {{ choiceLabel(choice) }}
  48. </li>
  49. </ul>
  50. <div
  51. v-else-if="isOpen && query"
  52. class="choice-combobox-list choice-combobox-empty"
  53. >
  54. No matching options
  55. </div>
  56. </div>
  57. </template>
  58. <script setup>
  59. import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
  60. import {
  61. choiceDisplayLabel,
  62. syncStateFromModelValue
  63. } from './choiceComboboxHelpers.js'
  64. import {
  65. argumentFieldListboxId,
  66. argumentFieldListboxOptionId
  67. } from '../utils/argumentFieldIds.js'
  68. const props = defineProps({
  69. id: {
  70. type: String,
  71. required: true
  72. },
  73. name: {
  74. type: String,
  75. required: true
  76. },
  77. choices: {
  78. type: Array,
  79. required: true
  80. },
  81. modelValue: {
  82. type: String,
  83. default: ''
  84. },
  85. required: {
  86. type: Boolean,
  87. default: false
  88. }
  89. })
  90. const emit = defineEmits(['update:modelValue'])
  91. const closeOthersEvent = 'olivetin-choice-combobox-close-others'
  92. const rootRef = ref(null)
  93. const searchInputRef = ref(null)
  94. const isOpen = ref(false)
  95. const query = ref('')
  96. const isUserFiltering = ref(false)
  97. const highlightedIndex = ref(0)
  98. const listboxId = computed(() => argumentFieldListboxId(props.name))
  99. const activeDescendantId = computed(() => {
  100. if (!isOpen.value || filteredChoices.value.length === 0) {
  101. return undefined
  102. }
  103. return listboxOptionId(highlightedIndex.value)
  104. })
  105. function listboxOptionId (index) {
  106. return argumentFieldListboxOptionId(props.name, index)
  107. }
  108. const placeholderText = computed(() => {
  109. if (props.required) {
  110. return 'Search and select...'
  111. }
  112. return 'Search options...'
  113. })
  114. const filteredChoices = computed(() => {
  115. if (!isUserFiltering.value) {
  116. return props.choices
  117. }
  118. const search = query.value.trim().toLowerCase()
  119. if (!search) {
  120. return props.choices
  121. }
  122. return props.choices.filter(choice => {
  123. const label = choiceLabel(choice).toLowerCase()
  124. const value = String(choice.value).toLowerCase()
  125. return label.includes(search) || value.includes(search)
  126. })
  127. })
  128. watch([() => props.modelValue, () => props.choices], () => {
  129. if (!isOpen.value) {
  130. syncFromModelValue()
  131. }
  132. }, { immediate: true })
  133. function choiceLabel (choice) {
  134. return choiceDisplayLabel(choice)
  135. }
  136. function syncFromModelValue () {
  137. const next = syncStateFromModelValue(props.choices, props.modelValue)
  138. query.value = next.query
  139. if (next.modelValue !== props.modelValue) {
  140. emitValue(next.modelValue)
  141. }
  142. }
  143. function selectedChoiceIndex (choices) {
  144. const index = choices.findIndex(choice => choice.value === props.modelValue)
  145. return index >= 0 ? index : 0
  146. }
  147. function openList () {
  148. document.dispatchEvent(new CustomEvent(closeOthersEvent, { detail: { id: props.id } }))
  149. const wasClosed = !isOpen.value
  150. isOpen.value = true
  151. if (wasClosed) {
  152. highlightedIndex.value = selectedChoiceIndex(filteredChoices.value)
  153. }
  154. }
  155. function closeList () {
  156. isOpen.value = false
  157. isUserFiltering.value = false
  158. syncFromModelValue()
  159. }
  160. function emitValue (value) {
  161. emit('update:modelValue', value)
  162. }
  163. function selectChoice (choice) {
  164. emitValue(choice.value)
  165. query.value = choiceLabel(choice)
  166. isOpen.value = false
  167. }
  168. function handleFocus () {
  169. if (!isOpen.value) {
  170. syncFromModelValue()
  171. isUserFiltering.value = false
  172. }
  173. openList()
  174. }
  175. function handleSearchInput (event) {
  176. isUserFiltering.value = true
  177. query.value = event.target.value
  178. openList()
  179. highlightedIndex.value = 0
  180. }
  181. function moveHighlight (delta) {
  182. if (filteredChoices.value.length === 0) {
  183. return
  184. }
  185. const nextIndex = highlightedIndex.value + delta
  186. if (nextIndex < 0) {
  187. highlightedIndex.value = filteredChoices.value.length - 1
  188. return
  189. }
  190. if (nextIndex >= filteredChoices.value.length) {
  191. highlightedIndex.value = 0
  192. return
  193. }
  194. highlightedIndex.value = nextIndex
  195. }
  196. function handleKeydown (event) {
  197. if (event.key === 'ArrowDown') {
  198. event.preventDefault()
  199. const wasOpen = isOpen.value
  200. openList()
  201. if (wasOpen) {
  202. moveHighlight(1)
  203. }
  204. return
  205. }
  206. if (event.key === 'ArrowUp') {
  207. event.preventDefault()
  208. const wasOpen = isOpen.value
  209. openList()
  210. if (wasOpen) {
  211. moveHighlight(-1)
  212. } else if (filteredChoices.value.length > 0) {
  213. highlightedIndex.value = filteredChoices.value.length - 1
  214. }
  215. return
  216. }
  217. if (event.key === 'Enter') {
  218. if (!isOpen.value || filteredChoices.value.length === 0) {
  219. return
  220. }
  221. event.preventDefault()
  222. selectChoice(filteredChoices.value[highlightedIndex.value])
  223. return
  224. }
  225. if (event.key === 'Escape') {
  226. event.preventDefault()
  227. closeList()
  228. searchInputRef.value?.blur()
  229. }
  230. }
  231. function handleBlur () {
  232. closeList()
  233. }
  234. function handleCloseOthers (event) {
  235. if (event.detail.id !== props.id) {
  236. closeList()
  237. }
  238. }
  239. function handleOutsideMouseDown (event) {
  240. if (!isOpen.value || rootRef.value?.contains(event.target)) {
  241. return
  242. }
  243. closeList()
  244. }
  245. watch(isOpen, open => {
  246. if (open) {
  247. document.addEventListener('mousedown', handleOutsideMouseDown, true)
  248. return
  249. }
  250. document.removeEventListener('mousedown', handleOutsideMouseDown, true)
  251. })
  252. onMounted(() => {
  253. document.addEventListener(closeOthersEvent, handleCloseOthers)
  254. })
  255. onBeforeUnmount(() => {
  256. document.removeEventListener('mousedown', handleOutsideMouseDown, true)
  257. document.removeEventListener(closeOthersEvent, handleCloseOthers)
  258. })
  259. </script>
  260. <style scoped>
  261. .choice-combobox {
  262. position: relative;
  263. width: 100%;
  264. }
  265. .choice-combobox:focus-within {
  266. z-index: 11;
  267. }
  268. .choice-combobox-input {
  269. width: 100%;
  270. max-width: 100%;
  271. box-sizing: border-box;
  272. }
  273. .choice-combobox-list {
  274. position: absolute;
  275. z-index: 10;
  276. left: 0;
  277. right: 0;
  278. max-height: 12rem;
  279. overflow-y: auto;
  280. margin: 0.125rem 0 0;
  281. padding: 0;
  282. list-style: none;
  283. border: 1px solid var(--border-color, #ccc);
  284. border-radius: 0.25rem;
  285. background: var(--standout-bg-color, #fff);
  286. color: var(--text-color, inherit);
  287. box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  288. }
  289. .choice-combobox-list li {
  290. padding: 0.375rem 0.5rem;
  291. cursor: pointer;
  292. }
  293. .choice-combobox-list li.highlighted,
  294. .choice-combobox-list li:hover {
  295. background: var(--hover-background-color, #eef3ff);
  296. color: var(--hover-text-color, inherit);
  297. }
  298. .choice-combobox-list li.selected {
  299. font-weight: 600;
  300. }
  301. .choice-combobox-empty {
  302. padding: 0.375rem 0.5rem;
  303. color: var(--disabled-text-color, #666);
  304. font-size: 0.875rem;
  305. }
  306. @media (prefers-color-scheme: dark) {
  307. .choice-combobox-list,
  308. .choice-combobox-empty {
  309. background-color: #4e4e4e;
  310. color: #ddd;
  311. border-color: var(--border-color, #595959);
  312. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
  313. }
  314. .choice-combobox-list li.highlighted,
  315. .choice-combobox-list li:hover {
  316. background-color: var(--hover-background-color, #1d345c);
  317. color: #fff;
  318. }
  319. .choice-combobox-empty {
  320. color: var(--disabled-text-color, #999);
  321. }
  322. }
  323. </style>