diff --git a/client/src/components/ContactSelecetList.js b/client/src/components/ContactSelecetList.js index baa4b2a20..ed10398c2 100644 --- a/client/src/components/ContactSelecetList.js +++ b/client/src/components/ContactSelecetList.js @@ -1,42 +1,47 @@ import React, { useCallback, useState, useEffect, useMemo } from 'react'; import { FormattedMessage as T } from 'react-intl'; -import { ListSelect } from 'components'; -import { MenuItem } from '@blueprintjs/core'; +import { MenuItem, Button } from '@blueprintjs/core'; +import { Select } from '@blueprintjs/select'; import classNames from 'classnames'; import { CLASSES } from 'common/classes'; export default function ContactSelecetList({ contactsList, + initialContactId, selectedContactId, + selectedContactType, defaultSelectText = , onContactSelected, popoverFill = false, - ...restProps + disabled = false, }) { - const [selecetedContact, setSelectedContact] = useState(null); - - // Filter Contact List - const FilterContacts = (query, contact, index, exactMatch) => { - const normalizedTitle = contact.display_name.toLowerCase(); - const normalizedQuery = query.toLowerCase(); - if (exactMatch) { - return normalizedTitle === normalizedQuery; - } else { - return ( - `${contact.display_name} ${normalizedTitle}`.indexOf(normalizedQuery) >= - 0 - ); - } - }; - - const onContactSelect = useCallback( - (contact) => { - setSelectedContact({ ...contact }); - onContactSelected && onContactSelected(contact); - }, - [setSelectedContact, onContactSelected], + const contacts = useMemo( + () => + contactsList.map((contact) => ({ + ...contact, + _id: `${contact.id}_${contact.contact_type}`, + })), + [contactsList], ); + const initialContact = useMemo( + () => contacts.find((a) => a.id === initialContactId), + [initialContactId, contacts], + ); + + const [selecetedContact, setSelectedContact] = useState( + initialContact || null, + ); + + useEffect(() => { + if (typeof selectedContactId !== 'undefined') { + const account = selectedContactId + ? contacts.find((a) => a.id === selectedContactId) + : null; + setSelectedContact(account); + } + }, [selectedContactId, contacts, setSelectedContact]); + const handleContactRenderer = useCallback( (contact, { handleClick }) => ( { + setSelectedContact({ ...contact }); + onContactSelected && onContactSelected(contact); + }, + [setSelectedContact, onContactSelected], + ); + + // Filter Contact List + const filterContacts = (query, contact, index, exactMatch) => { + const normalizedTitle = contact.display_name.toLowerCase(); + const normalizedQuery = query.toLowerCase(); + if (exactMatch) { + return normalizedTitle === normalizedQuery; + } else { + return ( + `${contact.display_name} ${normalizedTitle}`.indexOf(normalizedQuery) >= + 0 + ); + } + }; + return ( - } itemRenderer={handleContactRenderer} + itemPredicate={filterContacts} + filterable={true} + disabled={disabled} + onItemSelect={onContactSelect} popoverProps={{ minimal: true, usePortal: !popoverFill }} className={classNames(CLASSES.FORM_GROUP_LIST_SELECT, { [CLASSES.SELECT_LIST_FILL_POPOVER]: popoverFill, })} - {...restProps} - /> + > +