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.
@@ -14,10 +14,10 @@
14
14
  * SwapState
15
15
  *
16
16
  * separateValue.mode="swap" のときに使用する
17
- * 元 input 要素の状態スナップショットおよび復元ロジックを管理するクラス
17
+ * 元 input/textarea 要素の状態スナップショットおよび復元ロジックを管理するクラス
18
18
  *
19
19
  * 役割
20
- * - swap前の input の属性状態を保持する
20
+ * - swap前の input/textarea の属性状態を保持する
21
21
  * - raw化および display生成時に必要な属性を適用する
22
22
  * - detach時に元の状態へ復元する
23
23
  *
@@ -93,16 +93,16 @@ class SwapState {
93
93
  * swap時に生成された display 用 input 要素
94
94
  * detach時に削除するため保持する
95
95
  *
96
- * @type {HTMLInputElement|null}
96
+ * @type {HTMLInputElement|HTMLTextAreaElement|null}
97
97
  */
98
98
  createdDisplay;
99
99
 
100
100
  /**
101
- * @param {HTMLInputElement} input
101
+ * @param {HTMLInputElement|HTMLTextAreaElement} input
102
102
  * swap前の元 input 要素
103
103
  */
104
104
  constructor(input) {
105
- this.originalType = input.type;
105
+ this.originalType = input.type || "";
106
106
  this.originalId = input.getAttribute("id");
107
107
  this.originalName = input.getAttribute("name");
108
108
  this.originalClass = input.className;
@@ -113,22 +113,30 @@ class SwapState {
113
113
  this.createdDisplay = null;
114
114
 
115
115
  const UI_ATTRS = [
116
- "placeholder",
116
+ // input 有
117
117
  "list",
118
+ "size",
119
+ "pattern",
120
+
121
+ // input / textarea 共有
122
+ "placeholder",
118
123
  "inputmode",
119
124
  "autocomplete",
120
125
  "autocapitalize",
121
126
  "autocorrect",
122
127
  "minlength",
123
128
  "maxlength",
124
- "size",
125
- "pattern",
126
129
  "dir",
127
130
  "title",
128
131
  "tabindex",
129
132
  "style",
130
133
  "enterkeyhint",
131
- "spellcheck"
134
+ "spellcheck",
135
+
136
+ // textarea 用
137
+ "rows",
138
+ "cols",
139
+ "wrap"
132
140
  ];
133
141
 
134
142
  const UI_BOOL_ATTRS = [
@@ -164,12 +172,17 @@ class SwapState {
164
172
  * raw 元input を hidden 化する
165
173
  * 送信担当要素として扱う
166
174
  *
167
- * @param {HTMLInputElement} input
175
+ * @param {HTMLInputElement|HTMLTextAreaElement} input
168
176
  * @returns {void}
169
177
  */
170
178
  applyToRaw(input) {
171
179
  // raw化(送信担当)
172
- input.type = "hidden";
180
+ if (input instanceof HTMLInputElement) {
181
+ input.type = "hidden";
182
+ } else if (input instanceof HTMLTextAreaElement) {
183
+ input.setAttribute("hidden", "");
184
+ input.style.display = "none";
185
+ }
173
186
  input.removeAttribute("id");
174
187
  input.removeAttribute("class");
175
188
  input.className = "";
@@ -190,12 +203,22 @@ class SwapState {
190
203
  /**
191
204
  * display用 input を生成し UI属性 aria属性 data属性を適用
192
205
  *
193
- * @param {HTMLInputElement} raw hidden化された元input
194
- * @returns {HTMLInputElement}
206
+ * @param {HTMLInputElement|HTMLTextAreaElement} raw hidden化された元input
207
+ * @returns {HTMLInputElement|HTMLTextAreaElement}
195
208
  */
196
209
  createDisplay(raw) {
197
- const display = document.createElement("input");
198
- display.type = "text";
210
+ /**
211
+ * @type {HTMLInputElement|HTMLTextAreaElement}
212
+ */
213
+ let display;
214
+ if (raw instanceof HTMLInputElement) {
215
+ display = document.createElement("input");
216
+ display.type = "text";
217
+ } else if (raw instanceof HTMLTextAreaElement) {
218
+ display = document.createElement("textarea");
219
+ } else {
220
+ throw new Error("Unsupported element type for display creation");
221
+ }
199
222
  display.dataset.tigRole = "display";
200
223
 
201
224
  if (this.originalId) {
@@ -244,11 +267,16 @@ class SwapState {
244
267
  /**
245
268
  * raw hidden化された元input を元の状態へ復元する
246
269
  *
247
- * @param {HTMLInputElement} raw
270
+ * @param {HTMLInputElement|HTMLTextAreaElement} raw
248
271
  * @returns {void}
249
272
  */
250
273
  restoreRaw(raw) {
251
- raw.type = this.originalType;
274
+ if (raw instanceof HTMLInputElement) {
275
+ raw.type = this.originalType;
276
+ } else if (raw instanceof HTMLTextAreaElement) {
277
+ raw.removeAttribute("hidden");
278
+ raw.style.display = "";
279
+ }
252
280
 
253
281
  if (this.originalId) {
254
282
  raw.setAttribute("id", this.originalId);
@@ -361,7 +389,7 @@ class SwapState {
361
389
  * @typedef {Object} GuardContext
362
390
  * @property {HTMLElement} hostElement - 元の要素(swap時はraw側)
363
391
  * @property {HTMLElement} displayElement - ユーザーが操作する表示要素
364
- * @property {HTMLInputElement|null} rawElement - 送信用hidden要素(swap時のみ)
392
+ * @property {HTMLInputElement|HTMLTextAreaElement|null} rawElement - 送信用hidden要素(swap時のみ)
365
393
  * @property {ElementKind} kind - 要素種別(input / textarea)
366
394
  * @property {boolean} warn - warnログを出すかどうか
367
395
  * @property {string} invalidClass - エラー時に付与するclass名
@@ -732,7 +760,7 @@ class InputGuard {
732
760
  /**
733
761
  * swap時に生成される hidden(raw) input
734
762
  * swapしない場合は null
735
- * @type {HTMLInputElement|null}
763
+ * @type {HTMLInputElement|HTMLTextAreaElement|null}
736
764
  */
737
765
  this.rawElement = null;
738
766
 
@@ -951,7 +979,7 @@ class InputGuard {
951
979
 
952
980
  /**
953
981
  * separateValue.mode="swap" のとき、input を hidden(raw) にして display(input[type=text]) を生成する
954
- * - textarea は非対応(warnして無視)
982
+ * - textarea も対応(hidden属性とdisplay:noneを使用)
955
983
  * @returns {void}
956
984
  */
957
985
  applySeparateValue() {
@@ -967,25 +995,20 @@ class InputGuard {
967
995
  return;
968
996
  }
969
997
 
970
- if (this.kind !== "input") {
971
- warnLog('[text-input-guard] separateValue.mode="swap" is not supported for <textarea>. ignored.', this.warn);
972
- return;
973
- }
974
-
975
- const input = /** @type {HTMLInputElement} */ (this.originalElement);
998
+ const element = this.originalElement;
976
999
 
977
- const state = new SwapState(input);
978
- state.applyToRaw(input);
1000
+ const state = new SwapState(element);
1001
+ state.applyToRaw(element);
979
1002
 
980
- const display = state.createDisplay(input);
981
- input.after(display);
1003
+ const display = state.createDisplay(element);
1004
+ element.after(display);
982
1005
 
983
1006
  this.swapState = state;
984
1007
 
985
1008
  // elements更新
986
- this.hostElement = input; // raw
1009
+ this.hostElement = element; // raw
987
1010
  this.displayElement = display; // display
988
- this.rawElement = input;
1011
+ this.rawElement = element;
989
1012
 
990
1013
  // revert 機構
991
1014
  this.lastAcceptedValue = display.value;
@@ -2429,7 +2452,7 @@ function numeric(options = {}) {
2429
2452
 
2430
2453
  return {
2431
2454
  name: "numeric",
2432
- targets: ["input"],
2455
+ targets: ["input", "textarea"],
2433
2456
 
2434
2457
  /**
2435
2458
  * 文字単位の正規化(全角→半角、記号統一、不要文字の除去)
@@ -2778,7 +2801,6 @@ function roundFraction(intPart, fracPart, fracLimit) {
2778
2801
  * @returns {Rule}
2779
2802
  */
2780
2803
  function digits(options = {}) {
2781
- /** @type {DigitsRuleOptions} */
2782
2804
  const opt = {
2783
2805
  int: typeof options.int === "number" ? options.int : undefined,
2784
2806
  frac: typeof options.frac === "number" ? options.frac : undefined,
@@ -2792,7 +2814,7 @@ function digits(options = {}) {
2792
2814
 
2793
2815
  return {
2794
2816
  name: "digits",
2795
- targets: ["input"],
2817
+ targets: ["input", "textarea"],
2796
2818
 
2797
2819
  /**
2798
2820
  * 桁数チェック(入力中:エラーを積むだけ)
@@ -3045,7 +3067,7 @@ digits.fromDataset = function fromDataset(dataset, _el) {
3045
3067
  function comma() {
3046
3068
  return {
3047
3069
  name: "comma",
3048
- targets: ["input"],
3070
+ targets: ["input", "textarea"],
3049
3071
 
3050
3072
  /**
3051
3073
  * 表示整形(確定時のみ)
@@ -7524,7 +7546,7 @@ function prefix(options) {
7524
7546
 
7525
7547
  return {
7526
7548
  name: "prefix",
7527
- targets: ["input"],
7549
+ targets: ["input", "textarea"],
7528
7550
 
7529
7551
  /**
7530
7552
  * 手動入力された prefix を除去
@@ -7618,7 +7640,7 @@ function suffix(options) {
7618
7640
 
7619
7641
  return {
7620
7642
  name: "suffix",
7621
- targets: ["input"],
7643
+ targets: ["input", "textarea"],
7622
7644
 
7623
7645
  /**
7624
7646
  * 手動入力された suffix を除去
@@ -7784,11 +7806,11 @@ const rules = {
7784
7806
 
7785
7807
  /**
7786
7808
  * バージョン(ビルド時に置換したいならここを差し替える)
7787
- * 例: rollup replace で ""1.2.2"" を package.json の version に置換
7809
+ * 例: rollup replace で ""1.3.0"" を package.json の version に置換
7788
7810
  */
7789
7811
  // @ts-ignore
7790
7812
  // eslint-disable-next-line no-undef
7791
- const version = "1.2.2" ;
7813
+ const version = "1.3.0" ;
7792
7814
 
7793
7815
  /**
7794
7816
  * UMD公開時のグローバルオブジェクト