combobox.spec.js 61 KB

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