Skip to content

Commit

Permalink
fix #32
Browse files Browse the repository at this point in the history
  • Loading branch information
uNmAnNeR committed Jan 30, 2018
1 parent d606d93 commit 064b82e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
9 changes: 6 additions & 3 deletions guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,21 @@ <h2 id="dynamic" class="section-h"><a href="#dynamic">Dynamic</a></h2>
<pre><code>var dispatchMask = new IMask(element, {
mask: [
{
mask: '+{3\\0} {21} 0 000 0000',
mask: '+00 {21} 0 000 0000',
startsWith: '30',
lazy: false,
country: 'Greece'
},
{
mask: '+{7} 000 000-00-00',
mask: '+0 000 000-00-00',
startsWith: '7',
lazy: false,
country: 'Russia'
},
{
mask: '+{91}-0000-000000',
mask: '+00-0000-000000',
startsWith: '91',
lazy: false,
country: 'India'
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/change-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class ChangeDetails {
@returns {ChangeDetails} `this`
*/
aggregate (details: ChangeDetails): ChangeDetails {
if (details.rawInserted) this.rawInserted += details.rawInserted;
this.inserted += details.inserted;
this.shift += details.shift;
this.overflow = this.overflow || details.overflow;
if (details.rawInserted) this.rawInserted += details.rawInserted;
return this;
}

Expand Down
31 changes: 20 additions & 11 deletions src/masked/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ class MaskedDynamic extends Masked<DynamicMaskType> {
return details;
}

_applyDispatch (appended: string, ...args: *) {
_applyDispatch (appended: string='', ...args: *) {
const oldValueLength = this.value.length;
const inputValue = this.rawInputValue;
const oldMask = this.currentMask;
const details = new ChangeDetails();

// dispatch SHOULD NOT modify mask
this.currentMask = this.doDispatch(appended, ...args);

// restore state after dispatch
if (this.currentMask && this.currentMask !== oldMask) {
// if mask changed reapply input
this.currentMask.reset();
// $FlowFixMe - it's ok, we don't change current mask
this.currentMask._append(inputValue, {raw: true});
Expand Down Expand Up @@ -141,7 +145,7 @@ class MaskedDynamic extends Masked<DynamicMaskType> {
if (this.currentMask) {
details.aggregate(this.currentMask.remove(...args))
// update with dispatch
.aggregate(this._applyDispatch(''));
.aggregate(this._applyDispatch());
}

return details;
Expand All @@ -168,10 +172,13 @@ class MaskedDynamic extends Masked<DynamicMaskType> {
/**
@override
*/
_appendTail (...args: *): ChangeDetails {
return this.currentMask ?
this.currentMask._appendTail(...args) :
super._appendTail(...args);
_appendTail (tail?: TailDetails): ChangeDetails {
const details = new ChangeDetails();
if (tail) details.aggregate(this._applyDispatch(tail.value));

return details.aggregate(this.currentMask ?
this.currentMask._appendTail(tail) :
super._appendTail(tail));
}

/**
Expand All @@ -198,14 +205,16 @@ MaskedDynamic.DEFAULTS = {

const inputValue = masked.rawInputValue;

// update all
masked.compiledMasks.forEach(cm => {
cm.rawInputValue = inputValue;
cm._append(appended, flags);
// simulate input
const inputs = masked.compiledMasks.map((cm, index) => {
const m = cm.clone();
m.rawInputValue = inputValue;
m._append(appended, flags);

return {value: m.rawInputValue.length, index};
});

// pop masks with longer values first
const inputs = masked.compiledMasks.map((cm, index) => ({value: cm.rawInputValue.length, index}));
inputs.sort((i1, i2) => i2.value - i1.value);

return masked.compiledMasks[inputs[0].index];
Expand Down
2 changes: 1 addition & 1 deletion src/masked/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class MaskedPattern extends Masked<string> {
}

// for lazy if has aligned left inside fixed and has came to the start - use start position
if (direction === DIRECTION.LEFT && di === 0 && this.lazy &&
if (direction === DIRECTION.LEFT && di === 0 && this.lazy && !this.extractInput() &&
(!initialDef || !initialDef.isInput)) firstInputIndex = 0;

if (direction === DIRECTION.LEFT || firstInputIndex == null) {
Expand Down

0 comments on commit 064b82e

Please sign in to comment.