add AddressInput with ENS support to create safe form

This commit is contained in:
Mikhail Mikheev 2019-07-29 14:08:28 +04:00
parent 6e5bdf90e4
commit 167281179f
6 changed files with 114 additions and 97 deletions

View File

@ -19,6 +19,7 @@ type Props = {
fieldMutator: Function, fieldMutator: Function,
testId?: string, testId?: string,
validators?: Function[], validators?: Function[],
inputAdornment?: React.Element,
} }
const isValidEnsName = name => /^([\w-]+\.)+(eth|test)$/.test(name) const isValidEnsName = name => /^([\w-]+\.)+(eth|test)$/.test(name)
@ -35,6 +36,7 @@ const AddressInput = ({
placeholder = 'Recipient*', placeholder = 'Recipient*',
fieldMutator, fieldMutator,
testId, testId,
inputAdornment,
validators = [], validators = [],
}: Props): React.Element<*> => ( }: Props): React.Element<*> => (
<> <>
@ -47,6 +49,7 @@ const AddressInput = ({
ifElseValidator(isValidEnsName, ensResolverHasAddress, mustBeEthereumAddress), ifElseValidator(isValidEnsName, ensResolverHasAddress, mustBeEthereumAddress),
...validators, ...validators,
)} )}
inputAdornment={inputAdornment}
placeholder={placeholder} placeholder={placeholder}
text={text} text={text}
className={className} className={className}

View File

@ -61,7 +61,7 @@ export const ok = () => undefined
export const mustBeEthereumAddress = simpleMemoize((address: Field) => { export const mustBeEthereumAddress = simpleMemoize((address: Field) => {
const isAddress: boolean = getWeb3().utils.isAddress(address) const isAddress: boolean = getWeb3().utils.isAddress(address)
return isAddress ? undefined : 'Address should be a valid Ethereum address' return isAddress ? undefined : 'Address should be a valid Ethereum address or ENS domain'
}) })
export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`) export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`)

View File

@ -7,6 +7,7 @@ import MenuItem from '@material-ui/core/MenuItem'
import Field from '~/components/forms/Field' import Field from '~/components/forms/Field'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import SelectField from '~/components/forms/SelectField' import SelectField from '~/components/forms/SelectField'
import AddressInput from '~/components/forms/AddressInput'
import { import {
required, composeValidators, noErrorsOn, mustBeInteger, minValue, required, composeValidators, noErrorsOn, mustBeInteger, minValue,
} from '~/components/forms/validator' } from '~/components/forms/validator'
@ -28,7 +29,7 @@ import Hairline from '~/components/layout/Hairline'
import trash from '~/assets/icons/trash.svg' import trash from '~/assets/icons/trash.svg'
import QRIcon from '~/assets/icons/qrcode.svg' import QRIcon from '~/assets/icons/qrcode.svg'
import ScanQRModal from './ScanQRModal' import ScanQRModal from './ScanQRModal'
import { getAddressValidators } from './validators' import { getAddressValidator } from './validators'
import { styles } from './style' import { styles } from './style'
type Props = { type Props = {
@ -135,7 +136,7 @@ const SafeOwners = (props: Props) => {
/> />
</Col> </Col>
<Col xs={6}> <Col xs={6}>
<Field <AddressInput
name={addressName} name={addressName}
component={TextField} component={TextField}
inputAdornment={ inputAdornment={
@ -147,8 +148,11 @@ const SafeOwners = (props: Props) => {
), ),
} }
} }
fieldMutator={(val) => {
form.mutators.setValue(addressName, val)
}}
type="text" type="text"
validate={getAddressValidators(otherAccounts, index)} validators={[getAddressValidator(otherAccounts, index)]}
placeholder="Owner Address*" placeholder="Owner Address*"
text="Owner Address" text="Owner Address"
/> />

View File

@ -1,17 +1,14 @@
// @flow // @flow
import { import {
required,
composeValidators,
uniqueAddress, uniqueAddress,
mustBeEthereumAddress,
} from '~/components/forms/validator' } from '~/components/forms/validator'
export const getAddressValidators = (addresses: string[], position: number) => { export const getAddressValidator = (addresses: string[], position: number) => {
// thanks Rich Harris // thanks Rich Harris
// https://twitter.com/Rich_Harris/status/1125850391155965952 // https://twitter.com/Rich_Harris/status/1125850391155965952
const copy = addresses.slice() const copy = addresses.slice()
copy[position] = copy[copy.length - 1] copy[position] = copy[copy.length - 1]
copy.pop() copy.pop()
return composeValidators(required, mustBeEthereumAddress, uniqueAddress(copy)) return uniqueAddress(copy)
} }

View File

@ -18,7 +18,6 @@ import { type Owner } from '~/routes/safe/store/models/owner'
import { import {
composeValidators, composeValidators,
required, required,
mustBeEthereumAddress,
minMaxLength, minMaxLength,
uniqueAddress, uniqueAddress,
} from '~/components/forms/validator' } from '~/components/forms/validator'
@ -28,6 +27,12 @@ export const ADD_OWNER_NAME_INPUT_TEST_ID = 'add-owner-name-input'
export const ADD_OWNER_ADDRESS_INPUT_TEST_ID = 'add-owner-address-testid' export const ADD_OWNER_ADDRESS_INPUT_TEST_ID = 'add-owner-address-testid'
export const ADD_OWNER_NEXT_BTN_TEST_ID = 'add-owner-next-btn' export const ADD_OWNER_NEXT_BTN_TEST_ID = 'add-owner-next-btn'
const formMutators = {
setOwnerAddress: (args, state, utils) => {
utils.changeValue(state, 'ownerAddress', () => args[0])
},
}
type Props = { type Props = {
onClose: () => void, onClose: () => void,
classes: Object, classes: Object,
@ -35,12 +40,6 @@ type Props = {
owners: List<Owner>, owners: List<Owner>,
} }
const formMutators = {
setOwnerAddress: (args, state, utils) => {
utils.changeValue(state, 'ownerAddress', () => args[0])
},
}
const OwnerForm = ({ const OwnerForm = ({
classes, onClose, onSubmit, owners, classes, onClose, onSubmit, owners,
}: Props) => { }: Props) => {

View File

@ -9,6 +9,7 @@ import OpenInNew from '@material-ui/icons/OpenInNew'
import Paragraph from '~/components/layout/Paragraph' import Paragraph from '~/components/layout/Paragraph'
import Row from '~/components/layout/Row' import Row from '~/components/layout/Row'
import GnoForm from '~/components/forms/GnoForm' import GnoForm from '~/components/forms/GnoForm'
import AddressInput from '~/components/forms/AddressInput'
import Col from '~/components/layout/Col' import Col from '~/components/layout/Col'
import Button from '~/components/layout/Button' import Button from '~/components/layout/Button'
import Block from '~/components/layout/Block' import Block from '~/components/layout/Block'
@ -22,7 +23,6 @@ import { type Owner } from '~/routes/safe/store/models/owner'
import { import {
composeValidators, composeValidators,
required, required,
mustBeEthereumAddress,
minMaxLength, minMaxLength,
uniqueAddress, uniqueAddress,
} from '~/components/forms/validator' } from '~/components/forms/validator'
@ -38,6 +38,12 @@ const openIconStyle = {
color: secondary, color: secondary,
} }
const formMutators = {
setOwnerAddress: (args, state, utils) => {
utils.changeValue(state, 'ownerAddress', () => args[0])
},
}
type Props = { type Props = {
onClose: () => void, onClose: () => void,
classes: Object, classes: Object,
@ -68,8 +74,11 @@ const OwnerForm = ({
</IconButton> </IconButton>
</Row> </Row>
<Hairline /> <Hairline />
<GnoForm onSubmit={handleSubmit}> <GnoForm onSubmit={handleSubmit} formMutators={formMutators}>
{() => ( {(...args) => {
const mutators = args[3]
return (
<React.Fragment> <React.Fragment>
<Block className={classes.formContainer}> <Block className={classes.formContainer}>
<Row> <Row>
@ -94,7 +103,11 @@ const OwnerForm = ({
<Paragraph size="md" color="disabled" noMargin> <Paragraph size="md" color="disabled" noMargin>
{ownerAddress} {ownerAddress}
</Paragraph> </Paragraph>
<Link className={classes.open} to={getEtherScanLink('address', ownerAddress, network)} target="_blank"> <Link
className={classes.open}
to={getEtherScanLink('address', ownerAddress, network)}
target="_blank"
>
<OpenInNew style={openIconStyle} /> <OpenInNew style={openIconStyle} />
</Link> </Link>
</Block> </Block>
@ -120,14 +133,14 @@ const OwnerForm = ({
</Row> </Row>
<Row margin="md"> <Row margin="md">
<Col xs={8}> <Col xs={8}>
<Field <AddressInput
name="ownerAddress" name="ownerAddress"
component={TextField} component={TextField}
type="text" validators={[ownerDoesntExist]}
validate={composeValidators(required, mustBeEthereumAddress, ownerDoesntExist)}
placeholder="Owner address*" placeholder="Owner address*"
text="Owner address*" text="Owner address*"
className={classes.addressInput} className={classes.addressInput}
fieldMutator={mutators.setOwnerAddress}
testId={REPLACE_OWNER_ADDRESS_INPUT_TEST_ID} testId={REPLACE_OWNER_ADDRESS_INPUT_TEST_ID}
/> />
</Col> </Col>
@ -150,7 +163,8 @@ const OwnerForm = ({
</Button> </Button>
</Row> </Row>
</React.Fragment> </React.Fragment>
)} )
}}
</GnoForm> </GnoForm>
</React.Fragment> </React.Fragment>
) )