ja-input-mask 0.0.0-stage → 0.1.2

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/dist/dom.js ADDED
@@ -0,0 +1,114 @@
1
+ import { createInputMask } from './core.js';
2
+ const attached = new WeakMap();
3
+ function read(field) {
4
+ return { value: field.value, selectionStart: field.selectionStart ?? field.value.length,
5
+ selectionEnd: field.selectionEnd ?? field.value.length, selectionDirection: field.selectionDirection ?? 'none' };
6
+ }
7
+ function same(a, b) {
8
+ return a.value === b.value && a.selectionStart === b.selectionStart && a.selectionEnd === b.selectionEnd &&
9
+ a.selectionDirection === b.selectionDirection;
10
+ }
11
+ /** Attach to a text-like field. Attaching twice returns the existing controller. */
12
+ export function attachInputMask(field, options) {
13
+ const existing = attached.get(field);
14
+ if (existing)
15
+ return existing;
16
+ if (field.selectionStart === null)
17
+ throw new TypeError('attachInputMask requires an input with selection support');
18
+ const mask = typeof options.mask === 'function' ? options.mask : createInputMask(options.mask);
19
+ let active = true;
20
+ let composing = false;
21
+ let inputComposing = false;
22
+ let compositionStart;
23
+ let pending;
24
+ let before;
25
+ let last;
26
+ function apply(event, inputType, previous) {
27
+ const current = read(field);
28
+ const isComposing = composing || inputComposing || pending !== undefined;
29
+ const result = mask({ ...current, isComposing, ...(inputType && { inputType }), ...(previous && { previous }) });
30
+ if (!active || isComposing)
31
+ return result;
32
+ if (result.value !== current.value)
33
+ field.value = result.value;
34
+ if (!same(read(field), result))
35
+ field.setSelectionRange(result.selectionStart, result.selectionEnd, result.selectionDirection);
36
+ const updated = read(field);
37
+ const notify = !last || !same(last, updated) || !same(current, updated);
38
+ last = updated;
39
+ if (notify)
40
+ options.onChange?.(result, event);
41
+ return result;
42
+ }
43
+ function cancelCommit() {
44
+ if (pending !== undefined)
45
+ clearTimeout(pending);
46
+ pending = undefined;
47
+ }
48
+ function onCompositionStart() {
49
+ cancelCommit();
50
+ before = undefined;
51
+ composing = true;
52
+ compositionStart = read(field);
53
+ }
54
+ function onCompositionUpdate() {
55
+ composing = true;
56
+ }
57
+ function onCompositionEnd(event) {
58
+ composing = false;
59
+ inputComposing = false;
60
+ before = undefined;
61
+ cancelCommit();
62
+ // Some engines apply one last mutation after compositionend. Read the live value on the next task.
63
+ pending = setTimeout(() => {
64
+ pending = undefined;
65
+ const previous = compositionStart;
66
+ compositionStart = undefined;
67
+ apply(event, 'insertCompositionText', previous);
68
+ }, 0);
69
+ }
70
+ function onBeforeInput(event) {
71
+ const edit = event;
72
+ if (composing || pending !== undefined || edit.isComposing)
73
+ return;
74
+ before = { event: edit, state: read(field) };
75
+ }
76
+ function onInput(event) {
77
+ const edit = event;
78
+ if (pending !== undefined)
79
+ return;
80
+ inputComposing = edit.isComposing === true;
81
+ const captured = before;
82
+ before = undefined;
83
+ if (composing || inputComposing)
84
+ return;
85
+ const previous = captured && !captured.event.defaultPrevented && captured.event.inputType === edit.inputType
86
+ ? captured.state : undefined;
87
+ if (!previous && last && same(last, read(field)))
88
+ return;
89
+ apply(event, edit.inputType, previous);
90
+ }
91
+ const listeners = [
92
+ ['beforeinput', onBeforeInput], ['input', onInput], ['compositionstart', onCompositionStart],
93
+ ['compositionupdate', onCompositionUpdate], ['compositionend', onCompositionEnd],
94
+ ];
95
+ const controller = {
96
+ get isComposing() { return composing || inputComposing; },
97
+ sync() { return apply(null); },
98
+ detach() {
99
+ if (!active)
100
+ return;
101
+ active = false;
102
+ cancelCommit();
103
+ composing = inputComposing = false;
104
+ compositionStart = before = undefined;
105
+ for (const [type, listener] of listeners)
106
+ field.removeEventListener(type, listener);
107
+ attached.delete(field);
108
+ },
109
+ };
110
+ for (const [type, listener] of listeners)
111
+ field.addEventListener(type, listener);
112
+ attached.set(field, controller);
113
+ return controller;
114
+ }
@@ -0,0 +1,2 @@
1
+ export * from './core.js';
2
+ export * from './dom.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './core.js';
2
+ export * from './dom.js';
@@ -0,0 +1,3 @@
1
+ export declare const areaCodes: string[];
2
+ export declare const overlappingAreaCodes: string[];
3
+ export declare const ambiguousAreaRanges: readonly (readonly [number, number, number])[];
@@ -0,0 +1,172 @@
1
+ // Generated by scripts/update-landline.py. Source and attribution are in NOTICE.
2
+ // Area codes include the domestic trunk zero. Ranges cover the first six domestic digits.
3
+ export const areaCodes = [
4
+ '011', '0123', '0124', '0125', '0126', '01267', '0133', '0134', '0135', '0136', '0137', '01372', '01374', '01377', '0138', '0139',
5
+ '01392', '01397', '01398', '0142', '0143', '0144', '0145', '01456', '01457', '0146', '01466', '015', '0152', '0153', '0154', '01547',
6
+ '0155', '01558', '0156', '01564', '0157', '0158', '01586', '01587', '0162', '0163', '01632', '01634', '01635', '0164', '01648', '0165',
7
+ '01654', '01655', '01656', '01658', '0166', '0167', '017', '0172', '0173', '0174', '0175', '0176', '0178', '0179', '018', '0182',
8
+ '0183', '0184', '0185', '0186', '0187', '019', '0191', '0192', '0193', '0194', '0195', '0197', '0198', '022', '0220', '0223',
9
+ '0224', '0225', '0226', '0228', '0229', '023', '0233', '0234', '0235', '0237', '0238', '024', '0240', '0241', '0242', '0243',
10
+ '0244', '0246', '0247', '0248', '025', '0250', '0254', '0255', '0256', '0257', '0258', '0259', '026', '0260', '0261', '0263',
11
+ '0264', '0265', '0266', '0267', '0268', '0269', '027', '0270', '0274', '0276', '0277', '0278', '0279', '028', '0280', '0282',
12
+ '0283', '0284', '0285', '0287', '0288', '0289', '029', '0291', '0293', '0294', '0295', '0296', '0297', '0299', '03', '04',
13
+ '042', '0422', '0428', '043', '0436', '0438', '0439', '044', '045', '046', '0460', '0463', '0465', '0466', '0467', '047',
14
+ '0470', '0475', '0476', '0478', '0479', '048', '0480', '049', '0493', '0494', '0495', '04992', '04994', '04996', '04998', '052',
15
+ '053', '0531', '0532', '0533', '0536', '0537', '0538', '0539', '054', '0544', '0545', '0547', '0548', '055', '0550', '0551',
16
+ '0553', '0554', '0555', '0556', '0557', '0558', '0561', '0562', '0563', '0564', '0565', '0566', '0567', '0568', '0569', '0572',
17
+ '0573', '0574', '0575', '0576', '05769', '0577', '0578', '058', '0581', '0584', '0585', '0586', '0587', '059', '0594', '0595',
18
+ '0596', '0597', '05979', '0598', '0599', '06', '072', '0721', '0725', '073', '0735', '0736', '0737', '0738', '0739', '0740',
19
+ '0742', '0743', '0744', '0745', '0746', '07468', '0747', '0748', '0749', '075', '076', '0761', '0763', '0765', '0766', '0767',
20
+ '0768', '077', '0770', '0771', '0772', '0773', '0774', '0776', '0778', '0779', '078', '079', '0790', '0791', '0794', '0795',
21
+ '0796', '0797', '0798', '0799', '082', '0820', '0823', '0824', '0826', '0827', '0829', '083', '0833', '0834', '0835', '0836',
22
+ '0837', '0838', '08387', '08388', '08396', '084', '0845', '0846', '0847', '08477', '0848', '08512', '08514', '0852', '0853', '0854',
23
+ '0855', '0856', '0857', '0858', '0859', '086', '0863', '0865', '0866', '0867', '0868', '0869', '087', '0875', '0877', '0879',
24
+ '088', '0880', '0883', '0884', '0885', '0887', '0889', '089', '0892', '0893', '0894', '0895', '0896', '0897', '0898', '092',
25
+ '0920', '093', '0930', '0940', '0942', '0943', '0944', '0946', '0947', '0948', '0949', '095', '0950', '0952', '0954', '0955',
26
+ '0956', '0957', '0959', '096', '0964', '0965', '0966', '0967', '0968', '0969', '097', '0972', '0973', '0974', '0977', '0978',
27
+ '0979', '098', '0980', '09802', '0982', '0983', '0984', '0985', '0986', '0987', '099', '09912', '09913', '0993', '0994', '0995',
28
+ '0996', '09969', '0997',
29
+ ];
30
+ export const overlappingAreaCodes = ['0126', '0137', '0139', '0145', '0146', '015', '0154', '0155', '0156', '0158', '0163', '0164', '0165', '017', '018', '019', '022', '023', '024', '025', '026', '027', '028', '029', '04', '042', '043', '046', '047', '048', '049', '053', '054', '055', '0576', '058', '059', '0597', '072', '073', '0746', '076', '077', '079', '082', '083', '0838', '084', '0847', '086', '087', '088', '089', '092', '093', '095', '096', '097', '098', '0980', '099', '0996'];
31
+ export const ambiguousAreaRanges = [
32
+ [12620, 12660, 4], [12662, 12669, 4], [12672, 12679, 5], [13722, 13729, 5],
33
+ [13742, 13749, 5], [13750, 13760, 4], [13762, 13769, 4], [13772, 13779, 5],
34
+ [13780, 13780, 4], [13782, 13789, 4], [13922, 13929, 5], [13930, 13940, 4],
35
+ [13942, 13950, 4], [13952, 13960, 4], [13962, 13969, 4], [13972, 13979, 5],
36
+ [13982, 13989, 5], [14520, 14520, 4], [14522, 14540, 4], [14542, 14559, 4],
37
+ [14562, 14569, 5], [14572, 14579, 5], [14620, 14620, 4], [14622, 14630, 4],
38
+ [14632, 14640, 4], [14642, 14659, 4], [14662, 14669, 5], [15220, 15220, 4],
39
+ [15222, 15270, 4], [15272, 15289, 4], [15320, 15320, 4], [15322, 15350, 4],
40
+ [15352, 15370, 4], [15372, 15380, 4], [15382, 15399, 4], [15410, 15416, 3],
41
+ [15418, 15419, 3], [15420, 15469, 4], [15472, 15479, 5], [15480, 15480, 3],
42
+ [15482, 15489, 3], [15490, 15499, 4], [15510, 15516, 3], [15518, 15519, 3],
43
+ [15520, 15569, 4], [15570, 15570, 3], [15572, 15579, 3], [15582, 15589, 5],
44
+ [15590, 15599, 4], [15620, 15620, 4], [15622, 15639, 4], [15642, 15649, 5],
45
+ [15660, 15660, 4], [15662, 15679, 4], [15720, 15799, 4], [15820, 15820, 4],
46
+ [15822, 15840, 4], [15842, 15859, 4], [15862, 15869, 5], [15872, 15879, 5],
47
+ [15880, 15880, 4], [15882, 15899, 4], [16322, 16329, 5], [16342, 16349, 5],
48
+ [16352, 16359, 5], [16360, 16360, 4], [16362, 16380, 4], [16382, 16399, 4],
49
+ [16420, 16420, 4], [16422, 16430, 4], [16432, 16440, 4], [16442, 16450, 4],
50
+ [16452, 16460, 4], [16462, 16479, 4], [16482, 16489, 5], [16520, 16520, 4],
51
+ [16522, 16530, 4], [16532, 16539, 4], [16542, 16549, 5], [16552, 16559, 5],
52
+ [16562, 16569, 5], [16582, 16589, 5], [17220, 17299, 4], [17320, 17389, 4],
53
+ [17420, 17499, 4], [17520, 17549, 4], [17560, 17579, 4], [17620, 17699, 4],
54
+ [17700, 17716, 3], [17718, 17799, 3], [17820, 17899, 4], [17920, 17999, 4],
55
+ [18220, 18299, 4], [18320, 18399, 4], [18420, 18499, 4], [18520, 18589, 4],
56
+ [18620, 18699, 4], [18730, 18789, 4], [18800, 18899, 3], [19120, 19199, 4],
57
+ [19220, 19299, 4], [19320, 19399, 4], [19420, 19479, 4], [19520, 19589, 4],
58
+ [19600, 19616, 3], [19618, 19699, 3], [19720, 19789, 4], [19820, 19849, 4],
59
+ [19860, 19879, 4], [19900, 19929, 3], [22020, 22099, 4], [22200, 22309, 3],
60
+ [22320, 22339, 4], [22340, 22399, 3], [22420, 22489, 4], [22520, 22599, 4],
61
+ [22620, 22699, 4], [22700, 22799, 3], [22820, 22899, 4], [22920, 22999, 4],
62
+ [23320, 23399, 4], [23420, 23499, 4], [23520, 23599, 4], [23600, 23616, 3],
63
+ [23618, 23699, 3], [23720, 23789, 4], [23820, 23899, 4], [24020, 24049, 4],
64
+ [24120, 24176, 4], [24178, 24199, 4], [24220, 24299, 4], [24320, 24399, 4],
65
+ [24420, 24499, 4], [24500, 24516, 3], [24518, 24599, 3], [24620, 24699, 4],
66
+ [24720, 24789, 4], [24820, 24899, 4], [24900, 24916, 3], [24918, 24999, 3],
67
+ [25020, 25099, 4], [25200, 25216, 3], [25218, 25399, 3], [25420, 25490, 4],
68
+ [25492, 25499, 4], [25500, 25516, 3], [25518, 25569, 3], [25570, 25589, 4],
69
+ [25590, 25590, 3], [25592, 25609, 3], [25620, 25699, 4], [25700, 25716, 3],
70
+ [25718, 25719, 3], [25720, 25749, 4], [25750, 25790, 3], [25792, 25809, 3],
71
+ [25820, 25899, 4], [25920, 25989, 4], [26020, 26039, 4], [26120, 26176, 4],
72
+ [26178, 26199, 4], [26200, 26299, 3], [26320, 26399, 4], [26400, 26409, 3],
73
+ [26420, 26459, 4], [26460, 26499, 3], [26520, 26599, 4], [26620, 26699, 4],
74
+ [26720, 26799, 4], [26820, 26899, 4], [26920, 26989, 4], [27020, 27099, 4],
75
+ [27200, 27399, 3], [27420, 27489, 4], [27620, 27699, 4], [27720, 27799, 4],
76
+ [27800, 27809, 3], [27820, 27879, 4], [27880, 27899, 3], [27920, 27999, 4],
77
+ [28020, 28099, 4], [28220, 28299, 4], [28300, 28309, 3], [28320, 28329, 4],
78
+ [28330, 28349, 3], [28350, 28399, 4], [28420, 28499, 4], [28520, 28599, 4],
79
+ [28600, 28699, 3], [28720, 28799, 4], [28820, 28899, 4], [28900, 28909, 3],
80
+ [28920, 28959, 3], [28960, 28999, 4], [29130, 29149, 4], [29200, 29309, 3],
81
+ [29320, 29349, 4], [29350, 29399, 3], [29420, 29499, 4], [29550, 29570, 4],
82
+ [29572, 29579, 4], [29620, 29659, 4], [29670, 29689, 4], [29720, 29799, 4],
83
+ [29800, 29899, 3], [29920, 29999, 4], [42000, 42009, 2], [42200, 42209, 3],
84
+ [42220, 42299, 4], [42300, 42316, 3], [42318, 42416, 3], [42418, 42516, 3],
85
+ [42518, 42599, 3], [42610, 42616, 3], [42618, 42670, 3], [42672, 42680, 3],
86
+ [42682, 42716, 3], [42718, 42730, 3], [42732, 42780, 3], [42782, 42816, 3],
87
+ [42818, 42819, 3], [42820, 42839, 4], [42840, 42869, 3], [42870, 42899, 4],
88
+ [42900, 42909, 2], [42910, 42916, 3], [42918, 42919, 3], [42920, 42969, 2],
89
+ [42970, 42989, 3], [42990, 42999, 2], [43200, 43499, 3], [43620, 43699, 4],
90
+ [43820, 43899, 4], [43920, 43999, 4], [44200, 44999, 3], [45200, 45999, 3],
91
+ [46080, 46089, 4], [46200, 46216, 3], [46218, 46299, 3], [46320, 46399, 4],
92
+ [46400, 46419, 3], [46520, 46599, 4], [46620, 46699, 4], [46720, 46799, 4],
93
+ [46800, 46816, 3], [46818, 46899, 3], [47000, 47009, 2], [47020, 47089, 4],
94
+ [47090, 47116, 2], [47118, 47176, 2], [47178, 47199, 2], [47300, 47499, 3],
95
+ [47520, 47589, 4], [47620, 47699, 4], [47700, 47729, 3], [47750, 47779, 3],
96
+ [47850, 47889, 4], [47920, 47989, 4], [48020, 48099, 4], [48200, 48299, 3],
97
+ [48400, 48999, 3], [49200, 49299, 3], [49320, 49399, 4], [49420, 49499, 4],
98
+ [49520, 49599, 4], [49922, 49929, 5], [49942, 49949, 5], [49962, 49969, 5],
99
+ [49982, 49989, 5], [53120, 53149, 4], [53220, 53299, 4], [53320, 53399, 4],
100
+ [53400, 53599, 3], [53620, 53639, 4], [53660, 53689, 4], [53720, 53799, 4],
101
+ [53820, 53899, 4], [53920, 53959, 3], [53960, 53963, 4], [53964, 53973, 3],
102
+ [53974, 53974, 4], [53975, 53976, 3], [53977, 53977, 4], [53978, 53989, 3],
103
+ [53990, 53999, 4], [54200, 54299, 3], [54320, 54399, 3], [54420, 54499, 4],
104
+ [54520, 54599, 4], [54600, 54699, 3], [54720, 54799, 4], [54820, 54899, 4],
105
+ [54900, 54909, 3], [55020, 55099, 4], [55120, 55176, 4], [55178, 55199, 4],
106
+ [55200, 55216, 3], [55218, 55299, 3], [55320, 55399, 4], [55420, 55499, 4],
107
+ [55520, 55599, 4], [55620, 55660, 4], [55662, 55669, 4], [55720, 55799, 4],
108
+ [55820, 55899, 4], [55900, 55916, 3], [55918, 55999, 3], [57620, 57689, 4],
109
+ [57692, 57699, 5], [58120, 58176, 4], [58178, 58199, 4], [58200, 58399, 3],
110
+ [58420, 58499, 4], [58520, 58599, 4], [58620, 58699, 4], [58720, 58799, 4],
111
+ [59200, 59299, 3], [59310, 59399, 3], [59420, 59499, 4], [59520, 59599, 4],
112
+ [59620, 59679, 4], [59720, 59739, 4], [59742, 59749, 4], [59770, 59790, 4],
113
+ [59792, 59796, 5], [59797, 59798, 4], [59799, 59799, 5], [59820, 59889, 4],
114
+ [59920, 59989, 4], [59990, 59999, 3], [72120, 72176, 4], [72178, 72199, 4],
115
+ [72200, 72499, 3], [72520, 72599, 4], [72600, 72999, 3], [73400, 73499, 3],
116
+ [73520, 73579, 4], [73620, 73689, 4], [73720, 73799, 4], [73820, 73899, 4],
117
+ [73920, 73999, 4], [74630, 74669, 4], [74682, 74689, 5], [76120, 76184, 4],
118
+ [76200, 76216, 3], [76218, 76299, 3], [76320, 76399, 4], [76400, 76416, 3],
119
+ [76418, 76499, 3], [76520, 76599, 4], [76620, 76699, 4], [76720, 76789, 4],
120
+ [76820, 76889, 4], [77020, 77079, 4], [77120, 77189, 4], [77220, 77289, 4],
121
+ [77320, 77389, 4], [77420, 77499, 4], [77500, 77516, 3], [77518, 77599, 3],
122
+ [77620, 77699, 4], [77820, 77899, 4], [77960, 77971, 4], [77977, 77989, 4],
123
+ [79020, 79089, 4], [79120, 79129, 4], [79140, 79179, 4], [79200, 79339, 3],
124
+ [79400, 79409, 3], [79420, 79459, 3], [79460, 79489, 4], [79490, 79509, 3],
125
+ [79520, 79549, 4], [79550, 79569, 3], [79570, 79589, 4], [79590, 79609, 3],
126
+ [79620, 79659, 4], [79660, 79679, 3], [79680, 79699, 4], [79720, 79799, 4],
127
+ [79820, 79899, 4], [79920, 79989, 4], [82020, 82089, 4], [82200, 82299, 3],
128
+ [82320, 82399, 4], [82400, 82409, 3], [82420, 82439, 3], [82440, 82489, 4],
129
+ [82490, 82599, 3], [82620, 82689, 4], [82720, 82750, 4], [82752, 82799, 4],
130
+ [82800, 82909, 3], [82920, 82920, 4], [82921, 82929, 3], [82930, 82940, 4],
131
+ [82941, 82943, 3], [82944, 82959, 4], [82960, 82969, 3], [82970, 82989, 4],
132
+ [82990, 82999, 3], [83220, 83299, 3], [83320, 83399, 4], [83420, 83499, 4],
133
+ [83520, 83599, 4], [83600, 83609, 3], [83620, 83699, 4], [83720, 83765, 4],
134
+ [83766, 83768, 3], [83769, 83769, 4], [83770, 83789, 3], [83820, 83859, 4],
135
+ [83872, 83879, 5], [83882, 83889, 5], [83900, 83950, 3], [83952, 83960, 3],
136
+ [83962, 83962, 5], [83963, 83963, 3], [83964, 83965, 5], [83966, 83966, 3],
137
+ [83967, 83969, 5], [83970, 83999, 3], [84520, 84539, 4], [84620, 84649, 4],
138
+ [84660, 84679, 4], [84720, 84769, 4], [84772, 84779, 5], [84780, 84799, 4],
139
+ [84820, 84899, 4], [84900, 84999, 3], [86200, 86299, 3], [86320, 86359, 4],
140
+ [86362, 86365, 3], [86366, 86366, 4], [86367, 86369, 3], [86370, 86389, 4],
141
+ [86420, 86489, 3], [86520, 86529, 3], [86540, 86551, 4], [86552, 86553, 3],
142
+ [86554, 86579, 4], [86600, 86609, 3], [86620, 86690, 4], [86691, 86691, 3],
143
+ [86692, 86696, 4], [86697, 86698, 3], [86699, 86699, 4], [86720, 86721, 4],
144
+ [86722, 86724, 3], [86725, 86725, 4], [86726, 86726, 3], [86727, 86727, 4],
145
+ [86728, 86728, 3], [86729, 86736, 4], [86737, 86738, 3], [86739, 86799, 4],
146
+ [86800, 86809, 3], [86820, 86889, 4], [86890, 86909, 3], [86920, 86929, 4],
147
+ [86934, 86934, 4], [86940, 86959, 3], [86960, 86989, 4], [86992, 86993, 4],
148
+ [86994, 86999, 3], [87520, 87599, 4], [87720, 87799, 4], [87800, 87899, 3],
149
+ [87920, 87989, 4], [88020, 88099, 4], [88320, 88389, 4], [88420, 88489, 4],
150
+ [88530, 88530, 4], [88532, 88540, 4], [88542, 88549, 4], [88600, 88616, 3],
151
+ [88618, 88699, 3], [88720, 88720, 4], [88722, 88799, 4], [88800, 88816, 3],
152
+ [88818, 88899, 3], [88920, 88969, 4], [89220, 89299, 4], [89320, 89399, 4],
153
+ [89420, 89499, 4], [89520, 89589, 4], [89620, 89699, 4], [89720, 89789, 4],
154
+ [89820, 89899, 4], [89900, 89999, 3], [92040, 92059, 4], [92080, 92089, 4],
155
+ [92200, 92999, 3], [93020, 93059, 4], [93200, 93999, 3], [95020, 95099, 4],
156
+ [95220, 95299, 4], [95420, 95449, 4], [95460, 95479, 4], [95520, 95589, 4],
157
+ [95620, 95699, 4], [95720, 95789, 4], [95800, 95899, 3], [95920, 95999, 4],
158
+ [96200, 96399, 3], [96420, 96499, 4], [96520, 96599, 4], [96620, 96620, 4],
159
+ [96622, 96689, 4], [96720, 96720, 4], [96722, 96799, 4], [96820, 96889, 4],
160
+ [96920, 96989, 4], [97220, 97289, 4], [97320, 97359, 4], [97370, 97389, 4],
161
+ [97420, 97420, 4], [97422, 97449, 4], [97460, 97460, 4], [97462, 97479, 4],
162
+ [97500, 97599, 3], [97720, 97799, 4], [97820, 97820, 4], [97822, 97860, 4],
163
+ [97862, 97899, 4], [97920, 97999, 4], [98022, 98029, 5], [98030, 98070, 4],
164
+ [98072, 98080, 4], [98082, 98099, 4], [98220, 98299, 4], [98320, 98399, 4],
165
+ [98420, 98499, 4], [98520, 98599, 4], [98620, 98699, 4], [98720, 98799, 4],
166
+ [98800, 98999, 3], [99122, 99129, 5], [99132, 99139, 5], [99200, 99216, 3],
167
+ [99218, 99299, 3], [99320, 99330, 4], [99331, 99331, 3], [99332, 99342, 4],
168
+ [99343, 99343, 3], [99344, 99344, 4], [99345, 99345, 3], [99346, 99346, 4],
169
+ [99347, 99347, 3], [99348, 99389, 4], [99400, 99409, 3], [99420, 99469, 4],
170
+ [99470, 99489, 3], [99490, 99499, 4], [99520, 99579, 4], [99620, 99689, 4],
171
+ [99692, 99699, 5], [99720, 99720, 4], [99722, 99799, 4], [99800, 99839, 3],
172
+ ];
@@ -0,0 +1 @@
1
+ export declare function landlineAreaLength(digits: string): number | undefined;
@@ -0,0 +1,40 @@
1
+ import { ambiguousAreaRanges, areaCodes, overlappingAreaCodes } from './landline-data.js';
2
+ export function landlineAreaLength(digits) {
3
+ if (digits.length < 3 || !digits.startsWith('0'))
4
+ return undefined;
5
+ if (overlappingAreaCodes.some((code) => digits.startsWith(code)))
6
+ return overlappingAreaLength(digits);
7
+ let candidate;
8
+ for (const code of areaCodes) {
9
+ if (!digits.startsWith(code) && !code.startsWith(digits))
10
+ continue;
11
+ if (candidate !== undefined && candidate !== code.length)
12
+ return overlappingAreaLength(digits);
13
+ candidate = code.length;
14
+ }
15
+ return candidate;
16
+ }
17
+ function overlappingAreaLength(digits) {
18
+ const prefix = digits.slice(0, 6);
19
+ const lower = Number(prefix.padEnd(6, '0'));
20
+ const upper = Number(prefix.padEnd(6, '9'));
21
+ let left = 0;
22
+ let right = ambiguousAreaRanges.length;
23
+ while (left < right) {
24
+ const middle = Math.floor((left + right) / 2);
25
+ if (ambiguousAreaRanges[middle][1] < lower)
26
+ left = middle + 1;
27
+ else
28
+ right = middle;
29
+ }
30
+ let candidate;
31
+ for (let index = left; index < ambiguousAreaRanges.length; index++) {
32
+ const [start, , length] = ambiguousAreaRanges[index];
33
+ if (start > upper)
34
+ break;
35
+ if (length === 0 || candidate !== undefined && candidate !== length)
36
+ return undefined;
37
+ candidate = length;
38
+ }
39
+ return candidate;
40
+ }
@@ -0,0 +1,8 @@
1
+ import type { PresetName } from './types.js';
2
+ export type PhonePreset = Extract<PresetName, 'phoneNumber' | 'mobilePhone' | 'landlinePhone' | 'ipPhone' | 'tollFree'>;
3
+ export interface PhoneLayout {
4
+ readonly firstBreak: number;
5
+ readonly secondBreak: number;
6
+ readonly length: number;
7
+ }
8
+ export declare function phoneLayout(digits: string, preset: PhonePreset): PhoneLayout | undefined;
package/dist/phone.js ADDED
@@ -0,0 +1,21 @@
1
+ import { landlineAreaLength } from './landline.js';
2
+ const mobile = { firstBreak: 3, secondBreak: 7, length: 11 };
3
+ const ip = { firstBreak: 3, secondBreak: 7, length: 11 };
4
+ const tollFree0120 = { firstBreak: 4, secondBreak: 7, length: 10 };
5
+ const tollFree0800 = { firstBreak: 4, secondBreak: 7, length: 11 };
6
+ export function phoneLayout(digits, preset) {
7
+ const any = preset === 'phoneNumber';
8
+ if (digits.startsWith('0120'))
9
+ return any || preset === 'tollFree' ? tollFree0120 : undefined;
10
+ if (digits.startsWith('0800'))
11
+ return any || preset === 'tollFree' ? tollFree0800 : undefined;
12
+ if (digits.startsWith('050'))
13
+ return any || preset === 'ipPhone' ? ip : undefined;
14
+ if (/^0[6789]0/.test(digits))
15
+ return any || preset === 'mobilePhone' ? mobile : undefined;
16
+ if (any || preset === 'landlinePhone') {
17
+ const firstBreak = landlineAreaLength(digits);
18
+ if (firstBreak !== undefined)
19
+ return { firstBreak, secondBreak: 6, length: 10 };
20
+ }
21
+ }
@@ -0,0 +1,49 @@
1
+ export type PresetName = 'postalCode' | 'phoneNumber' | 'mobilePhone' | 'landlinePhone' | 'ipPhone' | 'tollFree' | 'date' | 'birthDate' | 'corporateNumber' | 'invoiceNumber' | 'bankAccount' | 'yen';
2
+ export interface MaskOptions {
3
+ readonly separator?: string;
4
+ readonly maxDigits?: number;
5
+ readonly fullWidthDigits?: 'convert' | 'preserve';
6
+ readonly invalid?: 'preserve' | 'drop';
7
+ readonly overflow?: 'preserve' | 'truncate';
8
+ }
9
+ export interface YenOptions extends MaskOptions {
10
+ readonly allowNegative?: boolean;
11
+ }
12
+ export type PresetOptions = MaskOptions & ({
13
+ readonly preset: 'yen';
14
+ readonly allowNegative?: boolean;
15
+ } | {
16
+ readonly preset: Exclude<PresetName, 'yen'>;
17
+ readonly allowNegative?: never;
18
+ });
19
+ export interface CustomMaskOptions extends MaskOptions {
20
+ readonly groups: readonly number[];
21
+ /** Matches one Unicode code point at a time, after optional width conversion. */
22
+ readonly allowed?: RegExp;
23
+ }
24
+ export type InputMaskOptions = PresetOptions | CustomMaskOptions;
25
+ export type SelectionDirection = 'forward' | 'backward' | 'none';
26
+ export interface InputSnapshot {
27
+ readonly value: string;
28
+ readonly selectionStart?: number;
29
+ readonly selectionEnd?: number;
30
+ readonly selectionDirection?: SelectionDirection;
31
+ }
32
+ export interface InputState extends InputSnapshot {
33
+ readonly inputType?: string;
34
+ readonly isComposing?: boolean;
35
+ /** The field before the native edit, typically captured in beforeinput. */
36
+ readonly previous?: InputSnapshot;
37
+ }
38
+ export interface RejectedCharacter {
39
+ readonly value: string;
40
+ readonly index: number;
41
+ readonly reason: 'invalid' | 'overflow';
42
+ }
43
+ export interface MaskResult extends Required<InputSnapshot> {
44
+ readonly rawValue: string;
45
+ readonly complete: boolean;
46
+ readonly rejected: readonly RejectedCharacter[];
47
+ readonly changed: boolean;
48
+ }
49
+ export type InputMask = (state: InputState) => MaskResult;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/docs/API.md ADDED
@@ -0,0 +1,78 @@
1
+ # API
2
+
3
+ The package root exports all public APIs. `ja-input-mask/core` exports pure functions and their types. `ja-input-mask/dom` exports `attachInputMask`, `AttachInputMaskOptions`, and `InputMaskController`.
4
+
5
+ ## String formatters
6
+
7
+ All take `(value: string, options?: MaskOptions)` and return a string. `maskYen` accepts `YenOptions`.
8
+
9
+ | Function | Preset | Default format and completion |
10
+ | --- | --- | --- |
11
+ | `maskPostalCode` | `postalCode` | 3–4 digits |
12
+ | `maskPhoneNumber` | `phoneNumber` | Automatic domestic layout |
13
+ | `maskMobilePhone` | `mobilePhone` | 3–4–4 for `060`, `070`, `080`, `090`, excluding `0800` |
14
+ | `maskLandlinePhone` | `landlinePhone` | 10 digits with a 2–5 digit domestic area code |
15
+ | `maskIpPhone` | `ipPhone` | 3–4–4 for `050` |
16
+ | `maskTollFree` | `tollFree` | 4–3–3 for `0120`, 4–3–4 for `0800` |
17
+ | `maskDate` / `maskBirthDate` | `date` / `birthDate` | 4/2/2 digits, `YYYY/MM/DD` |
18
+ | `maskCorporateNumber` | `corporateNumber` | 13 digits |
19
+ | `maskInvoiceNumber` | `invoiceNumber` | Typed `T` then 13 digits. Leading `t`, `T` and `t` become `T`. |
20
+ | `maskBankAccount` | `bankAccount` | 7 digits. Leading zeros remain. |
21
+ | `maskYen` | `yen` | Integer digits in thousands groups, optional leading minus. A leading `¥` or `¥` can be pasted as display decoration. |
22
+
23
+ Separators appear when the following group starts. Postal and phone masks accept common dash variants and phone masks accept spaces between digits. Date masks accept `/`, `.`, and common dash variants at the year and month boundaries, including full-width variants. Ambiguous dates such as `2024/2/29` remain visible and incomplete under the default policy. National and international dialing syntax such as `+81`, extensions, and service numbers outside the listed presets stays available as an unformatted draft.
24
+
25
+ Date masks report duplicate separators at the same boundary as invalid. With a `previous` snapshot, a separator that survived the edit takes priority over a newly inserted one. Without that history, the first separator at each boundary takes priority.
26
+
27
+ ## Stateful formatting
28
+
29
+ ```ts
30
+ applyInputMask(state, 'postalCode', options?)
31
+ applyInputMask(state, 'yen', yenOptions?)
32
+ applyInputMask(state, compiledMask)
33
+ const compiledMask = createInputMask({ preset: 'date', separator: '-' })
34
+ const custom = createInputMask({ groups: [2, 3], allowed: /[A-Z0-9]/u, separator: ' · ' })
35
+ custom({ value: 'AB123' }).value // 'AB · 123'
36
+ ```
37
+
38
+ `InputState.value` is the **post-edit** text. `selectionStart` and `selectionEnd` are UTF-16 code-unit offsets in that text. Start defaults to the end of the text and end defaults to start. `selectionDirection` accepts `'forward'`, `'backward'`, or `'none'`. Set `inputType` to the native `InputEvent.inputType` when available. Pass the pre-edit `InputSnapshot` in `previous` to retain existing suffix data when truncating an insertion and to support deleting through a generated separator. `isComposing: true` bypasses formatting.
39
+
40
+ | `MaskResult` field | Meaning |
41
+ | --- | --- |
42
+ | `value` | Display value to put back in the field |
43
+ | `rawValue` | Accepted data without display separators, including a typed invoice `T` or a yen minus. Preserved excess data remains here. Rejected non-data is available in `rejected`. During composition this equals the input value. |
44
+ | `selectionStart`, `selectionEnd`, `selectionDirection` | Selection in `value` |
45
+ | `complete` | Expected display shape reached. During composition, with retained rejections, or with an unresolved phone layout this is `false`. |
46
+ | `rejected` | Original token text, UTF-16 offset, and reason (`invalid` or `overflow`). Offsets address the draft being processed. |
47
+ | `changed` | Display value or selection offsets changed relative to the post-edit snapshot |
48
+
49
+ ## Options
50
+
51
+ | Option | Meaning |
52
+ | --- | --- |
53
+ | `separator` | Literal display separator. Use `''` for no separators. Its characters must be distinct from accepted data. |
54
+ | `maxDigits` | Positive maximum for data tokens, with a preset default where applicable. Prefixes are excluded. A custom mask counts accepted Unicode code points. A limit override leaves the preset completion length intact. |
55
+ | `fullWidthDigits` | `'convert'` (default) folds `0`–`9` to ASCII. `'preserve'` retains full-width data, with phone layout still inferred from digit values. |
56
+ | `invalid` | `'preserve'` (default) or `'drop'` |
57
+ | `overflow` | `'preserve'` (default) or `'truncate'`. A native edit with a usable `previous` snapshot retains existing data before accepting new excess digits. If a replacement shortens the automatic phone limit and truncation would discard the prefix that selected it, the previous value and selection are restored. Discarded digits are reported as `overflow` at their post-edit offsets. |
58
+ | `allowNegative` | Yen only. Defaults to `true`. |
59
+ | `groups` | Custom masks only. Non-empty list of positive group sizes. The sum defines the default limit and completion length. |
60
+ | `allowed` | Custom masks only. Regex that must match each complete code point after width handling. The regex is copied with `g`, `m`, and `y` removed. The default accepts ASCII and full-width digits. |
61
+
62
+ Retaining rejected tokens also retains the display text and structure around them. An explicitly enabled removal still applies to its own rejection class. Presets with no groups keep their identifiers continuous even if configured with a separator.
63
+
64
+ ## DOM adapter
65
+
66
+ ```ts
67
+ const handle = attachInputMask(input, {
68
+ mask: { preset: 'postalCode' }, // or a compiled InputMask
69
+ onChange(result, event) {},
70
+ })
71
+ handle.sync()
72
+ handle.isComposing
73
+ handle.detach()
74
+ ```
75
+
76
+ Supported targets are `textarea` and `input` types that implement `setSelectionRange`, including `text`, `search`, and `tel`. Composition events and `InputEvent.isComposing` protect active composition text. A task queued by `compositionend` reads the committed value even when an engine sends commit events in a different order. `beforeinput` captures the pre-edit field for native deletion and clipping. With only `input`, ordinary formatting still runs using the post-edit value.
77
+
78
+ `onChange` runs after the result is applied and receives the native event or `null` from `sync()`. Duplicate events with an unchanged field are coalesced. The adapter is imperative and dispatches no extra DOM events. Code that assigns `input.value` can call `sync()` explicitly. The first attachment owns the field until `detach()`. Calling `attachInputMask` again returns the existing handle. Detach is idempotent and cancels a pending composition update.
package/docs/design.md ADDED
@@ -0,0 +1,61 @@
1
+ # 調査と設計
2
+
3
+ ## 姉妹ライブラリ
4
+
5
+ ローカルの各 v1.7.0 について公開型、実装、テスト、`package.json`、配布物チェックを確認した。
6
+
7
+ | ライブラリ | 確認した内容 | `ja-input-mask` への反映 |
8
+ | --- | --- | --- |
9
+ | `normalize-ja-input` | `normalizeJaInput` と `normalizeJaInputWithChanges`。`phone`、`furigana`、`email`、`multilineAddress` のプリセットと幅・空白・かな・ハイフンのオプション。Vitestでオプションと日本語の境界ケースを網羅し、ESMとCLIを配布。 | 数字の幅とマスクの構造文字だけを局所的に扱う。かなや空白を含む汎用の正規化は、必要なら呼び出し側で適用する。 |
10
+ | `ja-form-validators` | 郵便番号の詳細結果、簡易電話判定、電話の厳密判定と `kind`、`format`、`kindMatching`。ふりがな・全角チェック。厳密電話は国内/国際の形式や局番の形を確認し、Vitestで境界を検証。ESMとlibphonenumberのメタデータを配布。 | DOMの値を確定した時点で検証を組み合わせられる。マスクは国内の表示区切りに使う総務省資料を同梱し、厳密判定を呼び出さない。 |
11
+ | `tiny-ja-date` | `DateInput`、`DateOptions`、`BusinessDayOptions`、`BusinessCalendar`。タイムゾーン、暦日、営業日、祝日・振替営業日を扱う。Vitestと型付きパッケージ確認を用意。 | 編集中の日付は文字列として `YYYY/MM/DD` に整形する。タイムゾーンや暦計算はアプリ側で確定後に適用する。 |
12
+
13
+ 公開リポジトリ。 [normalize-ja-input](https://github.com/fusaihanabi/normalize-ja-input) · [ja-form-validators](https://github.com/fusaihanabi/ja-form-validators) · [tiny-ja-date](https://github.com/fusaihanabi/tiny-ja-date)
14
+
15
+ ## 競合ライブラリ
16
+
17
+ - IMask は中間値を受け付けるパターン、モデルとビュー、文字のスキップや上書きが豊富。入力途中を独立した状態として扱う点を参考にした。 [IMask Guide](https://imask.js.org/guide)
18
+ - Inputmask は可変パターンや多くの入力形式に対応し、DOM分離型の利用方法も備える。本ライブラリの公開設定は日本向けプリセットと簡単なグループ定義に絞った。 [Inputmask](https://github.com/RobinHerbots/Inputmask) · [Inputmask changelog](https://github.com/RobinHerbots/Inputmask/blob/5.x/Changelog.md)
19
+ - Cleave.js の後継 cleave-zen は、整形関数をNode.jsや各種フレームワークから呼べる構成。純粋コアと任意のDOMアダプターを組み合わせられる構成にした。 [cleave-zen](https://github.com/nosir/cleave-zen)
20
+
21
+ ## 文字とカーソルの対応
22
+
23
+ コアは編集直後の文字列をデータ、構造文字、拒否対象に分け、各要素のUTF-16上の範囲を保持する。選択の両端を表示位置へ独立して写し、選択方向も引き継ぐ。既に表示されていた区切りの前後は可能な限り維持する。新しい区切りの位置にちょうど来たカーソルは区切りの手前に置く。複数コードユニットの区切りの内部は区切り直後に寄せる。
24
+
25
+ ブラウザがBackspaceまたはDeleteで区切りだけを消したことが編集前後の値から確認できる場合、生成した区切りの編集方向にあるデータ1文字を削除対象にする。選択範囲の削除・置換にはブラウザが適用した範囲を使う。上限設定で切り詰める場合は、編集前後で一致する既存データを優先する。入力欄へのプログラムによる値の再設定に伴い、ブラウザのネイティブUndo履歴はブラウザごとに変わり得る。
26
+
27
+ 電話は国内の種別を先頭から判定する。`080` の後は `0800` と携帯番号を区別できるまで区切りを挿入しない。固定電話は市外局番一覧と、重複する市外局番に対する先頭6桁の市内局番区画を参照する。例えば `042`、`0422`、`04` の区切りは後続桁で変わる。番号が不明、あるいは区切り位置を特定できないときは入力された区切りを維持する。割当の有効性には使わない。出典と更新手順は [NOTICE](../NOTICE) および下記を参照。
28
+
29
+ ### 局番データの更新
30
+
31
+ 総務省の [市外局番の一覧(PDF)](https://www.soumu.go.jp/main_content/000141817.pdf) と、[電気通信番号指定状況](https://www.soumu.go.jp/main_sosiki/joho_tsusin/top/tel_number/number_shitei.html) にある固定電話番号のXLSを1〜9まで取得する。`pypdf` と `xlrd` を導入したPythonで `python scripts/update-landline.py <PDF> <XLSフォルダ>` を実行し、`npm test` で桁区切りを確認する。配布物に同梱するのは加工後の数値情報。各XLSの基準日は個別の表に記載されている。
32
+
33
+ ## IMEとブラウザ
34
+
35
+ Chromeについては確定時の`input`が`compositionend`の前に来るケース、Firefoxについてはその前後の両方に`input`を追加する実装と従来の確定後の`input`が報告されている。Safari/WebKitでは`insertFromComposition`を含む順序と、確定キーと`compositionend`の順序差が報告されている。WebKitでは修正を有効化したあとに再び無効化された経緯もあり、ブラウザ名による分岐は置いていない。 [Mozillaの確定時イベントの調査](https://bugzilla.mozilla.org/show_bug.cgi?id=1305387) · [Firefoxの実装](https://bugzilla.mozilla.org/show_bug.cgi?id=2011114) · [WebKitの順序変更](https://bugs.webkit.org/show_bug.cgi?id=317127) · [W3C Input Events](https://www.w3.org/TR/input-events-2/)
36
+
37
+ アダプターは `compositionstart` と `compositionupdate`、`InputEvent.isComposing` で書き換えを抑止し、`compositionend` で次のタスクに整形を予約する。確定時の追加`input`が同じタスク内に届く場合も、予約処理が最新のDOMを読む。確定後に届いた重複通知は、フィールドと選択が同じであれば処理を繰り返さない。`beforeinput` は編集前のキャプチャに使う。IME状態の追跡やEnter送信の制御には `ja-ime-utils` を使える。 [関連ライブラリ](../README.md#関連ライブラリ)
38
+
39
+ ### 自動テストと実IMEの確認手順
40
+
41
+ PlaywrightではChromium、Firefox、WebKitを対象に、ブラウザが実行するキー入力、削除、カット、選択置換を確認する。貼り付けとIMEでは、ブラウザの編集で値が変わるタイミングを合成イベントで再現する。PlaywrightのWebKitはエンジンレベルの自動テストに使い、SafariでのOSの日本語入力は実機で確認する。 [PlaywrightのWebKitについて](https://playwright.dev/docs/browsers#webkit)
42
+
43
+ #### 実IMEの確認結果
44
+
45
+ 2026年11月28日、Windows(ビルド26300)、Chrome 158.0.8127.89、Microsoft IMEのひらがな入力で検証した。デモの郵便番号欄で「不正文字を除去」を有効にした。
46
+
47
+ | 操作 | 確認結果 |
48
+ | --- | --- |
49
+ | Enterで確定 | 未確定の `あ` と `1` は入力欄に残った。確定後に `あ` が拒否され、`1` が `1` になった。 |
50
+ | Escでキャンセル | 確定済みの `1` の後ろで `2` の変換候補一覧を開いた。Escで候補一覧を閉じ、変換を解除する間は `12` と変換中表示が維持された。今回の手順では3回目のEscで未確定の `2` だけが消え、`compositionend` の後に `1`、選択範囲 `1–1`、IME待機中に戻った。 |
51
+ | フォーカス移動 | `13` を変換中に「入力の状態」パネルの見出しをクリックした。入力欄のフォーカスが外れて `compositionend` が発生し、`13` に整形された。確定時の追加 `input` は履歴に現れなかった。入力欄に戻ると、続けて `4` の変換を開始できた。 |
52
+ | 候補クリックで確定 | OSの候補一覧に全角数字が表示され、候補を表示している間は入力欄が変換中のままであることを確認した。候補行は別プロセスの入力ウィンドウにあり、自動操作でのクリックが届かなかったため、クリックによる確定結果は未確認。 |
53
+
54
+ 同じビルドでPlaywrightのWebKit自動テストを再実行し、10件すべてが通過した。macOS Safari・iOS Safariは、利用できるApple端末や実機セッションがなかったため未検証。
55
+
56
+ #### 実機での再現手順
57
+
58
+ 1. `npm run demo` でページを開き、OSの日本語IMEを有効にする。入力欄で全角数字やかなを入力し、未確定の間に画面の値と選択が勝手に書き換わらないことを確認する。
59
+ 2. Enterと候補一覧のクリックまたはタップでそれぞれ確定し、全角数字が確定後に整形されることと、イベント履歴を確認する。「不正文字を除去」を有効にすると、かなが確定後に除去されることも確認できる。
60
+ 3. Escで候補一覧を閉じた時点と、変換が終了した時点を区別する。確定済み文字と選択位置を記録し、未確定の文字だけが取り消されることを確認する。iOSでEscキーを使う場合は外付けキーボードで確認する。
61
+ 4. 変換中に入力欄の外をクリックまたはタップし、フォーカスが外れたか、文字が確定または取り消されたか、再フォーカス後に編集できるかを確認する。OS、IME、ブラウザのバージョンとイベント順序を記録する。
@@ -0,0 +1,24 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+ import { attachInputMask } from 'ja-input-mask/dom'
3
+
4
+ export function PostalField() {
5
+ const input = useRef<HTMLInputElement>(null)
6
+ const [raw, setRaw] = useState('')
7
+
8
+ useEffect(() => {
9
+ if (!input.current) return
10
+ const controller = attachInputMask(input.current, {
11
+ mask: { preset: 'postalCode' },
12
+ onChange: ({ rawValue }) => setRaw(rawValue),
13
+ })
14
+ controller.sync()
15
+ return () => controller.detach()
16
+ }, [])
17
+
18
+ return (
19
+ <div>
20
+ <label>郵便番号 <input ref={input} name="postal" type="text" inputMode="numeric" autoComplete="postal-code" /></label>
21
+ <output>{raw}</output>
22
+ </div>
23
+ )
24
+ }
@@ -0,0 +1,24 @@
1
+ <script setup lang="ts">
2
+ import { onBeforeUnmount, onMounted, ref } from 'vue'
3
+ import { attachInputMask } from 'ja-input-mask/dom'
4
+ import type { InputMaskController } from 'ja-input-mask/dom'
5
+
6
+ const input = ref<HTMLInputElement | null>(null)
7
+ const raw = ref('')
8
+ let controller: InputMaskController | undefined
9
+
10
+ onMounted(() => {
11
+ if (!input.value) return
12
+ controller = attachInputMask(input.value, {
13
+ mask: { preset: 'postalCode' },
14
+ onChange: ({ rawValue }) => { raw.value = rawValue },
15
+ })
16
+ controller.sync()
17
+ })
18
+ onBeforeUnmount(() => controller?.detach())
19
+ </script>
20
+
21
+ <template>
22
+ <label>郵便番号 <input ref="input" name="postal" type="text" inputmode="numeric" autocomplete="postal-code"></label>
23
+ <output>{{ raw }}</output>
24
+ </template>