Browse Source

Allow sub magnitude in fiat field if below one cent

master
Gaëtan Renaudeau 7 years ago
parent
commit
f507af470d
  1. 15
      src/components/RequestAmount/index.js
  2. 16
      src/components/base/InputCurrency/index.js

15
src/components/RequestAmount/index.js

@ -79,6 +79,9 @@ const mapStateToProps = (state: State, props: OwnProps) => {
const counterValueCurrency = counterValueCurrencySelector(state) const counterValueCurrency = counterValueCurrencySelector(state)
const fromExchange = currencySettingsSelector(state, { currency }).exchange const fromExchange = currencySettingsSelector(state, { currency }).exchange
const toExchange = counterValueExchangeSelector(state) const toExchange = counterValueExchangeSelector(state)
// FIXME this make the component not working with "Pure". is there a way we can calculate here whatever needs to be?
// especially the value comes from props!
const getCounterValue = value => const getCounterValue = value =>
CounterValues.calculateWithIntermediarySelector(state, { CounterValues.calculateWithIntermediarySelector(state, {
from: currency, from: currency,
@ -87,8 +90,8 @@ const mapStateToProps = (state: State, props: OwnProps) => {
toExchange, toExchange,
to: counterValueCurrency, to: counterValueCurrency,
value, value,
disableRounding: true,
}) })
const getReverseCounterValue = value => const getReverseCounterValue = value =>
CounterValues.reverseWithIntermediarySelector(state, { CounterValues.reverseWithIntermediarySelector(state, {
from: currency, from: currency,
@ -130,10 +133,15 @@ export class RequestAmount extends PureComponent<Props> {
} }
} }
onLeftChange = this.handleChangeAmount('left')
onRightChange = this.handleChangeAmount('right')
renderInputs(containerProps: Object) { renderInputs(containerProps: Object) {
// TODO move this inlined into render() for less spaghetti
const { value, account, rightCurrency, getCounterValue, canBeSpent } = this.props const { value, account, rightCurrency, getCounterValue, canBeSpent } = this.props
const right = getCounterValue(value) || 0 const right = getCounterValue(value) || 0
const rightUnit = rightCurrency.units[0] const rightUnit = rightCurrency.units[0]
// FIXME: no way InputCurrency pure can work here. inlined InputRight (should be static func?), inline containerProps object..
return ( return (
<Box horizontal grow shrink> <Box horizontal grow shrink>
<InputCurrency <InputCurrency
@ -141,7 +149,7 @@ export class RequestAmount extends PureComponent<Props> {
containerProps={containerProps} containerProps={containerProps}
defaultUnit={account.unit} defaultUnit={account.unit}
value={value} value={value}
onChange={this.handleChangeAmount('left')} onChange={this.onLeftChange}
renderRight={<InputRight>{account.unit.code}</InputRight>} renderRight={<InputRight>{account.unit.code}</InputRight>}
/> />
<InputCenter>=</InputCenter> <InputCenter>=</InputCenter>
@ -149,9 +157,10 @@ export class RequestAmount extends PureComponent<Props> {
containerProps={containerProps} containerProps={containerProps}
defaultUnit={rightUnit} defaultUnit={rightUnit}
value={right} value={right}
onChange={this.handleChangeAmount('right')} onChange={this.onRightChange}
renderRight={<InputRight>{rightUnit.code}</InputRight>} renderRight={<InputRight>{rightUnit.code}</InputRight>}
showAllDigits showAllDigits
subMagnitude={3}
/> />
</Box> </Box>
) )

16
src/components/base/InputCurrency/index.js

@ -18,12 +18,13 @@ function parseValue(value) {
return value.toString().replace(/,/g, '.') return value.toString().replace(/,/g, '.')
} }
function format(unit: Unit, value: number, { isFocused, showAllDigits }) { function format(unit: Unit, value: number, { isFocused, showAllDigits, subMagnitude }) {
// FIXME do we need locale for the input too ? // FIXME do we need locale for the input too ?
return formatCurrencyUnit(unit, value, { return formatCurrencyUnit(unit, value, {
useGrouping: !isFocused, useGrouping: !isFocused,
disableRounding: true, disableRounding: true,
showAllDigits: !!showAllDigits && !isFocused, showAllDigits: !!showAllDigits && !isFocused,
subMagnitude: value < 1 ? subMagnitude : 0,
}) })
} }
@ -53,6 +54,7 @@ type Props = {
units: Unit[], units: Unit[],
value: number, value: number,
showAllDigits?: boolean, showAllDigits?: boolean,
subMagnitude: number,
} }
type State = { type State = {
@ -68,6 +70,7 @@ class InputCurrency extends PureComponent<Props, State> {
units: [], units: [],
value: 0, value: 0,
showAllDigits: false, showAllDigits: false,
subMagnitude: 0,
} }
state = { state = {
@ -94,6 +97,7 @@ class InputCurrency extends PureComponent<Props, State> {
: format(nextProps.unit, nextProps.value, { : format(nextProps.unit, nextProps.value, {
isFocused, isFocused,
showAllDigits: nextProps.showAllDigits, showAllDigits: nextProps.showAllDigits,
subMagnitude: nextProps.subMagnitude,
}), }),
}) })
} }
@ -135,11 +139,13 @@ class InputCurrency extends PureComponent<Props, State> {
} }
syncInput = ({ isFocused }: { isFocused: boolean }) => { syncInput = ({ isFocused }: { isFocused: boolean }) => {
const { value, showAllDigits, unit } = this.props const { value, showAllDigits, subMagnitude, unit } = this.props
this.setState({ this.setState({
isFocused, isFocused,
displayValue: displayValue:
value === '' || value === 0 ? '' : format(unit, value, { isFocused, showAllDigits }), value === '' || value === 0
? ''
: format(unit, value, { isFocused, showAllDigits, subMagnitude }),
}) })
} }
@ -183,7 +189,7 @@ class InputCurrency extends PureComponent<Props, State> {
} }
render() { render() {
const { renderRight, showAllDigits, unit } = this.props const { renderRight, showAllDigits, unit, subMagnitude } = this.props
const { displayValue } = this.state const { displayValue } = this.state
return ( return (
@ -195,7 +201,7 @@ class InputCurrency extends PureComponent<Props, State> {
onFocus={this.handleFocus} onFocus={this.handleFocus}
onBlur={this.handleBlur} onBlur={this.handleBlur}
renderRight={renderRight || this.renderListUnits()} renderRight={renderRight || this.renderListUnits()}
placeholder={format(unit, 0, { isFocused: false, showAllDigits })} placeholder={format(unit, 0, { isFocused: false, showAllDigits, subMagnitude })}
/> />
) )
} }

Loading…
Cancel
Save