import { AutocompleteItem , RenderFunction , RenderGroupFunction } from './types' ; export function removeChildren ( container ) { while ( container . firstChild ) { container . firstChild . remove ( ) ; } } export function defaultRenderGroup ( groupName ) { const groupDiv = document . createElement ( 'div' ) ; groupDiv . textContent = groupName ; groupDiv . className = 'group' ; return groupDiv ; } export function defaultRender ( item ) { const itemElement = document . createElement ( 'div' ) ; itemElement . textContent = item . label || '' ; return itemElement ; } export function renderItems < T extends AutocompleteItem > ( items , selected , inputValue , clickHandler , render = defaultRender , renderGroup = defaultRenderGroup ) { const fragment = document . createDocumentFragment ( ) ; const elements = [ ] ; let previousGroup = '#9?$' ; for ( const item of items ) { if ( item . group && item . group !== previousGroup ) { previousGroup = item . group ; const groupDiv = renderGroup ( item . group , inputValue ) ; elements . push ( groupDiv ) ; } const div = render ( item , inputValue ) ; div . addEventListener ( 'click' , ( event ) => clickHandler ( item , event ) ) ; if ( item === selected ) { div . classList . add ( 'selected' ) ; } elements . push ( div ) ; } fragment . append ( ... elements ) ; return fragment ; }