|
| 1 | +import React, { |
| 2 | + ChangeEvent, |
| 3 | + FunctionComponent, |
| 4 | + useEffect, |
| 5 | + useRef, |
| 6 | + useState, |
| 7 | +} from 'react'; |
| 8 | +import classnames from 'classnames'; |
| 9 | +import { |
| 10 | + CaipAccountId, |
| 11 | + CaipChainId, |
| 12 | + parseCaipAccountId, |
| 13 | + parseCaipChainId, |
| 14 | +} from '@metamask/utils'; |
| 15 | +import { |
| 16 | + Box, |
| 17 | + FormTextField, |
| 18 | + FormTextFieldProps, |
| 19 | + FormTextFieldSize, |
| 20 | + Icon, |
| 21 | + IconName, |
| 22 | + Label, |
| 23 | + Text, |
| 24 | +} from '../../../component-library'; |
| 25 | +import { useSnapInterfaceContext } from '../../../../contexts/snaps'; |
| 26 | +import { |
| 27 | + AlignItems, |
| 28 | + BackgroundColor, |
| 29 | + BorderColor, |
| 30 | + BorderRadius, |
| 31 | + Display, |
| 32 | + FlexDirection, |
| 33 | + FontWeight, |
| 34 | + IconColor, |
| 35 | + TextVariant, |
| 36 | +} from '../../../../helpers/constants/design-system'; |
| 37 | +import { SnapUIAvatar } from '../snap-ui-avatar'; |
| 38 | +import { useDisplayName } from '../../../../hooks/snaps/useDisplayName'; |
| 39 | + |
| 40 | +export type SnapUIAddressInputProps = { |
| 41 | + name: string; |
| 42 | + form?: string; |
| 43 | + label?: string; |
| 44 | + chainId: CaipChainId; |
| 45 | + displayAvatar?: boolean; |
| 46 | +}; |
| 47 | + |
| 48 | +export const SnapUIAddressInput: FunctionComponent< |
| 49 | + SnapUIAddressInputProps & FormTextFieldProps<'div'> |
| 50 | +> = ({ name, form, label, chainId, displayAvatar = true, error, ...props }) => { |
| 51 | + const { handleInputChange, getValue, focusedInput, setCurrentFocusedInput } = |
| 52 | + useSnapInterfaceContext(); |
| 53 | + |
| 54 | + const inputRef = useRef<HTMLDivElement>(null); |
| 55 | + const initialValue = getValue(name, form) as string; |
| 56 | + const { namespace, reference } = parseCaipChainId(chainId); |
| 57 | + |
| 58 | + const [value, setValue] = useState( |
| 59 | + initialValue |
| 60 | + ? parseCaipAccountId(initialValue as CaipAccountId).address |
| 61 | + : '', |
| 62 | + ); |
| 63 | + |
| 64 | + const displayName = useDisplayName({ |
| 65 | + address: value, |
| 66 | + chain: { |
| 67 | + namespace, |
| 68 | + reference, |
| 69 | + }, |
| 70 | + chainId, |
| 71 | + }); |
| 72 | + |
| 73 | + /* |
| 74 | + * Focus input if the last focused input was this input |
| 75 | + * This avoids losing the focus when the UI is re-rendered |
| 76 | + */ |
| 77 | + useEffect(() => { |
| 78 | + if (inputRef.current && name === focusedInput) { |
| 79 | + (inputRef.current.querySelector('input') as HTMLInputElement).focus(); |
| 80 | + } |
| 81 | + }, [inputRef]); |
| 82 | + |
| 83 | + const handleChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 84 | + setValue(event.target.value); |
| 85 | + |
| 86 | + const newValue = event.target.value |
| 87 | + ? `${chainId}:${event.target.value}` |
| 88 | + : ''; |
| 89 | + |
| 90 | + handleInputChange(name, newValue, form); |
| 91 | + }; |
| 92 | + |
| 93 | + const handleFocus = () => setCurrentFocusedInput(name); |
| 94 | + const handleBlur = () => setCurrentFocusedInput(null); |
| 95 | + |
| 96 | + const handleClear = () => { |
| 97 | + setValue(''); |
| 98 | + handleInputChange(name, '', form); |
| 99 | + }; |
| 100 | + |
| 101 | + const MatchedAccountInfo = () => { |
| 102 | + return ( |
| 103 | + <Box display={Display.Flex} flexDirection={FlexDirection.Column}> |
| 104 | + {label && ( |
| 105 | + <Label className={classnames('mm-form-text-field__label')}> |
| 106 | + {label} |
| 107 | + </Label> |
| 108 | + )} |
| 109 | + <Box |
| 110 | + display={Display.Flex} |
| 111 | + backgroundColor={BackgroundColor.backgroundDefault} |
| 112 | + alignItems={AlignItems.center} |
| 113 | + borderWidth={1} |
| 114 | + borderRadius={BorderRadius.LG} |
| 115 | + borderColor={BorderColor.borderMuted} |
| 116 | + paddingLeft={4} |
| 117 | + paddingRight={4} |
| 118 | + gap={2} |
| 119 | + style={{ |
| 120 | + height: '48px', |
| 121 | + }} |
| 122 | + > |
| 123 | + {displayAvatar && ( |
| 124 | + <SnapUIAvatar address={`${chainId}:${value}`} size="sm" /> |
| 125 | + )} |
| 126 | + <Box |
| 127 | + display={Display.Flex} |
| 128 | + alignItems={AlignItems.center} |
| 129 | + gap={2} |
| 130 | + style={{ |
| 131 | + flex: 1, |
| 132 | + minWidth: 0, |
| 133 | + }} |
| 134 | + > |
| 135 | + <Box |
| 136 | + flexDirection={FlexDirection.Column} |
| 137 | + gap={2} |
| 138 | + style={{ |
| 139 | + minWidth: 0, |
| 140 | + flex: 1, |
| 141 | + }} |
| 142 | + > |
| 143 | + <Text fontWeight={FontWeight.Medium}>{displayName}</Text> |
| 144 | + <Text variant={TextVariant.bodyXs} ellipsis> |
| 145 | + {value} |
| 146 | + </Text> |
| 147 | + </Box> |
| 148 | + </Box> |
| 149 | + <Icon |
| 150 | + onClick={handleClear} |
| 151 | + name={IconName.Close} |
| 152 | + color={IconColor.infoDefault} |
| 153 | + style={{ |
| 154 | + cursor: 'pointer', |
| 155 | + flexShrink: 0, |
| 156 | + }} |
| 157 | + /> |
| 158 | + </Box> |
| 159 | + </Box> |
| 160 | + ); |
| 161 | + }; |
| 162 | + |
| 163 | + if (displayName) { |
| 164 | + return <MatchedAccountInfo />; |
| 165 | + } |
| 166 | + |
| 167 | + return ( |
| 168 | + <FormTextField |
| 169 | + ref={inputRef} |
| 170 | + onFocus={handleFocus} |
| 171 | + onBlur={handleBlur} |
| 172 | + className={classnames('snap-ui-renderer__address-input', { |
| 173 | + 'snap-ui-renderer__field': label !== undefined, |
| 174 | + })} |
| 175 | + id={name} |
| 176 | + value={value} |
| 177 | + onChange={handleChange} |
| 178 | + label={label} |
| 179 | + error={Boolean(error)} |
| 180 | + size={FormTextFieldSize.Lg} |
| 181 | + helpText={error} |
| 182 | + textFieldProps={{ |
| 183 | + borderRadius: BorderRadius.LG, |
| 184 | + }} |
| 185 | + startAccessory={ |
| 186 | + displayAvatar && value ? ( |
| 187 | + <SnapUIAvatar address={`${chainId}:${value}`} size="sm" /> |
| 188 | + ) : null |
| 189 | + } |
| 190 | + endAccessory={ |
| 191 | + value ? ( |
| 192 | + <Icon |
| 193 | + onClick={handleClear} |
| 194 | + name={IconName.Close} |
| 195 | + color={IconColor.infoDefault} |
| 196 | + style={{ cursor: 'pointer' }} |
| 197 | + /> |
| 198 | + ) : null |
| 199 | + } |
| 200 | + {...props} |
| 201 | + /> |
| 202 | + ); |
| 203 | +}; |
0 commit comments