aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/pages/instant/config_generator_address_input.tsx
blob: ccbaf448279e1af03e05bb19976a8ebbdab6e23f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { colors } from '@0x/react-shared';
import { addressUtils } from '@0x/utils';
import * as _ from 'lodash';
import * as React from 'react';

import { Container } from 'ts/components/ui/container';
import { Input } from 'ts/components/ui/input';
import { Text } from 'ts/components/ui/text';

export interface ConfigGeneratorAddressInputProps {
    value?: string;
    onChange?: (address: string, isValid: boolean) => void;
}

export interface ConfigGeneratorAddressInputState {
    errMsg: string;
}

export class ConfigGeneratorAddressInput extends React.Component<
    ConfigGeneratorAddressInputProps,
    ConfigGeneratorAddressInputState
> {
    public state = {
        errMsg: '',
    };
    public render(): React.ReactNode {
        const { errMsg } = this.state;
        const hasError = !_.isEmpty(errMsg);
        const border = hasError ? '1px solid red' : undefined;
        return (
            <Container height="80px">
                <Input
                    width="100%"
                    fontSize="16px"
                    padding="0.7em 1em"
                    value={this.props.value}
                    onChange={this._handleChange}
                    placeholder="0xe99...aa8da4"
                    border={border}
                />
                <Container marginTop="5px" isHidden={!hasError} height="25px">
                    <Text fontSize="14px" fontColor={colors.grey} fontStyle="italic">
                        {errMsg}
                    </Text>
                </Container>
            </Container>
        );
    }

    private readonly _handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
        const address = event.target.value;
        const isValidAddress = addressUtils.isAddress(address.toLowerCase()) || address === '';
        const errMsg = isValidAddress ? '' : 'Please enter a valid Ethereum address';
        this.setState({
            errMsg,
        });
        this.props.onChange(address, isValidAddress);
    };
}