Skip to content

Commit

Permalink
fix(bal-number-input): allow negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mladenplaninicic committed Dec 19, 2022
1 parent d75a0ff commit fa100bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
} from '../../../utils/form-input'
import { debounceEvent, findItemLabel } from '../../../utils/helpers'
import { inheritAttributes } from '../../../utils/attributes'
import { getDecimalSeparator, getThousandSeparator } from '../../../utils/number'
import { getDecimalSeparator, getThousandSeparator, getNegativeSymbol } from '../../../utils/number'
import { formatInputValue } from './bal-input.utils'
import { BEM } from '../../../utils/bem'

Expand Down Expand Up @@ -226,7 +226,7 @@ export class NumberInput implements ComponentInterface, BalConfigObserver, FormI
}

private getAllowedKeys() {
return [...NUMBER_KEYS, ...ACTION_KEYS, getDecimalSeparator()]
return [...NUMBER_KEYS, ...ACTION_KEYS, getDecimalSeparator(), getNegativeSymbol()]
}

private getRawValue(): string {
Expand All @@ -246,7 +246,7 @@ export class NumberInput implements ComponentInterface, BalConfigObserver, FormI
if (!isNaN(parsedValue)) {
this.inputValue = parsedValue
} else {
if (!this.decimal || input.value !== getDecimalSeparator()) {
if (!this.decimal && input.value !== getNegativeSymbol() && input.value !== getDecimalSeparator()) {
this.inputValue = undefined
input.value = ''
}
Expand All @@ -260,7 +260,7 @@ export class NumberInput implements ComponentInterface, BalConfigObserver, FormI
inputHandleBlur(this, event)

const input = getInputTarget(event)
if (input && input.value === getDecimalSeparator()) {
if (input && (input.value === getDecimalSeparator() || input.value === getNegativeSymbol())) {
this.inputValue = undefined
input.value = ''
}
Expand All @@ -287,7 +287,13 @@ export class NumberInput implements ComponentInterface, BalConfigObserver, FormI
}
}

if ([...NUMBER_KEYS, getDecimalSeparator()].indexOf(event.key) >= 0) {
if (event.key === getNegativeSymbol()) {
if (value.length !== 0) {
return stopEventBubbling(event)
}
}

if ([...NUMBER_KEYS, getDecimalSeparator(), getNegativeSymbol()].indexOf(event.key) >= 0) {
const newValue = getUpcomingValue(this, event)
const decimalValue = newValue.includes(getDecimalSeparator()) ? newValue?.split(getDecimalSeparator())[1] : ''
if (decimalValue && decimalValue.length > this.decimal) {
Expand All @@ -308,7 +314,7 @@ export class NumberInput implements ComponentInterface, BalConfigObserver, FormI
if (suffix !== '') {
suffix = ` ${suffix}`
}
return `[0-9${getThousandSeparator()}${this.decimal > 0 ? getDecimalSeparator() : ''}${suffix}]*`
return `[-0-9${getThousandSeparator()}${this.decimal > 0 ? getDecimalSeparator() : ''}${suffix}]*`
}

render() {
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ export const parseLocaleNumber = (stringNumber: string): number => {
.replace(new RegExp('\\' + decimalSeparator), '.'),
)
}

export const getNegativeSymbol = (): string => {
return '-'
}

0 comments on commit fa100bb

Please sign in to comment.