Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1175 from 0xProject/feature/instant/input-fees-ro…
Browse files Browse the repository at this point in the history
…unding

[instant] Create a ScalingInput component and use it in the amount input and upgrade to styled-components v4
  • Loading branch information
fragosti authored Oct 26, 2018
2 parents d2bf23d + 30809e6 commit 4c5b26d
Show file tree
Hide file tree
Showing 22 changed files with 473 additions and 157 deletions.
3 changes: 2 additions & 1 deletion packages/instant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-devtools-extension": "^2.13.5",
"styled-components": "^3.4.9",
"styled-components": "^4.0.2",
"ts-optchain": "^0.1.1"
},
"devDependencies": {
Expand All @@ -73,6 +73,7 @@
"@types/react-dom": "^16.0.8",
"@types/react-redux": "^6.0.9",
"@types/redux": "^3.6.0",
"@types/styled-components": "^4.0.1",
"awesome-typescript-loader": "^5.2.1",
"enzyme": "^3.6.0",
"enzyme-adapter-react-16": "^1.5.0",
Expand Down
49 changes: 0 additions & 49 deletions packages/instant/src/components/amount_input.tsx

This file was deleted.

10 changes: 7 additions & 3 deletions packages/instant/src/components/animations/slide_animations.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { Keyframes } from 'styled-components';

import { keyframes, styled } from '../../style/theme';
import { css, keyframes, styled } from '../../style/theme';

const slideKeyframeGenerator = (fromY: string, toY: string) => keyframes`
from {
Expand All @@ -15,7 +16,7 @@ const slideKeyframeGenerator = (fromY: string, toY: string) => keyframes`
`;

export interface SlideAnimationProps {
keyframes: string;
keyframes: Keyframes;
animationType: string;
animationDirection?: string;
}
Expand All @@ -24,7 +25,10 @@ export const SlideAnimation =
styled.div <
SlideAnimationProps >
`
animation-name: ${props => props.keyframes};
animation-name: ${props =>
css`
${props.keyframes};
`};
animation-duration: 0.3s;
animation-timing-function: ${props => props.animationType};
animation-delay: 0s;
Expand Down
39 changes: 0 additions & 39 deletions packages/instant/src/components/asset_amount_input.tsx

This file was deleted.

84 changes: 84 additions & 0 deletions packages/instant/src/components/erc20_asset_amount_input.tsx
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;
};
}
6 changes: 4 additions & 2 deletions packages/instant/src/components/instant_heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import * as React from 'react';

import { SelectedAssetAmountInput } from '../containers/selected_asset_amount_input';
import { SelectedERC20AssetAmountInput } from '../containers/selected_erc20_asset_amount_input';
import { ColorOption } from '../style/theme';
import { AsyncProcessState, OrderProcessState, OrderState } from '../types';
import { format } from '../util/format';
Expand Down Expand Up @@ -48,7 +48,9 @@ export class InstantHeading extends React.Component<InstantHeadingProps, {}> {
</Text>
</Container>
<Flex direction="row" justify="space-between">
<SelectedAssetAmountInput fontSize="45px" />
<Flex height="60px">
<SelectedERC20AssetAmountInput startingFontSizePx={38} />
</Flex>
<Flex direction="column" justify="space-between">
{iconOrAmounts}
</Flex>
Expand Down
52 changes: 52 additions & 0 deletions packages/instant/src/components/scaling_amount_input.tsx
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);
};
}
Loading

0 comments on commit 4c5b26d

Please sign in to comment.