@telixon/web-sdk 1.0.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.
- package/LICENSE +202 -0
- package/README.md +78 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +523 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Martsin Labs
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @telixon/web-sdk
|
|
2
|
+
|
|
3
|
+
The DOM adapter for [`@telixon/core`](https://www.npmjs.com/package/@telixon/core), shipping two headless widgets. `PhoneInput` drives a plain `<input>` while `RegionList` feeds the region picker.
|
|
4
|
+
|
|
5
|
+
[](https://proof.telixon.dev/parity.html)
|
|
6
|
+
[](https://proof.telixon.dev/benchmark.html)
|
|
7
|
+
[](https://www.npmjs.com/package/@telixon/web-sdk)
|
|
8
|
+
|
|
9
|
+
**[Documentation](https://telixon.dev/web-sdk/)** · **[Live demo](https://telixon.dev/web-sdk/guides/complete-field/)**
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @telixon/core @telixon/web-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
`createPhoneInput` turns a plain `<input>` into a phone field, handling the events, the caret, and the history:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { ensureEngineReady } from '@telixon/core';
|
|
23
|
+
import { createPhoneInput } from '@telixon/web-sdk';
|
|
24
|
+
|
|
25
|
+
await ensureEngineReady(); // once per process; createPhoneInput throws until it resolves
|
|
26
|
+
|
|
27
|
+
const phone = createPhoneInput({
|
|
28
|
+
mode: 'international',
|
|
29
|
+
input: document.querySelector<HTMLInputElement>('#phone')!,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
phone.subscribe((state) => {
|
|
33
|
+
// After typing +442071838750:
|
|
34
|
+
// state.value '44 20 7183 8750'
|
|
35
|
+
// state.region 'GB'
|
|
36
|
+
// state.validationError null
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`createRegionList` feeds a region picker, with search, sorting, and pinned rows already applied:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createRegionList, regionToFlagEmoji } from '@telixon/web-sdk';
|
|
44
|
+
|
|
45
|
+
const regions = createRegionList({
|
|
46
|
+
prioritize: ['US', 'CA', 'GB'],
|
|
47
|
+
dataFactory: ({ region }) => regionToFlagEmoji(region),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
regions.getState().options[0];
|
|
51
|
+
// { region: 'US', callingCode: '1', displayName: 'United States', data: '🇺🇸' }
|
|
52
|
+
|
|
53
|
+
regions.search('united');
|
|
54
|
+
regions.getState().options.map((option) => option.region); // ['US', 'GB', 'AE']
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Highlights
|
|
58
|
+
|
|
59
|
+
- **Full input controller.** Live formatting on every keystroke with a stable caret, across
|
|
60
|
+
mid-string edits, deletions, and paste.
|
|
61
|
+
- **Controlled history.** Undo and redo restore the exact prior value and selection.
|
|
62
|
+
- **Headless.** No styles, no rendering. Binds a plain `<input>` in React, Vue, Angular, Svelte, or
|
|
63
|
+
vanilla JS.
|
|
64
|
+
|
|
65
|
+
## Support
|
|
66
|
+
|
|
67
|
+
Questions belong in [Discussions](https://github.com/martsinlabs/telixon/discussions). Bugs and
|
|
68
|
+
feature requests belong in [Issues](https://github.com/martsinlabs/telixon/issues). Vulnerabilities
|
|
69
|
+
follow [SECURITY.md](https://github.com/martsinlabs/telixon/blob/main/SECURITY.md).
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
Setup, workflow, and the engineering standards are in
|
|
74
|
+
[CONTRIBUTING.md](https://github.com/martsinlabs/telixon/blob/main/CONTRIBUTING.md).
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
[Apache-2.0](./LICENSE) © Martsin Labs
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { RegionCode, NumberType, InternationalInputControllerConfig, NationalInputControllerConfig, ValidationError, PhoneNumber } from '@telixon/core';
|
|
2
|
+
|
|
3
|
+
type PhoneInputBaseOptions = {
|
|
4
|
+
input: PhoneInputElement;
|
|
5
|
+
initialValue?: string;
|
|
6
|
+
regionFilter?: readonly RegionCode[] | null;
|
|
7
|
+
numberTypeFilter?: readonly NumberType[] | null;
|
|
8
|
+
placeholderNumberType?: NumberType;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The DOM element that PhoneInput attaches to. Must be an `<input type="text">` or `<input type="tel">`.
|
|
12
|
+
*/
|
|
13
|
+
type PhoneInputElement = HTMLInputElement;
|
|
14
|
+
/**
|
|
15
|
+
* Snapshot of the input controller produced after every state change.
|
|
16
|
+
*
|
|
17
|
+
* - `value`: formatted phone string currently in the input.
|
|
18
|
+
* - `region`: ISO region code resolved from the digits, or `null` when unresolved.
|
|
19
|
+
* - `selectionStart` / `selectionEnd`: caret range within `value` after the last operation.
|
|
20
|
+
* - `regionFilter` / `numberTypeFilter`: active filter values (`null` = no restriction).
|
|
21
|
+
* - `placeholder`: example for resolved region + display config, or `null` when unresolved.
|
|
22
|
+
* - `validationError`: structured outcome from `PhoneNumber.getValidationError()`, or `null` when none apply.
|
|
23
|
+
*/
|
|
24
|
+
type PhoneInputState = {
|
|
25
|
+
readonly value: string;
|
|
26
|
+
readonly region: RegionCode | null;
|
|
27
|
+
readonly selectionStart: number;
|
|
28
|
+
readonly selectionEnd: number;
|
|
29
|
+
readonly regionFilter: readonly RegionCode[] | null;
|
|
30
|
+
readonly numberTypeFilter: readonly NumberType[] | null;
|
|
31
|
+
readonly placeholder: string | null;
|
|
32
|
+
readonly validationError: ValidationError | null;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Options for a national PhoneInput. `defaultRegion` selects the territory whose national formatting applies.
|
|
36
|
+
*/
|
|
37
|
+
type NationalPhoneInputOptions = {
|
|
38
|
+
mode: 'national';
|
|
39
|
+
} & PhoneInputBaseOptions & NationalInputControllerConfig;
|
|
40
|
+
/**
|
|
41
|
+
* Options for an international PhoneInput. By default the calling code is part of the input value;
|
|
42
|
+
* set `display.callingCodeInInput: false` (with a `defaultRegion`) to keep it outside the input.
|
|
43
|
+
*/
|
|
44
|
+
type InternationalPhoneInputOptions = {
|
|
45
|
+
mode: 'international';
|
|
46
|
+
} & PhoneInputBaseOptions & InternationalInputControllerConfig;
|
|
47
|
+
/**
|
|
48
|
+
* Discriminated union of PhoneInput options keyed by `mode`.
|
|
49
|
+
*/
|
|
50
|
+
type PhoneInputOptions = NationalPhoneInputOptions | InternationalPhoneInputOptions;
|
|
51
|
+
/**
|
|
52
|
+
* Subscriber callback. Receives the latest state on every emit.
|
|
53
|
+
*/
|
|
54
|
+
type PhoneInputListener = (state: PhoneInputState) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Headless phone input controller bound to a DOM `<input>`.
|
|
57
|
+
*
|
|
58
|
+
* Subscribe to receive state changes; call mutators to drive the controller. The initial state is
|
|
59
|
+
* applied to the DOM at construction but not emitted to subscribers. Call `getState()` after
|
|
60
|
+
* `subscribe` to read the bootstrap value. Call `destroy` to detach all listeners.
|
|
61
|
+
*/
|
|
62
|
+
type PhoneInput = {
|
|
63
|
+
/** Subscribe to state changes. Returns an unsubscribe function. */
|
|
64
|
+
subscribe(listener: PhoneInputListener): () => void;
|
|
65
|
+
/** Replace the current value with the given digits as a single parse-and-format operation. Pushes a new history entry. */
|
|
66
|
+
setValue(value: string): void;
|
|
67
|
+
/** Switch the active region and re-resolve the current digits under its rules. Pushes a new history entry. */
|
|
68
|
+
setRegion(region: RegionCode): void;
|
|
69
|
+
/** Read the current state without subscribing. */
|
|
70
|
+
getState(): PhoneInputState;
|
|
71
|
+
/** True when an undo step is available. */
|
|
72
|
+
canUndo(): boolean;
|
|
73
|
+
/** True when a redo step is available. */
|
|
74
|
+
canRedo(): boolean;
|
|
75
|
+
/** Revert to the previous history entry. No-op when `canUndo()` is false. */
|
|
76
|
+
undo(): void;
|
|
77
|
+
/** Restore the next history entry. No-op when `canRedo()` is false. */
|
|
78
|
+
redo(): void;
|
|
79
|
+
/** Clear undo/redo history, keeping only the current entry. */
|
|
80
|
+
clearHistory(): void;
|
|
81
|
+
/** Build a {@link PhoneNumber} from the current digits and resolution. */
|
|
82
|
+
getPhoneNumber(): PhoneNumber;
|
|
83
|
+
/** Restrict resolution to the given regions. `null` removes the restriction. No-op when the value is unchanged. */
|
|
84
|
+
setRegionFilter(regions: readonly RegionCode[] | null): void;
|
|
85
|
+
/** Restrict resolution to the given number types. `null` removes the restriction. No-op when the value is unchanged. */
|
|
86
|
+
setNumberTypeFilter(numberTypes: readonly NumberType[] | null): void;
|
|
87
|
+
/** Detach all DOM listeners and clear subscribers. Idempotent. */
|
|
88
|
+
destroy(): void;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Attach the headless phone-field widget to a DOM `<input>` element.
|
|
93
|
+
*
|
|
94
|
+
* Wires DOM events (`beforeinput`, `compositionend`, undo/redo keyboard shortcuts) to a Telixon
|
|
95
|
+
* input controller and synchronizes the input value, selection, and resolved phone metadata.
|
|
96
|
+
*
|
|
97
|
+
* Throws if the element is not `type="text"` or `type="tel"`, if another PhoneInput is already
|
|
98
|
+
* attached to the same element, or if the engine is not ready (`EngineNotReadyError`). Call
|
|
99
|
+
* {@link PhoneInput.destroy} to release.
|
|
100
|
+
*/
|
|
101
|
+
declare function createPhoneInput(options: PhoneInputOptions): PhoneInput;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One entry in the rendered region list.
|
|
105
|
+
*
|
|
106
|
+
* - `region`: ISO region code (e.g. `'US'`).
|
|
107
|
+
* - `callingCode`: numeric calling code as string (e.g. `'1'`).
|
|
108
|
+
* - `displayName`: region name from `Intl.DisplayNames` for the active locale; falls back to the
|
|
109
|
+
* ISO region code when the runtime can't produce a localized name. The exact string depends on the
|
|
110
|
+
* runtime's ICU data, which leaves it byte-unstable across environments (Node/browser/ICU versions).
|
|
111
|
+
* - `data`: caller-defined payload produced by {@link RegionDataFactory}; `undefined` when no factory.
|
|
112
|
+
*/
|
|
113
|
+
type RegionOption<T = undefined> = {
|
|
114
|
+
readonly region: RegionCode;
|
|
115
|
+
readonly callingCode: string;
|
|
116
|
+
readonly displayName: string;
|
|
117
|
+
readonly data: T;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Input passed to the {@link RegionDataFactory}. Carries the already-computed base fields and the
|
|
121
|
+
* active locale so the factory can produce derived values without recomputing them.
|
|
122
|
+
*/
|
|
123
|
+
type RegionDataFactoryInput = {
|
|
124
|
+
readonly region: RegionCode;
|
|
125
|
+
readonly callingCode: string;
|
|
126
|
+
readonly displayName: string;
|
|
127
|
+
readonly locale: string;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Producer for the `data` slot on each {@link RegionOption}. Invoked once per region per base
|
|
131
|
+
* recomputation (construction and on `localize`). Expected to be pure.
|
|
132
|
+
*/
|
|
133
|
+
type RegionDataFactory<T = undefined> = (input: RegionDataFactoryInput) => T;
|
|
134
|
+
/**
|
|
135
|
+
* Custom search predicate. Returns `true` to include `option` in the filtered result.
|
|
136
|
+
*
|
|
137
|
+
* Empty and whitespace-only queries short-circuit without calling this function. The `query` arrives
|
|
138
|
+
* raw; normalize it inside the predicate when needed.
|
|
139
|
+
*/
|
|
140
|
+
type RegionSearchFn<T = undefined> = (query: string, option: RegionOption<T>) => boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Sort selector. Built-in comparators collate `displayName` in the list's `locale` and break ties by
|
|
143
|
+
* region code, which makes them total orders (stable run to run). Because `displayName` comes from
|
|
144
|
+
* `Intl.DisplayNames` and collation uses the runtime's ICU, the resulting order is locale/ICU dependent
|
|
145
|
+
* and not guaranteed identical across environments.
|
|
146
|
+
*
|
|
147
|
+
* - `'alphabetical'`: by `displayName`, then region code (default).
|
|
148
|
+
* - `'callingCode'`: by numeric calling code, then `displayName`, then region code.
|
|
149
|
+
* - Function: custom comparator (you own its determinism).
|
|
150
|
+
*/
|
|
151
|
+
type RegionListSort<T = undefined> = 'alphabetical' | 'callingCode' | ((a: RegionOption<T>, b: RegionOption<T>) => number);
|
|
152
|
+
/**
|
|
153
|
+
* Construction options for {@link RegionList}.
|
|
154
|
+
*
|
|
155
|
+
* All fields are optional. `null` filters mean no restriction; `[]` filters mean explicitly empty
|
|
156
|
+
* (kept distinct on purpose so callers can express "user-cleared filter" vs "intentionally narrowed
|
|
157
|
+
* to zero results").
|
|
158
|
+
*/
|
|
159
|
+
type RegionListOptions<T = undefined> = {
|
|
160
|
+
dataFactory?: RegionDataFactory<T>;
|
|
161
|
+
regionFilter?: readonly RegionCode[] | null;
|
|
162
|
+
numberTypeFilter?: readonly NumberType[] | null;
|
|
163
|
+
searchQuery?: string;
|
|
164
|
+
searchFn?: RegionSearchFn<T>;
|
|
165
|
+
locale?: string;
|
|
166
|
+
sort?: RegionListSort<T>;
|
|
167
|
+
prioritize?: readonly RegionCode[];
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Snapshot emitted on every state change.
|
|
171
|
+
*
|
|
172
|
+
* `options` is the post-filter, post-sort, post-prioritize list as currently rendered.
|
|
173
|
+
*/
|
|
174
|
+
type RegionListState<T = undefined> = {
|
|
175
|
+
readonly options: readonly RegionOption<T>[];
|
|
176
|
+
readonly regionFilter: readonly RegionCode[] | null;
|
|
177
|
+
readonly numberTypeFilter: readonly NumberType[] | null;
|
|
178
|
+
readonly searchQuery: string;
|
|
179
|
+
readonly locale: string;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Subscriber callback. Receives the latest state on every emit.
|
|
183
|
+
*/
|
|
184
|
+
type RegionListListener<T = undefined> = (state: RegionListState<T>) => void;
|
|
185
|
+
/**
|
|
186
|
+
* Headless region list controller. Produces a reactive, filtered, sorted, searched, localized list
|
|
187
|
+
* of region options for pickers, dropdowns, and selector UIs.
|
|
188
|
+
*
|
|
189
|
+
* Action verbs (`search`, `localize`) name user-driven interactions; `set*` names declarative config
|
|
190
|
+
* mutations. No initial emit is fired on construction. Call `getState()` after `subscribe` to read
|
|
191
|
+
* the bootstrap value.
|
|
192
|
+
*/
|
|
193
|
+
type RegionList<T = undefined> = {
|
|
194
|
+
/** Subscribe to state changes. Returns an unsubscribe function. */
|
|
195
|
+
subscribe(listener: RegionListListener<T>): () => void;
|
|
196
|
+
/** Read the current state without subscribing. */
|
|
197
|
+
getState(): RegionListState<T>;
|
|
198
|
+
/** Update the search query and re-run the filter pipeline. No-op when the query is unchanged. */
|
|
199
|
+
search(query: string): void;
|
|
200
|
+
/** Switch the locale, recompute display names and `data`, then re-run the pipeline. No-op when the locale is unchanged. */
|
|
201
|
+
localize(locale: string): void;
|
|
202
|
+
/** Recompute the base set (display names + `dataFactory`) and re-emit. Useful when external state read by `dataFactory` has changed. Always emits. */
|
|
203
|
+
refresh(): void;
|
|
204
|
+
/** Restrict the list to the given regions. `null` removes the restriction; `[]` matches nothing. No-op when the value is unchanged. */
|
|
205
|
+
setRegionFilter(value: readonly RegionCode[] | null): void;
|
|
206
|
+
/** Restrict the list to regions supporting at least one of the given number types. `null` removes the restriction; `[]` matches nothing. No-op when the value is unchanged. */
|
|
207
|
+
setNumberTypeFilter(value: readonly NumberType[] | null): void;
|
|
208
|
+
/** Clear all subscribers. Idempotent. */
|
|
209
|
+
destroy(): void;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Create the headless widget behind a region picker.
|
|
214
|
+
*
|
|
215
|
+
* Pipeline on every state change: base set (cached per locale) -> regionFilter -> numberTypeFilter
|
|
216
|
+
* -> searchFn -> sort -> prioritize -> emit. Mutators that don't change the underlying value skip
|
|
217
|
+
* emission (no-op detection via shallow array equality and string equality).
|
|
218
|
+
*
|
|
219
|
+
* Generic `T` flows from the optional `dataFactory` return type; when omitted, `T` defaults to
|
|
220
|
+
* `undefined` and the `data` slot is present but unused.
|
|
221
|
+
*
|
|
222
|
+
* Requires the engine to be ready (see `ensureEngineReady`); throws `EngineNotReadyError` otherwise.
|
|
223
|
+
*/
|
|
224
|
+
declare function createRegionList<T = undefined>(options?: RegionListOptions<T>): RegionList<T>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Convert an ISO region code to its flag emoji, built from the two Unicode regional indicator
|
|
228
|
+
* symbols for its letters. `regionToFlagEmoji('US')` returns the United States flag
|
|
229
|
+
* (U+1F1FA U+1F1F8).
|
|
230
|
+
*
|
|
231
|
+
* Rendering belongs to the platform. Systems with flag-emoji support (iOS, macOS, Android, most
|
|
232
|
+
* Linux) show a flag; systems without it (notably Windows) show the two letters. For a flag on every
|
|
233
|
+
* platform, render an SVG set keyed by the region code.
|
|
234
|
+
*
|
|
235
|
+
* Every {@link RegionCode} is two ASCII letters, which keeps the result a valid regional indicator
|
|
236
|
+
* pair.
|
|
237
|
+
*/
|
|
238
|
+
declare function regionToFlagEmoji(region: RegionCode): string;
|
|
239
|
+
|
|
240
|
+
export { type InternationalPhoneInputOptions, type NationalPhoneInputOptions, type PhoneInput, type PhoneInputElement, type PhoneInputListener, type PhoneInputOptions, type PhoneInputState, type RegionDataFactory, type RegionDataFactoryInput, type RegionList, type RegionListListener, type RegionListOptions, type RegionListSort, type RegionListState, type RegionOption, type RegionSearchFn, createPhoneInput, createRegionList, regionToFlagEmoji };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { isEngineReady, EngineNotReadyError, getPlaceholders, isNationalPrefixOptional, getCallingCodeForRegion, REGION_CODES, regionSupportsNumberTypes, createNationalInputController, createInternationalInputController } from '@telixon/core';
|
|
2
|
+
|
|
3
|
+
// src/utils/readonly-arrays-equal.ts
|
|
4
|
+
function readonlyArraysEqual(a, b) {
|
|
5
|
+
if (a === b) return true;
|
|
6
|
+
if (a === null || b === null) return false;
|
|
7
|
+
if (a.length !== b.length) return false;
|
|
8
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/modules/phone-input/constants/before-input-types.ts
|
|
13
|
+
var INSERT_INPUT_TYPES = /* @__PURE__ */ new Set([
|
|
14
|
+
"insertText",
|
|
15
|
+
"insertReplacementText",
|
|
16
|
+
"insertFromPaste",
|
|
17
|
+
"insertFromDrop",
|
|
18
|
+
"insertFromYank"
|
|
19
|
+
]);
|
|
20
|
+
var BACKWARD_DELETE_INPUT_TYPES = /* @__PURE__ */ new Set(["deleteContentBackward", "deleteByCut", "deleteContent", "deleteByDrag"]);
|
|
21
|
+
var FORWARD_DELETE_INPUT_TYPES = /* @__PURE__ */ new Set(["deleteContentForward"]);
|
|
22
|
+
var WORD_BACKWARD_DELETE_INPUT_TYPES = /* @__PURE__ */ new Set(["deleteWordBackward"]);
|
|
23
|
+
var WORD_FORWARD_DELETE_INPUT_TYPES = /* @__PURE__ */ new Set(["deleteWordForward"]);
|
|
24
|
+
function isInsertInputType(inputType) {
|
|
25
|
+
return INSERT_INPUT_TYPES.has(inputType);
|
|
26
|
+
}
|
|
27
|
+
function isBackwardDeleteInputType(inputType) {
|
|
28
|
+
return BACKWARD_DELETE_INPUT_TYPES.has(inputType);
|
|
29
|
+
}
|
|
30
|
+
function isForwardDeleteInputType(inputType) {
|
|
31
|
+
return FORWARD_DELETE_INPUT_TYPES.has(inputType);
|
|
32
|
+
}
|
|
33
|
+
function isWordBackwardDeleteInputType(inputType) {
|
|
34
|
+
return WORD_BACKWARD_DELETE_INPUT_TYPES.has(inputType);
|
|
35
|
+
}
|
|
36
|
+
function isWordForwardDeleteInputType(inputType) {
|
|
37
|
+
return WORD_FORWARD_DELETE_INPUT_TYPES.has(inputType);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/modules/phone-input/utils/apply-input-state.ts
|
|
41
|
+
function applyInputState(input, state) {
|
|
42
|
+
input.value = state.value;
|
|
43
|
+
input.setSelectionRange(state.selectionStart, state.selectionEnd);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/modules/phone-input/utils/assert-supported-input-type.ts
|
|
47
|
+
function assertSupportedInputType(input) {
|
|
48
|
+
if (input.type === "text" || input.type === "tel") return;
|
|
49
|
+
throw new Error(
|
|
50
|
+
`@telixon/web-sdk requires an <input type="text"> or <input type="tel"> element, received type="${input.type}".`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/modules/phone-input/utils/before-input.ts
|
|
55
|
+
function resolveInsertText(event) {
|
|
56
|
+
return event.data ?? event.dataTransfer?.getData("text/plain") ?? "";
|
|
57
|
+
}
|
|
58
|
+
function createNationalController(options) {
|
|
59
|
+
const {
|
|
60
|
+
input: _input,
|
|
61
|
+
mode: _mode,
|
|
62
|
+
regionFilter: _regionFilter,
|
|
63
|
+
numberTypeFilter: _numberTypeFilter,
|
|
64
|
+
placeholderNumberType: _placeholderNumberType,
|
|
65
|
+
...config
|
|
66
|
+
} = options;
|
|
67
|
+
return createNationalInputController(config);
|
|
68
|
+
}
|
|
69
|
+
function createInternationalController(options) {
|
|
70
|
+
const {
|
|
71
|
+
input: _input,
|
|
72
|
+
mode: _mode,
|
|
73
|
+
regionFilter: _regionFilter,
|
|
74
|
+
numberTypeFilter: _numberTypeFilter,
|
|
75
|
+
placeholderNumberType: _placeholderNumberType,
|
|
76
|
+
...config
|
|
77
|
+
} = options;
|
|
78
|
+
return createInternationalInputController(config);
|
|
79
|
+
}
|
|
80
|
+
function buildController(options) {
|
|
81
|
+
return options.mode === "national" ? createNationalController(options) : createInternationalController(options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/modules/phone-input/utils/derive-state.ts
|
|
85
|
+
function deriveState(inputState, regionFilter, numberTypeFilter, placeholder, validationError) {
|
|
86
|
+
return {
|
|
87
|
+
value: inputState.value,
|
|
88
|
+
region: inputState.region,
|
|
89
|
+
selectionStart: inputState.selectionStart,
|
|
90
|
+
selectionEnd: inputState.selectionEnd,
|
|
91
|
+
regionFilter,
|
|
92
|
+
numberTypeFilter,
|
|
93
|
+
placeholder,
|
|
94
|
+
validationError
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function buildPlaceholderConfig(options) {
|
|
98
|
+
const numberType = options.placeholderNumberType ?? "MOBILE";
|
|
99
|
+
if (options.mode === "national") return { mode: "national", numberType };
|
|
100
|
+
const display = options.display;
|
|
101
|
+
if (display === void 0) {
|
|
102
|
+
return { mode: "international", callingCodeInInput: true, plusPrefix: false, numberType };
|
|
103
|
+
}
|
|
104
|
+
if (display.callingCodeInInput === false) {
|
|
105
|
+
return { mode: "international", callingCodeInInput: false, numberType };
|
|
106
|
+
}
|
|
107
|
+
return { mode: "international", callingCodeInInput: true, plusPrefix: display.plusPrefix !== "none", numberType };
|
|
108
|
+
}
|
|
109
|
+
function resolvePlaceholder(region, config) {
|
|
110
|
+
if (region === null) return null;
|
|
111
|
+
const placeholders = getPlaceholders(region, config.numberType);
|
|
112
|
+
if (placeholders === null) return null;
|
|
113
|
+
if (config.mode === "national") {
|
|
114
|
+
return isNationalPrefixOptional(region, config.numberType) ? placeholders.national ?? placeholders.nationalWithPrefix ?? null : placeholders.nationalWithPrefix ?? placeholders.national ?? null;
|
|
115
|
+
}
|
|
116
|
+
if (config.callingCodeInInput === false) {
|
|
117
|
+
return placeholders.international ?? null;
|
|
118
|
+
}
|
|
119
|
+
if (placeholders.international === void 0) return null;
|
|
120
|
+
const callingCode = getCallingCodeForRegion(region);
|
|
121
|
+
return config.plusPrefix ? "+" + callingCode + " " + placeholders.international : callingCode + " " + placeholders.international;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/modules/phone-input/utils/word-boundary.ts
|
|
125
|
+
var CHAR_CODE_ZERO = 48;
|
|
126
|
+
var CHAR_CODE_NINE = 57;
|
|
127
|
+
function isDigit(charCode) {
|
|
128
|
+
return charCode >= CHAR_CODE_ZERO && charCode <= CHAR_CODE_NINE;
|
|
129
|
+
}
|
|
130
|
+
function findPreviousWordBoundary(value, from) {
|
|
131
|
+
let i = Math.min(from, value.length) - 1;
|
|
132
|
+
if (i < 0) return 0;
|
|
133
|
+
if (!isDigit(value.charCodeAt(i))) {
|
|
134
|
+
while (i >= 0 && !isDigit(value.charCodeAt(i))) i--;
|
|
135
|
+
while (i >= 0 && isDigit(value.charCodeAt(i))) i--;
|
|
136
|
+
} else {
|
|
137
|
+
while (i >= 0 && isDigit(value.charCodeAt(i))) i--;
|
|
138
|
+
}
|
|
139
|
+
return i + 1;
|
|
140
|
+
}
|
|
141
|
+
function findNextWordBoundary(value, from) {
|
|
142
|
+
const length = value.length;
|
|
143
|
+
let i = Math.max(0, from);
|
|
144
|
+
if (i >= length) return length;
|
|
145
|
+
if (!isDigit(value.charCodeAt(i))) {
|
|
146
|
+
while (i < length && !isDigit(value.charCodeAt(i))) i++;
|
|
147
|
+
while (i < length && isDigit(value.charCodeAt(i))) i++;
|
|
148
|
+
} else {
|
|
149
|
+
while (i < length && isDigit(value.charCodeAt(i))) i++;
|
|
150
|
+
}
|
|
151
|
+
return i;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/modules/phone-input/phone-input.ts
|
|
155
|
+
var ATTACHED_INPUTS = /* @__PURE__ */ new WeakSet();
|
|
156
|
+
function assertInputIsAvailable(input) {
|
|
157
|
+
if (!ATTACHED_INPUTS.has(input)) return;
|
|
158
|
+
throw new Error("@telixon/web-sdk cannot attach multiple phone inputs to the same DOM input element.");
|
|
159
|
+
}
|
|
160
|
+
function createPhoneInput(options) {
|
|
161
|
+
const { input } = options;
|
|
162
|
+
assertSupportedInputType(input);
|
|
163
|
+
assertInputIsAvailable(input);
|
|
164
|
+
const inputController = buildController(options);
|
|
165
|
+
ATTACHED_INPUTS.add(input);
|
|
166
|
+
const placeholderConfig = buildPlaceholderConfig(options);
|
|
167
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
168
|
+
let isDestroyed = false;
|
|
169
|
+
let currentRegionFilter = options.regionFilter ?? null;
|
|
170
|
+
let currentNumberTypeFilter = options.numberTypeFilter ?? null;
|
|
171
|
+
const placeholderFallbackRegion = options.defaultRegion ?? null;
|
|
172
|
+
let cachedPlaceholderRegion = inputController.currentState.region ?? placeholderFallbackRegion;
|
|
173
|
+
let cachedPlaceholder = resolvePlaceholder(cachedPlaceholderRegion, placeholderConfig);
|
|
174
|
+
if (currentRegionFilter !== null) inputController.setRegionFilter(currentRegionFilter);
|
|
175
|
+
if (currentNumberTypeFilter !== null) inputController.setNumberTypeFilter(currentNumberTypeFilter);
|
|
176
|
+
function currentPlaceholder(region) {
|
|
177
|
+
const effectiveRegion = region ?? placeholderFallbackRegion;
|
|
178
|
+
if (effectiveRegion === cachedPlaceholderRegion) return cachedPlaceholder;
|
|
179
|
+
cachedPlaceholderRegion = effectiveRegion;
|
|
180
|
+
cachedPlaceholder = resolvePlaceholder(effectiveRegion, placeholderConfig);
|
|
181
|
+
return cachedPlaceholder;
|
|
182
|
+
}
|
|
183
|
+
function buildState() {
|
|
184
|
+
const inputState = inputController.currentState;
|
|
185
|
+
return deriveState(
|
|
186
|
+
inputState,
|
|
187
|
+
currentRegionFilter,
|
|
188
|
+
currentNumberTypeFilter,
|
|
189
|
+
currentPlaceholder(inputState.region),
|
|
190
|
+
inputController.getPhoneNumber().getValidationError()
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
function emit(state) {
|
|
194
|
+
for (const listener of listeners) listener(state);
|
|
195
|
+
}
|
|
196
|
+
function notify(state) {
|
|
197
|
+
if (isDestroyed) return;
|
|
198
|
+
applyInputState(input, state);
|
|
199
|
+
emit(state);
|
|
200
|
+
}
|
|
201
|
+
function commit(change) {
|
|
202
|
+
change();
|
|
203
|
+
notify(buildState());
|
|
204
|
+
}
|
|
205
|
+
function handleCompositionEnd(event) {
|
|
206
|
+
if (event.target !== input) return;
|
|
207
|
+
if (!event.data) return;
|
|
208
|
+
const start = input.selectionStart ?? 0;
|
|
209
|
+
const end = input.selectionEnd ?? 0;
|
|
210
|
+
commit(() => inputController.insert(input.value, event.data, start, end));
|
|
211
|
+
}
|
|
212
|
+
function handleBeforeInput(event) {
|
|
213
|
+
if (event.target !== input) return;
|
|
214
|
+
if (event.isComposing) return;
|
|
215
|
+
const { inputType } = event;
|
|
216
|
+
const value = input.value;
|
|
217
|
+
const selectionStart = input.selectionStart ?? 0;
|
|
218
|
+
const selectionEnd = input.selectionEnd ?? 0;
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
if (isInsertInputType(inputType)) {
|
|
221
|
+
const insertText = resolveInsertText(event);
|
|
222
|
+
if (insertText === "") return;
|
|
223
|
+
commit(() => {
|
|
224
|
+
inputController.insert(value, insertText, selectionStart, selectionEnd);
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (isBackwardDeleteInputType(inputType)) {
|
|
229
|
+
commit(() => {
|
|
230
|
+
inputController.deleteBackward(value, selectionStart, selectionEnd);
|
|
231
|
+
});
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (isForwardDeleteInputType(inputType)) {
|
|
235
|
+
commit(() => {
|
|
236
|
+
inputController.deleteForward(value, selectionStart, selectionEnd);
|
|
237
|
+
});
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (isWordBackwardDeleteInputType(inputType)) {
|
|
241
|
+
const wordStart = selectionStart === selectionEnd ? findPreviousWordBoundary(value, selectionStart) : selectionStart;
|
|
242
|
+
commit(() => {
|
|
243
|
+
inputController.deleteBackward(value, wordStart, selectionEnd);
|
|
244
|
+
});
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (isWordForwardDeleteInputType(inputType)) {
|
|
248
|
+
const wordEnd = selectionStart === selectionEnd ? findNextWordBoundary(value, selectionEnd) : selectionEnd;
|
|
249
|
+
commit(() => {
|
|
250
|
+
inputController.deleteForward(value, selectionStart, wordEnd);
|
|
251
|
+
});
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
switch (inputType) {
|
|
255
|
+
case "deleteSoftLineBackward":
|
|
256
|
+
case "deleteHardLineBackward": {
|
|
257
|
+
commit(() => {
|
|
258
|
+
inputController.deleteBackward(value, 0, selectionEnd);
|
|
259
|
+
});
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
case "deleteSoftLineForward":
|
|
263
|
+
case "deleteHardLineForward": {
|
|
264
|
+
commit(() => {
|
|
265
|
+
inputController.deleteForward(value, selectionEnd, value.length);
|
|
266
|
+
});
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
case "deleteEntireSoftLine":
|
|
270
|
+
case "deleteEntireHardLine": {
|
|
271
|
+
commit(() => {
|
|
272
|
+
inputController.deleteBackward(value, 0, value.length);
|
|
273
|
+
});
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
case "historyUndo":
|
|
277
|
+
if (inputController.canUndo) commit(() => inputController.undo());
|
|
278
|
+
return;
|
|
279
|
+
case "historyRedo":
|
|
280
|
+
if (inputController.canRedo) commit(() => inputController.redo());
|
|
281
|
+
return;
|
|
282
|
+
default:
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function handleKeyDown(event) {
|
|
287
|
+
if (event.target !== input) return;
|
|
288
|
+
if (!event.ctrlKey && !event.metaKey) return;
|
|
289
|
+
const key = event.key.toLowerCase();
|
|
290
|
+
const isUndo = key === "z" && !event.shiftKey;
|
|
291
|
+
const isRedo = key === "z" && event.shiftKey || key === "y";
|
|
292
|
+
if (!isUndo && !isRedo) return;
|
|
293
|
+
event.preventDefault();
|
|
294
|
+
if (isUndo && inputController.canUndo) commit(() => inputController.undo());
|
|
295
|
+
else if (isRedo && inputController.canRedo) commit(() => inputController.redo());
|
|
296
|
+
}
|
|
297
|
+
input.addEventListener("compositionend", handleCompositionEnd);
|
|
298
|
+
input.addEventListener("beforeinput", handleBeforeInput);
|
|
299
|
+
input.addEventListener("keydown", handleKeyDown);
|
|
300
|
+
notify(buildState());
|
|
301
|
+
return {
|
|
302
|
+
getState: buildState,
|
|
303
|
+
canUndo() {
|
|
304
|
+
return inputController.canUndo;
|
|
305
|
+
},
|
|
306
|
+
canRedo() {
|
|
307
|
+
return inputController.canRedo;
|
|
308
|
+
},
|
|
309
|
+
subscribe(listener) {
|
|
310
|
+
listeners.add(listener);
|
|
311
|
+
return () => listeners.delete(listener);
|
|
312
|
+
},
|
|
313
|
+
setValue(value) {
|
|
314
|
+
commit(() => inputController.setValue(value));
|
|
315
|
+
},
|
|
316
|
+
setRegion(region) {
|
|
317
|
+
commit(() => inputController.setRegion(region));
|
|
318
|
+
},
|
|
319
|
+
undo() {
|
|
320
|
+
if (inputController.canUndo) commit(() => inputController.undo());
|
|
321
|
+
},
|
|
322
|
+
redo() {
|
|
323
|
+
if (inputController.canRedo) commit(() => inputController.redo());
|
|
324
|
+
},
|
|
325
|
+
clearHistory() {
|
|
326
|
+
inputController.clearHistory();
|
|
327
|
+
notify(buildState());
|
|
328
|
+
},
|
|
329
|
+
getPhoneNumber() {
|
|
330
|
+
return inputController.getPhoneNumber();
|
|
331
|
+
},
|
|
332
|
+
setRegionFilter(regions) {
|
|
333
|
+
if (readonlyArraysEqual(regions, currentRegionFilter)) return;
|
|
334
|
+
currentRegionFilter = regions;
|
|
335
|
+
inputController.setRegionFilter(regions);
|
|
336
|
+
notify(buildState());
|
|
337
|
+
},
|
|
338
|
+
setNumberTypeFilter(numberTypes) {
|
|
339
|
+
if (readonlyArraysEqual(numberTypes, currentNumberTypeFilter)) return;
|
|
340
|
+
currentNumberTypeFilter = numberTypes;
|
|
341
|
+
inputController.setNumberTypeFilter(numberTypes);
|
|
342
|
+
notify(buildState());
|
|
343
|
+
},
|
|
344
|
+
destroy() {
|
|
345
|
+
isDestroyed = true;
|
|
346
|
+
ATTACHED_INPUTS.delete(input);
|
|
347
|
+
input.removeEventListener("compositionend", handleCompositionEnd);
|
|
348
|
+
input.removeEventListener("beforeinput", handleBeforeInput);
|
|
349
|
+
input.removeEventListener("keydown", handleKeyDown);
|
|
350
|
+
listeners.clear();
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function computeBaseOptions(locale, dataFactory) {
|
|
355
|
+
const displayNames = new Intl.DisplayNames([locale], { type: "region" });
|
|
356
|
+
const baseSet = new Array(REGION_CODES.length);
|
|
357
|
+
for (let i = 0; i < REGION_CODES.length; i++) {
|
|
358
|
+
const region = REGION_CODES[i];
|
|
359
|
+
const callingCode = getCallingCodeForRegion(region);
|
|
360
|
+
const displayName = displayNames.of(region) ?? region;
|
|
361
|
+
const data = dataFactory ? dataFactory({ region, callingCode, displayName, locale }) : void 0;
|
|
362
|
+
baseSet[i] = { region, callingCode, displayName, data };
|
|
363
|
+
}
|
|
364
|
+
return baseSet;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/modules/region-list/utils/search.ts
|
|
368
|
+
var DIACRITICS_PATTERN = /[\u0300-\u036f]/g;
|
|
369
|
+
var normalizedCache = /* @__PURE__ */ new Map();
|
|
370
|
+
function normalizeSearchText(input) {
|
|
371
|
+
const cached = normalizedCache.get(input);
|
|
372
|
+
if (cached !== void 0) return cached;
|
|
373
|
+
const normalized = input.normalize("NFD").replace(DIACRITICS_PATTERN, "").toLowerCase().trim();
|
|
374
|
+
normalizedCache.set(input, normalized);
|
|
375
|
+
return normalized;
|
|
376
|
+
}
|
|
377
|
+
function defaultSearch(query, option) {
|
|
378
|
+
const normalizedQuery = normalizeSearchText(query);
|
|
379
|
+
if (normalizeSearchText(option.displayName).includes(normalizedQuery)) return true;
|
|
380
|
+
if (normalizeSearchText(option.region).includes(normalizedQuery)) return true;
|
|
381
|
+
const callingCodeQuery = normalizedQuery.startsWith("+") ? normalizedQuery.slice(1) : normalizedQuery;
|
|
382
|
+
if (option.callingCode.includes(callingCodeQuery)) return true;
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/modules/region-list/utils/sort.ts
|
|
387
|
+
function compareRegion(a, b) {
|
|
388
|
+
return a.region < b.region ? -1 : a.region > b.region ? 1 : 0;
|
|
389
|
+
}
|
|
390
|
+
function compareAlphabetical(locale) {
|
|
391
|
+
return (a, b) => a.displayName.localeCompare(b.displayName, locale) || compareRegion(a, b);
|
|
392
|
+
}
|
|
393
|
+
function compareCallingCode(locale) {
|
|
394
|
+
return (a, b) => {
|
|
395
|
+
const codeDiff = a.callingCode.localeCompare(b.callingCode, void 0, { numeric: true });
|
|
396
|
+
if (codeDiff !== 0) return codeDiff;
|
|
397
|
+
return a.displayName.localeCompare(b.displayName, locale) || compareRegion(a, b);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function resolveRegionListComparator(sort, locale) {
|
|
401
|
+
if (typeof sort === "function") return sort;
|
|
402
|
+
if (sort === "callingCode") return compareCallingCode(locale);
|
|
403
|
+
return compareAlphabetical(locale);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/modules/region-list/region-list.ts
|
|
407
|
+
var DEFAULT_LOCALE = "en";
|
|
408
|
+
function createRegionList(options = {}) {
|
|
409
|
+
if (!isEngineReady()) throw new EngineNotReadyError();
|
|
410
|
+
const dataFactory = options.dataFactory;
|
|
411
|
+
const searchFn = options.searchFn ?? defaultSearch;
|
|
412
|
+
const sortConfig = options.sort;
|
|
413
|
+
const prioritize = options.prioritize ?? [];
|
|
414
|
+
let locale = options.locale ?? DEFAULT_LOCALE;
|
|
415
|
+
let regionFilter = options.regionFilter ?? null;
|
|
416
|
+
let numberTypeFilter = options.numberTypeFilter ?? null;
|
|
417
|
+
let searchQuery = options.searchQuery ?? "";
|
|
418
|
+
let baseSet = computeBaseOptions(locale, dataFactory);
|
|
419
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
420
|
+
let isDestroyed = false;
|
|
421
|
+
let cachedState = null;
|
|
422
|
+
function runPipeline() {
|
|
423
|
+
const hasQuery = searchQuery.trim() !== "";
|
|
424
|
+
const hasRegionFilter = regionFilter !== null;
|
|
425
|
+
const hasNumberTypeFilter = numberTypeFilter !== null;
|
|
426
|
+
let filtered;
|
|
427
|
+
if (!hasRegionFilter && !hasNumberTypeFilter && !hasQuery) {
|
|
428
|
+
filtered = baseSet.slice();
|
|
429
|
+
} else {
|
|
430
|
+
filtered = [];
|
|
431
|
+
for (const option of baseSet) {
|
|
432
|
+
if (hasRegionFilter && !regionFilter.includes(option.region)) continue;
|
|
433
|
+
if (hasNumberTypeFilter && !regionSupportsNumberTypes(option.region, numberTypeFilter)) continue;
|
|
434
|
+
if (hasQuery && !searchFn(searchQuery, option)) continue;
|
|
435
|
+
filtered.push(option);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
filtered.sort(resolveRegionListComparator(sortConfig, locale));
|
|
439
|
+
if (prioritize.length > 0) {
|
|
440
|
+
const prioritizeSet = new Set(prioritize);
|
|
441
|
+
const byRegion = /* @__PURE__ */ new Map();
|
|
442
|
+
for (const option of filtered) byRegion.set(option.region, option);
|
|
443
|
+
const prioritized = [];
|
|
444
|
+
for (const region of prioritizeSet) {
|
|
445
|
+
const option = byRegion.get(region);
|
|
446
|
+
if (option) prioritized.push(option);
|
|
447
|
+
}
|
|
448
|
+
const rest = [];
|
|
449
|
+
for (const option of filtered) {
|
|
450
|
+
if (!prioritizeSet.has(option.region)) rest.push(option);
|
|
451
|
+
}
|
|
452
|
+
filtered = prioritized.concat(rest);
|
|
453
|
+
}
|
|
454
|
+
return filtered;
|
|
455
|
+
}
|
|
456
|
+
function buildState() {
|
|
457
|
+
if (cachedState !== null) return cachedState;
|
|
458
|
+
cachedState = {
|
|
459
|
+
options: runPipeline(),
|
|
460
|
+
regionFilter,
|
|
461
|
+
numberTypeFilter,
|
|
462
|
+
searchQuery,
|
|
463
|
+
locale
|
|
464
|
+
};
|
|
465
|
+
return cachedState;
|
|
466
|
+
}
|
|
467
|
+
function emit() {
|
|
468
|
+
cachedState = null;
|
|
469
|
+
if (isDestroyed) return;
|
|
470
|
+
const state = buildState();
|
|
471
|
+
for (const listener of listeners) listener(state);
|
|
472
|
+
}
|
|
473
|
+
return {
|
|
474
|
+
getState() {
|
|
475
|
+
return buildState();
|
|
476
|
+
},
|
|
477
|
+
subscribe(listener) {
|
|
478
|
+
listeners.add(listener);
|
|
479
|
+
return () => listeners.delete(listener);
|
|
480
|
+
},
|
|
481
|
+
search(query) {
|
|
482
|
+
if (query === searchQuery) return;
|
|
483
|
+
searchQuery = query;
|
|
484
|
+
emit();
|
|
485
|
+
},
|
|
486
|
+
localize(nextLocale) {
|
|
487
|
+
if (nextLocale === locale) return;
|
|
488
|
+
locale = nextLocale;
|
|
489
|
+
baseSet = computeBaseOptions(locale, dataFactory);
|
|
490
|
+
emit();
|
|
491
|
+
},
|
|
492
|
+
refresh() {
|
|
493
|
+
baseSet = computeBaseOptions(locale, dataFactory);
|
|
494
|
+
emit();
|
|
495
|
+
},
|
|
496
|
+
setRegionFilter(value) {
|
|
497
|
+
if (readonlyArraysEqual(value, regionFilter)) return;
|
|
498
|
+
regionFilter = value;
|
|
499
|
+
emit();
|
|
500
|
+
},
|
|
501
|
+
setNumberTypeFilter(value) {
|
|
502
|
+
if (readonlyArraysEqual(value, numberTypeFilter)) return;
|
|
503
|
+
numberTypeFilter = value;
|
|
504
|
+
emit();
|
|
505
|
+
},
|
|
506
|
+
destroy() {
|
|
507
|
+
isDestroyed = true;
|
|
508
|
+
listeners.clear();
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// src/modules/region-list/utils/region-to-flag-emoji.ts
|
|
514
|
+
var REGIONAL_INDICATOR_BASE = 127462;
|
|
515
|
+
var LETTER_A = 65;
|
|
516
|
+
function regionToFlagEmoji(region) {
|
|
517
|
+
return String.fromCodePoint(
|
|
518
|
+
REGIONAL_INDICATOR_BASE + (region.charCodeAt(0) - LETTER_A),
|
|
519
|
+
REGIONAL_INDICATOR_BASE + (region.charCodeAt(1) - LETTER_A)
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export { createPhoneInput, createRegionList, regionToFlagEmoji };
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@telixon/web-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "DOM adapter for @telixon/core: two headless widgets, PhoneInput driving a plain <input> and RegionList feeding region pickers.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"phone-input",
|
|
7
|
+
"phone-number",
|
|
8
|
+
"phone-validation",
|
|
9
|
+
"phone-formatter",
|
|
10
|
+
"intl-phone",
|
|
11
|
+
"headless"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://telixon.dev",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/martsinlabs/telixon/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/martsinlabs/telixon.git",
|
|
20
|
+
"directory": "packages/web-sdk"
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"author": "Myroslav Martsin <myroslav@martsinlabs.com>",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@telixon/core": "^1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"bundleSize": "3.77 kB brotli"
|
|
46
|
+
}
|