import classnames from 'classnames'; import { Token } from 'config/data'; import { isPositiveIntegerOrZero, isValidETHAddress } from 'libs/validators'; import React from 'react'; import translate from 'translations'; interface Props { onSave(params: Token): void; } interface State { address: string; symbol: string; decimal: string; } export default class AddCustomTokenForm extends React.Component { public state = { address: '', symbol: '', decimal: '' }; public render() { const { address, symbol, decimal } = this.state; const inputClasses = 'AddCustom-field-input form-control input-sm'; const errors = this.getErrors(); const fields = [ { name: 'address', value: address, label: translate('TOKEN_Addr') }, { name: 'symbol', value: symbol, label: translate('TOKEN_Symbol') }, { name: 'decimal', value: decimal, label: translate('TOKEN_Dec') } ]; return (
{fields.map(field => { return ( ); })}
); } public getErrors() { const { address, symbol, decimal } = this.state; const errors = { decimal: false, address: false, symbol: false }; if (!isPositiveIntegerOrZero(parseInt(decimal, 10))) { errors.decimal = true; } if (!isValidETHAddress(address)) { errors.address = true; } if (!symbol) { errors.symbol = true; } return errors; } public isValid() { return !Object.keys(this.getErrors()).length; } public onFieldChange = (e: React.SyntheticEvent) => { // TODO: typescript bug: https://github.com/Microsoft/TypeScript/issues/13948 const name: any = (e.target as HTMLInputElement).name; const value = (e.target as HTMLInputElement).value; this.setState({ [name]: value }); }; public onSave = (ev: React.SyntheticEvent) => { ev.preventDefault(); if (!this.isValid()) { return; } const { address, symbol, decimal } = this.state; this.props.onSave({ address, symbol, decimal: parseInt(decimal, 10) }); }; }