Skip to content

Commit

Permalink
simple currency with minus prefix example
Browse files Browse the repository at this point in the history
  • Loading branch information
uNmAnNeR committed Dec 22, 2023
1 parent 9bae1d6 commit 802ad97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
33 changes: 9 additions & 24 deletions packages/imask/example.html
Expand Up @@ -16,25 +16,20 @@ <h1>IMask Core Demo</h1>
thousandsSeparator: ' ',
scale: 2,
radix: '.',
expose: true,
min: 0,
};

class NegativeNumberMask extends IMask.MaskedNumber {
class CurrencyMaskedDynamic extends IMask.MaskedDynamic {
get typedValue () {
return -Math.abs(super.typedValue);
return Number(this.unmaskedValue);
}
set typedValue (value) {
super.typedValue = Math.abs(value);
}
}

function dispatchMaskIndex (value) {
if (!value) return 0;
else if (value.indexOf('-') === 0 || value.indexOf('-') === 1) return 2;
else return 1;
set typedValue (typedValue) {
this.unmaskedValue = String(typedValue);
}
}

const opts = {
const opts = new CurrencyMaskedDynamic({
mask: [
{
mask: '',
Expand All @@ -48,21 +43,11 @@ <h1>IMask Core Demo</h1>
{
mask: '{-}$num',
blocks: {
num: {
...numberOpts,
mask: NegativeNumberMask,
min: 0,
}
num: numberOpts,
}
}
],
dispatch: (appended, dynamicMasked, flags, tail) => {
const value = dynamicMasked.rawInputValue + appended + String(tail);
const masked = dynamicMasked.compiledMasks[dispatchMaskIndex(value)];
dynamicMasked.exposeMask = masked;
return masked;
}
};
});

const input = document.getElementById('input');
var result = document.getElementById('value');
Expand Down
6 changes: 0 additions & 6 deletions packages/imask/src/masked/number.ts
Expand Up @@ -342,16 +342,10 @@ class MaskedNumber extends Masked<number> {
this.typedValue = number;
}

/**
Is negative allowed
*/
get allowNegative (): boolean {
return (this.min != null && this.min < 0) || (this.max != null && this.max < 0);
}

/**
Is positive allowed
*/
get allowPositive (): boolean {
return (this.min != null && this.min > 0) || (this.max != null && this.max > 0);
}
Expand Down

0 comments on commit 802ad97

Please sign in to comment.