@42-central/ui-react 0.27.0 → 0.29.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/dist/{chunk-5H5YOPCF.js → chunk-2RQLPN4U.js} +6 -6
- package/dist/{chunk-5H5YOPCF.js.map → chunk-2RQLPN4U.js.map} +1 -1
- package/dist/{chunk-SOUDKESO.js → chunk-37HYN7PC.js} +3 -3
- package/dist/{chunk-SOUDKESO.js.map → chunk-37HYN7PC.js.map} +1 -1
- package/dist/{chunk-OVT2KZHY.js → chunk-3KARNPW4.js} +3 -3
- package/dist/{chunk-OVT2KZHY.js.map → chunk-3KARNPW4.js.map} +1 -1
- package/dist/{chunk-Y2G47ARQ.js → chunk-5EOMSWNO.js} +5 -5
- package/dist/{chunk-Y2G47ARQ.js.map → chunk-5EOMSWNO.js.map} +1 -1
- package/dist/chunk-6B2GNPJP.js +43 -0
- package/dist/chunk-6B2GNPJP.js.map +1 -0
- package/dist/{chunk-MCRTAUSZ.js → chunk-7FPSQBWB.js} +3 -3
- package/dist/{chunk-MCRTAUSZ.js.map → chunk-7FPSQBWB.js.map} +1 -1
- package/dist/{chunk-6IRMKF5T.js → chunk-B4GNSSHU.js} +4 -4
- package/dist/{chunk-6IRMKF5T.js.map → chunk-B4GNSSHU.js.map} +1 -1
- package/dist/{chunk-BMEJBY6H.js → chunk-B5YHYI7E.js} +22 -27
- package/dist/chunk-B5YHYI7E.js.map +1 -0
- package/dist/{chunk-WERJ44ZN.js → chunk-DTFSCR3U.js} +3 -3
- package/dist/{chunk-WERJ44ZN.js.map → chunk-DTFSCR3U.js.map} +1 -1
- package/dist/{chunk-PFZMB3US.js → chunk-FQJNQW3G.js} +3 -3
- package/dist/{chunk-PFZMB3US.js.map → chunk-FQJNQW3G.js.map} +1 -1
- package/dist/{chunk-X3YIHN2U.js → chunk-FSPS7TVD.js} +5 -5
- package/dist/{chunk-X3YIHN2U.js.map → chunk-FSPS7TVD.js.map} +1 -1
- package/dist/{chunk-7OGZT7U2.js → chunk-G3AD3NJG.js} +3 -3
- package/dist/{chunk-7OGZT7U2.js.map → chunk-G3AD3NJG.js.map} +1 -1
- package/dist/{chunk-MNYY7UU2.js → chunk-GEZYGBXY.js} +6 -6
- package/dist/{chunk-MNYY7UU2.js.map → chunk-GEZYGBXY.js.map} +1 -1
- package/dist/{chunk-ULF5LOBS.js → chunk-HMNXNKDA.js} +3 -3
- package/dist/{chunk-ULF5LOBS.js.map → chunk-HMNXNKDA.js.map} +1 -1
- package/dist/{chunk-NF2PBGD3.js → chunk-IANYMDKH.js} +3 -3
- package/dist/{chunk-NF2PBGD3.js.map → chunk-IANYMDKH.js.map} +1 -1
- package/dist/{chunk-QEMXXQQO.js → chunk-JKFPRQB2.js} +3 -3
- package/dist/{chunk-QEMXXQQO.js.map → chunk-JKFPRQB2.js.map} +1 -1
- package/dist/{chunk-ZVECQBGG.js → chunk-KNGSO7A3.js} +41 -13
- package/dist/chunk-KNGSO7A3.js.map +1 -0
- package/dist/{chunk-L24N5EON.js → chunk-MQHD3EJE.js} +3 -3
- package/dist/{chunk-L24N5EON.js.map → chunk-MQHD3EJE.js.map} +1 -1
- package/dist/{chunk-IUK7OJXZ.js → chunk-OBSZ4NE2.js} +5 -4
- package/dist/chunk-OBSZ4NE2.js.map +1 -0
- package/dist/{chunk-DJ72KP32.js → chunk-OXR7BXZA.js} +3 -3
- package/dist/{chunk-DJ72KP32.js.map → chunk-OXR7BXZA.js.map} +1 -1
- package/dist/{chunk-LIZWCWED.js → chunk-PQZSTI3R.js} +3 -3
- package/dist/{chunk-LIZWCWED.js.map → chunk-PQZSTI3R.js.map} +1 -1
- package/dist/{chunk-LKFHUDEW.js → chunk-QVOBLRCM.js} +3 -3
- package/dist/{chunk-LKFHUDEW.js.map → chunk-QVOBLRCM.js.map} +1 -1
- package/dist/{chunk-E52Z2FKU.js → chunk-S5CQRCTF.js} +5 -5
- package/dist/{chunk-E52Z2FKU.js.map → chunk-S5CQRCTF.js.map} +1 -1
- package/dist/{chunk-SCXXJDKQ.js → chunk-SP6LCCTZ.js} +21 -28
- package/dist/chunk-SP6LCCTZ.js.map +1 -0
- package/dist/{chunk-BGDVUC3B.js → chunk-TUP7WXQE.js} +3 -3
- package/dist/{chunk-BGDVUC3B.js.map → chunk-TUP7WXQE.js.map} +1 -1
- package/dist/{chunk-4D2V4JE4.js → chunk-V2QZG4IT.js} +4 -4
- package/dist/{chunk-4D2V4JE4.js.map → chunk-V2QZG4IT.js.map} +1 -1
- package/dist/{chunk-D2QIPQ74.js → chunk-W3XX7BYT.js} +2 -2
- package/dist/chunk-W3XX7BYT.js.map +1 -0
- package/dist/{chunk-F374BNL4.js → chunk-YVTM2KRR.js} +12 -8
- package/dist/chunk-YVTM2KRR.js.map +1 -0
- package/dist/colors.css +328 -0
- package/dist/components/action-icon/action-icon.d.ts +6 -5
- package/dist/components/action-icon/action-icon.d.ts.map +1 -1
- package/dist/components/action-icon/index.js +3 -2
- package/dist/components/alert/index.js +3 -3
- package/dist/components/ambient-background/index.js +1 -1
- package/dist/components/app-shell/index.js +6 -5
- package/dist/components/app-shell/index.js.map +1 -1
- package/dist/components/avatar/index.js +2 -2
- package/dist/components/badge/badge.d.ts +1 -1
- package/dist/components/badge/badge.d.ts.map +1 -1
- package/dist/components/badge/index.js +2 -1
- package/dist/components/button/button.d.ts +11 -9
- package/dist/components/button/button.d.ts.map +1 -1
- package/dist/components/button/index.js +3 -2
- package/dist/components/button-multi-select/index.js +4 -3
- package/dist/components/button-multi-select/index.js.map +1 -1
- package/dist/components/button-select/index.js +4 -3
- package/dist/components/button-select/index.js.map +1 -1
- package/dist/components/calendar/index.js +6 -5
- package/dist/components/calendar-heatmap/index.js +2 -2
- package/dist/components/camera/index.js +8 -7
- package/dist/components/carousel/index.js +2 -2
- package/dist/components/clipboard/index.js +3 -2
- package/dist/components/clipboard/index.js.map +1 -1
- package/dist/components/code-block/index.js +1 -1
- package/dist/components/code-block/static.js.map +1 -1
- package/dist/components/compare/index.js +2 -2
- package/dist/components/data-table/index.js +16 -15
- package/dist/components/data-table/index.js.map +1 -1
- package/dist/components/date-picker/index.js +11 -10
- package/dist/components/date-picker/index.js.map +1 -1
- package/dist/components/download-button/index.js +4 -3
- package/dist/components/drawer/index.js +6 -5
- package/dist/components/file-item/index.js +7 -6
- package/dist/components/file-preview/index.js +7 -6
- package/dist/components/file-preview/index.js.map +1 -1
- package/dist/components/file-trigger/index.js +4 -3
- package/dist/components/file-upload/index.js +7 -6
- package/dist/components/file-upload/index.js.map +1 -1
- package/dist/components/gallery/index.js +2 -2
- package/dist/components/image-cropper/index.js +7 -6
- package/dist/components/indicator/index.js +1 -1
- package/dist/components/kbd/index.js +3 -3
- package/dist/components/lightbox/index.js +9 -8
- package/dist/components/lightbox/index.js.map +1 -1
- package/dist/components/menu/index.js +2 -2
- package/dist/components/modal/index.js +6 -5
- package/dist/components/multi-select/index.js +3 -3
- package/dist/components/notification/index.js +3 -3
- package/dist/components/overlays/index.js +8 -7
- package/dist/components/overlays/index.js.map +1 -1
- package/dist/components/password-input/index.js +1 -1
- package/dist/components/phone-input/flag.d.ts +9 -0
- package/dist/components/phone-input/flag.d.ts.map +1 -0
- package/dist/components/phone-input/index.d.ts +4 -0
- package/dist/components/phone-input/index.d.ts.map +1 -0
- package/dist/components/phone-input/index.js +503 -0
- package/dist/components/phone-input/index.js.map +1 -0
- package/dist/components/phone-input/phone-input.d.ts +125 -0
- package/dist/components/phone-input/phone-input.d.ts.map +1 -0
- package/dist/components/phone-input/phone.d.ts +117 -0
- package/dist/components/phone-input/phone.d.ts.map +1 -0
- package/dist/components/photo-capture/index.js +11 -10
- package/dist/components/photo-capture/index.js.map +1 -1
- package/dist/components/pills-input/index.js +3 -3
- package/dist/components/schedule/index.js +7 -6
- package/dist/components/schedule/index.js.map +1 -1
- package/dist/components/segment-group/index.js +3 -3
- package/dist/components/spinner/index.js +1 -1
- package/dist/components/spinner/spinner.d.ts +1 -1
- package/dist/components/spinner/spinner.d.ts.map +1 -1
- package/dist/components/spotlight/index.js +8 -7
- package/dist/components/spotlight/index.js.map +1 -1
- package/dist/components/tabs/index.js +3 -3
- package/dist/components/tags-input/index.js +3 -3
- package/dist/components/theme-icon/index.js +1 -1
- package/dist/components/theme-icon/theme-icon.d.ts.map +1 -1
- package/dist/components/time-input/index.js +4 -4
- package/dist/components/timeline/index.d.ts +1 -1
- package/dist/components/timeline/index.d.ts.map +1 -1
- package/dist/components/timeline/index.js +40 -11
- package/dist/components/timeline/index.js.map +1 -1
- package/dist/components/timeline/timeline.d.ts +8 -3
- package/dist/components/timeline/timeline.d.ts.map +1 -1
- package/dist/components/tree-multi-select/index.js +3 -3
- package/dist/index.js +4 -4
- package/dist/lib/button-colors.d.ts +25 -0
- package/dist/lib/button-colors.d.ts.map +1 -0
- package/dist/lib/dev-warn.d.ts +1 -0
- package/dist/lib/dev-warn.d.ts.map +1 -1
- package/dist/lib/intl.d.ts +5 -0
- package/dist/lib/intl.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/as-child.contract.test.tsx +2 -0
- package/src/colors.css +328 -0
- package/src/components/action-icon/action-icon.test.tsx +22 -0
- package/src/components/action-icon/action-icon.tsx +24 -28
- package/src/components/badge/badge.test.tsx +53 -10
- package/src/components/badge/badge.tsx +64 -11
- package/src/components/button/button.test.tsx +41 -5
- package/src/components/button/button.tsx +28 -33
- package/src/components/phone-input/flag.tsx +86 -0
- package/src/components/phone-input/index.ts +14 -0
- package/src/components/phone-input/phone-input.test.tsx +285 -0
- package/src/components/phone-input/phone-input.tsx +527 -0
- package/src/components/phone-input/phone.test.ts +187 -0
- package/src/components/phone-input/phone.ts +298 -0
- package/src/components/spinner/spinner.tsx +3 -3
- package/src/components/theme-icon/theme-icon.test.tsx +7 -0
- package/src/components/theme-icon/theme-icon.tsx +10 -6
- package/src/components/timeline/index.ts +1 -0
- package/src/components/timeline/timeline.test.tsx +105 -4
- package/src/components/timeline/timeline.tsx +46 -11
- package/src/lib/button-colors.ts +55 -0
- package/src/lib/dev-warn.ts +1 -1
- package/src/lib/intl-direction.test.tsx +23 -0
- package/src/lib/intl.tsx +8 -2
- package/dist/chunk-BMEJBY6H.js.map +0 -1
- package/dist/chunk-D2QIPQ74.js.map +0 -1
- package/dist/chunk-F374BNL4.js.map +0 -1
- package/dist/chunk-IUK7OJXZ.js.map +0 -1
- package/dist/chunk-SCXXJDKQ.js.map +0 -1
- package/dist/chunk-ZVECQBGG.js.map +0 -1
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import {
|
|
3
|
+
caretAfter,
|
|
4
|
+
countryOfLocale,
|
|
5
|
+
deleteAcrossSeparator,
|
|
6
|
+
formatPhone,
|
|
7
|
+
parsePhone,
|
|
8
|
+
releaseCountry,
|
|
9
|
+
significantBefore,
|
|
10
|
+
switchCountry,
|
|
11
|
+
} from "./phone"
|
|
12
|
+
|
|
13
|
+
describe("formatPhone", () => {
|
|
14
|
+
it.each([
|
|
15
|
+
["FR", "0612345678", "06 12 34 56 78", "+33612345678"],
|
|
16
|
+
["US", "2015550123", "(201) 555-0123", "+12015550123"],
|
|
17
|
+
["GB", "02079460958", "020 7946 0958", "+442079460958"],
|
|
18
|
+
] as const)("formats a %s national number and reports E.164", (country, raw, text, value) => {
|
|
19
|
+
expect(formatPhone(raw, country)).toEqual({ text, country, value, isValid: true, consumed: 0 })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("folds pasted separators into the country's own grouping", () => {
|
|
23
|
+
expect(formatPhone("06.12-34 (56) 78", "FR").text).toBe("06 12 34 56 78")
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("reports a partial number as invalid, with its partial E.164", () => {
|
|
27
|
+
expect(formatPhone("0612", "FR")).toMatchObject({ value: "+33612", isValid: false })
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("is empty and null with nothing typed", () => {
|
|
31
|
+
expect(formatPhone(" -", "FR")).toEqual({
|
|
32
|
+
text: "",
|
|
33
|
+
country: "FR",
|
|
34
|
+
value: null,
|
|
35
|
+
isValid: false,
|
|
36
|
+
consumed: 0,
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("collapses a pasted international number's code into its country", () => {
|
|
41
|
+
expect(formatPhone("+44 20 7946 0958", "FR")).toEqual({
|
|
42
|
+
text: "20 7946 0958",
|
|
43
|
+
country: "GB",
|
|
44
|
+
value: "+442079460958",
|
|
45
|
+
isValid: true,
|
|
46
|
+
consumed: 3,
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("collapses the code the moment it is complete, typed with the caret at its end", () => {
|
|
51
|
+
expect(formatPhone("+", null, 1)).toMatchObject({ text: "+", country: null, consumed: 0 })
|
|
52
|
+
expect(formatPhone("+3", null, 2)).toMatchObject({ text: "+3", country: null, consumed: 0 })
|
|
53
|
+
expect(formatPhone("+33", null, 3)).toMatchObject({ text: "", country: "FR", consumed: 3 })
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("resolves a shared code to its main country, or the current one when it shares it", () => {
|
|
57
|
+
expect(formatPhone("+44", null, 3).country).toBe("GB")
|
|
58
|
+
expect(formatPhone("+1", "FR", 2).country).toBe("US")
|
|
59
|
+
expect(formatPhone("+1", "CA", 2).country).toBe("CA")
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("refines a resolved shared code from the national digits", () => {
|
|
63
|
+
expect(formatPhone("+44 7797 123456", null).country).toBe("JE")
|
|
64
|
+
expect(formatPhone("2045550123", "US", undefined, true).country).toBe("CA")
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it("leaves a chosen country alone", () => {
|
|
68
|
+
expect(formatPhone("2045550123", "US")).toMatchObject({
|
|
69
|
+
country: "US",
|
|
70
|
+
value: "+12045550123",
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it("keeps a code being edited as text instead of collapsing it", () => {
|
|
75
|
+
// `+33 6 12` with its second 3 deleted: `+36` is Hungary's code, but the caret is inside it.
|
|
76
|
+
expect(formatPhone("+3612", null, 2)).toMatchObject({
|
|
77
|
+
text: "+36 1 2",
|
|
78
|
+
country: "HU",
|
|
79
|
+
consumed: 0,
|
|
80
|
+
})
|
|
81
|
+
// Finishing the code (typing the 3 back) collapses it again.
|
|
82
|
+
expect(formatPhone("+33612", null, 3)).toMatchObject({ text: "6 12", country: "FR" })
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("has no number without a country unless it is international", () => {
|
|
86
|
+
expect(formatPhone("0612", null).value).toBeNull()
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
describe("national grouping", () => {
|
|
91
|
+
it("groups a collapsed number with no trunk prefix", () => {
|
|
92
|
+
expect(formatPhone("612345678", "FR").text).toBe("6 12 34 56 78")
|
|
93
|
+
expect(formatPhone("2079460958", "GB").text).toBe("20 7946 0958")
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it("keeps the national grouping where it applies", () => {
|
|
97
|
+
expect(formatPhone("0612345678", "FR").text).toBe("06 12 34 56 78")
|
|
98
|
+
expect(formatPhone("2015550123", "US").text).toBe("(201) 555-0123")
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
describe("parsePhone", () => {
|
|
103
|
+
it("shows a complete E.164 value in its national format", () => {
|
|
104
|
+
expect(parsePhone("+33612345678", null)).toEqual({
|
|
105
|
+
text: "06 12 34 56 78",
|
|
106
|
+
country: "FR",
|
|
107
|
+
value: "+33612345678",
|
|
108
|
+
isValid: true,
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it("keeps the fallback country for an empty value", () => {
|
|
113
|
+
expect(parsePhone(null, "BE")).toEqual({ text: "", country: "BE", value: null, isValid: false })
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("collapses the code of a value too short to be national", () => {
|
|
117
|
+
expect(parsePhone("+336", null)).toEqual({
|
|
118
|
+
text: "6",
|
|
119
|
+
country: "FR",
|
|
120
|
+
value: "+336",
|
|
121
|
+
isValid: false,
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
describe("switchCountry", () => {
|
|
127
|
+
it("drops the calling code of an international number", () => {
|
|
128
|
+
expect(switchCountry("+44 20 7946 0958", "FR").text).not.toMatch(/^\+/)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("re-formats a national number's digits under the new country", () => {
|
|
132
|
+
expect(switchCountry("2015550123", "US").text).toBe("(201) 555-0123")
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
describe("releaseCountry", () => {
|
|
137
|
+
it("is the inverse of typing the code's last digit when there is no number yet", () => {
|
|
138
|
+
expect(releaseCountry("", "FR")).toEqual({
|
|
139
|
+
next: { text: "+3", country: null, value: null, isValid: false },
|
|
140
|
+
caret: 2,
|
|
141
|
+
})
|
|
142
|
+
expect(releaseCountry("", "US").next.text).toBe("+")
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it("brings the code back whole, without deleting, when a number follows", () => {
|
|
146
|
+
expect(releaseCountry("6 12", "FR")).toEqual({
|
|
147
|
+
next: { text: "+33 6 12", country: null, value: "+33612", isValid: false },
|
|
148
|
+
caret: 3,
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe("caret helpers", () => {
|
|
154
|
+
it("counts digits and a leading plus", () => {
|
|
155
|
+
expect(significantBefore("+33 6 12", 5)).toBe(4)
|
|
156
|
+
expect(significantBefore("06 12", 3)).toBe(2)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it("places the caret right after the n-th significant character", () => {
|
|
160
|
+
expect(caretAfter("06 12 34", 3)).toBe(4)
|
|
161
|
+
expect(caretAfter("06 12 34", 0)).toBe(0)
|
|
162
|
+
expect(caretAfter("06 12 34", 99)).toBe(8)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it("backspace over a separator removes the digit before it", () => {
|
|
166
|
+
expect(deleteAcrossSeparator("06 12", 3, "backward")).toEqual({ raw: "0 12", count: 1 })
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it("delete over a separator removes the digit after it", () => {
|
|
170
|
+
expect(deleteAcrossSeparator("06 12", 2, "forward")).toEqual({ raw: "06 2", count: 2 })
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it("leaves a plain digit deletion to the browser", () => {
|
|
174
|
+
expect(deleteAcrossSeparator("06 12", 2, "backward")).toBeNull()
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
describe("countryOfLocale", () => {
|
|
179
|
+
it("reads the region, filling in a bare language", () => {
|
|
180
|
+
expect(countryOfLocale("en-GB")).toBe("GB")
|
|
181
|
+
expect(countryOfLocale("fr")).toBe("FR")
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it("is null for a region libphonenumber doesn't know", () => {
|
|
185
|
+
expect(countryOfLocale("en-001")).toBeNull()
|
|
186
|
+
})
|
|
187
|
+
})
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AsYouType,
|
|
3
|
+
type CountryCode,
|
|
4
|
+
getCountries,
|
|
5
|
+
getCountryCallingCode,
|
|
6
|
+
getExampleNumber,
|
|
7
|
+
parsePhoneNumberFromString,
|
|
8
|
+
} from "libphonenumber-js/min"
|
|
9
|
+
import metadata from "libphonenumber-js/min/metadata"
|
|
10
|
+
import examples from "libphonenumber-js/mobile/examples"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* PhoneInput's engine, with nothing visual in it: libphonenumber-js formats
|
|
14
|
+
* as you type and produces the E.164 value, and the caret helpers below keep
|
|
15
|
+
* the cursor where the user left it while the formatting moves characters
|
|
16
|
+
* around it.
|
|
17
|
+
*
|
|
18
|
+
* The dial code behaves like a token in front of the number. Typing a complete
|
|
19
|
+
* code (`+33`) moves it out of the text and into the country trigger. Backspace
|
|
20
|
+
* at the very start of the text pulls it back out as editable characters
|
|
21
|
+
* (`releaseCountry`). Whenever a country is selected, the text is national.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type { CountryCode }
|
|
25
|
+
|
|
26
|
+
/** Every country libphonenumber-js has metadata for. */
|
|
27
|
+
export const PHONE_COUNTRIES: readonly CountryCode[] = getCountries()
|
|
28
|
+
|
|
29
|
+
/** `"33"` for `"FR"`. */
|
|
30
|
+
export const callingCodeOf = (country: CountryCode): string => getCountryCallingCode(country)
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A real-looking mobile number for `country`, as its people write it (`06 12 34 56 78`,
|
|
34
|
+
* `07400 123456`, `(201) 555-0123`), for the placeholder. libphonenumber's example numbers
|
|
35
|
+
* weigh ~1.4 KB gzipped for every country.
|
|
36
|
+
*/
|
|
37
|
+
export const exampleOf = (country: CountryCode): string | undefined =>
|
|
38
|
+
getExampleNumber(country, examples)?.formatNational()
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The main country of a calling code shared by several (`44` is GB, GG, IM and JE; `1` is the
|
|
42
|
+
* US, Canada and the Caribbean). libphonenumber's metadata lists the main one first. It's the
|
|
43
|
+
* same metadata module the `min` build already bundles, so this costs nothing extra.
|
|
44
|
+
*/
|
|
45
|
+
const mainCountryOf = (callingCode: string): CountryCode | null =>
|
|
46
|
+
(metadata.country_calling_codes[callingCode]?.[0] as CountryCode | undefined) ?? null
|
|
47
|
+
|
|
48
|
+
/** What the field holds after a keystroke. */
|
|
49
|
+
export type PhoneState = {
|
|
50
|
+
/** The formatted text shown in the `<input>`. National whenever a country is selected. */
|
|
51
|
+
text: string
|
|
52
|
+
/** The selected country — resolved from a typed `+…` code, else the one the user picked. */
|
|
53
|
+
country: CountryCode | null
|
|
54
|
+
/** E.164 (`"+33612345678"`), or `null` while there's no number to speak of. */
|
|
55
|
+
value: string | null
|
|
56
|
+
/** Whether the number is a complete, valid number for its country. */
|
|
57
|
+
isValid: boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A keystroke's result, plus how many caret-relevant characters (the `+` and the code's
|
|
61
|
+
* digits) moved out of the text and into the trigger, so the caret can follow. */
|
|
62
|
+
export type FormattedPhone = PhoneState & { consumed: number }
|
|
63
|
+
|
|
64
|
+
const startsInternational = (text: string) => /^\s*\+/.test(text)
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Which country national `digits` belong to, among those sharing `country`'s calling code:
|
|
68
|
+
* `204 555 0123` under the US is Canadian. Stays on `country` while the digits can't tell yet.
|
|
69
|
+
*/
|
|
70
|
+
const refineCountry = (digits: string, country: CountryCode): CountryCode => {
|
|
71
|
+
const found = digits ? parsePhoneNumberFromString(digits, country)?.country : undefined
|
|
72
|
+
return found && callingCodeOf(found) === callingCodeOf(country) ? found : country
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Group national `digits` for display. libphonenumber's national grouping only kicks in when
|
|
77
|
+
* the digits start with the country's trunk prefix (`06 12 …` in France, `020 …` in the UK). A
|
|
78
|
+
* number whose code just collapsed into the trigger has no trunk prefix (`6 12 …`), and would
|
|
79
|
+
* come out as one unbroken run. Then the international grouping is used, minus its `+code`.
|
|
80
|
+
* NANP numbers, which have no trunk prefix in front, keep their national form,
|
|
81
|
+
* `(201) 555-0123`.
|
|
82
|
+
*/
|
|
83
|
+
const groupNational = (digits: string, country: CountryCode, national: string): string => {
|
|
84
|
+
if (/\D/.test(national)) return national
|
|
85
|
+
const code = callingCodeOf(country)
|
|
86
|
+
const international = new AsYouType().input(`+${code}${digits}`)
|
|
87
|
+
return international.startsWith(`+${code}`)
|
|
88
|
+
? international.slice(code.length + 1).trim()
|
|
89
|
+
: national
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** National `digits` under `country` (or bare digits with none, which make no number).
|
|
93
|
+
* `refine` lets the digits move the country within its calling code (see `formatPhone`). */
|
|
94
|
+
const formatNational = (
|
|
95
|
+
digits: string,
|
|
96
|
+
country: CountryCode | null,
|
|
97
|
+
refine: boolean,
|
|
98
|
+
): PhoneState => {
|
|
99
|
+
if (digits === "") return { text: "", country, value: null, isValid: false }
|
|
100
|
+
const resolved = country && (refine ? refineCountry(digits, country) : country)
|
|
101
|
+
const typer = new AsYouType(resolved ?? undefined)
|
|
102
|
+
const national = typer.input(digits)
|
|
103
|
+
const text = resolved ? groupNational(digits, resolved, national) : national
|
|
104
|
+
return {
|
|
105
|
+
text,
|
|
106
|
+
country: resolved,
|
|
107
|
+
value: resolved ? (typer.getNumber()?.number ?? null) : null,
|
|
108
|
+
isValid: typer.isValid(),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Format `raw` as typed under `country`. Only digits and a leading `+` survive,
|
|
114
|
+
* so pasted spaces, dashes, dots and parentheses all fold into the country's
|
|
115
|
+
* own grouping.
|
|
116
|
+
*
|
|
117
|
+
* A national number keeps the selected country. With `refine`, the digits move it
|
|
118
|
+
* within its calling code (`204 …` under the US becomes Canada): that's for a
|
|
119
|
+
* country resolved from a typed code, which the user never chose. A country they
|
|
120
|
+
* picked stays put; the E.164 value is the same either way. An international one
|
|
121
|
+
* collapses its code
|
|
122
|
+
* into the country as soon as the code is complete. Calling codes are
|
|
123
|
+
* prefix-free, so `+33` can't be the start of a longer one. A shared code
|
|
124
|
+
* resolves to the current country if it shares it, else to the code's main
|
|
125
|
+
* country, and the national digits refine it.
|
|
126
|
+
*
|
|
127
|
+
* `caret` is the number of caret-relevant characters before the caret. The code
|
|
128
|
+
* only collapses when the caret is at or past its end. With the caret inside the
|
|
129
|
+
* code, the user is editing it: `+3|6 12` after deleting a `3` is a complete
|
|
130
|
+
* Hungarian code, and collapsing it would jump to a country they never typed.
|
|
131
|
+
* It stays international text until they finish the code. Omit `caret` (a
|
|
132
|
+
* paste, a value) to collapse unconditionally.
|
|
133
|
+
*/
|
|
134
|
+
export const formatPhone = (
|
|
135
|
+
raw: string,
|
|
136
|
+
country: CountryCode | null,
|
|
137
|
+
caret?: number,
|
|
138
|
+
refine = false,
|
|
139
|
+
): FormattedPhone => {
|
|
140
|
+
const digits = raw.replace(/\D/g, "")
|
|
141
|
+
if (!startsInternational(raw)) return { ...formatNational(digits, country, refine), consumed: 0 }
|
|
142
|
+
|
|
143
|
+
const typer = new AsYouType()
|
|
144
|
+
const text = typer.input(`+${digits}`)
|
|
145
|
+
const callingCode = typer.getCallingCode()
|
|
146
|
+
const shared =
|
|
147
|
+
callingCode && country && callingCodeOf(country) === callingCode
|
|
148
|
+
? country
|
|
149
|
+
: callingCode
|
|
150
|
+
? mainCountryOf(callingCode)
|
|
151
|
+
: null
|
|
152
|
+
const codeEnd = 1 + (callingCode?.length ?? 0)
|
|
153
|
+
if (callingCode && shared && (caret === undefined || caret >= codeEnd)) {
|
|
154
|
+
return {
|
|
155
|
+
...formatNational(digits.slice(callingCode.length), shared, true),
|
|
156
|
+
consumed: codeEnd,
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Still typing (or editing) the code: international text, with whatever country the digits
|
|
160
|
+
// so far point to, for the flag.
|
|
161
|
+
return {
|
|
162
|
+
text,
|
|
163
|
+
country: typer.getCountry() ?? shared,
|
|
164
|
+
value: typer.getNumber()?.number ?? null,
|
|
165
|
+
isValid: typer.isValid(),
|
|
166
|
+
consumed: 0,
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Backspace at the very start of the text, with a country selected: the dial code comes back
|
|
172
|
+
* out of the trigger as editable text, and the country is unselected.
|
|
173
|
+
*
|
|
174
|
+
* - With no number yet, it's the exact inverse of the keystroke that created the token: `+33`
|
|
175
|
+
* collapsed to France, so Backspace leaves `+3`.
|
|
176
|
+
* - With a number after it, the code comes back whole (`+33| 6 12`, caret after the code)
|
|
177
|
+
* without deleting anything. Deleting its last digit would leave `+36 12`, a Hungarian number
|
|
178
|
+
* the user never typed.
|
|
179
|
+
*
|
|
180
|
+
* Returns the new state and the caret position, in caret-relevant characters.
|
|
181
|
+
*/
|
|
182
|
+
export const releaseCountry = (
|
|
183
|
+
text: string,
|
|
184
|
+
country: CountryCode,
|
|
185
|
+
): { next: PhoneState; caret: number } => {
|
|
186
|
+
const code = callingCodeOf(country)
|
|
187
|
+
const digits = text.replace(/\D/g, "")
|
|
188
|
+
if (digits === "") {
|
|
189
|
+
const raw = `+${code.slice(0, -1)}`
|
|
190
|
+
const { consumed: _, ...next } = formatPhone(raw, null, raw.length)
|
|
191
|
+
return { next, caret: raw.length }
|
|
192
|
+
}
|
|
193
|
+
const typer = new AsYouType()
|
|
194
|
+
return {
|
|
195
|
+
next: {
|
|
196
|
+
text: typer.input(`+${code}${digits}`),
|
|
197
|
+
country: null,
|
|
198
|
+
value: typer.getNumber()?.number ?? null,
|
|
199
|
+
isValid: typer.isValid(),
|
|
200
|
+
},
|
|
201
|
+
caret: 1 + code.length,
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The display state for an E.164 `value` handed in from outside (initial or
|
|
207
|
+
* controlled): national formatting once the number is possible for a known
|
|
208
|
+
* country, the collapsed code and national digits otherwise. A `null` value
|
|
209
|
+
* keeps `fallback`.
|
|
210
|
+
*/
|
|
211
|
+
export const parsePhone = (value: string | null, fallback: CountryCode | null): PhoneState => {
|
|
212
|
+
if (!value) return { text: "", country: fallback, value: null, isValid: false }
|
|
213
|
+
const parsed = parsePhoneNumberFromString(value)
|
|
214
|
+
if (parsed?.country && parsed.isPossible()) {
|
|
215
|
+
return {
|
|
216
|
+
text: parsed.formatNational(),
|
|
217
|
+
country: parsed.country,
|
|
218
|
+
value: parsed.number,
|
|
219
|
+
isValid: parsed.isValid(),
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const { consumed: _, ...state } = formatPhone(value, fallback)
|
|
223
|
+
return state
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Re-home the current text under a newly picked country: international text
|
|
228
|
+
* (a code still being typed) drops its calling code and keeps its national
|
|
229
|
+
* digits, a national one keeps its digits as they are.
|
|
230
|
+
*/
|
|
231
|
+
export const switchCountry = (text: string, country: CountryCode | null): PhoneState => {
|
|
232
|
+
if (!startsInternational(text)) return formatNational(text.replace(/\D/g, ""), country, false)
|
|
233
|
+
const typer = new AsYouType()
|
|
234
|
+
typer.input(text)
|
|
235
|
+
const callingCode = typer.getCallingCode() ?? ""
|
|
236
|
+
return formatNational(text.replace(/\D/g, "").slice(callingCode.length), country, false)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** How many caret-relevant characters (digits, and a leading `+`) sit before `end`. */
|
|
240
|
+
export const significantBefore = (text: string, end: number): number => {
|
|
241
|
+
const head = text.slice(0, end)
|
|
242
|
+
return (head.match(/\d/g)?.length ?? 0) + (startsInternational(head) ? 1 : 0)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const isSignificant = (text: string, index: number) =>
|
|
246
|
+
/\d/.test(text.charAt(index)) ||
|
|
247
|
+
(text.charAt(index) === "+" && startsInternational(text.slice(0, index + 1)))
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The caret position in `text` right after its `count`-th significant
|
|
251
|
+
* character — where a caret that had `count` digits before it belongs once
|
|
252
|
+
* the text is reformatted.
|
|
253
|
+
*/
|
|
254
|
+
export const caretAfter = (text: string, count: number): number => {
|
|
255
|
+
if (count <= 0) return 0
|
|
256
|
+
const index = [...text].findIndex((_, i) => significantBefore(text, i + 1) === count)
|
|
257
|
+
return index === -1 ? text.length : index + 1
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Backspace / Delete on a formatting character (a space, a parenthesis, a
|
|
262
|
+
* dash) would be undone by the next format and look like a dead key. Instead
|
|
263
|
+
* it removes the nearest digit in that direction, the way every phone field
|
|
264
|
+
* that formats as you type behaves. Returns `null` when the native edit is
|
|
265
|
+
* fine as it is: a selection, or a digit next to the caret.
|
|
266
|
+
*/
|
|
267
|
+
export const deleteAcrossSeparator = (
|
|
268
|
+
text: string,
|
|
269
|
+
caret: number,
|
|
270
|
+
direction: "backward" | "forward",
|
|
271
|
+
): { raw: string; count: number } | null => {
|
|
272
|
+
const neighbour = direction === "backward" ? caret - 1 : caret
|
|
273
|
+
if (neighbour < 0 || neighbour >= text.length || isSignificant(text, neighbour)) return null
|
|
274
|
+
const indices = Array.from(text, (_, i) => i)
|
|
275
|
+
const target =
|
|
276
|
+
direction === "backward"
|
|
277
|
+
? indices.filter((i) => i < caret && isSignificant(text, i)).at(-1)
|
|
278
|
+
: indices.find((i) => i >= caret && isSignificant(text, i))
|
|
279
|
+
if (target === undefined) return null
|
|
280
|
+
return {
|
|
281
|
+
raw: text.slice(0, target) + text.slice(target + 1),
|
|
282
|
+
count: significantBefore(text, direction === "backward" ? target : caret),
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The country a locale implies (`"fr"` → `FR`, `"en-GB"` → `GB`), when
|
|
288
|
+
* libphonenumber knows it. `Intl.Locale#maximize` fills in the likely region
|
|
289
|
+
* of a bare language tag.
|
|
290
|
+
*/
|
|
291
|
+
export const countryOfLocale = (locale: string): CountryCode | null => {
|
|
292
|
+
try {
|
|
293
|
+
const region = new Intl.Locale(locale).maximize().region
|
|
294
|
+
return PHONE_COUNTRIES.find((country) => country === region) ?? null
|
|
295
|
+
} catch {
|
|
296
|
+
return null
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -24,7 +24,7 @@ import { TEXT_COLOR_CLASSES } from "../../lib/typography-size"
|
|
|
24
24
|
* paints the primitives behind Figma's `Text/text-primary` (`brand-950` / `white`).
|
|
25
25
|
*
|
|
26
26
|
* Sizing follows the same logic: omit `size` to inherit from a parent's
|
|
27
|
-
* `[&_svg]:size-*`
|
|
27
|
+
* svg size rule (Button's `[:where(&)_svg]:size-*`), or set it explicitly for
|
|
28
28
|
* standalone use.
|
|
29
29
|
*
|
|
30
30
|
* Accessibility: a standalone spinner is a `role="status"` live region holding
|
|
@@ -59,7 +59,7 @@ export type SpinnerColor = Color | "currentColor" | "default"
|
|
|
59
59
|
|
|
60
60
|
export type SpinnerProps = Omit<ComponentPropsWithoutRef<"span">, "color" | "children"> &
|
|
61
61
|
WithTestId & {
|
|
62
|
-
/** Explicit size; omit to inherit from a parent's
|
|
62
|
+
/** Explicit size; omit to inherit from a parent's svg size rule. */
|
|
63
63
|
size?: SpinnerSize
|
|
64
64
|
/**
|
|
65
65
|
* Stroke color. Omit (or pass `"currentColor"`) to inherit the surrounding
|
|
@@ -122,7 +122,7 @@ export const Spinner = ({
|
|
|
122
122
|
strokeLinejoin="round"
|
|
123
123
|
aria-hidden
|
|
124
124
|
// Sized: fill the root. Unsized: keep the 24px intrinsic size, or a parent's
|
|
125
|
-
//
|
|
125
|
+
// svg size rule (Button's), which the root then hugs.
|
|
126
126
|
className={cn("block", size && "size-full")}
|
|
127
127
|
>
|
|
128
128
|
{withTrack && <circle cx="12" cy="12" r="10" strokeWidth={1} className="opacity-20" />}
|
|
@@ -17,4 +17,11 @@ describe("ThemeIcon", () => {
|
|
|
17
17
|
const link = screen.getByRole("link", { name: "settings" })
|
|
18
18
|
expect(link.className).toContain("focus-visible:ring-brand-500")
|
|
19
19
|
})
|
|
20
|
+
|
|
21
|
+
it("sizes its icon through a zero-specificity rule, so a class on the icon wins", () => {
|
|
22
|
+
render(<ThemeIcon testId="icon" size="md" />)
|
|
23
|
+
const classes = [...screen.getByTestId("icon").classList]
|
|
24
|
+
expect(classes).toContain("[:where(&)_svg]:size-4")
|
|
25
|
+
expect(classes.some((c) => c.startsWith("[&_svg]"))).toBe(false)
|
|
26
|
+
})
|
|
20
27
|
})
|
|
@@ -26,7 +26,11 @@ const themeIconVariants = cva(
|
|
|
26
26
|
"inline-flex items-center justify-center",
|
|
27
27
|
"aspect-square shrink-0 select-none",
|
|
28
28
|
"rounded-lg border",
|
|
29
|
-
|
|
29
|
+
// Every svg rule is `[:where(&)_svg]`: zero class specificity, so a size
|
|
30
|
+
// class on the icon itself (`<Star className="size-5" />`) or a nested
|
|
31
|
+
// component's own svg rule wins whatever the CSS order. Lucide's `size`
|
|
32
|
+
// prop is only an HTML attribute and still loses. Same fix as Input (#427).
|
|
33
|
+
"[:where(&)_svg]:shrink-0 [:where(&)_svg]:pointer-events-none",
|
|
30
34
|
// Inert on the default non-focusable <div>; applies once `asChild`
|
|
31
35
|
// renders ThemeIcon onto a real <a>/<button>. See `focusStyles`.
|
|
32
36
|
focusStyles(),
|
|
@@ -98,11 +102,11 @@ const themeIconVariants = cva(
|
|
|
98
102
|
// a 28/32/36/40/48px tile — under half the tile, so the tile reads as a
|
|
99
103
|
// frame around the icon, not as the icon's own padding.
|
|
100
104
|
size: {
|
|
101
|
-
xs: "h-7 [&_svg]:size-3",
|
|
102
|
-
sm: "h-8 [&_svg]:size-3.5",
|
|
103
|
-
md: "h-9 [&_svg]:size-4",
|
|
104
|
-
lg: "h-10 [&_svg]:size-4.5",
|
|
105
|
-
xl: "h-12 [&_svg]:size-5.5",
|
|
105
|
+
xs: "h-7 [:where(&)_svg]:size-3",
|
|
106
|
+
sm: "h-8 [:where(&)_svg]:size-3.5",
|
|
107
|
+
md: "h-9 [:where(&)_svg]:size-4",
|
|
108
|
+
lg: "h-10 [:where(&)_svg]:size-4.5",
|
|
109
|
+
xl: "h-12 [:where(&)_svg]:size-5.5",
|
|
106
110
|
},
|
|
107
111
|
radius: {
|
|
108
112
|
xs: "rounded-xs",
|