(
newValue: boolean,
select: Element,
values: HTMLElement | HTMLElement[] | string[] | string,
init?: MouseEventInit,
{skipPointerEventsCheck = false}: PointerOptions = {},
)
| 10 | import {hover, unhover} from './hover' |
| 11 | |
| 12 | function selectOptionsBase( |
| 13 | newValue: boolean, |
| 14 | select: Element, |
| 15 | values: HTMLElement | HTMLElement[] | string[] | string, |
| 16 | init?: MouseEventInit, |
| 17 | {skipPointerEventsCheck = false}: PointerOptions = {}, |
| 18 | ) { |
| 19 | if (!newValue && !(select as HTMLSelectElement).multiple) { |
| 20 | throw getConfig().getElementError( |
| 21 | `Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`, |
| 22 | select, |
| 23 | ) |
| 24 | } |
| 25 | const valArray = Array.isArray(values) ? values : [values] |
| 26 | const allOptions = Array.from( |
| 27 | select.querySelectorAll('option, [role="option"]'), |
| 28 | ) |
| 29 | const selectedOptions = valArray |
| 30 | .map(val => { |
| 31 | if (typeof val !== 'string' && allOptions.includes(val)) { |
| 32 | return val |
| 33 | } else { |
| 34 | const matchingOption = allOptions.find( |
| 35 | o => |
| 36 | (o as HTMLInputElement | HTMLTextAreaElement).value === val || |
| 37 | o.innerHTML === val, |
| 38 | ) |
| 39 | if (matchingOption) { |
| 40 | return matchingOption |
| 41 | } else { |
| 42 | throw getConfig().getElementError( |
| 43 | `Value "${String(val)}" not found in options`, |
| 44 | select, |
| 45 | ) |
| 46 | } |
| 47 | } |
| 48 | }) |
| 49 | .filter(option => !isDisabled(option)) |
| 50 | |
| 51 | if (isDisabled(select) || !selectedOptions.length) return |
| 52 | |
| 53 | if (isElementType(select, 'select')) { |
| 54 | if (select.multiple) { |
| 55 | for (const option of selectedOptions) { |
| 56 | const withPointerEvents = skipPointerEventsCheck |
| 57 | ? true |
| 58 | : hasPointerEvents(option) |
| 59 | |
| 60 | // events fired for multiple select are weird. Can't use hover... |
| 61 | if (withPointerEvents) { |
| 62 | fireEvent.pointerOver(option, init) |
| 63 | fireEvent.pointerEnter(select, init) |
| 64 | fireEvent.mouseOver(option) |
| 65 | fireEvent.mouseEnter(select) |
| 66 | fireEvent.pointerMove(option, init) |
| 67 | fireEvent.mouseMove(option, init) |
| 68 | fireEvent.pointerDown(option, init) |
| 69 | fireEvent.mouseDown(option, init) |
nothing calls this directly
no test coverage detected