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