Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmberInputMask does not play well with multiple masks property #143

Open
brunoocasali opened this issue Feb 13, 2021 · 1 comment · May be fixed by #144
Open

EmberInputMask does not play well with multiple masks property #143

brunoocasali opened this issue Feb 13, 2021 · 1 comment · May be fixed by #144

Comments

@brunoocasali
Copy link

brunoocasali commented Feb 13, 2021

Hello!!

In my app I have a field with two kinds of masks:

<OneWayInputMask
  @mask={{array '999.999.999-99' '99.999.999/9999-99'}}
  @value={{this.value}}
  {{!-- @update={{this.update}} --}}
  @options={{hash oncomplete=(action this.onMaskFinished)}}
/>

The idea is simple, when any of the masks is complete I just want to call a function (covered in inputmask.js) to do some business logic there.

The problem is: The input does not allow more typing after the first mask is complete! I've checked if is a inputmask bug but I think is not, because everything works when I remove @update part (of course with bunch of console errors because @update is required by the component).

I've tried to reproduce this behaviour in tests right here in this repo, as placed in: #144

brunoocasali added a commit to brunoocasali/ember-inputmask that referenced this issue Feb 13, 2021
@brunoocasali brunoocasali linked a pull request Feb 13, 2021 that will close this issue
@brunoocasali
Copy link
Author

brunoocasali commented Feb 17, 2021

To workaround this i've implemented a really simple version:

import Component from '@glimmer/component';
import Inputmask from 'inputmask';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

const DEFAULT_OPTIONS = {
  rightAlign: false,
};

// TODO: must be tested or replaced by ember-input-mask version when this issue was closed
// https://github.com/brandynbennett/ember-inputmask/issues/143
export default class FormsInputMaskComponent extends Component {
  @tracked inputmask;

  @action setupMask(element) {
    let options = { ...DEFAULT_OPTIONS, ...this.args.options };

    this.inputmask = new Inputmask(this.args.mask, options);
    this.inputmask.mask(element);
  }

  @action processNewValue({ target }) {
    let cursorStart = target.selectionStart;
    let cursorEnd = target.selectionEnd;

    this.sendUpdate(target.inputmask.unmaskedvalue(), target.value);

    target.setSelectionRange(cursorStart, cursorEnd);
  }

  sendUpdate(unmaskedValue, value) {
    this.args.update && this.args.update(unmaskedValue, value);
  }

  willDestroy() {
    this.inputmask.remove();
  }
}
<Input
  ...attributes
  type="text"
  {{on "input" (action this.processNewValue)}}
  {{did-insert this.setupMask}}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant