text-input-guard 1.2.2 → 1.3.0

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.
@@ -18,10 +18,10 @@
18
18
  * SwapState
19
19
  *
20
20
  * separateValue.mode="swap" のときに使用する
21
- * 元 input 要素の状態スナップショットおよび復元ロジックを管理するクラス
21
+ * 元 input/textarea 要素の状態スナップショットおよび復元ロジックを管理するクラス
22
22
  *
23
23
  * 役割
24
- * - swap前の input の属性状態を保持する
24
+ * - swap前の input/textarea の属性状態を保持する
25
25
  * - raw化および display生成時に必要な属性を適用する
26
26
  * - detach時に元の状態へ復元する
27
27
  *
@@ -97,16 +97,16 @@
97
97
  * swap時に生成された display 用 input 要素
98
98
  * detach時に削除するため保持する
99
99
  *
100
- * @type {HTMLInputElement|null}
100
+ * @type {HTMLInputElement|HTMLTextAreaElement|null}
101
101
  */
102
102
  createdDisplay;
103
103
 
104
104
  /**
105
- * @param {HTMLInputElement} input
105
+ * @param {HTMLInputElement|HTMLTextAreaElement} input
106
106
  * swap前の元 input 要素
107
107
  */
108
108
  constructor(input) {
109
- this.originalType = input.type;
109
+ this.originalType = input.type || "";
110
110
  this.originalId = input.getAttribute("id");
111
111
  this.originalName = input.getAttribute("name");
112
112
  this.originalClass = input.className;
@@ -117,22 +117,30 @@
117
117
  this.createdDisplay = null;
118
118
 
119
119
  const UI_ATTRS = [
120
- "placeholder",
120
+ // input 有
121
121
  "list",
122
+ "size",
123
+ "pattern",
124
+
125
+ // input / textarea 共有
126
+ "placeholder",
122
127
  "inputmode",
123
128
  "autocomplete",
124
129
  "autocapitalize",
125
130
  "autocorrect",
126
131
  "minlength",
127
132
  "maxlength",
128
- "size",
129
- "pattern",
130
133
  "dir",
131
134
  "title",
132
135
  "tabindex",
133
136
  "style",
134
137
  "enterkeyhint",
135
- "spellcheck"
138
+ "spellcheck",
139
+
140
+ // textarea 用
141
+ "rows",
142
+ "cols",
143
+ "wrap"
136
144
  ];
137
145
 
138
146
  const UI_BOOL_ATTRS = [
@@ -168,12 +176,17 @@
168
176
  * raw 元input を hidden 化する
169
177
  * 送信担当要素として扱う
170
178
  *
171
- * @param {HTMLInputElement} input
179
+ * @param {HTMLInputElement|HTMLTextAreaElement} input
172
180
  * @returns {void}
173
181
  */
174
182
  applyToRaw(input) {
175
183
  // raw化(送信担当)
176
- input.type = "hidden";
184
+ if (input instanceof HTMLInputElement) {
185
+ input.type = "hidden";
186
+ } else if (input instanceof HTMLTextAreaElement) {
187
+ input.setAttribute("hidden", "");
188
+ input.style.display = "none";
189
+ }
177
190
  input.removeAttribute("id");
178
191
  input.removeAttribute("class");
179
192
  input.className = "";
@@ -194,12 +207,22 @@
194
207
  /**
195
208
  * display用 input を生成し UI属性 aria属性 data属性を適用
196
209
  *
197
- * @param {HTMLInputElement} raw hidden化された元input
198
- * @returns {HTMLInputElement}
210
+ * @param {HTMLInputElement|HTMLTextAreaElement} raw hidden化された元input
211
+ * @returns {HTMLInputElement|HTMLTextAreaElement}
199
212
  */
200
213
  createDisplay(raw) {
201
- const display = document.createElement("input");
202
- display.type = "text";
214
+ /**
215
+ * @type {HTMLInputElement|HTMLTextAreaElement}
216
+ */
217
+ let display;
218
+ if (raw instanceof HTMLInputElement) {
219
+ display = document.createElement("input");
220
+ display.type = "text";
221
+ } else if (raw instanceof HTMLTextAreaElement) {
222
+ display = document.createElement("textarea");
223
+ } else {
224
+ throw new Error("Unsupported element type for display creation");
225
+ }
203
226
  display.dataset.tigRole = "display";
204
227
 
205
228
  if (this.originalId) {
@@ -248,11 +271,16 @@
248
271
  /**
249
272
  * raw hidden化された元input を元の状態へ復元する
250
273
  *
251
- * @param {HTMLInputElement} raw
274
+ * @param {HTMLInputElement|HTMLTextAreaElement} raw
252
275
  * @returns {void}
253
276
  */
254
277
  restoreRaw(raw) {
255
- raw.type = this.originalType;
278
+ if (raw instanceof HTMLInputElement) {
279
+ raw.type = this.originalType;
280
+ } else if (raw instanceof HTMLTextAreaElement) {
281
+ raw.removeAttribute("hidden");
282
+ raw.style.display = "";
283
+ }
256
284
 
257
285
  if (this.originalId) {
258
286
  raw.setAttribute("id", this.originalId);
@@ -365,7 +393,7 @@
365
393
  * @typedef {Object} GuardContext
366
394
  * @property {HTMLElement} hostElement - 元の要素(swap時はraw側)
367
395
  * @property {HTMLElement} displayElement - ユーザーが操作する表示要素
368
- * @property {HTMLInputElement|null} rawElement - 送信用hidden要素(swap時のみ)
396
+ * @property {HTMLInputElement|HTMLTextAreaElement|null} rawElement - 送信用hidden要素(swap時のみ)
369
397
  * @property {ElementKind} kind - 要素種別(input / textarea)
370
398
  * @property {boolean} warn - warnログを出すかどうか
371
399
  * @property {string} invalidClass - エラー時に付与するclass名
@@ -736,7 +764,7 @@
736
764
  /**
737
765
  * swap時に生成される hidden(raw) input
738
766
  * swapしない場合は null
739
- * @type {HTMLInputElement|null}
767
+ * @type {HTMLInputElement|HTMLTextAreaElement|null}
740
768
  */
741
769
  this.rawElement = null;
742
770
 
@@ -955,7 +983,7 @@
955
983
 
956
984
  /**
957
985
  * separateValue.mode="swap" のとき、input を hidden(raw) にして display(input[type=text]) を生成する
958
- * - textarea は非対応(warnして無視)
986
+ * - textarea も対応(hidden属性とdisplay:noneを使用)
959
987
  * @returns {void}
960
988
  */
961
989
  applySeparateValue() {
@@ -971,25 +999,20 @@
971
999
  return;
972
1000
  }
973
1001
 
974
- if (this.kind !== "input") {
975
- warnLog('[text-input-guard] separateValue.mode="swap" is not supported for <textarea>. ignored.', this.warn);
976
- return;
977
- }
978
-
979
- const input = /** @type {HTMLInputElement} */ (this.originalElement);
1002
+ const element = this.originalElement;
980
1003
 
981
- const state = new SwapState(input);
982
- state.applyToRaw(input);
1004
+ const state = new SwapState(element);
1005
+ state.applyToRaw(element);
983
1006
 
984
- const display = state.createDisplay(input);
985
- input.after(display);
1007
+ const display = state.createDisplay(element);
1008
+ element.after(display);
986
1009
 
987
1010
  this.swapState = state;
988
1011
 
989
1012
  // elements更新
990
- this.hostElement = input; // raw
1013
+ this.hostElement = element; // raw
991
1014
  this.displayElement = display; // display
992
- this.rawElement = input;
1015
+ this.rawElement = element;
993
1016
 
994
1017
  // revert 機構
995
1018
  this.lastAcceptedValue = display.value;
@@ -2433,7 +2456,7 @@
2433
2456
 
2434
2457
  return {
2435
2458
  name: "numeric",
2436
- targets: ["input"],
2459
+ targets: ["input", "textarea"],
2437
2460
 
2438
2461
  /**
2439
2462
  * 文字単位の正規化(全角→半角、記号統一、不要文字の除去)
@@ -2782,7 +2805,6 @@
2782
2805
  * @returns {Rule}
2783
2806
  */
2784
2807
  function digits(options = {}) {
2785
- /** @type {DigitsRuleOptions} */
2786
2808
  const opt = {
2787
2809
  int: typeof options.int === "number" ? options.int : undefined,
2788
2810
  frac: typeof options.frac === "number" ? options.frac : undefined,
@@ -2796,7 +2818,7 @@
2796
2818
 
2797
2819
  return {
2798
2820
  name: "digits",
2799
- targets: ["input"],
2821
+ targets: ["input", "textarea"],
2800
2822
 
2801
2823
  /**
2802
2824
  * 桁数チェック(入力中:エラーを積むだけ)
@@ -3049,7 +3071,7 @@
3049
3071
  function comma() {
3050
3072
  return {
3051
3073
  name: "comma",
3052
- targets: ["input"],
3074
+ targets: ["input", "textarea"],
3053
3075
 
3054
3076
  /**
3055
3077
  * 表示整形(確定時のみ)
@@ -7528,7 +7550,7 @@
7528
7550
 
7529
7551
  return {
7530
7552
  name: "prefix",
7531
- targets: ["input"],
7553
+ targets: ["input", "textarea"],
7532
7554
 
7533
7555
  /**
7534
7556
  * 手動入力された prefix を除去
@@ -7622,7 +7644,7 @@
7622
7644
 
7623
7645
  return {
7624
7646
  name: "suffix",
7625
- targets: ["input"],
7647
+ targets: ["input", "textarea"],
7626
7648
 
7627
7649
  /**
7628
7650
  * 手動入力された suffix を除去
@@ -7788,11 +7810,11 @@
7788
7810
 
7789
7811
  /**
7790
7812
  * バージョン(ビルド時に置換したいならここを差し替える)
7791
- * 例: rollup replace で ""1.2.2"" を package.json の version に置換
7813
+ * 例: rollup replace で ""1.3.0"" を package.json の version に置換
7792
7814
  */
7793
7815
  // @ts-ignore
7794
7816
  // eslint-disable-next-line no-undef
7795
- const version = "1.2.2" ;
7817
+ const version = "1.3.0" ;
7796
7818
 
7797
7819
  /**
7798
7820
  * UMD公開時のグローバルオブジェクト