1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00
metamask-extension/ui/pages/send/send-content/add-recipient/domain-input.component.js
Garrett Bear 3e520214c9
update icons to ts enum version (#18698)
Co-authored-by: Nidhi Kumari <nidhi.kumari@consensys.net>
Co-authored-by: George Marshall <george.marshall@consensys.net>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
2023-04-24 09:19:19 -05:00

176 lines
5.2 KiB
JavaScript

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { addHexPrefix } from '../../../../../app/scripts/lib/util';
import { isValidDomainName } from '../../../../helpers/utils/util';
import {
isBurnAddress,
isValidHexAddress,
} from '../../../../../shared/modules/hexstring-utils';
import {
ButtonIcon,
Icon,
IconName,
IconSize,
} from '../../../../components/component-library';
import { IconColor } from '../../../../helpers/constants/design-system';
export default class DomainInput extends Component {
static contextTypes = {
t: PropTypes.func,
metricsEvent: PropTypes.func,
};
static propTypes = {
className: PropTypes.string,
selectedAddress: PropTypes.string,
selectedName: PropTypes.string,
scanQrCode: PropTypes.func,
onPaste: PropTypes.func,
onValidAddressTyped: PropTypes.func,
internalSearch: PropTypes.bool,
userInput: PropTypes.string,
onChange: PropTypes.func.isRequired,
onReset: PropTypes.func.isRequired,
lookupEnsName: PropTypes.func.isRequired,
initializeDomainSlice: PropTypes.func.isRequired,
resetDomainResolution: PropTypes.func.isRequired,
};
componentDidMount() {
this.props.initializeDomainSlice();
}
onPaste = (event) => {
if (event.clipboardData.items?.length) {
const clipboardItem = event.clipboardData.items[0];
clipboardItem?.getAsString((text) => {
const input = text.trim();
if (
!isBurnAddress(input) &&
isValidHexAddress(input, { mixedCaseUseChecksum: true })
) {
this.props.onPaste(addHexPrefix(input));
}
});
}
};
onChange = ({ target: { value } }) => {
const {
onValidAddressTyped,
internalSearch,
onChange,
lookupEnsName,
resetDomainResolution,
} = this.props;
const input = value.trim();
onChange(input);
if (internalSearch) {
return null;
}
// Empty ENS state if input is empty
// maybe scan ENS
if (isValidDomainName(input)) {
lookupEnsName(input);
} else {
resetDomainResolution();
if (
onValidAddressTyped &&
!isBurnAddress(input) &&
isValidHexAddress(input, { mixedCaseUseChecksum: true })
) {
onValidAddressTyped(addHexPrefix(input));
}
}
return null;
};
render() {
const { t } = this.context;
const { className, selectedAddress, selectedName, userInput } = this.props;
const hasSelectedAddress = Boolean(selectedAddress);
return (
<div className={classnames('ens-input', className)}>
<div
className={classnames('ens-input__wrapper', {
'ens-input__wrapper__status-icon--error': false,
'ens-input__wrapper__status-icon--valid': false,
'ens-input__wrapper--valid': hasSelectedAddress,
})}
>
{hasSelectedAddress ? (
<Icon
className="ens-input__wrapper__status-icon"
name={IconName.Check}
color={IconColor.successDefault}
/>
) : (
<Icon
name={IconName.Search}
color={IconColor.iconMuted}
className="ens-input__wrapper__status-icon"
/>
)}
{hasSelectedAddress ? (
<>
<div className="ens-input__wrapper__input ens-input__wrapper__input--selected">
<div className="ens-input__selected-input__title">
{selectedName || selectedAddress}
</div>
{selectedName !== selectedAddress && (
<div className="ens-input__selected-input__subtitle">
{selectedAddress}
</div>
)}
</div>
<ButtonIcon
iconName={IconName.Close}
ariaLabel={t('close')}
onClick={this.props.onReset}
className="ens-input__wrapper__action-icon-button"
size={IconSize.Sm}
/>
</>
) : (
<>
<input
className="ens-input__wrapper__input"
type="text"
dir="auto"
placeholder={t('recipientAddressPlaceholder')}
onChange={this.onChange}
onPaste={this.onPaste}
spellCheck="false"
value={selectedAddress || userInput}
autoFocus
data-testid="ens-input"
/>
<ButtonIcon
className="ens-input__wrapper__action-icon-button"
onClick={() => {
if (userInput) {
this.props.onReset();
} else {
this.props.scanQrCode();
}
}}
iconName={userInput ? IconName.Close : IconName.ScanBarcode}
ariaLabel={t(userInput ? 'close' : 'scanQrCode')}
color={
userInput ? IconColor.iconDefault : IconColor.primaryDefault
}
/>
</>
)}
</div>
</div>
);
}
}