Skip to content

Commit 992944d

Browse files
authored
Add MaskedInput component (#189)
1 parent 3d4e646 commit 992944d

6 files changed

Lines changed: 49 additions & 0 deletions

File tree

lib/ruby_ui/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import DialogController from "./dialog/dialog_controller";
1616
import DropdownMenuController from "./dropdown_menu/dropdown_menu_controller";
1717
import FormFieldController from "./form/form_field_controller";
1818
import HoverCardController from "./hover_card/hover_card_controller";
19+
import MaskedInputController from "./masked_input/masked_input_controller";
1920
import PopoverController from "./popover/popover_controller";
2021
import TabsController from "./tabs/tabs_controller";
2122
import ThemeToggleController from "./theme_toggle/theme_toggle_controller";
@@ -51,6 +52,7 @@ function initialize(application) {
5152
registerIfNotExists("ruby-ui--dropdown-menu", DropdownMenuController);
5253
registerIfNotExists("ruby-ui--form-field", FormFieldController);
5354
registerIfNotExists("ruby-ui--hover-card", HoverCardController);
55+
registerIfNotExists("ruby-ui--masked-input", MaskedInputController);
5456
registerIfNotExists("ruby-ui--popover", PopoverController);
5557
registerIfNotExists("ruby-ui--tabs", TabsController);
5658
registerIfNotExists("ruby-ui--theme-toggle", ThemeToggleController);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class MaskedInput < Base
5+
def view_template
6+
Input(type: "text", **attrs)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{data: {controller: "ruby-ui--masked-input"}}
13+
end
14+
end
15+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Controller } from "@hotwired/stimulus";
2+
import { MaskInput } from "maska";
3+
4+
// Connects to data-controller="ruby-ui--masked-input"
5+
export default class extends Controller {
6+
connect() {
7+
new MaskInput(this.element)
8+
}
9+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"chart.js": "^4.4.1",
2929
"date-fns": "^2.30.0",
3030
"fuse.js": "^7.0.0",
31+
"maska": "^3.0.3",
3132
"motion": "^10.16.4",
3233
"mustache": "^4.2.0",
3334
"tippy.js": "^6.3.7"

test/ruby_ui/masked_input_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::MaskedInputTest < Minitest::Test
6+
include Phlex::Testing::ViewHelper
7+
8+
def test_render
9+
output = phlex_context do
10+
RubyUI.MaskedInput(data: {maska: "#####-###"})
11+
end
12+
13+
assert_match(/<input type="text"/, output)
14+
assert_match(/data-controller="ruby-ui--masked-input"/, output)
15+
assert_match(/data-maska="#####-###"/, output)
16+
end
17+
end

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ hey-listen@^1.0.8:
126126
resolved "https://registry.yarnpkg.com/hey-listen/-/hey-listen-1.0.8.tgz#8e59561ff724908de1aa924ed6ecc84a56a9aa68"
127127
integrity sha512-COpmrF2NOg4TBWUJ5UVyaCU2A88wEMkUPK4hNqyCkqHbxT92BbvfjoSozkAIIm6XhicGlJHhFdullInrdhwU8Q==
128128

129+
maska@^3.0.3:
130+
version "3.0.3"
131+
resolved "https://registry.yarnpkg.com/maska/-/maska-3.0.3.tgz#36e3c2c77bf35db09842f318315c590e2855a9ce"
132+
integrity sha512-ItFbuqVeBKk1JmC4QCRxKeNaX+Ym/oMUYZVXwvAPKAwMeO4bYZpIGjNWOcZy+L8YXQaPvCZ68+5eYpGRdyaA8w==
133+
129134
motion@^10.16.4:
130135
version "10.18.0"
131136
resolved "https://registry.yarnpkg.com/motion/-/motion-10.18.0.tgz#8fd035cc3a668800fe7e2479568eeb98af484ab3"

0 commit comments

Comments
 (0)