mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 15:50:32 +00:00
Fix:ContactSelectList.
This commit is contained in:
@@ -1,41 +1,46 @@
|
|||||||
import React, { useCallback, useState, useEffect, useMemo } from 'react';
|
import React, { useCallback, useState, useEffect, useMemo } from 'react';
|
||||||
import { FormattedMessage as T } from 'react-intl';
|
import { FormattedMessage as T } from 'react-intl';
|
||||||
import { ListSelect } from 'components';
|
import { MenuItem, Button } from '@blueprintjs/core';
|
||||||
import { MenuItem } from '@blueprintjs/core';
|
import { Select } from '@blueprintjs/select';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
|
|
||||||
export default function ContactSelecetList({
|
export default function ContactSelecetList({
|
||||||
contactsList,
|
contactsList,
|
||||||
|
initialContactId,
|
||||||
selectedContactId,
|
selectedContactId,
|
||||||
|
selectedContactType,
|
||||||
defaultSelectText = <T id={'select_contact'} />,
|
defaultSelectText = <T id={'select_contact'} />,
|
||||||
onContactSelected,
|
onContactSelected,
|
||||||
popoverFill = false,
|
popoverFill = false,
|
||||||
...restProps
|
disabled = false,
|
||||||
}) {
|
}) {
|
||||||
const [selecetedContact, setSelectedContact] = useState(null);
|
const contacts = useMemo(
|
||||||
|
() =>
|
||||||
// Filter Contact List
|
contactsList.map((contact) => ({
|
||||||
const FilterContacts = (query, contact, index, exactMatch) => {
|
...contact,
|
||||||
const normalizedTitle = contact.display_name.toLowerCase();
|
_id: `${contact.id}_${contact.contact_type}`,
|
||||||
const normalizedQuery = query.toLowerCase();
|
})),
|
||||||
if (exactMatch) {
|
[contactsList],
|
||||||
return normalizedTitle === normalizedQuery;
|
|
||||||
} else {
|
|
||||||
return (
|
|
||||||
`${contact.display_name} ${normalizedTitle}`.indexOf(normalizedQuery) >=
|
|
||||||
0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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 onContactSelect = useCallback(
|
|
||||||
(contact) => {
|
|
||||||
setSelectedContact({ ...contact });
|
|
||||||
onContactSelected && onContactSelected(contact);
|
|
||||||
},
|
|
||||||
[setSelectedContact, onContactSelected],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleContactRenderer = useCallback(
|
const handleContactRenderer = useCallback(
|
||||||
(contact, { handleClick }) => (
|
(contact, { handleClick }) => (
|
||||||
@@ -48,21 +53,48 @@ export default function ContactSelecetList({
|
|||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const onContactSelect = useCallback(
|
||||||
|
(contact) => {
|
||||||
|
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 (
|
return (
|
||||||
<ListSelect
|
`${contact.display_name} ${normalizedTitle}`.indexOf(normalizedQuery) >=
|
||||||
items={contactsList}
|
0
|
||||||
selectedItemProp={'id'}
|
);
|
||||||
selectedItem={selectedContactId}
|
}
|
||||||
labelProp={'display_name'}
|
};
|
||||||
defaultText={defaultSelectText}
|
|
||||||
onItemSelect={onContactSelect}
|
return (
|
||||||
itemPredicate={FilterContacts}
|
<Select
|
||||||
|
items={contacts}
|
||||||
|
noResults={<MenuItem disabled={true} text="No results." />}
|
||||||
itemRenderer={handleContactRenderer}
|
itemRenderer={handleContactRenderer}
|
||||||
|
itemPredicate={filterContacts}
|
||||||
|
filterable={true}
|
||||||
|
disabled={disabled}
|
||||||
|
onItemSelect={onContactSelect}
|
||||||
popoverProps={{ minimal: true, usePortal: !popoverFill }}
|
popoverProps={{ minimal: true, usePortal: !popoverFill }}
|
||||||
className={classNames(CLASSES.FORM_GROUP_LIST_SELECT, {
|
className={classNames(CLASSES.FORM_GROUP_LIST_SELECT, {
|
||||||
[CLASSES.SELECT_LIST_FILL_POPOVER]: popoverFill,
|
[CLASSES.SELECT_LIST_FILL_POPOVER]: popoverFill,
|
||||||
})}
|
})}
|
||||||
{...restProps}
|
>
|
||||||
|
<Button
|
||||||
|
disabled={disabled}
|
||||||
|
text={
|
||||||
|
selecetedContact ? selecetedContact.display_name : defaultSelectText
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
|
</Select>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
import React, { useCallback, useMemo } from 'react';
|
|
||||||
import { MenuItem } from '@blueprintjs/core';
|
|
||||||
import classNames from 'classnames';
|
|
||||||
import { CLASSES } from 'common/classes';
|
|
||||||
import ListSelect from 'components/ListSelect';
|
|
||||||
import { FormattedMessage as T } from 'react-intl';
|
|
||||||
|
|
||||||
export default function ContactsListField({
|
|
||||||
contacts,
|
|
||||||
onContactSelected,
|
|
||||||
selectedContactId,
|
|
||||||
selectedContactType,
|
|
||||||
defautlSelectText = <T id={'select_contact'} />,
|
|
||||||
popoverFill = false,
|
|
||||||
}) {
|
|
||||||
// Contact item of select accounts field.
|
|
||||||
const contactRenderer = useCallback(
|
|
||||||
(item, { handleClick, modifiers, query }) => (
|
|
||||||
<MenuItem text={item.display_name} key={item.id} onClick={handleClick} />
|
|
||||||
),
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
|
|
||||||
const onContactSelect = useCallback(
|
|
||||||
(contact) => {
|
|
||||||
onContactSelected && onContactSelected(contact);
|
|
||||||
},
|
|
||||||
[onContactSelected],
|
|
||||||
);
|
|
||||||
|
|
||||||
const items = useMemo(
|
|
||||||
() =>
|
|
||||||
contacts.map((contact) => ({
|
|
||||||
...contact,
|
|
||||||
_id: `${contact.id}_${contact.contact_type}`,
|
|
||||||
})),
|
|
||||||
[contacts],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ListSelect
|
|
||||||
items={items}
|
|
||||||
noResults={<MenuItem disabled={true} text="No results." />}
|
|
||||||
itemRenderer={contactRenderer}
|
|
||||||
filterable={true}
|
|
||||||
onItemSelect={onContactSelect}
|
|
||||||
labelProp={'display_name'}
|
|
||||||
selectedItem={`${selectedContactId}_${selectedContactType}`}
|
|
||||||
selectedItemProp={'_id'}
|
|
||||||
defaultText={defautlSelectText}
|
|
||||||
popoverProps={{ minimal: true, usePortal: !popoverFill }}
|
|
||||||
className={classNames(CLASSES.FORM_GROUP_LIST_SELECT, {
|
|
||||||
[CLASSES.SELECT_LIST_FILL_POPOVER]: popoverFill,
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,20 +1,23 @@
|
|||||||
import React, { useState, useCallback, useMemo } from 'react';
|
import React, { useState, useCallback, useMemo } 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 ContactsListField from 'components/ContactsListField';
|
import { ContactSelecetList } from 'components';
|
||||||
|
|
||||||
export default function ContactsListCellRenderer({
|
export default function ContactsListCellRenderer({
|
||||||
column: { id },
|
column: { id },
|
||||||
row: { index, original },
|
row: { index, original },
|
||||||
cell: { value },
|
cell: { value },
|
||||||
payload: { contacts, updateData, errors }
|
payload: { contacts, updateData, errors },
|
||||||
}) {
|
}) {
|
||||||
const handleContactSelected = useCallback((contact) => {
|
const handleContactSelected = useCallback(
|
||||||
|
(contact) => {
|
||||||
updateData(index, {
|
updateData(index, {
|
||||||
contact_id: contact.id,
|
contact_id: contact.id,
|
||||||
contact_type: contact.contact_type,
|
contact_type: contact.contact_type,
|
||||||
});
|
});
|
||||||
}, [updateData, index, id]);
|
},
|
||||||
|
[updateData, index, id],
|
||||||
|
);
|
||||||
|
|
||||||
const error = errors?.[index]?.[id];
|
const error = errors?.[index]?.[id];
|
||||||
|
|
||||||
@@ -27,8 +30,8 @@ export default function ContactsListCellRenderer({
|
|||||||
Classes.FILL,
|
Classes.FILL,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<ContactsListField
|
<ContactSelecetList
|
||||||
contacts={contacts}
|
contactsList={contacts}
|
||||||
onContactSelected={handleContactSelected}
|
onContactSelected={handleContactSelected}
|
||||||
selectedContactId={original?.contact_id}
|
selectedContactId={original?.contact_id}
|
||||||
selectedContactType={original?.contact_type}
|
selectedContactType={original?.contact_type}
|
||||||
|
|||||||
Reference in New Issue
Block a user