MyCrypto/common/components/AmountField.tsx
Connor Bryan 0d13dc93c5 Add additional validation to Amount input field (#1982)
* Adjust the validator and update validity class logic on AmountField

* Improve readability and add a condition to invalidity for Input

* Undo changes to Input and pass down showInvalidWithoutValue

* Fix prop ordering issue

* Add removed validator

* Adjust logic for NoneField
2018-07-02 16:51:51 -05:00

50 lines
1.5 KiB
TypeScript

import React from 'react';
import translate from 'translations';
import { UnitDropDown, SendEverything } from 'components';
import { Input } from 'components/ui';
import { AmountFieldFactory } from './AmountFieldFactory';
interface Props {
hasUnitDropdown?: boolean;
hasSendEverything?: boolean;
showAllTokens?: boolean;
showInvalidWithoutValue?: boolean;
customValidator?(rawAmount: string): boolean;
}
export const AmountField: React.SFC<Props> = ({
hasUnitDropdown,
hasSendEverything,
showAllTokens,
customValidator,
showInvalidWithoutValue
}) => (
<AmountFieldFactory
withProps={({ currentValue: { raw }, isValid, onChange, readOnly }) => (
<div className="AmountField input-group-wrapper">
<label className="AmountField-group input-group input-group-inline">
<div className="input-group-header">{translate('SEND_AMOUNT_SHORT')}</div>
<Input
isValid={isAmountValid(raw, customValidator, isValid)}
type="number"
placeholder="1"
value={raw}
readOnly={!!readOnly}
onChange={onChange}
showInvalidWithoutValue={showInvalidWithoutValue}
/>
{hasSendEverything && <SendEverything />}
{hasUnitDropdown && <UnitDropDown showAllTokens={showAllTokens} />}
</label>
</div>
)}
/>
);
const isAmountValid = (
raw: string,
customValidator: ((rawAmount: string) => boolean) | undefined,
isValid: boolean
) => (customValidator ? customValidator(raw) : isValid);