Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@soramitsu/soramitsu-js-ui",
"version": "1.0.22",
"version": "1.0.23",
"private": false,
"publishConfig": {
"registry": "https://nexus.iroha.tech/repository/npm-soramitsu/"
Expand Down
38 changes: 23 additions & 15 deletions src/components/Input/SFloatInput/SFloatInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,20 @@ export default class SFloatInput extends Vue {
}

async handleInput (value: string): Promise<void> {
if (this.hasLocaleString) {
// save selection position to update it later in new formatted value
this.saveSelectionPosition(value)
// Cleanup value's format
value = value.replace(new RegExp('\\' + this.delimiters.thousand, 'g'), '')
if (this.delimiters.decimal !== DEFAULT_DECIMAL_DELIMITER) {
value = value.replace(this.delimiters.decimal, DEFAULT_DECIMAL_DELIMITER)
}
}
// save selection position to update it later in new formatted value
this.saveSelectionPosition(value)

const newValue = [
(v) => this.normalizeDelimeters(v),
(v) => this.formatNumberField(v, this.decimals),
(v) => isNumberLikeValue(v) ? v : DEFAULT_VALUE,
(v) => this.checkValueForExtremum(v)
].reduce((buffer, rule) => rule(buffer), value)

this.onInput(newValue)

if (this.hasLocaleString) {
await this.$nextTick()
this.updateSelectionPosition()
}
await this.$nextTick()
this.updateSelectionPosition()
}

onBlur (event: Event): void {
Expand Down Expand Up @@ -156,19 +148,35 @@ export default class SFloatInput extends Vue {
return value
}

private normalizeDelimeters (value: string): string {
const formatted = value.replace(new RegExp('\\' + this.delimiters.thousand, 'g'), '')

if (this.delimiters.decimal !== DEFAULT_DECIMAL_DELIMITER) {
return formatted.replace(this.delimiters.decimal, DEFAULT_DECIMAL_DELIMITER)
}

return formatted
}

private formatNumberField (value: string, decimals: number): string {
if (!['string', 'number'].includes(typeof value)) return value

// remove non digits & decimal delimeter symbols
let formatted = String(value).replace(new RegExp('[^\\d' + DEFAULT_DECIMAL_DELIMITER + ']', 'g'), '')
const decimalDelimiterIndex = formatted.indexOf(DEFAULT_DECIMAL_DELIMITER)

// remove non significant leading zeros
if (/^0+/.test(formatted) && formatted.indexOf(DEFAULT_DECIMAL_DELIMITER) !== -1) {
formatted = formatted.replace(/^0+(\d)/, '$1')
}

// Add prefix zero if needed
if (decimalDelimiterIndex === 0) {
if (formatted.indexOf(DEFAULT_DECIMAL_DELIMITER) === 0) {
formatted = '0' + formatted
}

// Avoid several decimal delimiters
if ((value.match(new RegExp(`\\${DEFAULT_DECIMAL_DELIMITER}`, 'g')) || []).length > 1) {
const decimalDelimiterIndex = formatted.indexOf(DEFAULT_DECIMAL_DELIMITER)
formatted = formatted.substring(0, decimalDelimiterIndex + 1) + formatted.substring(decimalDelimiterIndex + 1).replace(DEFAULT_DECIMAL_DELIMITER, '')
}

Expand Down