Skip to content

Commit

Permalink
fix #888
Browse files Browse the repository at this point in the history
  • Loading branch information
uNmAnNeR committed Jul 18, 2023
1 parent ddac2b2 commit a2a7809
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
17 changes: 14 additions & 3 deletions packages/imask/example.html
Expand Up @@ -12,9 +12,20 @@ <h1>IMask Core Demo</h1>
<!-- <script src="https://unpkg.com/imask"></script> -->
<script type="text/javascript">
const opts = {
mask: IMask.MaskedEnum,
enum: ['a', 'bc', 'def'],
lazy: false,
mask: `d $`,
blocks: {
d: {
mask: Number,
scale: 2,
signed: false,
thousandsSeparator: " ",
padFractionalZeros: false,
normalizeZeros: false,
radix: ",",
mapToRadix: [".", "б", "^", "ю", "&"]
}
},
lazy: false
};

var result = document.getElementById('value');
Expand Down
10 changes: 3 additions & 7 deletions packages/imask/src/masked/number.ts
Expand Up @@ -18,9 +18,7 @@ type MaskedNumberOptions = MaskedOptions<MaskedNumber,
| 'padFractionalZeros'
>;

/**
Number mask
*/
/** Number mask */
export default
class MaskedNumber extends Masked<number> {
static UNMASKED_RADIX = '.';
Expand Down Expand Up @@ -112,7 +110,7 @@ class MaskedNumber extends Masked<number> {
}

override doPrepareChar (ch: string, flags: AppendFlags={}): [string, ChangeDetails] {
ch = this._removeThousandsSeparators(
const [prepCh, details] = super.doPrepareChar(this._removeThousandsSeparators(
this.scale && this.mapToRadix.length && (
/*
radix should be mapped when
Expand All @@ -125,9 +123,7 @@ class MaskedNumber extends Masked<number> {
flags.input && flags.raw ||
!flags.input && !flags.raw
) ? ch.replace(this._mapToRadixRegExp, this.radix) : ch
);

const [prepCh, details] = super.doPrepareChar(ch, flags);
), flags);
if (ch && !prepCh) details.skip = true;

if (prepCh && !this.allowPositive && !this.value && prepCh !== '-') details.aggregate(this._appendChar('-'));
Expand Down
30 changes: 16 additions & 14 deletions packages/react-imask/example.html
Expand Up @@ -17,22 +17,24 @@ <h1>React IMask Plugin Demo</h1>
function IMaskHook () {
const [ opts, setOpts ] = React.useState({ mask: Number });

const { ref, value, setValue } = ReactIMask.useIMask(opts, /* { onAccept, onComplete } */);

React.useEffect(() => {
if (value) return;

/*setTimeout(() => {
setValue('123')
console.log('set value from hook');
}, 1000); */
const { ref, value, setValue } = ReactIMask.useIMask({
blocks: {
d: {
mask: Number,
scale: 2,
signed: false,
thousandsSeparator: " ",
padFractionalZeros: false,
normalizeZeros: false,
radix: ",",
mapToRadix: [".", "б", "^", "ю", "&"]
}
},
mask: `d $`,
lazy: false
});

React.useEffect(() => {
console.log('masked value changed', value);
}, [value]);

return React.createElement('input', { ref, defaultValue: 10 });
return React.createElement('input', { ref, defaultValue: '10000000' });
}

class App extends React.Component {
Expand Down
1 change: 1 addition & 0 deletions packages/react-imask/src/hook.ts
Expand Up @@ -34,6 +34,7 @@ function useIMask<
const [typedValue, setTypedValue] = useState<InputMask<Opts>['typedValue']>();

const _destroyMask = useCallback(() => {
if (!initialized) return;
maskRef.current?.destroy();
maskRef.current = null;
}, []);
Expand Down

0 comments on commit a2a7809

Please sign in to comment.