import React, { useEffect, useState } from 'react'; import { Text, TextStyle } from 'react-native'; import { BlockContext } from '@rocket.chat/ui-kit'; import Button from '../../Button'; import { useTheme } from '../../../theme'; import { IText } from '../interfaces'; import Chips from './Chips'; import Input from './Input'; import styles from './styles'; import { useActionSheet } from '../../ActionSheet'; import { MultiSelectContent } from './MultiSelectContent'; export interface IItemData { value: any; text: { text: string }; imageUrl?: string; } interface IMultiSelect { options?: IItemData[]; onChange: Function; placeholder?: IText; context?: BlockContext; loading?: boolean; multiselect?: boolean; onSearch?: (keyword: string) => IItemData[] | Promise; onClose?: () => void; inputStyle?: TextStyle; value?: any[]; disabled?: boolean; innerInputStyle?: object; } export const MultiSelect = React.memo( ({ options = [], onChange, placeholder = { text: 'Search' }, context, loading, value: values, multiselect = false, onSearch, onClose = () => {}, disabled, inputStyle, innerInputStyle }: IMultiSelect) => { const { colors } = useTheme(); const [selected, select] = useState(Array.isArray(values) ? values : []); const [currentValue, setCurrentValue] = useState(''); const { showActionSheet, hideActionSheet } = useActionSheet(); useEffect(() => { if (Array.isArray(values)) { select(values); } }, [values]); useEffect(() => { if (values && values.length && !multiselect) { setCurrentValue(values[0].text); } }, []); const onShow = () => { showActionSheet({ children: ( ), onClose, headerHeight: 275 }); }; const onHide = () => { onClose(); hideActionSheet(); }; const onSelect = (item: IItemData) => { const { value, text: { text } } = item; if (multiselect) { let newSelect = []; if (!selected.includes(value)) { newSelect = [...selected, value]; } else { newSelect = selected.filter((s: any) => s !== value); } select(newSelect); onChange({ value: newSelect }); } else { onChange({ value }); setCurrentValue(text); } }; let button = multiselect ? (