From 29d934006b05973dba761b7b58811521e086d7cc Mon Sep 17 00:00:00 2001 From: katspaugh Date: Thu, 22 Jun 2023 17:15:42 +0200 Subject: [PATCH] Fix: address book entry dialog --- .../address-book/EntryDialog/index.tsx | 17 ++++++------ .../common/AddressBookInput/index.tsx | 27 ++++++++++++++++--- .../common/AddressBookInput/styles.module.css | 6 +++++ src/components/common/AddressInput/index.tsx | 13 +++++---- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/components/address-book/EntryDialog/index.tsx b/src/components/address-book/EntryDialog/index.tsx index af9571d0e9..fe63ae7b08 100644 --- a/src/components/address-book/EntryDialog/index.tsx +++ b/src/components/address-book/EntryDialog/index.tsx @@ -1,8 +1,5 @@ -import Box from '@mui/material/Box' -import Button from '@mui/material/Button' -import DialogActions from '@mui/material/DialogActions' -import DialogContent from '@mui/material/DialogContent' -import type { ReactElement } from 'react' +import type { ReactElement, BaseSyntheticEvent } from 'react' +import { Box, Button, DialogActions, DialogContent } from '@mui/material' import { FormProvider, useForm } from 'react-hook-form' import AddressInput from '@/components/common/AddressInput' @@ -41,16 +38,20 @@ const EntryDialog = ({ const { handleSubmit, formState } = methods - const onSubmit = (data: AddressEntry) => { + const submitCallback = handleSubmit((data: AddressEntry) => { dispatch(upsertAddressBookEntry({ ...data, chainId: chainId || currentChainId })) - handleClose() + }) + + const onSubmit = (e: BaseSyntheticEvent) => { + e.stopPropagation() + submitCallback(e) } return ( -
+ diff --git a/src/components/common/AddressBookInput/index.tsx b/src/components/common/AddressBookInput/index.tsx index 8a04fad4f4..6d7a78e393 100644 --- a/src/components/common/AddressBookInput/index.tsx +++ b/src/components/common/AddressBookInput/index.tsx @@ -6,6 +6,7 @@ import useAddressBook from '@/hooks/useAddressBook' import AddressInput, { type AddressInputProps } from '../AddressInput' import EthHashInfo from '../EthHashInfo' import InfoIcon from '@/public/images/notifications/info.svg' +import EntryDialog from '@/components/address-book/EntryDialog' import css from './styles.module.css' const abFilterOptions = createFilterOptions({ @@ -15,11 +16,12 @@ const abFilterOptions = createFilterOptions({ /** * Temporary component until revamped safe components are done */ -const AddressBookInput = ({ name, canAdd, ...props }: AddressInputProps): ReactElement => { +const AddressBookInput = ({ name, canAdd, ...props }: AddressInputProps & { canAdd?: boolean }): ReactElement => { const addressBook = useAddressBook() const { setValue, control } = useFormContext() const addressValue = useWatch({ name, control }) const [open, setOpen] = useState(false) + const [openAddressBook, setOpenAddressBook] = useState(false) const addressBookEntries = Object.entries(addressBook).map(([address, name]) => ({ label: address, @@ -35,6 +37,12 @@ const AddressBookInput = ({ name, canAdd, ...props }: AddressInputProps): ReactE setOpen((value) => !value) } + const onAddressBookClick = canAdd + ? () => { + setOpenAddressBook(true) + } + : undefined + return ( <> )} /> {canAdd ? ( - This is an unknown address. Consider adding it to your address book. + + This is an unknown address. You can{' '} + + add it to your address book + + . + ) : null} + + {openAddressBook && ( + setOpenAddressBook(false)} + defaultValues={{ name: '', address: addressValue }} + /> + )} ) } diff --git a/src/components/common/AddressBookInput/styles.module.css b/src/components/common/AddressBookInput/styles.module.css index f9e328c609..c1366746d2 100644 --- a/src/components/common/AddressBookInput/styles.module.css +++ b/src/components/common/AddressBookInput/styles.module.css @@ -12,3 +12,9 @@ .unknownAddress svg { height: auto; } + +.unknownAddress a { + color: inherit; + text-decoration: underline; + cursor: pointer; +} diff --git a/src/components/common/AddressInput/index.tsx b/src/components/common/AddressInput/index.tsx index c0ae50d9f5..fae4fc7997 100644 --- a/src/components/common/AddressInput/index.tsx +++ b/src/components/common/AddressInput/index.tsx @@ -19,18 +19,17 @@ import { cleanInputValue, parsePrefixedAddress } from '@/utils/addresses' import useDebounce from '@/hooks/useDebounce' import CaretDownIcon from '@/public/images/common/caret-down.svg' import SaveAddressIcon from '@/public/images/common/save-address.svg' -import EntryDialog from '@/components/address-book/EntryDialog' import classnames from 'classnames' import css from './styles.module.css' export type AddressInputProps = TextFieldProps & { name: string address?: string - canAdd?: boolean onOpenListClick?: () => void isAutocompleteOpen?: boolean validate?: Validate deps?: string | string[] + onAddressBookClick?: () => void } const AddressInput = ({ @@ -39,7 +38,7 @@ const AddressInput = ({ required = true, onOpenListClick, isAutocompleteOpen, - canAdd, + onAddressBookClick, deps, ...props }: AddressInputProps): ReactElement => { @@ -55,7 +54,6 @@ const AddressInput = ({ const watchedValue = useWatch({ name, control }) const currentShortName = currentChain?.shortName || '' const [isValidating, setIsValidating] = useState(false) - const [open, setOpen] = useState(false) // Fetch an ENS resolution for the current address const isDomainLookupEnabled = !!currentChain && hasFeature(currentChain, FEATURES.DOMAIN_LOOKUP) @@ -91,12 +89,14 @@ const AddressInput = ({ ) : !props.disabled ? ( <> - {canAdd && ( - setOpen(true)}> + {onAddressBookClick && ( + )} + + {onOpenListClick && ( - {open && setOpen(false)} defaultValues={{ name: '', address: watchedValue }} />} ) }