|
|
@ -2,7 +2,6 @@ const Component = require('react').Component |
|
|
|
const h = require('react-hyperscript') |
|
|
|
const h = require('react-hyperscript') |
|
|
|
const inherits = require('util').inherits |
|
|
|
const inherits = require('util').inherits |
|
|
|
const Identicon = require('../identicon') |
|
|
|
const Identicon = require('../identicon') |
|
|
|
const AutosizeInput = require('react-input-autosize').default |
|
|
|
|
|
|
|
const { conversionUtil } = require('../../conversion-util') |
|
|
|
const { conversionUtil } = require('../../conversion-util') |
|
|
|
|
|
|
|
|
|
|
|
module.exports = CurrencyDisplay |
|
|
|
module.exports = CurrencyDisplay |
|
|
@ -17,29 +16,16 @@ function CurrencyDisplay () { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function isValidNumber (text) { |
|
|
|
function isValidInput (text) { |
|
|
|
const re = /^([1-9]\d*|0)(\.|\.\d*)?$/ |
|
|
|
const re = /^([1-9]\d*|0)(\.|\.\d*)?$/ |
|
|
|
return re.test(text) |
|
|
|
return re.test(text) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CurrencyDisplay.prototype.componentDidMount = function () { |
|
|
|
function resetCaretIfPastEnd (value, event) { |
|
|
|
this.setState({ |
|
|
|
const caretPosition = event.target.selectionStart |
|
|
|
minWidth: this.refs.currencyDisplayInput.sizer.clientWidth + 10, |
|
|
|
|
|
|
|
currentclientWidth: this.refs.currencyDisplayInput.sizer.clientWidth, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CurrencyDisplay.prototype.componentWillUpdate = function ({ value: nextValue }) { |
|
|
|
if (caretPosition > value.length) { |
|
|
|
const { value: currentValue } = this.props |
|
|
|
event.target.setSelectionRange(value.length, value.length) |
|
|
|
const { currentclientWidth } = this.state |
|
|
|
|
|
|
|
const newclientWidth = this.refs.currencyDisplayInput.sizer.clientWidth |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (currentclientWidth !== newclientWidth) { |
|
|
|
|
|
|
|
const clientWidthChange = newclientWidth - currentclientWidth |
|
|
|
|
|
|
|
this.setState({ |
|
|
|
|
|
|
|
minWidth: this.state.minWidth + clientWidthChange, |
|
|
|
|
|
|
|
currentclientWidth: newclientWidth, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -54,7 +40,6 @@ CurrencyDisplay.prototype.render = function () { |
|
|
|
convertedPrefix = '', |
|
|
|
convertedPrefix = '', |
|
|
|
readOnly = false, |
|
|
|
readOnly = false, |
|
|
|
handleChange, |
|
|
|
handleChange, |
|
|
|
inputFontSize, |
|
|
|
|
|
|
|
} = this.props |
|
|
|
} = this.props |
|
|
|
const { minWidth } = this.state |
|
|
|
const { minWidth } = this.state |
|
|
|
|
|
|
|
|
|
|
@ -71,27 +56,33 @@ CurrencyDisplay.prototype.render = function () { |
|
|
|
|
|
|
|
|
|
|
|
h('div.currency-display__primary-row', [ |
|
|
|
h('div.currency-display__primary-row', [ |
|
|
|
|
|
|
|
|
|
|
|
h(AutosizeInput, { |
|
|
|
h('div.currency-display__input-wrapper', [ |
|
|
|
ref: 'currencyDisplayInput', |
|
|
|
|
|
|
|
className: 'currency-display__input-wrapper', |
|
|
|
h('input.currency-display__input', { |
|
|
|
inputClassName: 'currency-display__input', |
|
|
|
value: `${value} ${primaryCurrency}`, |
|
|
|
value, |
|
|
|
placeholder: `${0} ${primaryCurrency}`, |
|
|
|
placeholder, |
|
|
|
readOnly, |
|
|
|
readOnly, |
|
|
|
onChange: (event) => { |
|
|
|
minWidth, |
|
|
|
let newValue = event.target.value.split(' ')[0] |
|
|
|
onChange: (event) => { |
|
|
|
|
|
|
|
const newValue = event.target.value |
|
|
|
if (newValue === '') { |
|
|
|
if (newValue && !isValidNumber(newValue)) { |
|
|
|
handleChange('0') |
|
|
|
event.preventDefault() |
|
|
|
} |
|
|
|
} |
|
|
|
else if (newValue.match(/^0[1-9]$/)) { |
|
|
|
else { |
|
|
|
handleChange(newValue.match(/[1-9]/)[0]) |
|
|
|
handleChange(newValue) |
|
|
|
} |
|
|
|
} |
|
|
|
else if (newValue && !isValidInput(newValue)) { |
|
|
|
}, |
|
|
|
event.preventDefault() |
|
|
|
style: { fontSize: inputFontSize }, |
|
|
|
} |
|
|
|
}), |
|
|
|
else { |
|
|
|
|
|
|
|
handleChange(newValue) |
|
|
|
h('span.currency-display__primary-currency', {}, primaryCurrency), |
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
onKeyUp: event => resetCaretIfPastEnd(value, event), |
|
|
|
|
|
|
|
onClick: event => resetCaretIfPastEnd(value, event), |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
|
]), |
|
|
|
]), |
|
|
|
|
|
|
|
|
|
|
|