Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/ruby_ui/masked_input/masked_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

module RubyUI
class MaskedInput < Base
def initialize(save_unmasked: false, **attrs)
@save_unmasked = save_unmasked
super(**attrs)
end

def view_template
Input(type: "text", **attrs)
if @save_unmasked
Input(type: "text", **attrs.merge(name: nil))
input(type: "hidden", name: attrs[:name], value: attrs[:value])
else
Input(type: "text", **attrs)
end
end

private
Expand Down
13 changes: 13 additions & 0 deletions lib/ruby_ui/masked_input/masked_input_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@ import { MaskInput } from "maska";
export default class extends Controller {
connect() {
new MaskInput(this.element)
this.#boundSync = this.#sync.bind(this);
this.element.addEventListener("maska", this.#boundSync);
}

disconnect() {
this.element.removeEventListener("maska", this.#boundSync);
}

#boundSync = null;

#sync(event) {
const hidden = this.element.nextElementSibling;
if (hidden?.type === "hidden") hidden.value = event.detail.unmasked;
}
}
14 changes: 14 additions & 0 deletions test/ruby_ui/masked_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@ def test_render
assert_match(/<input type="text"/, output)
assert_match(/data-controller="ruby-ui--masked-input"/, output)
assert_match(/data-maska="#####-###"/, output)
refute_match(/<input type="hidden"/, output)
end

def test_render_with_save_unmasked
output = phlex do
RubyUI.MaskedInput(save_unmasked: true, name: "agency", value: "0000", data: {maska: "####"})
end

assert_match(/<input type="text"/, output)
assert_match(/<input type="hidden" name="agency" value="0000"/, output)
assert_match(/data-maska="####"/, output)
assert_match(/data-controller="ruby-ui--masked-input"/, output)

refute_match(/<input type="text" name="agency" value="0000"/, output)
end
end
Loading