From bdbf3422706e44aeaf941f3bfbed336cbfac0b9b Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Fri, 5 Mar 2021 16:11:50 -0500 Subject: [PATCH] fix(phone/messages): fixed autocomplete getting bricked (#138) --- .../components/form/NewMessageGroupForm.tsx | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index afa9481..ac40cd1 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -13,15 +13,14 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const { t } = useTranslation(); const [participants, setParticipants] = useState([]); const [label, setLabel] = useState(''); - const { contacts } = useContacts(); + const { getContactByNumber, contacts } = useContacts(); useEffect(() => { if (phoneNumber) { - const find = contacts.find((c) => c.number === phoneNumber); - const newPart = find || { number: phoneNumber }; - setParticipants((all) => [...all, newPart]); + const find = getContactByNumber(phoneNumber) || { number: phoneNumber }; + setParticipants((all) => [...all, find]); } - }, [phoneNumber, contacts]); + }, [phoneNumber, getContactByNumber]); const isGroupChat = participants.length > 1; @@ -47,7 +46,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { [history, label, participants, isGroupChat], ); - const onAutocompleteChange = useCallback((_e, value: any) => { + const onAutocompleteChange = (_e, value: any) => { const lastIdx = value.length - 1; if (typeof value[lastIdx] === 'string') { value.splice(lastIdx, 1, { number: value[lastIdx] }); @@ -55,21 +54,24 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { return; } setParticipants(value as any[]); - }, []); + }; - const renderAutocompleteInput = useCallback( - (params) => ( - - ), - [t], + const renderAutocompleteInput = (params) => ( + { + if (e.key === 'Enter' && e.currentTarget.value) { + e.preventDefault(); + onAutocompleteChange(e, [...participants, e.currentTarget.value]); + } + }, + autoFocus: true, + }} + /> ); const submitDisabled = !participants.length || (isGroupChat && !label);