1
0

combobox.spec.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. import { beVisible, beHidden, haveAttribute, haveClasses, notHaveClasses, haveText, contain, notContain, html, notBeVisible, notHaveAttribute, notExist, haveFocus, test, haveValue, haveLength} from '../../../utils'
  2. test('it works with x-model',
  3. [html`
  4. <div
  5. x-data="{
  6. query: '',
  7. selected: null,
  8. people: [
  9. { id: 1, name: 'Wade Cooper' },
  10. { id: 2, name: 'Arlene Mccoy' },
  11. { id: 3, name: 'Devon Webb' },
  12. { id: 4, name: 'Tom Cook' },
  13. { id: 5, name: 'Tanya Fox', disabled: true },
  14. { id: 6, name: 'Hellen Schmidt' },
  15. { id: 7, name: 'Caroline Schultz' },
  16. { id: 8, name: 'Mason Heaney' },
  17. { id: 9, name: 'Claudie Smitham' },
  18. { id: 10, name: 'Emil Schaefer' },
  19. ],
  20. get filteredPeople() {
  21. return this.query === ''
  22. ? this.people
  23. : this.people.filter((person) => {
  24. return person.name.toLowerCase().includes(this.query.toLowerCase())
  25. })
  26. },
  27. }"
  28. >
  29. <div x-combobox x-model="selected">
  30. <label x-combobox:label>Select person</label>
  31. <div>
  32. <div>
  33. <input
  34. x-combobox:input
  35. :display-value="person => person.name"
  36. @change="query = $event.target.value"
  37. placeholder="Search..."
  38. />
  39. <button x-combobox:button>Toggle</button>
  40. </div>
  41. <div x-combobox:options>
  42. <ul>
  43. <template
  44. x-for="person in filteredPeople"
  45. :key="person.id"
  46. hidden
  47. >
  48. <li
  49. x-combobox:option
  50. :option="person.id"
  51. :value="person"
  52. :disabled="person.disabled"
  53. x-text="person.name"
  54. >
  55. </li>
  56. </template>
  57. </ul>
  58. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  59. </div>
  60. </div>
  61. <article x-text="selected?.name"></article>
  62. </div>
  63. </div>
  64. `],
  65. ({ get }) => {
  66. get('ul').should(notBeVisible())
  67. get('button').click()
  68. get('ul').should(beVisible())
  69. get('button').click()
  70. get('ul').should(notBeVisible())
  71. get('button').click()
  72. get('[option="2"]').click()
  73. get('ul').should(notBeVisible())
  74. get('input').should(haveValue('Arlene Mccoy'))
  75. get('article').should(haveText('Arlene Mccoy'))
  76. get('button').click()
  77. get('ul').should(contain('Wade Cooper'))
  78. .should(contain('Arlene Mccoy'))
  79. .should(contain('Devon Webb'))
  80. get('[option="3"]').click()
  81. get('ul').should(notBeVisible())
  82. get('input').should(haveValue('Devon Webb'))
  83. get('article').should(haveText('Devon Webb'))
  84. get('button').click()
  85. get('ul').should(contain('Wade Cooper'))
  86. .should(contain('Arlene Mccoy'))
  87. .should(contain('Devon Webb'))
  88. get('[option="1"]').click()
  89. get('ul').should(notBeVisible())
  90. get('input').should(haveValue('Wade Cooper'))
  91. get('article').should(haveText('Wade Cooper'))
  92. },
  93. )
  94. test('it works with internal state',
  95. [html`
  96. <div
  97. x-data="{ people: [
  98. { id: 1, name: 'Wade Cooper' },
  99. { id: 2, name: 'Arlene Mccoy' },
  100. { id: 3, name: 'Devon Webb' },
  101. { id: 4, name: 'Tom Cook' },
  102. { id: 5, name: 'Tanya Fox', disabled: true },
  103. { id: 6, name: 'Hellen Schmidt' },
  104. { id: 7, name: 'Caroline Schultz' },
  105. { id: 8, name: 'Mason Heaney' },
  106. { id: 9, name: 'Claudie Smitham' },
  107. { id: 10, name: 'Emil Schaefer' },
  108. ]}"
  109. x-combobox
  110. >
  111. <label x-combobox:label>Assigned to</label>
  112. <input x-combobox:input :display-value="(person) => person.name" type="text">
  113. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  114. <ul x-combobox:options>
  115. <template x-for="person in people" :key="person.id">
  116. <li
  117. :option="person.id"
  118. x-combobox:option
  119. :value="person"
  120. :disabled="person.disabled"
  121. >
  122. <span x-text="person.name"></span>
  123. </li>
  124. </template>
  125. </ul>
  126. </div>
  127. `],
  128. ({ get }) => {
  129. get('ul').should(notBeVisible())
  130. get('button')
  131. .should(haveText('Select Person'))
  132. .click()
  133. get('ul').should(beVisible())
  134. get('button').click()
  135. get('ul').should(notBeVisible())
  136. get('button').click()
  137. get('[option="2"]').click()
  138. get('ul').should(notBeVisible())
  139. get('button').should(haveText('Arlene Mccoy'))
  140. get('input').should(haveValue('Arlene Mccoy'))
  141. },
  142. )
  143. test('$combobox/$comboboxOption',
  144. [html`
  145. <div
  146. x-data="{ people: [
  147. { id: 1, name: 'Wade Cooper' },
  148. { id: 2, name: 'Arlene Mccoy' },
  149. { id: 3, name: 'Devon Webb' },
  150. { id: 4, name: 'Tom Cook' },
  151. { id: 5, name: 'Tanya Fox', disabled: true },
  152. { id: 6, name: 'Hellen Schmidt' },
  153. { id: 7, name: 'Caroline Schultz' },
  154. { id: 8, name: 'Mason Heaney' },
  155. { id: 9, name: 'Claudie Smitham' },
  156. { id: 10, name: 'Emil Schaefer' },
  157. ]}"
  158. x-combobox
  159. >
  160. <label x-combobox:label>Assigned to</label>
  161. <input x-combobox:input :display-value="(person) => person.name" type="text">
  162. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  163. <p x-text="$combobox.activeIndex"></p>
  164. <article x-text="$combobox.activeOption?.name"></article>
  165. <ul x-combobox:options>
  166. <template x-for="person in people" :key="person.id">
  167. <li
  168. :option="person.id"
  169. x-combobox:option
  170. :value="person"
  171. :disabled="person.disabled"
  172. :class="{
  173. 'selected': $comboboxOption.isSelected,
  174. 'active': $comboboxOption.isActive,
  175. 'disabled': $comboboxOption.isDisabled,
  176. }"
  177. >
  178. <span x-text="person.name"></span>
  179. </li>
  180. </template>
  181. </ul>
  182. </div>
  183. `],
  184. ({ get }) => {
  185. get('article').should(haveText(''))
  186. get('[option="5"]').should(haveClasses(['disabled']))
  187. get('button')
  188. .should(haveText('Select Person'))
  189. .click()
  190. get('[option="1"]').should(haveClasses(['active']))
  191. get('input').type('{downarrow}')
  192. get('article').should(haveText('Arlene Mccoy'))
  193. get('p').should(haveText('1'))
  194. get('[option="2"]').should(haveClasses(['active']))
  195. get('button').should(haveText('Select Person'))
  196. get('[option="2"]').click()
  197. get('button').should(haveText('Arlene Mccoy'))
  198. get('[option="2"]').should(haveClasses(['selected']))
  199. },
  200. )
  201. test('"name" prop',
  202. [html`
  203. <div
  204. x-data="{ people: [
  205. { id: 1, name: 'Wade Cooper' },
  206. { id: 2, name: 'Arlene Mccoy' },
  207. { id: 3, name: 'Devon Webb' },
  208. { id: 4, name: 'Tom Cook' },
  209. { id: 5, name: 'Tanya Fox', disabled: true },
  210. { id: 6, name: 'Hellen Schmidt' },
  211. { id: 7, name: 'Caroline Schultz' },
  212. { id: 8, name: 'Mason Heaney' },
  213. { id: 9, name: 'Claudie Smitham' },
  214. { id: 10, name: 'Emil Schaefer' },
  215. ]}"
  216. x-combobox
  217. name="person"
  218. >
  219. <label x-combobox:label>Assigned to</label>
  220. <input x-combobox:input :display-value="(person) => person.name" type="text">
  221. <button x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  222. <ul x-combobox:options>
  223. <template x-for="person in people" :key="person.id">
  224. <li
  225. :option="person.id"
  226. x-combobox:option
  227. :value="person.id"
  228. :disabled="person.disabled"
  229. :class="{
  230. 'selected': $comboboxOption.isSelected,
  231. 'active': $comboboxOption.isActive,
  232. }"
  233. >
  234. <span x-text="person.name"></span>
  235. </li>
  236. </template>
  237. </ul>
  238. </div>
  239. `],
  240. ({ get }) => {
  241. get('input').should(haveAttribute('value', 'null'))
  242. get('button').click()
  243. get('input').should(haveAttribute('value', 'null'))
  244. get('[option="2"]').click()
  245. get('input').should(beHidden())
  246. .should(haveAttribute('name', 'person'))
  247. .should(haveAttribute('value', '2'))
  248. .should(haveAttribute('type', 'hidden'))
  249. get('button').click()
  250. get('[option="4"]').click()
  251. get('input').should(beHidden())
  252. .should(haveAttribute('name', 'person'))
  253. .should(haveAttribute('value', '4'))
  254. .should(haveAttribute('type', 'hidden'))
  255. },
  256. );
  257. test('Preserves currenty active keyboard selection while options change from searching even if there\'s a selected option in the filtered results',
  258. [html`
  259. <div
  260. x-data="{
  261. query: '',
  262. selected: null,
  263. people: [
  264. { id: 1, name: 'Wade Cooper' },
  265. { id: 2, name: 'Arlene Mccoy' },
  266. { id: 3, name: 'Devon Webb' },
  267. { id: 4, name: 'Tom Cook' },
  268. { id: 5, name: 'Tanya Fox', disabled: true },
  269. { id: 6, name: 'Hellen Schmidt' },
  270. { id: 7, name: 'Caroline Schultz' },
  271. { id: 8, name: 'Mason Heaney' },
  272. { id: 9, name: 'Claudie Smitham' },
  273. { id: 10, name: 'Emil Schaefer' },
  274. ],
  275. get filteredPeople() {
  276. return this.query === ''
  277. ? this.people
  278. : this.people.filter((person) => {
  279. return person.name.toLowerCase().includes(this.query.toLowerCase())
  280. })
  281. },
  282. }"
  283. >
  284. <div x-combobox x-model="selected">
  285. <label x-combobox:label>Select person</label>
  286. <div>
  287. <div>
  288. <input
  289. x-combobox:input
  290. :display-value="person => person.name"
  291. @change="query = $event.target.value"
  292. placeholder="Search..."
  293. />
  294. <button x-combobox:button>Toggle</button>
  295. </div>
  296. <div x-combobox:options>
  297. <ul>
  298. <template
  299. x-for="person in filteredPeople"
  300. :key="person.id"
  301. hidden
  302. >
  303. <li
  304. x-combobox:option
  305. :option="person.id"
  306. :value="person"
  307. :disabled="person.disabled"
  308. >
  309. <span x-text="person.name"></span>
  310. <span x-show="$comboboxOption.isActive">*</span>
  311. <span x-show="$comboboxOption.isSelected">x</span>
  312. </li>
  313. </template>
  314. </ul>
  315. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  316. </div>
  317. </div>
  318. </div>
  319. <article>lorem ipsum</article>
  320. </div>
  321. `],
  322. ({ get }) => {
  323. get('input').should(haveText(''))
  324. get('button').click()
  325. get('[option="3"]').click()
  326. cy.wait(100)
  327. get('input').type('{selectAll}{backspace}')
  328. cy.wait(100)
  329. get('input').type('{downArrow}')
  330. cy.wait(100)
  331. get('[option="3"]').should(contain('*'))
  332. get('input').type('{upArrow}{upArrow}')
  333. cy.wait(100)
  334. get('[option="1"]').should(contain('*'))
  335. cy.wait(100)
  336. get('input').type('d')
  337. get('input').trigger('change')
  338. cy.wait(100)
  339. get('[option="1"]').should(contain('*'))
  340. },
  341. );
  342. test('Ignore active selection while options change if not selected by a keyboard event',
  343. [html`
  344. <div
  345. x-data="{
  346. query: '',
  347. selected: null,
  348. people: [
  349. { id: 1, name: 'Wade Cooper' },
  350. { id: 2, name: 'Arlene Mccoy' },
  351. { id: 3, name: 'Devon Webb' },
  352. { id: 4, name: 'Tom Cook' },
  353. { id: 5, name: 'Tanya Fox', disabled: true },
  354. { id: 6, name: 'Hellen Schmidt' },
  355. { id: 7, name: 'Caroline Schultz' },
  356. { id: 8, name: 'Mason Heaney' },
  357. { id: 9, name: 'Claudie Smitham' },
  358. { id: 10, name: 'Emil Schaefer' },
  359. ],
  360. get filteredPeople() {
  361. return this.query === ''
  362. ? this.people
  363. : this.people.filter((person) => {
  364. return person.name.toLowerCase().includes(this.query.toLowerCase())
  365. })
  366. },
  367. }"
  368. >
  369. <div x-combobox x-model="selected">
  370. <label x-combobox:label>Select person</label>
  371. <div>
  372. <div>
  373. <input
  374. x-combobox:input
  375. :display-value="person => person.name"
  376. @change="query = $event.target.value"
  377. placeholder="Search..."
  378. />
  379. <button x-combobox:button>Toggle</button>
  380. </div>
  381. <div x-combobox:options>
  382. <ul>
  383. <template
  384. x-for="person in filteredPeople"
  385. :key="person.id"
  386. hidden
  387. >
  388. <li
  389. x-combobox:option
  390. :option="person.id"
  391. :value="person"
  392. :disabled="person.disabled"
  393. >
  394. <span x-text="person.name"></span>
  395. <span x-show="$comboboxOption.isActive">*</span>
  396. <span x-show="$comboboxOption.isSelected">x</span>
  397. </li>
  398. </template>
  399. </ul>
  400. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  401. </div>
  402. </div>
  403. </div>
  404. <article>lorem ipsum</article>
  405. </div>
  406. `],
  407. ({ get }) => {
  408. get('input').should(haveText(''))
  409. get('button').click()
  410. get('[option="1"]').should(contain('*'))
  411. get('input').type('t')
  412. get('input').trigger('change')
  413. get('[option="4"]').should(contain('*'))
  414. get('input').type('{backspace}')
  415. get('input').trigger('change')
  416. get('[option="1"]').should(contain('*'))
  417. },
  418. );
  419. test('"name" prop with object value',
  420. [html`
  421. <div
  422. x-data="{ people: [
  423. { id: 1, name: 'Wade Cooper' },
  424. { id: 2, name: 'Arlene Mccoy' },
  425. { id: 3, name: 'Devon Webb' },
  426. { id: 4, name: 'Tom Cook' },
  427. { id: 5, name: 'Tanya Fox', disabled: true },
  428. { id: 6, name: 'Hellen Schmidt' },
  429. { id: 7, name: 'Caroline Schultz' },
  430. { id: 8, name: 'Mason Heaney' },
  431. { id: 9, name: 'Claudie Smitham' },
  432. { id: 10, name: 'Emil Schaefer' },
  433. ]}"
  434. x-combobox
  435. name="person"
  436. >
  437. <label x-combobox:label>Assigned to</label>
  438. <input x-combobox:input :display-value="(person) => person.name" type="text">
  439. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select Person'"></button>
  440. <ul x-combobox:options>
  441. <template x-for="person in people" :key="person.id">
  442. <li
  443. :option="person.id"
  444. x-combobox:option
  445. :value="person"
  446. :disabled="person.disabled"
  447. :class="{
  448. 'selected': $comboboxOption.isSelected,
  449. 'active': $comboboxOption.isActive,
  450. }"
  451. >
  452. <span x-text="person.name"></span>
  453. </li>
  454. </template>
  455. </ul>
  456. </div>
  457. `],
  458. ({ get }) => {
  459. get('input[name="person"]').should(haveAttribute('value', 'null'))
  460. get('button').click()
  461. get('[name="person[id]"]').should(notExist())
  462. get('[option="2"]').click()
  463. get('input[name="person"]').should(notExist())
  464. get('[name="person[id]"]').should(beHidden())
  465. .should(haveAttribute('value', '2'))
  466. .should(haveAttribute('type', 'hidden'))
  467. get('[name="person[name]"]').should(beHidden())
  468. .should(haveAttribute('value', 'Arlene Mccoy'))
  469. .should(haveAttribute('type', 'hidden'))
  470. get('button').click()
  471. get('[option="4"]').click()
  472. get('[name="person[id]"]').should(beHidden())
  473. .should(haveAttribute('value', '4'))
  474. .should(haveAttribute('type', 'hidden'))
  475. get('[name="person[name]"]').should(beHidden())
  476. .should(haveAttribute('value', 'Tom Cook'))
  477. .should(haveAttribute('type', 'hidden'))
  478. },
  479. );
  480. test('"default-value" prop',
  481. [html`
  482. <div
  483. x-data="{ people: [
  484. { id: 1, name: 'Wade Cooper' },
  485. { id: 2, name: 'Arlene Mccoy' },
  486. { id: 3, name: 'Devon Webb' },
  487. { id: 4, name: 'Tom Cook' },
  488. { id: 5, name: 'Tanya Fox', disabled: true },
  489. { id: 6, name: 'Hellen Schmidt' },
  490. { id: 7, name: 'Caroline Schultz' },
  491. { id: 8, name: 'Mason Heaney' },
  492. { id: 9, name: 'Claudie Smitham' },
  493. { id: 10, name: 'Emil Schaefer' },
  494. ]}"
  495. x-combobox
  496. name="person"
  497. default-value="2"
  498. >
  499. <label x-combobox:label>Assigned to</label>
  500. <input x-combobox:input :display-value="(person) => person.name" type="text">
  501. <button x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  502. <ul x-combobox:options>
  503. <template x-for="person in people" :key="person.id">
  504. <li
  505. :option="person.id"
  506. x-combobox:option
  507. :value="person.id"
  508. :disabled="person.disabled"
  509. :class="{
  510. 'selected': $comboboxOption.isSelected,
  511. 'active': $comboboxOption.isActive,
  512. }"
  513. >
  514. <span x-text="person.name"></span>
  515. </li>
  516. </template>
  517. </ul>
  518. </div>
  519. `],
  520. ({ get }) => {
  521. get('input[name="person"]').should(beHidden())
  522. .should(haveAttribute('value', '2'))
  523. .should(haveAttribute('type', 'hidden'))
  524. },
  525. );
  526. test('"multiple" prop',
  527. [html`
  528. <div
  529. x-data="{
  530. people: [
  531. { id: 1, name: 'Wade Cooper' },
  532. { id: 2, name: 'Arlene Mccoy' },
  533. { id: 3, name: 'Devon Webb' },
  534. { id: 4, name: 'Tom Cook' },
  535. { id: 5, name: 'Tanya Fox', disabled: true },
  536. { id: 6, name: 'Hellen Schmidt' },
  537. { id: 7, name: 'Caroline Schultz' },
  538. { id: 8, name: 'Mason Heaney' },
  539. { id: 9, name: 'Claudie Smitham' },
  540. { id: 10, name: 'Emil Schaefer' },
  541. ]
  542. }"
  543. x-combobox
  544. multiple
  545. >
  546. <label x-combobox:label>Assigned to</label>
  547. <input x-combobox:input :display-value="(person) => person.name" type="text">
  548. <button x-combobox:button x-text="$combobox.value ? $combobox.value.join(',') : 'Select People'"></button>
  549. <ul x-combobox:options>
  550. <template x-for="person in people" :key="person.id">
  551. <li
  552. :option="person.id"
  553. x-combobox:option
  554. :value="person.id"
  555. :disabled="person.disabled"
  556. :class="{
  557. 'selected': $comboboxOption.isSelected,
  558. 'active': $comboboxOption.isActive,
  559. }"
  560. >
  561. <span x-text="person.name"></span>
  562. </li>
  563. </template>
  564. </ul>
  565. </div>
  566. `],
  567. ({ get }) => {
  568. get('button').click()
  569. get('[option="2"]').click()
  570. get('ul').should(beVisible())
  571. get('button').should(haveText('2'))
  572. get('[option="4"]').click()
  573. get('button').should(haveText('2,4'))
  574. get('ul').should(beVisible())
  575. get('[option="4"]').click()
  576. get('button').should(haveText('2'))
  577. get('ul').should(beVisible())
  578. get('input').type('Tom')
  579. get('input').type('{enter}')
  580. get('button').should(haveText('2,4'))
  581. // input field doesn't reset when a new option is selected
  582. get('input').should(haveValue('Tom'))
  583. },
  584. );
  585. test('"multiple" and "name" props together',
  586. [html`
  587. <div
  588. x-data="{
  589. people: [
  590. { id: 1, name: 'Wade Cooper' },
  591. { id: 2, name: 'Arlene Mccoy' },
  592. { id: 3, name: 'Devon Webb' },
  593. { id: 4, name: 'Tom Cook' },
  594. { id: 5, name: 'Tanya Fox', disabled: true },
  595. { id: 6, name: 'Hellen Schmidt' },
  596. { id: 7, name: 'Caroline Schultz' },
  597. { id: 8, name: 'Mason Heaney' },
  598. { id: 9, name: 'Claudie Smitham' },
  599. { id: 10, name: 'Emil Schaefer' },
  600. ]
  601. }"
  602. x-combobox
  603. multiple
  604. name="people"
  605. >
  606. <label x-combobox:label>Assigned to</label>
  607. <input x-combobox:input :display-value="(person) => person.name" type="text">
  608. <button x-combobox:button x-text="$combobox.value ? $combobox.value.map(p => p.id).join(',') : 'Select People'"></button>
  609. <ul x-combobox:options>
  610. <template x-for="person in people" :key="person.id">
  611. <li
  612. :option="person.id"
  613. x-combobox:option
  614. :value="person"
  615. :disabled="person.disabled"
  616. :class="{
  617. 'selected': $comboboxOption.isSelected,
  618. 'active': $comboboxOption.isActive,
  619. }"
  620. >
  621. <span x-text="person.name"></span>
  622. </li>
  623. </template>
  624. </ul>
  625. </div>
  626. `],
  627. ({ get }) => {
  628. // get('input[name="people"]').should(haveAttribute('value', 'null'))
  629. get('button').click()
  630. get('[name="people[0][id]"]').should(notExist())
  631. get('[option="2"]').click()
  632. get('ul').should(beVisible())
  633. get('button').should(haveText('2'))
  634. get('input[name="people"]').should(notExist())
  635. get('[name="people[0][id]"]').should(beHidden())
  636. .should(haveAttribute('value', '2'))
  637. .should(haveAttribute('type', 'hidden'))
  638. get('[name="people[0][name]"]').should(beHidden())
  639. .should(haveAttribute('value', 'Arlene Mccoy'))
  640. .should(haveAttribute('type', 'hidden'))
  641. get('[option="4"]').click()
  642. get('[name="people[0][id]"]').should(beHidden())
  643. .should(haveAttribute('value', '2'))
  644. .should(haveAttribute('type', 'hidden'))
  645. get('[name="people[0][name]"]').should(beHidden())
  646. .should(haveAttribute('value', 'Arlene Mccoy'))
  647. .should(haveAttribute('type', 'hidden'))
  648. get('[name="people[1][id]"]').should(beHidden())
  649. .should(haveAttribute('value', '4'))
  650. .should(haveAttribute('type', 'hidden'))
  651. get('[name="people[1][name]"]').should(beHidden())
  652. .should(haveAttribute('value', 'Tom Cook'))
  653. .should(haveAttribute('type', 'hidden'))
  654. get('button').should(haveText('2,4'))
  655. get('ul').should(beVisible())
  656. get('[option="4"]').click()
  657. get('[name="people[0][id]"]').should(beHidden())
  658. .should(haveAttribute('value', '2'))
  659. .should(haveAttribute('type', 'hidden'))
  660. get('[name="people[0][name]"]').should(beHidden())
  661. .should(haveAttribute('value', 'Arlene Mccoy'))
  662. .should(haveAttribute('type', 'hidden'))
  663. get('[name="people[1][id]"]').should(notExist())
  664. get('[name="people[1][name]"]').should(notExist())
  665. get('button').should(haveText('2'))
  666. get('ul').should(beVisible())
  667. },
  668. );
  669. test('"by" prop with string value',
  670. [html`
  671. <div
  672. x-data="{
  673. people: [
  674. { id: 1, name: 'Wade Cooper' },
  675. { id: 2, name: 'Arlene Mccoy' },
  676. { id: 3, name: 'Devon Webb' },
  677. { id: 4, name: 'Tom Cook' },
  678. { id: 5, name: 'Tanya Fox', disabled: true },
  679. { id: 6, name: 'Hellen Schmidt' },
  680. { id: 7, name: 'Caroline Schultz' },
  681. { id: 8, name: 'Mason Heaney' },
  682. { id: 9, name: 'Claudie Smitham' },
  683. { id: 10, name: 'Emil Schaefer' },
  684. ]
  685. }"
  686. x-combobox
  687. by="id"
  688. >
  689. <label x-combobox:label>Assigned to</label>
  690. <input x-combobox:input :display-value="(person) => person" type="text">
  691. <button x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select People'"></button>
  692. <ul x-combobox:options>
  693. <template x-for="person in people" :key="person.id">
  694. <li
  695. :option="person.id"
  696. x-combobox:option
  697. :value="person.id"
  698. :disabled="person.disabled"
  699. :class="{
  700. 'selected': $comboboxOption.isSelected,
  701. 'active': $comboboxOption.isActive,
  702. }"
  703. >
  704. <span x-text="person.name"></span>
  705. </li>
  706. </template>
  707. </ul>
  708. </div>
  709. `],
  710. ({ get }) => {
  711. get('ul').should(notBeVisible())
  712. get('button').click()
  713. get('ul').should(beVisible())
  714. get('button').click()
  715. get('ul').should(notBeVisible())
  716. get('button').click()
  717. get('[option="2"]').click()
  718. get('ul').should(notBeVisible())
  719. get('input').should(haveValue('2'))
  720. get('button').should(haveText('2'))
  721. get('button').click()
  722. get('ul').should(contain('Wade Cooper'))
  723. .should(contain('Arlene Mccoy'))
  724. .should(contain('Devon Webb'))
  725. get('[option="3"]').click()
  726. get('ul').should(notBeVisible())
  727. get('input').should(haveValue('3'))
  728. get('button').should(haveText('3'))
  729. get('button').click()
  730. get('ul').should(contain('Wade Cooper'))
  731. .should(contain('Arlene Mccoy'))
  732. .should(contain('Devon Webb'))
  733. get('[option="1"]').click()
  734. get('ul').should(notBeVisible())
  735. get('input').should(haveValue('1'))
  736. get('button').should(haveText('1'))
  737. },
  738. );
  739. test('keyboard controls',
  740. [html`
  741. <div
  742. x-data="{ active: null, people: [
  743. { id: 1, name: 'Wade Cooper' },
  744. { id: 2, name: 'Arlene Mccoy' },
  745. { id: 3, name: 'Devon Webb', disabled: true },
  746. { id: 4, name: 'Tom Cook' },
  747. { id: 5, name: 'Tanya Fox', disabled: true },
  748. { id: 6, name: 'Hellen Schmidt' },
  749. { id: 7, name: 'Caroline Schultz' },
  750. { id: 8, name: 'Mason Heaney' },
  751. { id: 9, name: 'Claudie Smitham' },
  752. { id: 10, name: 'Emil Schaefer' },
  753. ]}"
  754. x-combobox
  755. x-model="active"
  756. >
  757. <label x-combobox:label>Assigned to</label>
  758. <input x-combobox:input :display-value="(person) => person.name" type="text">
  759. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  760. <ul x-combobox:options options>
  761. <template x-for="person in people" :key="person.id">
  762. <li
  763. :option="person.id"
  764. x-combobox:option
  765. :value="person"
  766. :disabled="person.disabled"
  767. :class="{
  768. 'selected': $comboboxOption.isSelected,
  769. 'active': $comboboxOption.isActive,
  770. }"
  771. >
  772. <span x-text="person.name"></span>
  773. </li>
  774. </template>
  775. </ul>
  776. </div>
  777. `],
  778. ({ get }) => {
  779. get('.active').should(notExist())
  780. get('button').click()
  781. get('[options]')
  782. .should(beVisible())
  783. get('input').should(haveFocus())
  784. get('[option="1"]')
  785. .should(haveClasses(['active']))
  786. get('input')
  787. .type('{downarrow}')
  788. get('[option="2"]')
  789. .should(haveClasses(['active']))
  790. get('input')
  791. .type('{downarrow}')
  792. get('[option="4"]')
  793. .should(haveClasses(['active']))
  794. get('input')
  795. .type('{uparrow}')
  796. get('[option="2"]')
  797. .should(haveClasses(['active']))
  798. get('input')
  799. .type('{home}')
  800. get('[option="1"]')
  801. .should(haveClasses(['active']))
  802. get('input')
  803. .type('{end}')
  804. get('[option="10"]')
  805. .should(haveClasses(['active']))
  806. get('input')
  807. .type('{pageUp}')
  808. get('[option="1"]')
  809. .should(haveClasses(['active']))
  810. get('input')
  811. .type('{pageDown}')
  812. get('[option="10"]')
  813. .should(haveClasses(['active']))
  814. get('input')
  815. .tab()
  816. .should(haveFocus())
  817. get('[options]')
  818. .should(beVisible())
  819. get('input')
  820. .type('{esc}')
  821. get('[options]')
  822. .should(notBeVisible())
  823. },
  824. )
  825. test('changing value manually changes internal state',
  826. [html`
  827. <div
  828. x-data="{ active: null, people: [
  829. { id: 1, name: 'Wade Cooper' },
  830. { id: 2, name: 'Arlene Mccoy' },
  831. { id: 3, name: 'Devon Webb', disabled: true },
  832. { id: 4, name: 'Tom Cook' },
  833. { id: 5, name: 'Tanya Fox', disabled: true },
  834. { id: 6, name: 'Hellen Schmidt' },
  835. { id: 7, name: 'Caroline Schultz' },
  836. { id: 8, name: 'Mason Heaney' },
  837. { id: 9, name: 'Claudie Smitham' },
  838. { id: 10, name: 'Emil Schaefer' },
  839. ]}"
  840. x-combobox
  841. x-model="active"
  842. >
  843. <label x-combobox:label>Assigned to</label>
  844. <input x-combobox:input :display-value="(person) => person.name" type="text">
  845. <button toggle x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  846. <button select-tim @click="active = 4">Select Tim</button>
  847. <ul x-combobox:options options>
  848. <template x-for="person in people" :key="person.id">
  849. <li
  850. :option="person.id"
  851. x-combobox:option
  852. :value="person.id"
  853. :disabled="person.disabled"
  854. :class="{
  855. 'selected': $comboboxOption.isSelected,
  856. 'active': $comboboxOption.isActive,
  857. }"
  858. >
  859. <span x-text="person.name"></span>
  860. </li>
  861. </template>
  862. </ul>
  863. </div>
  864. `],
  865. ({ get }) => {
  866. get('[select-tim]').click()
  867. get('[option="4"]').should(haveClasses(['selected']))
  868. get('[option="1"]').should(notHaveClasses(['selected']))
  869. get('[toggle]').should(haveText('4'))
  870. },
  871. )
  872. test('has accessibility attributes',
  873. [html`
  874. <div
  875. x-data="{ active: null, people: [
  876. { id: 1, name: 'Wade Cooper' },
  877. { id: 2, name: 'Arlene Mccoy' },
  878. { id: 3, name: 'Devon Webb', disabled: true },
  879. { id: 4, name: 'Tom Cook' },
  880. { id: 5, name: 'Tanya Fox', disabled: true },
  881. { id: 6, name: 'Hellen Schmidt' },
  882. { id: 7, name: 'Caroline Schultz' },
  883. { id: 8, name: 'Mason Heaney' },
  884. { id: 9, name: 'Claudie Smitham' },
  885. { id: 10, name: 'Emil Schaefer' },
  886. ]}"
  887. x-combobox
  888. x-model="active"
  889. >
  890. <label x-combobox:label>Assigned to</label>
  891. <input x-combobox:input :display-value="(person) => person.name" type="text">
  892. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  893. <ul x-combobox:options options>
  894. <template x-for="person in people" :key="person.id">
  895. <li
  896. :option="person.id"
  897. x-combobox:option
  898. :value="person"
  899. :disabled="person.disabled"
  900. :class="{
  901. 'selected': $comboboxOption.isSelected,
  902. 'active': $comboboxOption.isActive,
  903. }"
  904. >
  905. <span x-text="person.name"></span>
  906. </li>
  907. </template>
  908. </ul>
  909. </div>
  910. `],
  911. ({ get }) => {
  912. get('input')
  913. .should(haveAttribute('aria-expanded', 'false'))
  914. get('button')
  915. .should(haveAttribute('aria-haspopup', 'true'))
  916. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1 alpine-combobox-button-1'))
  917. .should(haveAttribute('aria-expanded', 'false'))
  918. .should(notHaveAttribute('aria-controls'))
  919. .should(haveAttribute('id', 'alpine-combobox-button-1'))
  920. .should(haveAttribute('tabindex', '-1'))
  921. .click()
  922. .should(haveAttribute('aria-expanded', 'true'))
  923. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  924. get('[options]')
  925. .should(haveAttribute('role', 'listbox'))
  926. .should(haveAttribute('id', 'alpine-combobox-options-1'))
  927. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  928. get('[option="1"]')
  929. .should(haveAttribute('role', 'option'))
  930. .should(haveAttribute('id', 'alpine-combobox-option-1'))
  931. .should(haveAttribute('tabindex', '-1'))
  932. .should(haveAttribute('aria-selected', 'true'))
  933. get('[option="2"]')
  934. .should(haveAttribute('role', 'option'))
  935. .should(haveAttribute('id', 'alpine-combobox-option-2'))
  936. .should(haveAttribute('tabindex', '-1'))
  937. .should(notHaveAttribute('aria-selected'))
  938. get('input')
  939. .should(haveAttribute('role', 'combobox'))
  940. .should(haveAttribute('aria-autocomplete', 'list'))
  941. .should(haveAttribute('tabindex', '0'))
  942. .should(haveAttribute('aria-expanded', 'true'))
  943. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  944. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  945. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-1'))
  946. .type('{downarrow}')
  947. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-2'))
  948. get('[option="2"]')
  949. .should(haveAttribute('aria-selected', 'true'))
  950. },
  951. )
  952. test('"static" prop',
  953. [html`
  954. <div
  955. x-data="{ active: null, show: false, people: [
  956. { id: 1, name: 'Wade Cooper' },
  957. { id: 2, name: 'Arlene Mccoy' },
  958. { id: 3, name: 'Devon Webb' },
  959. { id: 4, name: 'Tom Cook' },
  960. { id: 5, name: 'Tanya Fox', disabled: true },
  961. { id: 6, name: 'Hellen Schmidt' },
  962. { id: 7, name: 'Caroline Schultz' },
  963. { id: 8, name: 'Mason Heaney' },
  964. { id: 9, name: 'Claudie Smitham' },
  965. { id: 10, name: 'Emil Schaefer' },
  966. ]}"
  967. x-combobox
  968. x-model="active"
  969. >
  970. <label x-combobox:label>Assigned to</label>
  971. <input x-combobox:input :display-value="(person) => person.name" type="text">
  972. <button normal-toggle x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  973. <button real-toggle @click="show = ! show">Toggle</button>
  974. <ul x-combobox:options x-show="show" static>
  975. <template x-for="person in people" :key="person.id">
  976. <li
  977. :option="person.id"
  978. x-combobox:option
  979. :value="person"
  980. :disabled="person.disabled"
  981. >
  982. <span x-text="person.name"></span>
  983. </li>
  984. </template>
  985. </ul>
  986. </div>
  987. `],
  988. ({ get }) => {
  989. get('ul').should(notBeVisible())
  990. get('[normal-toggle]')
  991. .should(haveText('Select Person'))
  992. .click()
  993. get('ul').should(notBeVisible())
  994. get('[real-toggle]').click()
  995. get('ul').should(beVisible())
  996. get('[option="2"]').click()
  997. get('ul').should(beVisible())
  998. get('[normal-toggle]').should(haveText('Arlene Mccoy'))
  999. },
  1000. )
  1001. test('input reset',
  1002. [html`
  1003. <div
  1004. x-data="{
  1005. query: '',
  1006. selected: null,
  1007. people: [
  1008. { id: 1, name: 'Wade Cooper' },
  1009. { id: 2, name: 'Arlene Mccoy' },
  1010. { id: 3, name: 'Devon Webb' },
  1011. { id: 4, name: 'Tom Cook' },
  1012. { id: 5, name: 'Tanya Fox', disabled: true },
  1013. { id: 6, name: 'Hellen Schmidt' },
  1014. { id: 7, name: 'Caroline Schultz' },
  1015. { id: 8, name: 'Mason Heaney' },
  1016. { id: 9, name: 'Claudie Smitham' },
  1017. { id: 10, name: 'Emil Schaefer' },
  1018. ],
  1019. get filteredPeople() {
  1020. return this.query === ''
  1021. ? this.people
  1022. : this.people.filter((person) => {
  1023. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1024. })
  1025. },
  1026. }"
  1027. >
  1028. <div x-combobox x-model="selected">
  1029. <label x-combobox:label>Select person</label>
  1030. <div>
  1031. <div>
  1032. <input
  1033. x-combobox:input
  1034. :display-value="person => person.name"
  1035. @change="query = $event.target.value"
  1036. placeholder="Search..."
  1037. />
  1038. <button x-combobox:button>Toggle</button>
  1039. </div>
  1040. <div x-combobox:options>
  1041. <ul>
  1042. <template
  1043. x-for="person in filteredPeople"
  1044. :key="person.id"
  1045. hidden
  1046. >
  1047. <li
  1048. x-combobox:option
  1049. :option="person.id"
  1050. :value="person"
  1051. :disabled="person.disabled"
  1052. x-text="person.name"
  1053. >
  1054. </li>
  1055. </template>
  1056. </ul>
  1057. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1058. </div>
  1059. </div>
  1060. </div>
  1061. <article>lorem ipsum</article>
  1062. </div>
  1063. `],
  1064. ({ get }) => {
  1065. // Test after closing with button
  1066. get('button').click()
  1067. get('input').type('w')
  1068. get('button').click()
  1069. get('input').should(haveValue(''))
  1070. // Test correct state after closing with ESC
  1071. get('button').click()
  1072. get('input').type('w')
  1073. get('input').type('{esc}')
  1074. get('input').should(haveValue(''))
  1075. // Test correct state after closing with TAB
  1076. get('button').click()
  1077. get('input').type('w')
  1078. get('input').tab()
  1079. get('input').should(haveValue(''))
  1080. // Test correct state after closing with external click
  1081. get('button').click()
  1082. get('input').type('w')
  1083. get('article').click()
  1084. get('input').should(haveValue(''))
  1085. // Select something
  1086. get('button').click()
  1087. get('ul').should(beVisible())
  1088. get('[option="2"]').click()
  1089. get('input').should(haveValue('Arlene Mccoy'))
  1090. // Test after closing with button
  1091. get('button').click()
  1092. get('input').type('w')
  1093. get('button').click()
  1094. get('input').should(haveValue('Arlene Mccoy'))
  1095. // Test correct state after closing with ESC and reopening
  1096. get('button').click()
  1097. get('input').type('w')
  1098. get('input').type('{esc}')
  1099. get('input').should(haveValue('Arlene Mccoy'))
  1100. // Test correct state after closing with TAB and reopening
  1101. get('button').click()
  1102. get('input').type('w')
  1103. get('input').tab()
  1104. get('input').should(haveValue('Arlene Mccoy'))
  1105. // Test correct state after closing with external click and reopening
  1106. get('button').click()
  1107. get('input').type('w')
  1108. get('article').click()
  1109. get('input').should(haveValue('Arlene Mccoy'))
  1110. },
  1111. )
  1112. test('combobox shows all options when opening',
  1113. [html`
  1114. <div
  1115. x-data="{
  1116. query: '',
  1117. selected: null,
  1118. people: [
  1119. { id: 1, name: 'Wade Cooper' },
  1120. { id: 2, name: 'Arlene Mccoy' },
  1121. { id: 3, name: 'Devon Webb' },
  1122. { id: 4, name: 'Tom Cook' },
  1123. { id: 5, name: 'Tanya Fox', disabled: true },
  1124. { id: 6, name: 'Hellen Schmidt' },
  1125. { id: 7, name: 'Caroline Schultz' },
  1126. { id: 8, name: 'Mason Heaney' },
  1127. { id: 9, name: 'Claudie Smitham' },
  1128. { id: 10, name: 'Emil Schaefer' },
  1129. ],
  1130. get filteredPeople() {
  1131. return this.query === ''
  1132. ? this.people
  1133. : this.people.filter((person) => {
  1134. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1135. })
  1136. },
  1137. }"
  1138. >
  1139. <div x-combobox x-model="selected">
  1140. <label x-combobox:label>Select person</label>
  1141. <div>
  1142. <div>
  1143. <input
  1144. x-combobox:input
  1145. :display-value="person => person.name"
  1146. @change="query = $event.target.value"
  1147. placeholder="Search..."
  1148. />
  1149. <button x-combobox:button>Toggle</button>
  1150. </div>
  1151. <div x-combobox:options>
  1152. <ul>
  1153. <template
  1154. x-for="person in filteredPeople"
  1155. :key="person.id"
  1156. hidden
  1157. >
  1158. <li
  1159. x-combobox:option
  1160. :option="person.id"
  1161. :value="person"
  1162. :disabled="person.disabled"
  1163. x-text="person.name"
  1164. >
  1165. </li>
  1166. </template>
  1167. </ul>
  1168. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1169. </div>
  1170. </div>
  1171. </div>
  1172. <article>lorem ipsum</article>
  1173. </div>
  1174. `],
  1175. ({ get }) => {
  1176. get('button').click()
  1177. get('li').should(haveLength('10'))
  1178. // Test after closing with button and reopening
  1179. get('input').type('w').trigger('input')
  1180. get('li').should(haveLength('2'))
  1181. get('button').click()
  1182. get('button').click()
  1183. get('li').should(haveLength('10'))
  1184. // Test correct state after closing with ESC and reopening
  1185. get('input').type('w').trigger('input')
  1186. get('li').should(haveLength('2'))
  1187. get('input').type('{esc}')
  1188. get('button').click()
  1189. get('li').should(haveLength('10'))
  1190. // Test correct state after closing with TAB and reopening
  1191. get('input').type('w').trigger('input')
  1192. get('li').should(haveLength('2'))
  1193. get('input').tab()
  1194. get('button').click()
  1195. get('li').should(haveLength('10'))
  1196. // Test correct state after closing with external click and reopening
  1197. get('input').type('w').trigger('input')
  1198. get('li').should(haveLength('2'))
  1199. get('article').click()
  1200. get('button').click()
  1201. get('li').should(haveLength('10'))
  1202. },
  1203. )
  1204. test('active element logic when opening a combobox',
  1205. [html`
  1206. <div
  1207. x-data="{
  1208. query: '',
  1209. selected: null,
  1210. people: [
  1211. { id: 1, name: 'Wade Cooper' },
  1212. { id: 2, name: 'Arlene Mccoy' },
  1213. { id: 3, name: 'Devon Webb' },
  1214. { id: 4, name: 'Tom Cook' },
  1215. { id: 5, name: 'Tanya Fox', disabled: true },
  1216. { id: 6, name: 'Hellen Schmidt' },
  1217. { id: 7, name: 'Caroline Schultz' },
  1218. { id: 8, name: 'Mason Heaney' },
  1219. { id: 9, name: 'Claudie Smitham' },
  1220. { id: 10, name: 'Emil Schaefer' },
  1221. ],
  1222. get filteredPeople() {
  1223. return this.query === ''
  1224. ? this.people
  1225. : this.people.filter((person) => {
  1226. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1227. })
  1228. },
  1229. }"
  1230. >
  1231. <div x-combobox x-model="selected">
  1232. <label x-combobox:label>Select person</label>
  1233. <div>
  1234. <div>
  1235. <input
  1236. x-combobox:input
  1237. :display-value="person => person.name"
  1238. @change="query = $event.target.value"
  1239. placeholder="Search..."
  1240. />
  1241. <button x-combobox:button>Toggle</button>
  1242. </div>
  1243. <div x-combobox:options>
  1244. <ul>
  1245. <template
  1246. x-for="person in filteredPeople"
  1247. :key="person.id"
  1248. hidden
  1249. >
  1250. <li
  1251. x-combobox:option
  1252. :option="person.id"
  1253. :value="person"
  1254. :disabled="person.disabled"
  1255. x-text="person.name"
  1256. >
  1257. </li>
  1258. </template>
  1259. </ul>
  1260. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1261. </div>
  1262. </div>
  1263. </div>
  1264. </div>
  1265. `],
  1266. ({ get }) => {
  1267. get('button').click()
  1268. // First option is selected on opening if no preselection
  1269. get('ul').should(beVisible())
  1270. get('[option="1"]').should(haveAttribute('aria-selected', 'true'))
  1271. // First match is selected while typing
  1272. get('[option="4"]').should(notHaveAttribute('aria-selected'))
  1273. get('input').type('T')
  1274. get('input').trigger('change')
  1275. get('[option="4"]').should(haveAttribute('aria-selected', 'true'))
  1276. // Reset state and select option 3
  1277. get('button').click()
  1278. get('button').click()
  1279. get('[option="3"]').click()
  1280. // Previous selection is selected
  1281. get('button').click()
  1282. get('[option="4"]').should(notHaveAttribute('aria-selected'))
  1283. get('[option="3"]').should(haveAttribute('aria-selected', 'true'))
  1284. }
  1285. )