Skip to content

Commit 225c159

Browse files
committed
feat(masked-input): add save_unmasked option to submit raw value
1 parent 856136f commit 225c159

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

lib/ruby_ui/masked_input/masked_input.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
module RubyUI
44
class MaskedInput < Base
5+
def initialize(save_unmasked: false, **attrs)
6+
@save_unmasked = save_unmasked
7+
super(**attrs)
8+
end
9+
510
def view_template
6-
Input(type: "text", **attrs)
11+
if @save_unmasked
12+
Input(type: "text", **attrs.deep_merge(name: "#{attrs[:name]}-masked"))
13+
input(type: "hidden", name: attrs[:name], value: attrs[:value])
14+
else
15+
Input(type: "text", **attrs)
16+
end
717
end
818

919
private

lib/ruby_ui/masked_input/masked_input_controller.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,18 @@ import { MaskInput } from "maska";
55
export default class extends Controller {
66
connect() {
77
new MaskInput(this.element)
8+
this.#boundSync = this.#sync.bind(this);
9+
this.element.addEventListener("maska", this.#boundSync);
10+
}
11+
12+
disconnect() {
13+
this.element.removeEventListener("maska", this.#boundSync);
14+
}
15+
16+
#boundSync = null;
17+
18+
#sync(event) {
19+
const hidden = this.element.nextElementSibling;
20+
if (hidden?.type === "hidden") hidden.value = event.detail.unmasked;
821
}
922
}

test/ruby_ui/masked_input_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,19 @@ def test_render
1111
assert_match(/<input type="text"/, output)
1212
assert_match(/data-controller="ruby-ui--masked-input"/, output)
1313
assert_match(/data-maska="#####-###"/, output)
14+
refute_match(/<input type="hidden"/, output)
15+
end
16+
17+
def test_render_with_save_unmasked
18+
output = phlex do
19+
RubyUI.MaskedInput(save_unmasked: true, name: "agency", value: "0000", data: {maska: "####"})
20+
end
21+
22+
assert_match(/<input type="text"/, output)
23+
assert_match(/<input type="hidden" name="agency" value="0000"/, output)
24+
assert_match(/data-maska="####"/, output)
25+
assert_match(/data-controller="ruby-ui--masked-input"/, output)
26+
27+
refute_match(/<input type="text" name="agency" value="0000"/, output)
1428
end
1529
end

0 commit comments

Comments
 (0)