This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1175 from 0xProject/feature/instant/input-fees-ro…
…unding [instant] Create a ScalingInput component and use it in the amount input and upgrade to styled-components v4
- Loading branch information
Showing
22 changed files
with
473 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
packages/instant/src/components/erc20_asset_amount_input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import * as _ from 'lodash'; | ||
import * as React from 'react'; | ||
|
||
import { ColorOption, transparentWhite } from '../style/theme'; | ||
import { ERC20Asset } from '../types'; | ||
import { assetUtils } from '../util/asset'; | ||
import { BigNumberInput } from '../util/big_number_input'; | ||
import { util } from '../util/util'; | ||
|
||
import { ScalingAmountInput } from './scaling_amount_input'; | ||
import { Container, Text } from './ui'; | ||
|
||
// Asset amounts only apply to ERC20 assets | ||
export interface ERC20AssetAmountInputProps { | ||
asset?: ERC20Asset; | ||
value?: BigNumberInput; | ||
onChange: (value?: BigNumberInput, asset?: ERC20Asset) => void; | ||
startingFontSizePx: number; | ||
fontColor?: ColorOption; | ||
} | ||
|
||
export interface ERC20AssetAmountInputState { | ||
currentFontSizePx: number; | ||
} | ||
|
||
export class ERC20AssetAmountInput extends React.Component<ERC20AssetAmountInputProps, ERC20AssetAmountInputState> { | ||
public static defaultProps = { | ||
onChange: util.boundNoop, | ||
}; | ||
constructor(props: ERC20AssetAmountInputProps) { | ||
super(props); | ||
this.state = { | ||
currentFontSizePx: props.startingFontSizePx, | ||
}; | ||
} | ||
public render(): React.ReactNode { | ||
const { asset, onChange, ...rest } = this.props; | ||
return ( | ||
<Container whiteSpace="nowrap"> | ||
<Container borderBottom={`1px solid ${transparentWhite}`} display="inline-block"> | ||
<ScalingAmountInput | ||
{...rest} | ||
textLengthThreshold={this._textLengthThresholdForAsset(asset)} | ||
maxFontSizePx={this.props.startingFontSizePx} | ||
onChange={this._handleChange} | ||
onFontSizeChange={this._handleFontSizeChange} | ||
/> | ||
</Container> | ||
<Container display="inline-flex" marginLeft="10px" title={assetUtils.bestNameForAsset(asset)}> | ||
<Text | ||
fontSize={`${this.state.currentFontSizePx}px`} | ||
fontColor={ColorOption.white} | ||
textTransform="uppercase" | ||
> | ||
{assetUtils.formattedSymbolForAsset(asset)} | ||
</Text> | ||
</Container> | ||
</Container> | ||
); | ||
} | ||
private readonly _handleChange = (value?: BigNumberInput): void => { | ||
this.props.onChange(value, this.props.asset); | ||
}; | ||
private readonly _handleFontSizeChange = (fontSizePx: number): void => { | ||
this.setState({ | ||
currentFontSizePx: fontSizePx, | ||
}); | ||
}; | ||
// For assets with symbols of different length, | ||
// start scaling the input at different character lengths | ||
private readonly _textLengthThresholdForAsset = (asset?: ERC20Asset): number => { | ||
if (_.isUndefined(asset)) { | ||
return 3; | ||
} | ||
const symbol = asset.metaData.symbol; | ||
if (symbol.length <= 3) { | ||
return 5; | ||
} | ||
if (symbol.length === 5) { | ||
return 3; | ||
} | ||
return 4; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as _ from 'lodash'; | ||
import * as React from 'react'; | ||
|
||
import { ColorOption } from '../style/theme'; | ||
import { BigNumberInput } from '../util/big_number_input'; | ||
import { util } from '../util/util'; | ||
|
||
import { ScalingInput } from './scaling_input'; | ||
|
||
export interface ScalingAmountInputProps { | ||
maxFontSizePx: number; | ||
textLengthThreshold: number; | ||
fontColor?: ColorOption; | ||
value?: BigNumberInput; | ||
onChange: (value?: BigNumberInput) => void; | ||
onFontSizeChange: (fontSizePx: number) => void; | ||
} | ||
|
||
export class ScalingAmountInput extends React.Component<ScalingAmountInputProps> { | ||
public static defaultProps = { | ||
onChange: util.boundNoop, | ||
onFontSizeChange: util.boundNoop, | ||
}; | ||
public render(): React.ReactNode { | ||
const { textLengthThreshold, fontColor, maxFontSizePx, value, onFontSizeChange } = this.props; | ||
return ( | ||
<ScalingInput | ||
maxFontSizePx={maxFontSizePx} | ||
textLengthThreshold={textLengthThreshold} | ||
onFontSizeChange={onFontSizeChange} | ||
fontColor={fontColor} | ||
onChange={this._handleChange} | ||
value={!_.isUndefined(value) ? value.toDisplayString() : ''} | ||
placeholder="0.00" | ||
emptyInputWidthCh={3.5} | ||
/> | ||
); | ||
} | ||
private readonly _handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => { | ||
const value = event.target.value; | ||
let bigNumberValue; | ||
if (!_.isEmpty(value)) { | ||
try { | ||
bigNumberValue = new BigNumberInput(value); | ||
} catch { | ||
// We don't want to allow values that can't be a BigNumber, so don't even call onChange. | ||
return; | ||
} | ||
} | ||
this.props.onChange(bigNumberValue); | ||
}; | ||
} |
Oops, something went wrong.