import { InputSubmitContent } from '@/types' import { PictureChoiceBlock, defaultPictureChoiceOptions, } from '@typebot.io/schemas/features/blocks/inputs/pictureChoice' import { For, Show, createSignal, onMount } from 'solid-js' import { Checkbox } from '../buttons/components/Checkbox' import { SendButton } from '@/components' import { isDefined, isEmpty } from '@typebot.io/lib' import { SearchInput } from '@/components/inputs/SearchInput' import { isMobile } from '@/utils/isMobileSignal' type Props = { defaultItems: PictureChoiceBlock['items'] options: PictureChoiceBlock['options'] onSubmit: (value: InputSubmitContent) => void } export const MultiplePictureChoice = (props: Props) => { let inputRef: HTMLInputElement | undefined const [filteredItems, setFilteredItems] = createSignal(props.defaultItems) const [selectedItemIds, setSelectedItemIds] = createSignal([]) onMount(() => { if (!isMobile() && inputRef) inputRef.focus() }) const handleClick = (itemId: string) => { toggleSelectedItemId(itemId) } const toggleSelectedItemId = (itemId: string) => { const existingIndex = selectedItemIds().indexOf(itemId) if (existingIndex !== -1) { setSelectedItemIds((selectedItemIds) => selectedItemIds.filter((selectedItemId) => selectedItemId !== itemId) ) } else { setSelectedItemIds((selectedIndices) => [...selectedIndices, itemId]) } } const handleSubmit = () => props.onSubmit({ value: selectedItemIds() .map((selectedItemId) => { const item = props.defaultItems.find( (item) => item.id === selectedItemId ) return item?.title ?? item?.pictureSrc }) .join(', '), }) const filterItems = (inputValue: string) => { setFilteredItems( props.defaultItems.filter( (item) => item.title ?.toLowerCase() .includes((inputValue ?? '').toLowerCase()) || item.description ?.toLowerCase() .includes((inputValue ?? '').toLowerCase()) ) ) } return (
setFilteredItems(props.defaultItems)} />
{(item, index) => (
selectedItemId === item.id )} on:click={() => handleClick(item.id)} class={ 'flex flex-col focus:outline-none cursor-pointer select-none typebot-selectable-picture' + (selectedItemIds().some( (selectedItemId) => selectedItemId === item.id ) ? ' selected' : '') } data-itemid={item.id} > {item.title
selectedItemId === item.id )} class={item.title || item.description ? 'mt-1' : undefined} />
{item.title} {item.description}
)}
filteredItems().every((item) => item.id !== selectedItemId) ) .map((selectedItemId) => props.defaultItems.find((item) => item.id === selectedItemId) ) .filter(isDefined)} > {(selectedItem, index) => (
handleClick(selectedItem.id)} class={ 'flex flex-col focus:outline-none cursor-pointer select-none typebot-selectable-picture selected' } data-itemid={selectedItem.id} > item.id === selectedItem.id) ?.pictureSrc } alt={selectedItem.title ?? `Selected picture ${index() + 1}`} elementtiming={`Selected picture choice ${index() + 1}`} fetchpriority={'high'} />
selectedItemId === selectedItem.id )} class={ selectedItem.title || selectedItem.description ? 'mt-1' : undefined } />
{selectedItem.title} {selectedItem.description}
)}
{selectedItemIds().length > 0 && ( {props.options?.buttonLabel ?? defaultPictureChoiceOptions.buttonLabel} )}
) }