text-input-guard 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 natade
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # TextInputGuard
2
+
3
+ TextInputGuard は、**開発中**の日本向けの入力欄ガードライブラリです。
4
+
5
+ `<input>` / `<textarea>` に対して、数値入力や日本語特有の制約(全角混在、桁数、表示整形など)を扱いやすい形で提供します。
6
+
7
+ 利用方法は次の通りです。
8
+
9
+ - `attach()`:1要素ずつ明示的に有効化する
10
+ - `attachAll()`:まとめて有効化し、まとめて `detach()` する
11
+ - `autoAttach()`:`data-tig-*` の指定から自動で `attach()` する
12
+
13
+ ## インストール
14
+
15
+ ```bash
16
+ npm i text-input-guard
17
+ ```
18
+
19
+ ## 使い方
20
+
21
+ ### 1) attach(単一要素に適用)
22
+
23
+ 最も基本の使い方です。1つの要素に対してガードを適用します。
24
+
25
+ ```js
26
+ import { attach, rules } from "text-input-guard";
27
+
28
+ const input = document.querySelector("#price");
29
+
30
+ const guard = attach(input, {
31
+ rules: [
32
+ rules.numeric({ allowFullWidth: true, allowMinus: true, allowDecimal: true }),
33
+ rules.digits({
34
+ int: 6,
35
+ frac: 2,
36
+ overflowInputInt: "block",
37
+ overflowInputFrac: "block",
38
+ fixFracOnBlur: "round"
39
+ }),
40
+ rules.comma()
41
+ ]
42
+ });
43
+
44
+ // 解除
45
+ // guard.detach();
46
+ ```
47
+
48
+ `attach()` が返す `Guard` は、次のメソッドを持ちます。
49
+
50
+ - `detach()`:解除(イベント削除・swap復元)
51
+ - `isValid()`:現在エラーが無いか
52
+ - `getErrors()`:エラー一覧
53
+ - `getRawValue()`:送信用の正規化済み値
54
+
55
+ ### 2) attachAll(複数要素にまとめて適用)
56
+
57
+ 通常は入力欄が複数あるため、`querySelectorAll()` の戻り値に対してまとめて適用できます。
58
+
59
+ ```js
60
+ import { attachAll, rules } from "text-input-guard";
61
+
62
+ const group = attachAll(document.querySelectorAll(".tig-price"), {
63
+ rules: [
64
+ rules.numeric({ allowFullWidth: true, allowMinus: true, allowDecimal: true }),
65
+ rules.digits({ int: 6, frac: 2 }),
66
+ rules.comma()
67
+ ]
68
+ });
69
+
70
+ // まとめて解除
71
+ // group.detach();
72
+
73
+ // 全部 valid なら true
74
+ // group.isValid();
75
+
76
+ // 全部のエラーを集約して取得
77
+ // group.getErrors();
78
+
79
+ // 個別 Guard 配列が欲しい場合
80
+ // const guards = group.getGuards();
81
+ ```
82
+
83
+ `attachAll()` は `GuardGroup` を返します。
84
+
85
+ - `detach()`:全て解除
86
+ - `isValid()`:全て valid なら true
87
+ - `getErrors()`:全てのエラーを集約
88
+ - `getGuards()`:個別の `Guard[]` を返す
89
+
90
+ ### 3) autoAttach(data属性から自動で適用)
91
+
92
+ HTML側に `data-tig-*` を書いておき、JS側では `autoAttach()` を呼ぶだけで適用できます。
93
+
94
+ ```html
95
+ <input
96
+ class="price"
97
+ name="price"
98
+ data-tig-rules-numeric
99
+ data-tig-rules-numeric-allow-full-width="true"
100
+ data-tig-rules-numeric-allow-minus="true"
101
+ data-tig-rules-numeric-allow-decimal="true"
102
+ data-tig-rules-digits
103
+ data-tig-rules-digits-int="6"
104
+ data-tig-rules-digits-frac="2"
105
+ data-tig-rules-digits-overflow-input-int="block"
106
+ data-tig-rules-digits-overflow-input-frac="block"
107
+ data-tig-rules-digits-fix-frac-on-blur="round"
108
+ data-tig-rules-comma
109
+ />
110
+ ```
111
+
112
+ ```js
113
+ import { autoAttach } from "text-input-guard";
114
+
115
+ // document 全体を対象に自動適用
116
+ const guards = autoAttach();
117
+
118
+ // 動的追加したコンテナだけ適用したい場合
119
+ // autoAttach(container);
120
+ ```
121
+
122
+ `autoAttach()` は attachした `GuardGroup` を返します。
123
+
124
+ - 既に `data-tig-attached` が付いている要素はスキップします
125
+ - `data-tig-rules-*` を読み取って `rules` に変換します
126
+
127
+ ## ルール
128
+
129
+ 現在のルール例(公開API:`rules.xxx(...)`):
130
+
131
+ - `rules.numeric(...)`:数値入力の正規化(全角→半角、記号統一、不要文字除去)
132
+ - `rules.digits(...)`:整数部/小数部の桁数チェック、確定時の穏やか補正、入力ブロック
133
+ - `rules.comma()`:確定時のカンマ付与(表示整形)
134
+
135
+ ## ライセンス
136
+
137
+ MIT