mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
feat:contacts suggest field.
This commit is contained in:
106
client/src/components/ContactsSuggestField.js
Normal file
106
client/src/components/ContactsSuggestField.js
Normal file
@@ -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 }) => (
|
||||||
|
<MenuItem
|
||||||
|
key={contact.id}
|
||||||
|
text={contact.display_name}
|
||||||
|
onClick={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 (
|
||||||
|
<Suggest
|
||||||
|
items={contacts}
|
||||||
|
noResults={<MenuItem disabled={true} text={<T id={'no_results'} />} />}
|
||||||
|
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,
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import React, { useState, useCallback, useMemo } from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { FormGroup, Intent, Classes } from '@blueprintjs/core';
|
import { FormGroup, Intent, Classes } from '@blueprintjs/core';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { ContactSelecetList } from 'components';
|
import { ContactSelecetList } from 'components';
|
||||||
|
import ContactsSuggestField from 'components/ContactsSuggestField';
|
||||||
|
|
||||||
export default function ContactsListCellRenderer({
|
export default function ContactsListCellRenderer({
|
||||||
column: { id },
|
column: { id },
|
||||||
@@ -30,7 +31,7 @@ export default function ContactsListCellRenderer({
|
|||||||
Classes.FILL,
|
Classes.FILL,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<ContactSelecetList
|
<ContactsSuggestField
|
||||||
contactsList={contacts}
|
contactsList={contacts}
|
||||||
onContactSelected={handleContactSelected}
|
onContactSelected={handleContactSelected}
|
||||||
selectedContactId={original?.contact_id}
|
selectedContactId={original?.contact_id}
|
||||||
|
|||||||
Reference in New Issue
Block a user