combobox.spec.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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 currently 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('"by" prop with string value and "nullable"',
  740. [html`
  741. <div
  742. x-data="{
  743. people: [
  744. { id: 1, name: 'Wade Cooper' },
  745. { id: 2, name: 'Arlene Mccoy' },
  746. { id: 3, name: 'Devon Webb' },
  747. { id: 4, name: 'Tom Cook' },
  748. { id: 5, name: 'Tanya Fox', disabled: true },
  749. { id: 6, name: 'Hellen Schmidt' },
  750. { id: 7, name: 'Caroline Schultz' },
  751. { id: 8, name: 'Mason Heaney' },
  752. { id: 9, name: 'Claudie Smitham' },
  753. { id: 10, name: 'Emil Schaefer' },
  754. ]
  755. }"
  756. x-combobox
  757. by="id"
  758. default-value="5"
  759. nullable
  760. >
  761. <label x-combobox:label>Assigned to</label>
  762. <input x-combobox:input :display-value="(person) => person?.name" type="text">
  763. <button x-combobox:button x-text="$combobox.value ? $combobox.value.name : 'Select People'"></button>
  764. <ul x-combobox:options>
  765. <template x-for="person in people" :key="person.id">
  766. <li
  767. :option="person.id"
  768. x-combobox:option
  769. :value="person"
  770. :disabled="person.disabled"
  771. :class="{
  772. 'selected': $comboboxOption.isSelected,
  773. 'active': $comboboxOption.isActive,
  774. }"
  775. >
  776. <span x-text="person.name"></span>
  777. </li>
  778. </template>
  779. </ul>
  780. </div>
  781. `],
  782. ({ get }) => {
  783. get('ul').should(notBeVisible())
  784. get('button').click()
  785. get('ul').should(beVisible())
  786. get('button').click()
  787. get('ul').should(notBeVisible())
  788. get('button').click()
  789. get('[option="2"]').click()
  790. get('ul').should(notBeVisible())
  791. get('input').should(haveValue('Arlene Mccoy'))
  792. get('button').should(haveText('Arlene Mccoy'))
  793. get('button').click()
  794. get('ul').should(contain('Wade Cooper'))
  795. .should(contain('Arlene Mccoy'))
  796. .should(contain('Devon Webb'))
  797. get('[option="3"]').click()
  798. get('ul').should(notBeVisible())
  799. get('input').should(haveValue('Devon Webb'))
  800. get('button').should(haveText('Devon Webb'))
  801. get('button').click()
  802. get('ul').should(contain('Wade Cooper'))
  803. .should(contain('Arlene Mccoy'))
  804. .should(contain('Devon Webb'))
  805. get('[option="1"]').click()
  806. get('ul').should(notBeVisible())
  807. get('input').should(haveValue('Wade Cooper'))
  808. get('button').should(haveText('Wade Cooper'))
  809. },
  810. );
  811. test('keyboard controls',
  812. [html`
  813. <div
  814. x-data="{ active: null, people: [
  815. { id: 1, name: 'Wade Cooper' },
  816. { id: 2, name: 'Arlene Mccoy' },
  817. { id: 3, name: 'Devon Webb', disabled: true },
  818. { id: 4, name: 'Tom Cook' },
  819. { id: 5, name: 'Tanya Fox', disabled: true },
  820. { id: 6, name: 'Hellen Schmidt' },
  821. { id: 7, name: 'Caroline Schultz' },
  822. { id: 8, name: 'Mason Heaney' },
  823. { id: 9, name: 'Claudie Smitham' },
  824. { id: 10, name: 'Emil Schaefer' },
  825. ]}"
  826. x-combobox
  827. x-model="active"
  828. >
  829. <label x-combobox:label>Assigned to</label>
  830. <input x-combobox:input :display-value="(person) => person.name" type="text">
  831. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  832. <ul x-combobox:options options>
  833. <template x-for="person in people" :key="person.id">
  834. <li
  835. :option="person.id"
  836. x-combobox:option
  837. :value="person"
  838. :disabled="person.disabled"
  839. :class="{
  840. 'selected': $comboboxOption.isSelected,
  841. 'active': $comboboxOption.isActive,
  842. }"
  843. >
  844. <span x-text="person.name"></span>
  845. </li>
  846. </template>
  847. </ul>
  848. </div>
  849. `],
  850. ({ get }) => {
  851. get('.active').should(notExist())
  852. get('button').click()
  853. get('[options]')
  854. .should(beVisible())
  855. get('input').should(haveFocus())
  856. get('[option="1"]')
  857. .should(haveClasses(['active']))
  858. get('input')
  859. .type('{downarrow}')
  860. get('[option="2"]')
  861. .should(haveClasses(['active']))
  862. get('input')
  863. .type('{downarrow}')
  864. get('[option="4"]')
  865. .should(haveClasses(['active']))
  866. get('input')
  867. .type('{uparrow}')
  868. get('[option="2"]')
  869. .should(haveClasses(['active']))
  870. get('input')
  871. .type('{home}')
  872. get('[option="1"]')
  873. .should(haveClasses(['active']))
  874. get('input')
  875. .type('{end}')
  876. get('[option="10"]')
  877. .should(haveClasses(['active']))
  878. get('input')
  879. .type('{pageUp}')
  880. get('[option="1"]')
  881. .should(haveClasses(['active']))
  882. get('input')
  883. .type('{pageDown}')
  884. get('[option="10"]')
  885. .should(haveClasses(['active']))
  886. get('input')
  887. .tab()
  888. .should(haveFocus())
  889. get('[options]')
  890. .should(beVisible())
  891. get('input')
  892. .type('{esc}')
  893. get('[options]')
  894. .should(notBeVisible())
  895. },
  896. )
  897. test('changing value manually changes internal state',
  898. [html`
  899. <div
  900. x-data="{ active: null, people: [
  901. { id: 1, name: 'Wade Cooper' },
  902. { id: 2, name: 'Arlene Mccoy' },
  903. { id: 3, name: 'Devon Webb', disabled: true },
  904. { id: 4, name: 'Tom Cook' },
  905. { id: 5, name: 'Tanya Fox', disabled: true },
  906. { id: 6, name: 'Hellen Schmidt' },
  907. { id: 7, name: 'Caroline Schultz' },
  908. { id: 8, name: 'Mason Heaney' },
  909. { id: 9, name: 'Claudie Smitham' },
  910. { id: 10, name: 'Emil Schaefer' },
  911. ]}"
  912. x-combobox
  913. x-model="active"
  914. >
  915. <label x-combobox:label>Assigned to</label>
  916. <input x-combobox:input :display-value="(person) => person.name" type="text">
  917. <button toggle x-combobox:button x-text="$combobox.value ? $combobox.value : 'Select Person'"></button>
  918. <button select-tim @click="active = 4">Select Tim</button>
  919. <ul x-combobox:options options>
  920. <template x-for="person in people" :key="person.id">
  921. <li
  922. :option="person.id"
  923. x-combobox:option
  924. :value="person.id"
  925. :disabled="person.disabled"
  926. :class="{
  927. 'selected': $comboboxOption.isSelected,
  928. 'active': $comboboxOption.isActive,
  929. }"
  930. >
  931. <span x-text="person.name"></span>
  932. </li>
  933. </template>
  934. </ul>
  935. </div>
  936. `],
  937. ({ get }) => {
  938. get('[select-tim]').click()
  939. get('[option="4"]').should(haveClasses(['selected']))
  940. get('[option="1"]').should(notHaveClasses(['selected']))
  941. get('[toggle]').should(haveText('4'))
  942. },
  943. )
  944. test('has accessibility attributes',
  945. [html`
  946. <div
  947. x-data="{ active: null, people: [
  948. { id: 1, name: 'Wade Cooper' },
  949. { id: 2, name: 'Arlene Mccoy' },
  950. { id: 3, name: 'Devon Webb', disabled: true },
  951. { id: 4, name: 'Tom Cook' },
  952. { id: 5, name: 'Tanya Fox', disabled: true },
  953. { id: 6, name: 'Hellen Schmidt' },
  954. { id: 7, name: 'Caroline Schultz' },
  955. { id: 8, name: 'Mason Heaney' },
  956. { id: 9, name: 'Claudie Smitham' },
  957. { id: 10, name: 'Emil Schaefer' },
  958. ]}"
  959. x-combobox
  960. x-model="active"
  961. >
  962. <label x-combobox:label>Assigned to</label>
  963. <input x-combobox:input :display-value="(person) => person.name" type="text">
  964. <button x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  965. <ul x-combobox:options options>
  966. <template x-for="person in people" :key="person.id">
  967. <li
  968. :option="person.id"
  969. x-combobox:option
  970. :value="person"
  971. :disabled="person.disabled"
  972. :class="{
  973. 'selected': $comboboxOption.isSelected,
  974. 'active': $comboboxOption.isActive,
  975. }"
  976. >
  977. <span x-text="person.name"></span>
  978. </li>
  979. </template>
  980. </ul>
  981. </div>
  982. `],
  983. ({ get }) => {
  984. get('input')
  985. .should(haveAttribute('aria-expanded', 'false'))
  986. get('button')
  987. .should(haveAttribute('aria-haspopup', 'true'))
  988. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1 alpine-combobox-button-1'))
  989. .should(haveAttribute('aria-expanded', 'false'))
  990. .should(notHaveAttribute('aria-controls'))
  991. .should(haveAttribute('id', 'alpine-combobox-button-1'))
  992. .should(haveAttribute('tabindex', '-1'))
  993. .click()
  994. .should(haveAttribute('aria-expanded', 'true'))
  995. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  996. get('[options]')
  997. .should(haveAttribute('role', 'listbox'))
  998. .should(haveAttribute('id', 'alpine-combobox-options-1'))
  999. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  1000. get('[option="1"]')
  1001. .should(haveAttribute('role', 'option'))
  1002. .should(haveAttribute('id', 'alpine-combobox-option-1'))
  1003. .should(haveAttribute('tabindex', '-1'))
  1004. .should(haveAttribute('aria-selected', 'true'))
  1005. get('[option="2"]')
  1006. .should(haveAttribute('role', 'option'))
  1007. .should(haveAttribute('id', 'alpine-combobox-option-2'))
  1008. .should(haveAttribute('tabindex', '-1'))
  1009. .should(notHaveAttribute('aria-selected'))
  1010. get('input')
  1011. .should(haveAttribute('role', 'combobox'))
  1012. .should(haveAttribute('aria-autocomplete', 'list'))
  1013. .should(haveAttribute('tabindex', '0'))
  1014. .should(haveAttribute('aria-expanded', 'true'))
  1015. .should(haveAttribute('aria-labelledby', 'alpine-combobox-label-1'))
  1016. .should(haveAttribute('aria-controls', 'alpine-combobox-options-1'))
  1017. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-1'))
  1018. .type('{downarrow}')
  1019. .should(haveAttribute('aria-activedescendant', 'alpine-combobox-option-2'))
  1020. get('[option="2"]')
  1021. .should(haveAttribute('aria-selected', 'true'))
  1022. },
  1023. )
  1024. test('"static" prop',
  1025. [html`
  1026. <div
  1027. x-data="{ active: null, show: false, people: [
  1028. { id: 1, name: 'Wade Cooper' },
  1029. { id: 2, name: 'Arlene Mccoy' },
  1030. { id: 3, name: 'Devon Webb' },
  1031. { id: 4, name: 'Tom Cook' },
  1032. { id: 5, name: 'Tanya Fox', disabled: true },
  1033. { id: 6, name: 'Hellen Schmidt' },
  1034. { id: 7, name: 'Caroline Schultz' },
  1035. { id: 8, name: 'Mason Heaney' },
  1036. { id: 9, name: 'Claudie Smitham' },
  1037. { id: 10, name: 'Emil Schaefer' },
  1038. ]}"
  1039. x-combobox
  1040. x-model="active"
  1041. >
  1042. <label x-combobox:label>Assigned to</label>
  1043. <input x-combobox:input :display-value="(person) => person.name" type="text">
  1044. <button normal-toggle x-combobox:button x-text="active ? active.name : 'Select Person'"></button>
  1045. <button real-toggle @click="show = ! show">Toggle</button>
  1046. <ul x-combobox:options x-show="show" static>
  1047. <template x-for="person in people" :key="person.id">
  1048. <li
  1049. :option="person.id"
  1050. x-combobox:option
  1051. :value="person"
  1052. :disabled="person.disabled"
  1053. >
  1054. <span x-text="person.name"></span>
  1055. </li>
  1056. </template>
  1057. </ul>
  1058. </div>
  1059. `],
  1060. ({ get }) => {
  1061. get('ul').should(notBeVisible())
  1062. get('[normal-toggle]')
  1063. .should(haveText('Select Person'))
  1064. .click()
  1065. get('ul').should(notBeVisible())
  1066. get('[real-toggle]').click()
  1067. get('ul').should(beVisible())
  1068. get('[option="2"]').click()
  1069. get('ul').should(beVisible())
  1070. get('[normal-toggle]').should(haveText('Arlene Mccoy'))
  1071. },
  1072. )
  1073. test('input reset',
  1074. [html`
  1075. <div
  1076. x-data="{
  1077. query: '',
  1078. selected: null,
  1079. people: [
  1080. { id: 1, name: 'Wade Cooper' },
  1081. { id: 2, name: 'Arlene Mccoy' },
  1082. { id: 3, name: 'Devon Webb' },
  1083. { id: 4, name: 'Tom Cook' },
  1084. { id: 5, name: 'Tanya Fox', disabled: true },
  1085. { id: 6, name: 'Hellen Schmidt' },
  1086. { id: 7, name: 'Caroline Schultz' },
  1087. { id: 8, name: 'Mason Heaney' },
  1088. { id: 9, name: 'Claudie Smitham' },
  1089. { id: 10, name: 'Emil Schaefer' },
  1090. ],
  1091. get filteredPeople() {
  1092. return this.query === ''
  1093. ? this.people
  1094. : this.people.filter((person) => {
  1095. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1096. })
  1097. },
  1098. }"
  1099. >
  1100. <div x-combobox x-model="selected">
  1101. <label x-combobox:label>Select person</label>
  1102. <div>
  1103. <div>
  1104. <input
  1105. x-combobox:input
  1106. :display-value="person => person.name"
  1107. @change="query = $event.target.value"
  1108. placeholder="Search..."
  1109. />
  1110. <button x-combobox:button>Toggle</button>
  1111. </div>
  1112. <div x-combobox:options>
  1113. <ul>
  1114. <template
  1115. x-for="person in filteredPeople"
  1116. :key="person.id"
  1117. hidden
  1118. >
  1119. <li
  1120. x-combobox:option
  1121. :option="person.id"
  1122. :value="person"
  1123. :disabled="person.disabled"
  1124. x-text="person.name"
  1125. >
  1126. </li>
  1127. </template>
  1128. </ul>
  1129. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1130. </div>
  1131. </div>
  1132. </div>
  1133. <article>lorem ipsum</article>
  1134. </div>
  1135. `],
  1136. ({ get }) => {
  1137. // Test after closing with button
  1138. get('button').click()
  1139. get('input').type('w')
  1140. get('button').click()
  1141. get('input').should(haveValue(''))
  1142. // Test correct state after closing with ESC
  1143. get('button').click()
  1144. get('input').type('w')
  1145. get('input').type('{esc}')
  1146. get('input').should(haveValue(''))
  1147. // Test correct state after closing with TAB
  1148. get('button').click()
  1149. get('input').type('w')
  1150. get('input').tab()
  1151. get('input').should(haveValue(''))
  1152. // Test correct state after closing with external click
  1153. get('button').click()
  1154. get('input').type('w')
  1155. get('article').click()
  1156. get('input').should(haveValue(''))
  1157. // Select something
  1158. get('button').click()
  1159. get('ul').should(beVisible())
  1160. get('[option="2"]').click()
  1161. get('input').should(haveValue('Arlene Mccoy'))
  1162. // Test after closing with button
  1163. get('button').click()
  1164. get('input').type('w')
  1165. get('button').click()
  1166. get('input').should(haveValue('Arlene Mccoy'))
  1167. // Test correct state after closing with ESC and reopening
  1168. get('button').click()
  1169. get('input').type('w')
  1170. get('input').type('{esc}')
  1171. get('input').should(haveValue('Arlene Mccoy'))
  1172. // Test correct state after closing with TAB and reopening
  1173. get('button').click()
  1174. get('input').type('w')
  1175. get('input').tab()
  1176. get('input').should(haveValue('Arlene Mccoy'))
  1177. // Test correct state after closing with external click and reopening
  1178. get('button').click()
  1179. get('input').type('w')
  1180. get('article').click()
  1181. get('input').should(haveValue('Arlene Mccoy'))
  1182. },
  1183. )
  1184. test('combobox shows all options when opening',
  1185. [html`
  1186. <div
  1187. x-data="{
  1188. query: '',
  1189. selected: null,
  1190. people: [
  1191. { id: 1, name: 'Wade Cooper' },
  1192. { id: 2, name: 'Arlene Mccoy' },
  1193. { id: 3, name: 'Devon Webb' },
  1194. { id: 4, name: 'Tom Cook' },
  1195. { id: 5, name: 'Tanya Fox', disabled: true },
  1196. { id: 6, name: 'Hellen Schmidt' },
  1197. { id: 7, name: 'Caroline Schultz' },
  1198. { id: 8, name: 'Mason Heaney' },
  1199. { id: 9, name: 'Claudie Smitham' },
  1200. { id: 10, name: 'Emil Schaefer' },
  1201. ],
  1202. get filteredPeople() {
  1203. return this.query === ''
  1204. ? this.people
  1205. : this.people.filter((person) => {
  1206. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1207. })
  1208. },
  1209. }"
  1210. >
  1211. <div x-combobox x-model="selected">
  1212. <label x-combobox:label>Select person</label>
  1213. <div>
  1214. <div>
  1215. <input
  1216. x-combobox:input
  1217. :display-value="person => person.name"
  1218. @change="query = $event.target.value"
  1219. placeholder="Search..."
  1220. />
  1221. <button x-combobox:button>Toggle</button>
  1222. </div>
  1223. <div x-combobox:options>
  1224. <ul>
  1225. <template
  1226. x-for="person in filteredPeople"
  1227. :key="person.id"
  1228. hidden
  1229. >
  1230. <li
  1231. x-combobox:option
  1232. :option="person.id"
  1233. :value="person"
  1234. :disabled="person.disabled"
  1235. x-text="person.name"
  1236. >
  1237. </li>
  1238. </template>
  1239. </ul>
  1240. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1241. </div>
  1242. </div>
  1243. </div>
  1244. <article>lorem ipsum</article>
  1245. </div>
  1246. `],
  1247. ({ get }) => {
  1248. get('button').click()
  1249. get('li').should(haveLength('10'))
  1250. // Test after closing with button and reopening
  1251. get('input').type('w').trigger('input')
  1252. get('li').should(haveLength('2'))
  1253. get('button').click()
  1254. get('button').click()
  1255. get('li').should(haveLength('10'))
  1256. // Test correct state after closing with ESC and reopening
  1257. get('input').type('w').trigger('input')
  1258. get('li').should(haveLength('2'))
  1259. get('input').type('{esc}')
  1260. get('button').click()
  1261. get('li').should(haveLength('10'))
  1262. // Test correct state after closing with TAB and reopening
  1263. get('input').type('w').trigger('input')
  1264. get('li').should(haveLength('2'))
  1265. get('input').tab()
  1266. get('button').click()
  1267. get('li').should(haveLength('10'))
  1268. // Test correct state after closing with external click and reopening
  1269. get('input').type('w').trigger('input')
  1270. get('li').should(haveLength('2'))
  1271. get('article').click()
  1272. get('button').click()
  1273. get('li').should(haveLength('10'))
  1274. },
  1275. )
  1276. test('active element logic when opening a combobox',
  1277. [html`
  1278. <div
  1279. x-data="{
  1280. query: '',
  1281. selected: null,
  1282. people: [
  1283. { id: 1, name: 'Wade Cooper' },
  1284. { id: 2, name: 'Arlene Mccoy' },
  1285. { id: 3, name: 'Devon Webb' },
  1286. { id: 4, name: 'Tom Cook' },
  1287. { id: 5, name: 'Tanya Fox', disabled: true },
  1288. { id: 6, name: 'Hellen Schmidt' },
  1289. { id: 7, name: 'Caroline Schultz' },
  1290. { id: 8, name: 'Mason Heaney' },
  1291. { id: 9, name: 'Claudie Smitham' },
  1292. { id: 10, name: 'Emil Schaefer' },
  1293. ],
  1294. get filteredPeople() {
  1295. return this.query === ''
  1296. ? this.people
  1297. : this.people.filter((person) => {
  1298. return person.name.toLowerCase().includes(this.query.toLowerCase())
  1299. })
  1300. },
  1301. }"
  1302. >
  1303. <div x-combobox x-model="selected">
  1304. <label x-combobox:label>Select person</label>
  1305. <div>
  1306. <div>
  1307. <input
  1308. x-combobox:input
  1309. :display-value="person => person.name"
  1310. @change="query = $event.target.value"
  1311. placeholder="Search..."
  1312. />
  1313. <button x-combobox:button>Toggle</button>
  1314. </div>
  1315. <div x-combobox:options>
  1316. <ul>
  1317. <template
  1318. x-for="person in filteredPeople"
  1319. :key="person.id"
  1320. hidden
  1321. >
  1322. <li
  1323. x-combobox:option
  1324. :option="person.id"
  1325. :value="person"
  1326. :disabled="person.disabled"
  1327. x-text="person.name"
  1328. >
  1329. </li>
  1330. </template>
  1331. </ul>
  1332. <p x-show="filteredPeople.length == 0">No people match your query.</p>
  1333. </div>
  1334. </div>
  1335. </div>
  1336. </div>
  1337. `],
  1338. ({ get }) => {
  1339. get('button').click()
  1340. // First option is selected on opening if no preselection
  1341. get('ul').should(beVisible())
  1342. get('[option="1"]').should(haveAttribute('aria-selected', 'true'))
  1343. // First match is selected while typing
  1344. get('[option="4"]').should(notHaveAttribute('aria-selected'))
  1345. get('input').type('T')
  1346. get('input').trigger('change')
  1347. get('[option="4"]').should(haveAttribute('aria-selected', 'true'))
  1348. // Reset state and select option 3
  1349. get('button').click()
  1350. get('button').click()
  1351. get('[option="3"]').click()
  1352. // Previous selection is selected
  1353. get('button').click()
  1354. get('[option="4"]').should(notHaveAttribute('aria-selected'))
  1355. get('[option="3"]').should(haveAttribute('aria-selected', 'true'))
  1356. }
  1357. )
  1358. test.only('can remove an option without other options getting removed',
  1359. [html`<div
  1360. x-data="{
  1361. query: '',
  1362. selected: [],
  1363. frameworks: [
  1364. {
  1365. id: 1,
  1366. name: 'Laravel',
  1367. disabled: false,
  1368. },
  1369. {
  1370. id: 2,
  1371. name: 'Ruby on Rails',
  1372. disabled: false,
  1373. },
  1374. {
  1375. id: 3,
  1376. name: 'Django',
  1377. disabled: false,
  1378. },
  1379. ],
  1380. get filteredFrameworks() {
  1381. return this.query === ''
  1382. ? this.frameworks
  1383. : this.frameworks.filter((framework) => {
  1384. return framework.name.toLowerCase().includes(this.query.toLowerCase())
  1385. })
  1386. },
  1387. remove(framework) {
  1388. this.selected = this.selected.filter((i) => i !== framework)
  1389. }
  1390. }"
  1391. >
  1392. <div x-combobox x-model="selected" multiple>
  1393. <div x-show="selected.length">
  1394. <template x-for="selectedFramework in selected" :key="selectedFramework.id">
  1395. <button x-on:click.prevent="remove(selectedFramework)" :remove-option="selectedFramework.id">
  1396. <span x-text="selectedFramework.name"></span>
  1397. </button>
  1398. </template>
  1399. </div>
  1400. <div>
  1401. <div>
  1402. <input
  1403. x-combobox:input
  1404. @change="query = $event.target.value;"
  1405. placeholder="Search..."
  1406. />
  1407. <button x-combobox:button>
  1408. Show options
  1409. </button>
  1410. </div>
  1411. <div x-combobox:options x-cloak x-transition.out.opacity>
  1412. <ul>
  1413. <template
  1414. x-for="framework in filteredFrameworks"
  1415. :key="framework.id"
  1416. hidden
  1417. >
  1418. <li
  1419. x-combobox:option
  1420. :option="framework.id"
  1421. :value="framework"
  1422. :disabled="framework.disabled"
  1423. >
  1424. <span x-text="framework.name"></span>
  1425. <span x-show="$comboboxOption.isSelected" :check="framework.id">&check;</span>
  1426. </li>
  1427. </template>
  1428. </ul>
  1429. <p x-show="filteredFrameworks.length == 0">No frameworks match your query.</p>
  1430. </div>
  1431. </div>
  1432. </div>
  1433. </div>
  1434. `],
  1435. ({ get }) => {
  1436. get('input').type('a').trigger('input')
  1437. cy.wait(100)
  1438. get('[option="1"]').click()
  1439. get('[option="2"]').click()
  1440. get('[option="3"]').click()
  1441. get('[remove-option="3"]').click()
  1442. get('[option="1"]').should(haveAttribute('aria-selected', 'true'))
  1443. get('[option="2"]').should(haveAttribute('aria-selected', 'true'))
  1444. get('[option="3"]').should(haveAttribute('aria-selected', 'false'))
  1445. get('button').click()
  1446. get('[check="1"]').should(beVisible())
  1447. get('[check="2"]').should(beVisible())
  1448. get('[check="3"]').should(notBeVisible())
  1449. },
  1450. );