combobox.spec.js 49 KB

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