fix(phone/messages): fixed autocomplete getting bricked (#138)

This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-03-05 16:11:50 -05:00
1 parent 6419245d93
commit bdbf342270
1 file changed
+22 -20
@@ -13,15 +13,14 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const [participants, setParticipants] = useState([]); const [participants, setParticipants] = useState([]);
const [label, setLabel] = useState(''); const [label, setLabel] = useState('');
const { contacts } = useContacts(); const { getContactByNumber, contacts } = useContacts();
useEffect(() => { useEffect(() => {
if (phoneNumber) { if (phoneNumber) {
const find = contacts.find((c) => c.number === phoneNumber); const find = getContactByNumber(phoneNumber) || { number: phoneNumber };
const newPart = find || { number: phoneNumber }; setParticipants((all) => [...all, find]);
setParticipants((all) => [...all, newPart]);
} }
}, [phoneNumber, contacts]); }, [phoneNumber, getContactByNumber]);
const isGroupChat = participants.length > 1; const isGroupChat = participants.length > 1;
@@ -47,7 +46,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
[history, label, participants, isGroupChat], [history, label, participants, isGroupChat],
); );
const onAutocompleteChange = useCallback((_e, value: any) => { const onAutocompleteChange = (_e, value: any) => {
const lastIdx = value.length - 1; const lastIdx = value.length - 1;
if (typeof value[lastIdx] === 'string') { if (typeof value[lastIdx] === 'string') {
value.splice(lastIdx, 1, { number: value[lastIdx] }); value.splice(lastIdx, 1, { number: value[lastIdx] });
@@ -55,21 +54,24 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
return; return;
} }
setParticipants(value as any[]); setParticipants(value as any[]);
}, []); };
const renderAutocompleteInput = useCallback( const renderAutocompleteInput = (params) => (
(params) => ( <TextField
<TextField {...params}
{...params} fullWidth
fullWidth label={t('APPS_MESSAGES_INPUT_NAME_OR_NUMBER')}
label={t('APPS_MESSAGES_INPUT_NAME_OR_NUMBER')} inputProps={{
inputProps={{ ...params.inputProps,
...params.inputProps, onKeyPress: (e) => {
autoFocus: true, if (e.key === 'Enter' && e.currentTarget.value) {
}} e.preventDefault();
/> onAutocompleteChange(e, [...participants, e.currentTarget.value]);
), }
[t], },
autoFocus: true,
}}
/>
); );
const submitDisabled = !participants.length || (isGroupChat && !label); const submitDisabled = !participants.length || (isGroupChat && !label);