combobox.spec.js 67 KB

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