From 7038fd90e3ddc213faeb1134bf222d91c332f5a2 Mon Sep 17 00:00:00 2001 From: elforjani3 Date: Thu, 31 Dec 2020 12:25:21 +0200 Subject: [PATCH] feat:contacts suggest field. --- client/src/components/ContactsSuggestField.js | 106 ++++++++++++++++++ .../DataTableCells/ContactsListFieldCell.js | 5 +- 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 client/src/components/ContactsSuggestField.js diff --git a/client/src/components/ContactsSuggestField.js b/client/src/components/ContactsSuggestField.js new file mode 100644 index 000000000..cb8187920 --- /dev/null +++ b/client/src/components/ContactsSuggestField.js @@ -0,0 +1,106 @@ +import React, { useCallback, useState, useEffect, useMemo } from 'react'; +import { MenuItem } from '@blueprintjs/core'; +import { Suggest } from '@blueprintjs/select'; + +import { FormattedMessage as T } from 'react-intl'; +import classNames from 'classnames'; +import { CLASSES } from 'common/classes'; + +export default function ContactsSuggestField({ + contactsList, + initialContactId, + selectedContactId, + defaultTextSelect = 'Select contact', + onContactSelected, + + selectedContactType = [], + popoverFill = false, + + ...suggestProps +}) { + // filteredContacts + 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 contact = selectedContactId + ? contacts.find((a) => a.id === selectedContactId) + : null; + setSelectedContact(contact); + } + }, [selectedContactId, contacts, setSelectedContact]); + + const contactRenderer = useCallback( + (contact, { handleClick }) => ( + + ), + [], + ); + + const onContactSelect = useCallback( + (contact) => { + setSelectedContact({ ...contact }); + onContactSelected && onContactSelected(contact); + }, + [setSelectedContact, onContactSelected], + ); + + const handleInputValueRenderer = (inputValue) => { + if (inputValue) { + return inputValue.display_name.toString(); + } + }; + + 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={contactRenderer} + itemPredicate={filterContacts} + onItemSelect={onContactSelect} + selectedItem={selecetedContact} + inputProps={{ placeholder: defaultTextSelect }} + resetOnClose={true} + // fill={true} + popoverProps={{ minimal: true }} + {...suggestProps} + inputValueRenderer={handleInputValueRenderer} + className={classNames(CLASSES.FORM_GROUP_LIST_SELECT, { + [CLASSES.SELECT_LIST_FILL_POPOVER]: popoverFill, + })} + /> + ); +} diff --git a/client/src/components/DataTableCells/ContactsListFieldCell.js b/client/src/components/DataTableCells/ContactsListFieldCell.js index 16eb5286b..14676e0f8 100644 --- a/client/src/components/DataTableCells/ContactsListFieldCell.js +++ b/client/src/components/DataTableCells/ContactsListFieldCell.js @@ -1,7 +1,8 @@ -import React, { useState, useCallback, useMemo } from 'react'; +import React, { useCallback } from 'react'; import { FormGroup, Intent, Classes } from '@blueprintjs/core'; import classNames from 'classnames'; import { ContactSelecetList } from 'components'; +import ContactsSuggestField from 'components/ContactsSuggestField'; export default function ContactsListCellRenderer({ column: { id }, @@ -30,7 +31,7 @@ export default function ContactsListCellRenderer({ Classes.FILL, )} > -