1
0

combobox.spec.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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('"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. )