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 +21 -0
- package/README.md +137 -0
- package/dist/cjs/text-input-guard.cjs +2141 -0
- package/dist/esm/text-input-guard.js +2132 -0
- package/dist/types/text-input-guard.d.ts +260 -0
- package/dist/umd/text-input-guard.js +2147 -0
- package/dist/umd/text-input-guard.min.js +6 -0
- package/package.json +68 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指定した1要素に対してガードを適用し、Guard API を返す
|
|
3
|
+
* @param {HTMLInputElement|HTMLTextAreaElement} element
|
|
4
|
+
* @param {AttachOptions} [options]
|
|
5
|
+
* @returns {Guard}
|
|
6
|
+
*/
|
|
7
|
+
declare function attach(element: HTMLInputElement | HTMLTextAreaElement, options?: AttachOptions): Guard;
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} GuardGroup
|
|
10
|
+
* @property {() => void} detach - 全部 detach
|
|
11
|
+
* @property {() => boolean} isValid - 全部 valid なら true
|
|
12
|
+
* @property {() => TigError[]} getErrors - 全部のエラーを集約
|
|
13
|
+
* @property {() => Guard[]} getGuards - 個別Guard配列
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @param {Iterable<HTMLInputElement|HTMLTextAreaElement>} elements
|
|
17
|
+
* @param {AttachOptions} [options]
|
|
18
|
+
* @returns {GuardGroup}
|
|
19
|
+
*/
|
|
20
|
+
declare function attachAll(elements: Iterable<HTMLInputElement | HTMLTextAreaElement>, options?: AttachOptions): GuardGroup;
|
|
21
|
+
type GuardGroup = {
|
|
22
|
+
/**
|
|
23
|
+
* - 全部 detach
|
|
24
|
+
*/
|
|
25
|
+
detach: () => void;
|
|
26
|
+
/**
|
|
27
|
+
* - 全部 valid なら true
|
|
28
|
+
*/
|
|
29
|
+
isValid: () => boolean;
|
|
30
|
+
/**
|
|
31
|
+
* - 全部のエラーを集約
|
|
32
|
+
*/
|
|
33
|
+
getErrors: () => TigError[];
|
|
34
|
+
/**
|
|
35
|
+
* - 個別Guard配列
|
|
36
|
+
*/
|
|
37
|
+
getGuards: () => Guard[];
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 対象要素の種別(現在は input と textarea のみ対応)
|
|
41
|
+
*/
|
|
42
|
+
type ElementKind = "input" | "textarea";
|
|
43
|
+
/**
|
|
44
|
+
* ルール実行フェーズ名(パイプラインの固定順)
|
|
45
|
+
* normalize.char → normalize.structure → validate → fix → format
|
|
46
|
+
*/
|
|
47
|
+
type PhaseName = "normalize.char" | "normalize.structure" | "validate" | "fix" | "format";
|
|
48
|
+
/**
|
|
49
|
+
* バリデーションエラー情報を表すオブジェクト
|
|
50
|
+
*/
|
|
51
|
+
type TigError = {
|
|
52
|
+
/**
|
|
53
|
+
* - エラー識別子(例: "digits.int_overflow")
|
|
54
|
+
*/
|
|
55
|
+
code: string;
|
|
56
|
+
/**
|
|
57
|
+
* - エラーを発生させたルール名
|
|
58
|
+
*/
|
|
59
|
+
rule: string;
|
|
60
|
+
/**
|
|
61
|
+
* - 発生したフェーズ
|
|
62
|
+
*/
|
|
63
|
+
phase: PhaseName;
|
|
64
|
+
/**
|
|
65
|
+
* - 追加情報(制限値など)
|
|
66
|
+
*/
|
|
67
|
+
detail?: any;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* attach() が返す公開API(利用者が触れる最小インターフェース)
|
|
71
|
+
*/
|
|
72
|
+
type Guard = {
|
|
73
|
+
/**
|
|
74
|
+
* - ガード解除(イベント削除・swap復元)
|
|
75
|
+
*/
|
|
76
|
+
detach: () => void;
|
|
77
|
+
/**
|
|
78
|
+
* - 現在エラーが無いかどうか
|
|
79
|
+
*/
|
|
80
|
+
isValid: () => boolean;
|
|
81
|
+
/**
|
|
82
|
+
* - エラー一覧を取得
|
|
83
|
+
*/
|
|
84
|
+
getErrors: () => TigError[];
|
|
85
|
+
/**
|
|
86
|
+
* - 送信用の正規化済み値を取得
|
|
87
|
+
*/
|
|
88
|
+
getRawValue: () => string;
|
|
89
|
+
/**
|
|
90
|
+
* - ユーザーが実際に操作している要素(swap時はdisplay側)
|
|
91
|
+
*/
|
|
92
|
+
getDisplayElement: () => HTMLInputElement | HTMLTextAreaElement;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* 各ルールに渡される実行コンテキスト
|
|
96
|
+
* - DOM参照や状態、エラー登録用関数などをまとめたもの
|
|
97
|
+
*/
|
|
98
|
+
type GuardContext = {
|
|
99
|
+
/**
|
|
100
|
+
* - 元の要素(swap時はraw側)
|
|
101
|
+
*/
|
|
102
|
+
hostElement: HTMLElement;
|
|
103
|
+
/**
|
|
104
|
+
* - ユーザーが操作する表示要素
|
|
105
|
+
*/
|
|
106
|
+
displayElement: HTMLElement;
|
|
107
|
+
/**
|
|
108
|
+
* - 送信用hidden要素(swap時のみ)
|
|
109
|
+
*/
|
|
110
|
+
rawElement: HTMLInputElement | null;
|
|
111
|
+
/**
|
|
112
|
+
* - 要素種別(input / textarea)
|
|
113
|
+
*/
|
|
114
|
+
kind: ElementKind;
|
|
115
|
+
/**
|
|
116
|
+
* - warnログを出すかどうか
|
|
117
|
+
*/
|
|
118
|
+
warn: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* - エラー時に付与するclass名
|
|
121
|
+
*/
|
|
122
|
+
invalidClass: string;
|
|
123
|
+
/**
|
|
124
|
+
* - IME変換中かどうか
|
|
125
|
+
*/
|
|
126
|
+
composing: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* - エラーを登録する関数
|
|
129
|
+
*/
|
|
130
|
+
pushError: (e: TigError) => void;
|
|
131
|
+
/**
|
|
132
|
+
* - 入力を直前の受理値へ巻き戻す要求
|
|
133
|
+
*/
|
|
134
|
+
requestRevert: (req: RevertRequest) => void;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* 1つの入力制御ルール定義
|
|
138
|
+
* - 各フェーズの処理を必要に応じて実装する
|
|
139
|
+
*/
|
|
140
|
+
type Rule = {
|
|
141
|
+
/**
|
|
142
|
+
* - ルール名(識別用)
|
|
143
|
+
*/
|
|
144
|
+
name: string;
|
|
145
|
+
/**
|
|
146
|
+
* - 適用可能な要素種別
|
|
147
|
+
*/
|
|
148
|
+
targets: ("input" | "textarea")[];
|
|
149
|
+
/**
|
|
150
|
+
* - 文字単位の正規化(全角→半角など)
|
|
151
|
+
*/
|
|
152
|
+
normalizeChar?: (value: string, ctx: GuardContext) => string;
|
|
153
|
+
/**
|
|
154
|
+
* - 構造の正規化(-位置修正など)
|
|
155
|
+
*/
|
|
156
|
+
normalizeStructure?: (value: string, ctx: GuardContext) => string;
|
|
157
|
+
/**
|
|
158
|
+
* - エラー判定(値は変更しない)
|
|
159
|
+
*/
|
|
160
|
+
validate?: (value: string, ctx: GuardContext) => void;
|
|
161
|
+
/**
|
|
162
|
+
* - 確定時の穏やか補正(切り捨て等)
|
|
163
|
+
*/
|
|
164
|
+
fix?: (value: string, ctx: GuardContext) => string;
|
|
165
|
+
/**
|
|
166
|
+
* - 表示整形(カンマ付与など)
|
|
167
|
+
*/
|
|
168
|
+
format?: (value: string, ctx: GuardContext) => string;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* 表示値(display)と内部値(raw)の分離設定
|
|
172
|
+
*/
|
|
173
|
+
type SeparateValueOptions = {
|
|
174
|
+
/**
|
|
175
|
+
* - "auto": format系ルールがある場合のみ自動でswapする(既定)
|
|
176
|
+
* - "swap": 常にswapする(inputのみ対応)
|
|
177
|
+
* - "off": 分離しない(displayとrawを同一に扱う)
|
|
178
|
+
*/
|
|
179
|
+
mode?: "auto" | "swap" | "off";
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* attach() に渡す設定オプション
|
|
183
|
+
*/
|
|
184
|
+
type AttachOptions = {
|
|
185
|
+
/**
|
|
186
|
+
* - 適用するルール配列(順番がフェーズ内実行順になる)
|
|
187
|
+
*/
|
|
188
|
+
rules?: Rule[];
|
|
189
|
+
/**
|
|
190
|
+
* - 非対応ルールなどを console.warn するか
|
|
191
|
+
*/
|
|
192
|
+
warn?: boolean;
|
|
193
|
+
/**
|
|
194
|
+
* - エラー時に付けるclass名
|
|
195
|
+
*/
|
|
196
|
+
invalidClass?: string;
|
|
197
|
+
/**
|
|
198
|
+
* - 表示値と内部値の分離設定
|
|
199
|
+
*/
|
|
200
|
+
separateValue?: SeparateValueOptions;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* swap時に退避する元inputの情報
|
|
204
|
+
* detach時に元の状態へ復元するために使用する
|
|
205
|
+
*/
|
|
206
|
+
type SwapState = {
|
|
207
|
+
/**
|
|
208
|
+
* - 元のinput.type
|
|
209
|
+
*/
|
|
210
|
+
originalType: string;
|
|
211
|
+
/**
|
|
212
|
+
* - 元のid属性
|
|
213
|
+
*/
|
|
214
|
+
originalId: string | null;
|
|
215
|
+
/**
|
|
216
|
+
* - 元のname属性
|
|
217
|
+
*/
|
|
218
|
+
originalName: string | null;
|
|
219
|
+
/**
|
|
220
|
+
* - 元のclass文字列
|
|
221
|
+
*/
|
|
222
|
+
originalClass: string;
|
|
223
|
+
/**
|
|
224
|
+
* - 生成した表示用input
|
|
225
|
+
*/
|
|
226
|
+
createdDisplay: HTMLInputElement;
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* selection(カーソル/選択範囲)の退避情報
|
|
230
|
+
*/
|
|
231
|
+
type SelectionState = {
|
|
232
|
+
/**
|
|
233
|
+
* - selectionStart
|
|
234
|
+
*/
|
|
235
|
+
start: number | null;
|
|
236
|
+
/**
|
|
237
|
+
* - selectionEnd
|
|
238
|
+
*/
|
|
239
|
+
end: number | null;
|
|
240
|
+
/**
|
|
241
|
+
* - selectionDirection
|
|
242
|
+
*/
|
|
243
|
+
direction: "forward" | "backward" | "none" | null;
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* revert要求(入力を巻き戻す指示)
|
|
247
|
+
*/
|
|
248
|
+
type RevertRequest = {
|
|
249
|
+
/**
|
|
250
|
+
* - ルール名や理由(例: "digits.int_overflow")
|
|
251
|
+
*/
|
|
252
|
+
reason: string;
|
|
253
|
+
/**
|
|
254
|
+
* - デバッグ用の詳細
|
|
255
|
+
*/
|
|
256
|
+
detail?: any;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export { attach, attachAll };
|
|
260
|
+
export type { AttachOptions, ElementKind, Guard, GuardContext, GuardGroup, PhaseName, RevertRequest, Rule, SelectionState, SeparateValueOptions, SwapState, TigError };
|