-
Notifications
You must be signed in to change notification settings - Fork 62
Add MaskedInput component #189
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class MaskedInput < Base | ||
| def view_template | ||
| Input(type: "text", **attrs) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def default_attrs | ||
| {data: {controller: "ruby-ui--masked-input"}} | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Controller } from "@hotwired/stimulus"; | ||
| import { MaskInput } from "maska"; | ||
|
|
||
| // Connects to data-controller="ruby-ui--masked-input" | ||
| export default class extends Controller { | ||
| connect() { | ||
| new MaskInput(this.element) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class RubyUI::MaskedInputTest < Minitest::Test | ||
| include Phlex::Testing::ViewHelper | ||
|
|
||
| def test_render | ||
| output = phlex_context do | ||
| RubyUI.MaskedInput(data: {maska: "#####-###"}) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if we should try to make it more generic like using mask as the data attribute name. But doing so, we will have to map all data attributes expected by maska. It will be simpler if we explicitly tell the users that maska is doing the mask job and point them to the mask docs.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think we are tied to the js lib API anyway, it would be difficult to have a generic approach here since each mask lib have distinct config formats. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @cirdes. I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brandondrew yeah, me too, but we would need to rename all maska config attributes to use mask instead of maska (data-mask, data-mask-tokens, data-mask-tokens-replace, etc). This would create an unwanted abstraction + additional code that I'm not sure if it will bring any benefits.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree 100% with @stephannv . This seems odd at first but it makes it explicitly clear that something is going on and if they look into it it will show we are using maska which will make sense. |
||
| end | ||
|
|
||
| assert_match(/<input type="text"/, output) | ||
| assert_match(/data-controller="ruby-ui--masked-input"/, output) | ||
| assert_match(/data-maska="#####-###"/, output) | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephannv , I like the way you did it here. It's using the Input component that we already have, which makes MaskedInput compatible with our forms.
We will have to find a way to mark the dependencies so the component generator could be smart enough to eject all dependencies correctly. But we can do that later