2017-11-12 19:45:52 +00:00
|
|
|
import { toTokenBase } from 'libs/units';
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
|
|
|
interface IChildren {
|
|
|
|
onUserInput: UnitConverter['onUserInput'];
|
|
|
|
convertedUnit: string;
|
|
|
|
}
|
|
|
|
interface IFakeEvent {
|
|
|
|
currentTarget: {
|
|
|
|
value: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
decimal: number;
|
|
|
|
children({ onUserInput, convertedUnit }: IChildren): React.ReactElement<any>;
|
2017-12-19 22:46:34 +00:00
|
|
|
onChange(baseUnit: IFakeEvent): void;
|
2017-11-12 19:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
userInput: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState = { userInput: '' };
|
|
|
|
|
|
|
|
export class UnitConverter extends Component<Props, State> {
|
|
|
|
public state: State = initialState;
|
|
|
|
|
|
|
|
public componentWillReceiveProps(nextProps: Props) {
|
|
|
|
const { userInput } = this.state;
|
|
|
|
|
|
|
|
if (this.props.decimal !== nextProps.decimal) {
|
|
|
|
this.baseUnitCb(userInput, nextProps.decimal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public onUserInput = (e: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
const { value } = e.currentTarget;
|
|
|
|
const { decimal } = this.props;
|
|
|
|
this.baseUnitCb(value, decimal);
|
|
|
|
this.setState({ userInput: value });
|
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
return this.props.children({
|
|
|
|
onUserInput: this.onUserInput,
|
|
|
|
convertedUnit: this.state.userInput
|
|
|
|
});
|
|
|
|
}
|
|
|
|
private baseUnitCb = (value: string, decimal: number) => {
|
|
|
|
const baseUnit = toTokenBase(value, decimal).toString();
|
|
|
|
const fakeEvent = {
|
|
|
|
currentTarget: {
|
|
|
|
value: baseUnit
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.props.onChange(fakeEvent);
|
|
|
|
};
|
|
|
|
}
|