|
| 1 | +--- |
| 2 | +import type { ListProps } from './list' |
| 3 | +import ConditionalWrapper from '../ConditionalWrapper/ConditionalWrapper.astro' |
| 4 | +import Input from '../Input/Input.astro' |
| 5 | +
|
| 6 | +import searchIcon from '../../icons/search.svg?raw' |
| 7 | +import styles from './list.module.scss' |
| 8 | +
|
| 9 | +interface Props extends ListProps {} |
| 10 | +
|
| 11 | +const { |
| 12 | + showSearchBar, |
| 13 | + showSearchBarIcon, |
| 14 | + searchBarPlaceholder, |
| 15 | + noResultsLabel = 'No results.', |
| 16 | + maxHeight, |
| 17 | + id, |
| 18 | + className, |
| 19 | + wrapperClassName, |
| 20 | + itemGroups |
| 21 | +} = Astro.props |
| 22 | +
|
| 23 | +const classes = [ |
| 24 | + styles.list, |
| 25 | + !showSearchBar && styles.container, |
| 26 | + className |
| 27 | +] |
| 28 | +
|
| 29 | +const wrapperClasses = [ |
| 30 | + styles.container, |
| 31 | + wrapperClassName |
| 32 | +] |
| 33 | +--- |
| 34 | + |
| 35 | +<ConditionalWrapper condition={!!showSearchBar}> |
| 36 | + <div slot="wrapper" class:list={wrapperClasses} data-id="w-list-wrapper"> |
| 37 | + <Input |
| 38 | + type="search" |
| 39 | + placeholder={searchBarPlaceholder} |
| 40 | + data-id="w-list-search" |
| 41 | + > |
| 42 | + <Fragment |
| 43 | + set:html={searchIcon} |
| 44 | + slot={showSearchBarIcon ? 'default' : null} |
| 45 | + /> |
| 46 | + </Input> |
| 47 | + children |
| 48 | + </div> |
| 49 | + <ul class:list={classes} id={id} data-id="w-list" style={maxHeight && `max-height: ${maxHeight}`}> |
| 50 | + {itemGroups.map((group: ListProps['itemGroups'][0]) => ( |
| 51 | + <Fragment> |
| 52 | + {group.title && ( |
| 53 | + <li class={styles.title} data-id="w-list-title"> |
| 54 | + {group.title} |
| 55 | + </li> |
| 56 | + )} |
| 57 | + {group.items.map(item => ( |
| 58 | + <li |
| 59 | + tabindex={item.href || item.disabled ? undefined : 0} |
| 60 | + data-value={item.value} |
| 61 | + data-name={item.name} |
| 62 | + data-disabled={item.disabled} |
| 63 | + data-selected={item.selected} |
| 64 | + > |
| 65 | + <ConditionalWrapper condition={!!item.href}> |
| 66 | + <a |
| 67 | + slot="wrapper" |
| 68 | + href={item.href} |
| 69 | + target={item.target} |
| 70 | + > |
| 71 | + children |
| 72 | + </a> |
| 73 | + |
| 74 | + <ConditionalWrapper condition={!!(item.icon && item.subText)}> |
| 75 | + <div slot="wrapper">children</div> |
| 76 | + {item.icon && <Fragment set:html={item.icon} />} |
| 77 | + {item.name} |
| 78 | + </ConditionalWrapper> |
| 79 | + {item.subText && <span>{item.subText}</span>} |
| 80 | + </ConditionalWrapper> |
| 81 | + </li> |
| 82 | + ))} |
| 83 | + </Fragment> |
| 84 | + ))} |
| 85 | + {showSearchBar && ( |
| 86 | + <li data-id="w-no-results" data-hidden>{noResultsLabel}</li> |
| 87 | + )} |
| 88 | + </ul> |
| 89 | +</ConditionalWrapper> |
| 90 | + |
| 91 | +<script> |
| 92 | + import { dispatch } from '../../utils/event' |
| 93 | + |
| 94 | + const lists = document.querySelectorAll('[data-id="w-list"]') |
| 95 | + const searchInputs = document.querySelectorAll('[data-id="w-list-search"]') |
| 96 | + |
| 97 | + const handleClick = (list: Element, items: Element[], event: Event) => { |
| 98 | + const target = event.target as HTMLElement |
| 99 | + |
| 100 | + if (target.dataset.value) { |
| 101 | + dispatch('listOnSelect', { |
| 102 | + value: target.dataset.value, |
| 103 | + name: target.dataset.name, |
| 104 | + list, |
| 105 | + }) |
| 106 | + |
| 107 | + items.forEach(item => item.removeAttribute('data-selected')) |
| 108 | + target.dataset.selected = 'true' |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + Array.from(lists).forEach(list => { |
| 113 | + const items = Array.from(list.children) |
| 114 | + |
| 115 | + list.addEventListener('click', event => handleClick(list, items, event)) |
| 116 | + list.addEventListener('keyup', event => { |
| 117 | + if ((event as KeyboardEvent).key === 'Enter') { |
| 118 | + handleClick(list, items, event) |
| 119 | + } |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + Array.from(searchInputs).forEach(element => { |
| 124 | + element.addEventListener('input', event => { |
| 125 | + const target = event.target as HTMLInputElement |
| 126 | + const ul = target.closest('[data-id="w-list-wrapper"]') |
| 127 | + ?.querySelector('ul') as HTMLUListElement |
| 128 | + |
| 129 | + const noResults = ul.querySelector('[data-id="w-no-results"]') |
| 130 | + const items = Array.from(ul.children) |
| 131 | + const value = target.value.toLowerCase() |
| 132 | + |
| 133 | + items.forEach(item => { |
| 134 | + const li = item as HTMLLIElement |
| 135 | + const hideItem = (!li.dataset.value?.toLowerCase().includes(value) |
| 136 | + && !li.innerText.toLowerCase().includes(value)) |
| 137 | + || li.dataset.id === 'w-list-title' |
| 138 | + || li.dataset.id === 'w-no-results' |
| 139 | + |
| 140 | + if (hideItem) { |
| 141 | + li.dataset.hidden = 'true' |
| 142 | + } else if (li.dataset.id !== 'w-no-results') { |
| 143 | + li.removeAttribute('data-hidden') |
| 144 | + } |
| 145 | + }) |
| 146 | + |
| 147 | + const numberOfResults = items.filter(item => { |
| 148 | + const li = item as HTMLLIElement |
| 149 | + return li.dataset.name && !li.dataset.hidden |
| 150 | + }).length |
| 151 | + |
| 152 | + if (!numberOfResults) { |
| 153 | + noResults?.removeAttribute('data-hidden') |
| 154 | + } |
| 155 | + |
| 156 | + if (!value) { |
| 157 | + items.forEach(item => { |
| 158 | + const li = item as HTMLLIElement |
| 159 | + |
| 160 | + li.dataset.id === 'w-no-results' |
| 161 | + ? li.dataset.hidden = 'true' |
| 162 | + : li.removeAttribute('data-hidden') |
| 163 | + }) |
| 164 | + } |
| 165 | + }) |
| 166 | + }) |
| 167 | +</script> |
0 commit comments