From d24571d76791395b57cde81fb1703aba6f11b9ed Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 20 Feb 2019 13:20:50 +0100 Subject: [PATCH] Fix InputBalance (with value prop, si calc) (#738) * Fix InputBalance (with value prop, si calc) * Cater for si and non-si cases (with si apply for default) * Cleanup InputNumber interface --- packages/ui-app/src/InputNumber.tsx | 35 ++++++++++++----------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/ui-app/src/InputNumber.tsx b/packages/ui-app/src/InputNumber.tsx index 25c7ef738f..09282384ea 100644 --- a/packages/ui-app/src/InputNumber.tsx +++ b/packages/ui-app/src/InputNumber.tsx @@ -42,26 +42,18 @@ type State = { const DEFAULT_BITLENGTH = BitLengthOption.NORMAL_NUMBERS as BitLength; const KEYS_ALLOWED: Array = [KEYS.ARROW_LEFT, KEYS.ARROW_RIGHT, KEYS.BACKSPACE, KEYS.ENTER, KEYS.ESCAPE, KEYS.TAB]; -function maxConservativeLength (maxValueLength: number): number { - const conservativenessFactor = 1; - - return maxValueLength - conservativenessFactor; -} - class InputNumber extends React.PureComponent { constructor (props: Props) { super(props); - const { defaultValue, value } = this.props; + const { defaultValue, isSi, value } = this.props; let valueBN = new BN(value || 0); const si = formatBalance.calcSi(valueBN.toString()); - valueBN = valueBN.div(new BN(10).pow(new BN(si.power))); - this.state = { - defaultValue: defaultValue - ? defaultValue.toString() - : valueBN.toString(), + defaultValue: isSi + ? new BN(defaultValue || valueBN).div(new BN(10).pow(new BN(si.power))).toString() + : (defaultValue || valueBN).toString(), isPreKeyDown: false, isValid: !isUndefined(value), siOptions: formatBalance.getOptions().map(({ power, text, value }) => ({ @@ -94,7 +86,7 @@ class InputNumber extends React.PureComponent { render () { const { bitLength = DEFAULT_BITLENGTH, className, defaultValue = '0', isSi, isDisabled, maxLength, style, t } = this.props; const { isValid } = this.state; - const maxValueLength = this.maxValue(bitLength).toString().length; + const maxValueLength = this.maxValue(bitLength).toString().length - 1; const value = this.state.defaultValue || defaultValue; return ( @@ -109,7 +101,7 @@ class InputNumber extends React.PureComponent { isAction={isSi} isDisabled={isDisabled} isError={!isValid} - maxLength={maxLength || maxConservativeLength(maxValueLength)} + maxLength={maxLength || maxValueLength} onChange={this.onChange} onKeyDown={this.onKeyDown} onKeyUp={this.onKeyUp} @@ -145,11 +137,13 @@ class InputNumber extends React.PureComponent { ); } - private maxValue = (bitLength?: number): BN => - new BN(2).pow(new BN(bitLength || DEFAULT_BITLENGTH)).subn(1) + private maxValue (bitLength?: number): BN { + return new BN(2).pow(new BN(bitLength || DEFAULT_BITLENGTH)).subn(1); + } - private isValidBitLength = (value: BN, bitLength?: number): boolean => - value.bitLength() <= (bitLength || DEFAULT_BITLENGTH) + private isValidBitLength (value: BN, bitLength?: number): boolean { + return value.bitLength() <= (bitLength || DEFAULT_BITLENGTH); + } private isValidKey = (event: React.KeyboardEvent, isPreKeyDown: boolean): boolean => { const { value: previousValue } = event.target as HTMLInputElement; @@ -182,7 +176,7 @@ class InputNumber extends React.PureComponent { return true; } - private isValidNumber = (input: BN, bitLength: number = DEFAULT_BITLENGTH): boolean => { + private isValidNumber (input: BN, bitLength: number = DEFAULT_BITLENGTH): boolean { const maxBN = this.maxValue(bitLength); if (!input.lt(maxBN) || !this.isValidBitLength(input, bitLength)) { @@ -293,8 +287,7 @@ class InputNumber extends React.PureComponent { } export { - InputNumber, - maxConservativeLength + InputNumber }; export default translate(InputNumber);