@krak-stack/registry 0.7.0 → 0.8.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/CHANGELOG.md +6 -0
- package/dist/components/ui/cron-expression-editor.d.ts +14 -0
- package/dist/components/ui/cron-expression-editor.js +206 -0
- package/dist/components/ui/cron-expression-editor.schema.d.ts +7 -0
- package/dist/components/ui/phone-number-input.d.ts +20 -0
- package/dist/components/ui/phone-number-input.js +682 -0
- package/package.json +11 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @krak-stack/registry
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9f63def: Add a restricted typed Cron Expression Editor with automatic spacing and per-field validation, and a numeric Phone Number Input with live formatting, protected calling codes and searchable countries. Include bilingual guides and interactive previews. Keep scheduling, phone verification and persistence application-owned.
|
|
8
|
+
|
|
3
9
|
## 0.7.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { CronDraftSchema, formatCronDraft, } from "./cron-expression-editor.schema.js";
|
|
2
|
+
export declare const getCronPreview: (expression: string, timeZone: string, referenceTime: Date, count?: number) => readonly Date[] | null;
|
|
3
|
+
export type CronExpressionEditorProps = {
|
|
4
|
+
value: string;
|
|
5
|
+
onValueChange: (value: string) => void;
|
|
6
|
+
timeZone: string;
|
|
7
|
+
/** Explicit instant keeps server rendering and DST previews reproducible. */
|
|
8
|
+
referenceTime: Date;
|
|
9
|
+
locale?: string;
|
|
10
|
+
label?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare const CronExpressionEditor: ({ value, onValueChange, timeZone, referenceTime, locale, label, name, disabled, }: CronExpressionEditorProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// ../../src/components/ui/cron-expression-editor.tsx
|
|
2
|
+
import { useId, useLayoutEffect, useRef } from "react";
|
|
3
|
+
import { Effect as Effect2, Option as Option2 } from "effect";
|
|
4
|
+
import { CronExpressionParser as CronExpressionParser2 } from "cron-parser";
|
|
5
|
+
|
|
6
|
+
// ../../src/components/ui/input.tsx
|
|
7
|
+
import { Input as InputPrimitive } from "@base-ui/react/input";
|
|
8
|
+
|
|
9
|
+
// ../../src/lib/utils.ts
|
|
10
|
+
import { cn } from "cn";
|
|
11
|
+
|
|
12
|
+
// ../../src/components/ui/input.tsx
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
function Input({ className, type, ...props }) {
|
|
15
|
+
return /* @__PURE__ */ jsx(InputPrimitive, {
|
|
16
|
+
type,
|
|
17
|
+
"data-slot": "input",
|
|
18
|
+
className: cn("h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40", className),
|
|
19
|
+
...props
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ../../src/components/ui/cron-expression-editor.tsx
|
|
24
|
+
import { useKrakstackLocale } from "@krak-stack/registry/krakstack-provider";
|
|
25
|
+
|
|
26
|
+
// ../../src/components/ui/cron-expression-editor.schema.ts
|
|
27
|
+
import { Effect, Option, Schema } from "effect";
|
|
28
|
+
import { CronExpressionParser } from "cron-parser";
|
|
29
|
+
var acceptsField = (token, index) => {
|
|
30
|
+
const fields = ["*", "*", "*", "*", "*"];
|
|
31
|
+
fields[index] = token;
|
|
32
|
+
return Option.isSome(Effect.runSync(Effect.option(Effect.try(() => CronExpressionParser.parse(fields.join(" "))))));
|
|
33
|
+
};
|
|
34
|
+
var CronDraftSchema = Schema.Struct({
|
|
35
|
+
text: Schema.String.check(Schema.isPattern(/^[\d*/,\- ]*$/), Schema.isMaxLength(150)),
|
|
36
|
+
cursor: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
|
37
|
+
}).check(Schema.makeFilter(({ text, cursor }) => {
|
|
38
|
+
if (!text)
|
|
39
|
+
return true;
|
|
40
|
+
const fields = text.split(" ");
|
|
41
|
+
if (fields.length > 5)
|
|
42
|
+
return false;
|
|
43
|
+
const active = text.slice(0, cursor).split(" ").length - 1;
|
|
44
|
+
return fields.every((token, index) => {
|
|
45
|
+
if (acceptsField(token, index))
|
|
46
|
+
return true;
|
|
47
|
+
if (index !== active || /\/0+$/.test(token))
|
|
48
|
+
return false;
|
|
49
|
+
if (!token)
|
|
50
|
+
return index === fields.length - 1;
|
|
51
|
+
return Array.from({ length: 60 }, (_, number) => String(number)).concat("*").some((suffix) => acceptsField(`${token}${suffix}`, index));
|
|
52
|
+
});
|
|
53
|
+
})).annotate({ identifier: "CronDraft" });
|
|
54
|
+
var formatCronDraft = (text, cursor = text.length) => {
|
|
55
|
+
const normalized = text.replace(/\s+/g, " ").trimStart();
|
|
56
|
+
const normalizedCursor = text.slice(0, cursor).replace(/\s+/g, " ").trimStart().length;
|
|
57
|
+
return Schema.is(CronDraftSchema)({
|
|
58
|
+
text: normalized,
|
|
59
|
+
cursor: normalizedCursor
|
|
60
|
+
}) ? normalized : null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// ../../src/components/ui/cron-expression-editor.tsx
|
|
64
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
65
|
+
var getCronPreview = (expression, timeZone, referenceTime, count = 5) => {
|
|
66
|
+
if (expression.trim().split(/\s+/).length !== 5 || !Number.isFinite(referenceTime.getTime()) || !Number.isInteger(count) || count < 1 || count > 20)
|
|
67
|
+
return null;
|
|
68
|
+
const result = Effect2.runSync(Effect2.option(Effect2.try(() => {
|
|
69
|
+
new Intl.DateTimeFormat("en", { timeZone }).format(referenceTime);
|
|
70
|
+
const interval = CronExpressionParser2.parse(expression, {
|
|
71
|
+
currentDate: referenceTime,
|
|
72
|
+
tz: timeZone
|
|
73
|
+
});
|
|
74
|
+
return Array.from({ length: count }, () => interval.next().toDate());
|
|
75
|
+
})));
|
|
76
|
+
return Option2.getOrNull(result);
|
|
77
|
+
};
|
|
78
|
+
var CronExpressionEditor = ({
|
|
79
|
+
value,
|
|
80
|
+
onValueChange,
|
|
81
|
+
timeZone,
|
|
82
|
+
referenceTime,
|
|
83
|
+
locale,
|
|
84
|
+
label,
|
|
85
|
+
name,
|
|
86
|
+
disabled
|
|
87
|
+
}) => {
|
|
88
|
+
const id = useId();
|
|
89
|
+
const language = useKrakstackLocale(locale);
|
|
90
|
+
const french = language.startsWith("fr");
|
|
91
|
+
const dates = getCronPreview(value, timeZone, referenceTime);
|
|
92
|
+
const selection = useRef(null);
|
|
93
|
+
useLayoutEffect(() => {
|
|
94
|
+
const pending = selection.current;
|
|
95
|
+
if (!pending)
|
|
96
|
+
return;
|
|
97
|
+
selection.current = null;
|
|
98
|
+
pending.element.setSelectionRange(pending.position, pending.position);
|
|
99
|
+
});
|
|
100
|
+
const firstRun = dates?.[0];
|
|
101
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
102
|
+
className: "grid min-w-0 gap-2",
|
|
103
|
+
children: [
|
|
104
|
+
/* @__PURE__ */ jsx2("label", {
|
|
105
|
+
htmlFor: id,
|
|
106
|
+
className: "text-sm font-medium",
|
|
107
|
+
children: label ?? (french ? "Expression cron" : "Cron expression")
|
|
108
|
+
}),
|
|
109
|
+
/* @__PURE__ */ jsx2(Input, {
|
|
110
|
+
id,
|
|
111
|
+
name,
|
|
112
|
+
value,
|
|
113
|
+
disabled,
|
|
114
|
+
autoComplete: "off",
|
|
115
|
+
autoCapitalize: "off",
|
|
116
|
+
spellCheck: false,
|
|
117
|
+
placeholder: "*/15 9 * * 1-5",
|
|
118
|
+
className: "font-mono tracking-wide",
|
|
119
|
+
"aria-invalid": !dates,
|
|
120
|
+
"aria-describedby": `${id}-help`,
|
|
121
|
+
onKeyDown: (event) => {
|
|
122
|
+
if (!event.ctrlKey && !event.metaKey && !event.altKey && event.key.length === 1 && !/[\d*/,\- ]/.test(event.key))
|
|
123
|
+
event.preventDefault();
|
|
124
|
+
},
|
|
125
|
+
onChange: (event) => {
|
|
126
|
+
const raw = event.target.value;
|
|
127
|
+
const cursor = event.target.selectionStart ?? raw.length;
|
|
128
|
+
const next = formatCronDraft(raw, cursor);
|
|
129
|
+
if (next === null)
|
|
130
|
+
return;
|
|
131
|
+
selection.current = {
|
|
132
|
+
element: event.target,
|
|
133
|
+
position: raw.slice(0, cursor).replace(/\s+/g, " ").trimStart().length
|
|
134
|
+
};
|
|
135
|
+
onValueChange(next);
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
/* @__PURE__ */ jsx2("p", {
|
|
139
|
+
id: `${id}-help`,
|
|
140
|
+
className: "text-muted-foreground text-xs",
|
|
141
|
+
children: french ? "Minute · heure · jour du mois · mois · jour de la semaine" : "Minute · hour · day of month · month · day of week"
|
|
142
|
+
}),
|
|
143
|
+
dates && firstRun ? /* @__PURE__ */ jsxs("details", {
|
|
144
|
+
className: "text-muted-foreground min-w-0 text-xs",
|
|
145
|
+
children: [
|
|
146
|
+
/* @__PURE__ */ jsxs("summary", {
|
|
147
|
+
className: "cursor-pointer rounded-sm outline-offset-4",
|
|
148
|
+
children: [
|
|
149
|
+
/* @__PURE__ */ jsx2("span", {
|
|
150
|
+
className: "text-foreground font-medium",
|
|
151
|
+
children: french ? "Prochaines exécutions" : "Next runs"
|
|
152
|
+
}),
|
|
153
|
+
" · ",
|
|
154
|
+
/* @__PURE__ */ jsx2("time", {
|
|
155
|
+
dateTime: firstRun.toISOString(),
|
|
156
|
+
title: timeZone,
|
|
157
|
+
children: new Intl.DateTimeFormat(language, {
|
|
158
|
+
timeZone,
|
|
159
|
+
month: "short",
|
|
160
|
+
day: "numeric",
|
|
161
|
+
hour: "2-digit",
|
|
162
|
+
minute: "2-digit",
|
|
163
|
+
timeZoneName: "short"
|
|
164
|
+
}).format(firstRun)
|
|
165
|
+
}),
|
|
166
|
+
" ",
|
|
167
|
+
/* @__PURE__ */ jsxs("span", {
|
|
168
|
+
children: [
|
|
169
|
+
"+",
|
|
170
|
+
dates.length - 1
|
|
171
|
+
]
|
|
172
|
+
})
|
|
173
|
+
]
|
|
174
|
+
}),
|
|
175
|
+
/* @__PURE__ */ jsx2("p", {
|
|
176
|
+
className: "mt-2 break-all",
|
|
177
|
+
children: timeZone
|
|
178
|
+
}),
|
|
179
|
+
/* @__PURE__ */ jsx2("ol", {
|
|
180
|
+
className: "mt-1 grid gap-1",
|
|
181
|
+
children: dates.map((date) => /* @__PURE__ */ jsx2("li", {
|
|
182
|
+
children: /* @__PURE__ */ jsx2("time", {
|
|
183
|
+
dateTime: date.toISOString(),
|
|
184
|
+
children: new Intl.DateTimeFormat(language, {
|
|
185
|
+
timeZone,
|
|
186
|
+
dateStyle: "medium",
|
|
187
|
+
timeStyle: "long"
|
|
188
|
+
}).format(date)
|
|
189
|
+
})
|
|
190
|
+
}, date.toISOString()))
|
|
191
|
+
})
|
|
192
|
+
]
|
|
193
|
+
}) : /* @__PURE__ */ jsx2("p", {
|
|
194
|
+
role: "status",
|
|
195
|
+
className: "text-destructive text-sm",
|
|
196
|
+
children: french ? "Complétez les cinq champs. Si la règle est complète, vérifiez le fuseau, la date et les prochaines exécutions possibles." : "Complete all five fields. If the rule is complete, check the time zone, reference date and possible next runs."
|
|
197
|
+
})
|
|
198
|
+
]
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
export {
|
|
202
|
+
CronDraftSchema,
|
|
203
|
+
CronExpressionEditor,
|
|
204
|
+
formatCronDraft,
|
|
205
|
+
getCronPreview
|
|
206
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** The active field may be unfinished, but must still be completable. */
|
|
3
|
+
export declare const CronDraftSchema: Schema.Struct<{
|
|
4
|
+
readonly text: Schema.String;
|
|
5
|
+
readonly cursor: Schema.Int;
|
|
6
|
+
}>;
|
|
7
|
+
export declare const formatCronDraft: (text: string, cursor?: number) => string | null;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ComponentProps } from "react";
|
|
2
|
+
import { type CountryCode } from "libphonenumber-js/max";
|
|
3
|
+
import { Input } from "./input.js";
|
|
4
|
+
export type PhoneNumberDetails = {
|
|
5
|
+
e164: string;
|
|
6
|
+
extension: string | null;
|
|
7
|
+
country: CountryCode | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const normalizePhoneNumber: (value: string, country: CountryCode) => PhoneNumberDetails | null;
|
|
10
|
+
export type PhoneNumberInputProps = Omit<ComponentProps<typeof Input>, "value" | "defaultValue" | "onChange" | "type" | "country"> & {
|
|
11
|
+
value: string;
|
|
12
|
+
country: CountryCode;
|
|
13
|
+
onCountryChange: (country: CountryCode) => void;
|
|
14
|
+
onValueChange: (value: string, details: PhoneNumberDetails | null) => void;
|
|
15
|
+
countries?: readonly CountryCode[];
|
|
16
|
+
locale?: string;
|
|
17
|
+
label?: string;
|
|
18
|
+
withExtension?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare const PhoneNumberInput: ({ value, country, onCountryChange, onValueChange, countries, locale, label, id, onBlur, name, withExtension, onKeyDown, ...props }: PhoneNumberInputProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
// ../../src/components/ui/phone-number-input.tsx
|
|
2
|
+
import { useId, useLayoutEffect, useRef as useRef2 } from "react";
|
|
3
|
+
import {
|
|
4
|
+
getCountries,
|
|
5
|
+
AsYouType,
|
|
6
|
+
getCountryCallingCode,
|
|
7
|
+
parsePhoneNumberFromString
|
|
8
|
+
} from "libphonenumber-js/max";
|
|
9
|
+
|
|
10
|
+
// ../../src/components/ui/input.tsx
|
|
11
|
+
import { Input as InputPrimitive } from "@base-ui/react/input";
|
|
12
|
+
|
|
13
|
+
// ../../src/lib/utils.ts
|
|
14
|
+
import { cn } from "cn";
|
|
15
|
+
|
|
16
|
+
// ../../src/components/ui/input.tsx
|
|
17
|
+
import { jsx } from "react/jsx-runtime";
|
|
18
|
+
function Input({ className, type, ...props }) {
|
|
19
|
+
return /* @__PURE__ */ jsx(InputPrimitive, {
|
|
20
|
+
type,
|
|
21
|
+
"data-slot": "input",
|
|
22
|
+
className: cn("h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40", className),
|
|
23
|
+
...props
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ../../src/components/ui/phone-number-input.tsx
|
|
28
|
+
import { useKrakstackLocale as useKrakstackLocale2 } from "@krak-stack/registry/krakstack-provider";
|
|
29
|
+
|
|
30
|
+
// ../../src/components/ui/button.tsx
|
|
31
|
+
import { Button as ButtonPrimitive } from "@base-ui/react/button";
|
|
32
|
+
import { cva } from "class-variance-authority";
|
|
33
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
34
|
+
var buttonVariants = cva("group/button inline-flex shrink-0 items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", {
|
|
35
|
+
variants: {
|
|
36
|
+
variant: {
|
|
37
|
+
default: "bg-primary text-primary-foreground hover:bg-primary/80",
|
|
38
|
+
outline: "border-border bg-background shadow-xs hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50",
|
|
39
|
+
secondary: "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
|
|
40
|
+
ghost: "hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50",
|
|
41
|
+
destructive: "bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40",
|
|
42
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
43
|
+
},
|
|
44
|
+
size: {
|
|
45
|
+
default: "h-9 gap-1.5 px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
46
|
+
xs: "h-6 gap-1 rounded-[min(var(--radius-md),8px)] px-2 text-xs in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
|
|
47
|
+
sm: "h-8 gap-1 rounded-[min(var(--radius-md),10px)] px-2.5 in-data-[slot=button-group]:rounded-md has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",
|
|
48
|
+
lg: "h-10 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
49
|
+
icon: "size-9",
|
|
50
|
+
"icon-xs": "size-6 rounded-[min(var(--radius-md),8px)] in-data-[slot=button-group]:rounded-md [&_svg:not([class*='size-'])]:size-3",
|
|
51
|
+
"icon-sm": "size-8 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-md",
|
|
52
|
+
"icon-lg": "size-10"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
defaultVariants: {
|
|
56
|
+
variant: "default",
|
|
57
|
+
size: "default"
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
function Button({
|
|
61
|
+
className,
|
|
62
|
+
variant = "default",
|
|
63
|
+
size = "default",
|
|
64
|
+
...props
|
|
65
|
+
}) {
|
|
66
|
+
return /* @__PURE__ */ jsx2(ButtonPrimitive, {
|
|
67
|
+
"data-slot": "button",
|
|
68
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
69
|
+
...props
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ../../src/components/ui/virtualized-combobox.tsx
|
|
74
|
+
import { Combobox as ComboboxPrimitive } from "@base-ui/react";
|
|
75
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
76
|
+
import { Check, ChevronsUpDown, X } from "lucide-react";
|
|
77
|
+
import {
|
|
78
|
+
useCallback,
|
|
79
|
+
useEffect,
|
|
80
|
+
useImperativeHandle,
|
|
81
|
+
useRef
|
|
82
|
+
} from "react";
|
|
83
|
+
|
|
84
|
+
// ../../src/components/ui/input-group.tsx
|
|
85
|
+
import { cva as cva2 } from "class-variance-authority";
|
|
86
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
87
|
+
function InputGroup({ className, ...props }) {
|
|
88
|
+
return /* @__PURE__ */ jsx3("div", {
|
|
89
|
+
"data-slot": "input-group",
|
|
90
|
+
role: "group",
|
|
91
|
+
className: cn("group/input-group relative flex h-9 w-full min-w-0 items-center rounded-md border border-input shadow-xs transition-[color,box-shadow] outline-none in-data-[slot=combobox-content]:focus-within:border-inherit in-data-[slot=combobox-content]:focus-within:ring-0 has-[[data-slot=input-group-control]:focus-visible]:border-ring has-[[data-slot=input-group-control]:focus-visible]:ring-3 has-[[data-slot=input-group-control]:focus-visible]:ring-ring/50 has-[[data-slot][aria-invalid=true]]:border-destructive has-[[data-slot][aria-invalid=true]]:ring-3 has-[[data-slot][aria-invalid=true]]:ring-destructive/20 has-[>[data-align=block-end]]:h-auto has-[>[data-align=block-end]]:flex-col has-[>[data-align=block-start]]:h-auto has-[>[data-align=block-start]]:flex-col has-[>textarea]:h-auto dark:bg-input/30 dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40 has-[>[data-align=block-end]]:[&>input]:pt-3 has-[>[data-align=block-start]]:[&>input]:pb-3 has-[>[data-align=inline-end]]:[&>input]:pr-1.5 has-[>[data-align=inline-start]]:[&>input]:pl-1.5", className),
|
|
92
|
+
...props
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
var inputGroupAddonVariants = cva2("flex h-auto cursor-text items-center justify-center gap-2 py-1.5 text-sm font-medium text-muted-foreground select-none group-data-[disabled=true]/input-group:opacity-50 [&>kbd]:rounded-[calc(var(--radius)-5px)] [&>svg:not([class*='size-'])]:size-4", {
|
|
96
|
+
variants: {
|
|
97
|
+
align: {
|
|
98
|
+
"inline-start": "order-first pl-2 has-[>button]:-ml-1 has-[>kbd]:ml-[-0.15rem]",
|
|
99
|
+
"inline-end": "order-last pr-2 has-[>button]:-mr-1 has-[>kbd]:mr-[-0.15rem]",
|
|
100
|
+
"block-start": "order-first w-full justify-start px-2.5 pt-2 group-has-[>input]/input-group:pt-2 [.border-b]:pb-2",
|
|
101
|
+
"block-end": "order-last w-full justify-start px-2.5 pb-2 group-has-[>input]/input-group:pb-2 [.border-t]:pt-2"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
defaultVariants: { align: "inline-start" }
|
|
105
|
+
});
|
|
106
|
+
var inputGroupButtonVariants = cva2("flex items-center gap-2 text-sm shadow-none", {
|
|
107
|
+
variants: {
|
|
108
|
+
size: {
|
|
109
|
+
xs: "h-6 gap-1 rounded-[calc(var(--radius)-5px)] px-1.5 [&>svg:not([class*='size-'])]:size-3.5",
|
|
110
|
+
sm: "",
|
|
111
|
+
"icon-xs": "size-6 rounded-[calc(var(--radius)-5px)] p-0 has-[>svg]:p-0",
|
|
112
|
+
"icon-sm": "size-8 p-0 has-[>svg]:p-0"
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
defaultVariants: { size: "xs" }
|
|
116
|
+
});
|
|
117
|
+
function InputGroupInput({
|
|
118
|
+
className,
|
|
119
|
+
...props
|
|
120
|
+
}) {
|
|
121
|
+
return /* @__PURE__ */ jsx3(Input, {
|
|
122
|
+
"data-slot": "input-group-control",
|
|
123
|
+
className: cn("flex-1 rounded-none border-0 bg-transparent shadow-none ring-0 focus-visible:ring-0 aria-invalid:ring-0 dark:bg-transparent", className),
|
|
124
|
+
...props
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ../../src/components/ui/virtualized-combobox.tsx
|
|
129
|
+
import { useKrakstackLocale } from "@krak-stack/registry/krakstack-provider";
|
|
130
|
+
import { jsx as jsx4, jsxs, Fragment } from "react/jsx-runtime";
|
|
131
|
+
"use client";
|
|
132
|
+
var messages = {
|
|
133
|
+
en: {
|
|
134
|
+
clear: "Clear selection",
|
|
135
|
+
search: "Search...",
|
|
136
|
+
selected: (count) => `${count} selected`,
|
|
137
|
+
selectMore: "Select more"
|
|
138
|
+
},
|
|
139
|
+
fr: {
|
|
140
|
+
clear: "Effacer la sélection",
|
|
141
|
+
search: "Rechercher...",
|
|
142
|
+
selected: (count) => `${count} sélectionnés`,
|
|
143
|
+
selectMore: "Sélectionner davantage"
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var virtualizedComboboxMessages = (locale, translations) => ({
|
|
147
|
+
...locale.startsWith("fr") ? messages.fr : messages.en,
|
|
148
|
+
...translations?.[locale.split("-")[0] ?? locale],
|
|
149
|
+
...translations?.[locale]
|
|
150
|
+
});
|
|
151
|
+
var optionEquals = (item, selected) => item.value === selected.value;
|
|
152
|
+
var VirtualizedComboboxList = ({
|
|
153
|
+
emptyLabel,
|
|
154
|
+
groupSelections,
|
|
155
|
+
itemIndexRef,
|
|
156
|
+
locale = "en",
|
|
157
|
+
messages: messageOverrides,
|
|
158
|
+
renderItem,
|
|
159
|
+
selectedValues,
|
|
160
|
+
virtualizerRef
|
|
161
|
+
}) => {
|
|
162
|
+
const filteredItems = ComboboxPrimitive.useFilteredItems();
|
|
163
|
+
const labels = virtualizedComboboxMessages(locale, messageOverrides);
|
|
164
|
+
const itemEntries = filteredItems.map((item, index) => ({
|
|
165
|
+
kind: "item",
|
|
166
|
+
item,
|
|
167
|
+
index
|
|
168
|
+
}));
|
|
169
|
+
const selectedEntries = itemEntries.filter(({ item }) => selectedValues.has(item.value));
|
|
170
|
+
const availableEntries = itemEntries.filter(({ item }) => !selectedValues.has(item.value));
|
|
171
|
+
const entries = filteredItems.length === 0 ? [] : groupSelections ? [
|
|
172
|
+
...selectedEntries.length > 0 ? [
|
|
173
|
+
{
|
|
174
|
+
kind: "group",
|
|
175
|
+
key: "selected",
|
|
176
|
+
label: labels.selected(selectedValues.size)
|
|
177
|
+
},
|
|
178
|
+
...selectedEntries
|
|
179
|
+
] : [],
|
|
180
|
+
{
|
|
181
|
+
kind: "group",
|
|
182
|
+
key: "available",
|
|
183
|
+
label: labels.selectMore
|
|
184
|
+
},
|
|
185
|
+
...availableEntries
|
|
186
|
+
] : itemEntries;
|
|
187
|
+
const scrollRef = useRef(null);
|
|
188
|
+
const virtualizer = useVirtualizer({
|
|
189
|
+
count: entries.length,
|
|
190
|
+
getScrollElement: () => scrollRef.current,
|
|
191
|
+
getItemKey: (index) => {
|
|
192
|
+
const entry = entries[index];
|
|
193
|
+
return entry?.kind === "group" ? `group-${entry.key}` : entry?.item.value ?? index;
|
|
194
|
+
},
|
|
195
|
+
estimateSize: () => 36,
|
|
196
|
+
overscan: 8
|
|
197
|
+
});
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
itemIndexRef.current = new Map(entries.flatMap((entry, index) => entry.kind === "item" ? [[entry.index, index]] : []));
|
|
200
|
+
});
|
|
201
|
+
useImperativeHandle(virtualizerRef, () => virtualizer, [virtualizer]);
|
|
202
|
+
const handleScrollElementRef = useCallback((element) => {
|
|
203
|
+
scrollRef.current = element;
|
|
204
|
+
if (element)
|
|
205
|
+
virtualizer.measure();
|
|
206
|
+
}, [virtualizer]);
|
|
207
|
+
return /* @__PURE__ */ jsxs(Fragment, {
|
|
208
|
+
children: [
|
|
209
|
+
filteredItems.length === 0 && /* @__PURE__ */ jsx4("div", {
|
|
210
|
+
className: "text-muted-foreground flex min-h-0 flex-1 flex-col items-center justify-center overflow-auto py-6 text-center text-sm",
|
|
211
|
+
children: emptyLabel
|
|
212
|
+
}),
|
|
213
|
+
/* @__PURE__ */ jsx4(ComboboxPrimitive.List, {
|
|
214
|
+
className: "min-h-0 flex-1 scroll-py-1 overflow-auto overscroll-contain p-1",
|
|
215
|
+
hidden: filteredItems.length === 0,
|
|
216
|
+
ref: handleScrollElementRef,
|
|
217
|
+
style: {
|
|
218
|
+
height: Math.min(virtualizer.getTotalSize() + 8, 288),
|
|
219
|
+
maxHeight: "var(--available-height)"
|
|
220
|
+
},
|
|
221
|
+
children: /* @__PURE__ */ jsx4("div", {
|
|
222
|
+
className: "relative w-full",
|
|
223
|
+
role: "presentation",
|
|
224
|
+
style: { height: virtualizer.getTotalSize() },
|
|
225
|
+
children: virtualizer.getVirtualItems().map((virtualItem) => {
|
|
226
|
+
const entry = entries[virtualItem.index];
|
|
227
|
+
if (!entry)
|
|
228
|
+
return null;
|
|
229
|
+
const style = {
|
|
230
|
+
height: virtualItem.size,
|
|
231
|
+
transform: `translateY(${virtualItem.start}px)`
|
|
232
|
+
};
|
|
233
|
+
if (entry.kind === "group") {
|
|
234
|
+
return /* @__PURE__ */ jsx4(ComboboxPrimitive.Group, {
|
|
235
|
+
className: "contents",
|
|
236
|
+
children: /* @__PURE__ */ jsx4(ComboboxPrimitive.GroupLabel, {
|
|
237
|
+
className: "text-muted-foreground absolute top-0 left-0 w-full px-2 pt-3 pb-1 text-xs",
|
|
238
|
+
"data-index": virtualItem.index,
|
|
239
|
+
ref: virtualizer.measureElement,
|
|
240
|
+
style,
|
|
241
|
+
children: entry.label
|
|
242
|
+
})
|
|
243
|
+
}, entry.key);
|
|
244
|
+
}
|
|
245
|
+
return /* @__PURE__ */ jsxs(ComboboxPrimitive.Item, {
|
|
246
|
+
"aria-posinset": entry.index + 1,
|
|
247
|
+
"aria-setsize": filteredItems.length,
|
|
248
|
+
className: "data-highlighted:bg-accent data-highlighted:text-accent-foreground absolute top-0 left-0 flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50",
|
|
249
|
+
"data-index": virtualItem.index,
|
|
250
|
+
index: entry.index,
|
|
251
|
+
ref: virtualizer.measureElement,
|
|
252
|
+
style,
|
|
253
|
+
value: entry.item,
|
|
254
|
+
children: [
|
|
255
|
+
renderItem?.(entry.item) ?? /* @__PURE__ */ jsx4("span", {
|
|
256
|
+
className: "min-w-0 truncate",
|
|
257
|
+
children: entry.item.label
|
|
258
|
+
}),
|
|
259
|
+
/* @__PURE__ */ jsx4(ComboboxPrimitive.ItemIndicator, {
|
|
260
|
+
className: "absolute right-2 flex size-4 items-center justify-center",
|
|
261
|
+
children: /* @__PURE__ */ jsx4(Check, {})
|
|
262
|
+
})
|
|
263
|
+
]
|
|
264
|
+
}, entry.item.value);
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
]
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
var VirtualizedComboboxContent = ({
|
|
272
|
+
contentClassName,
|
|
273
|
+
emptyLabel,
|
|
274
|
+
groupSelections,
|
|
275
|
+
itemIndexRef,
|
|
276
|
+
locale = "en",
|
|
277
|
+
messages: messageOverrides,
|
|
278
|
+
renderItem,
|
|
279
|
+
selectedValues,
|
|
280
|
+
virtualizerRef
|
|
281
|
+
}) => {
|
|
282
|
+
const labels = virtualizedComboboxMessages(locale, messageOverrides);
|
|
283
|
+
return /* @__PURE__ */ jsx4(ComboboxPrimitive.Portal, {
|
|
284
|
+
children: /* @__PURE__ */ jsx4(ComboboxPrimitive.Positioner, {
|
|
285
|
+
align: "start",
|
|
286
|
+
className: "isolate z-50",
|
|
287
|
+
side: "bottom",
|
|
288
|
+
sideOffset: 6,
|
|
289
|
+
children: /* @__PURE__ */ jsxs(ComboboxPrimitive.Popup, {
|
|
290
|
+
className: cn("bg-popover text-popover-foreground ring-foreground/10 data-[side=bottom]:slide-in-from-top-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-in-95 relative flex max-h-(--available-height) w-(--anchor-width) max-w-(--available-width) min-w-0 origin-(--transform-origin) flex-col overflow-hidden rounded-md shadow-md ring-1 duration-100", contentClassName),
|
|
291
|
+
children: [
|
|
292
|
+
/* @__PURE__ */ jsx4("div", {
|
|
293
|
+
className: "min-w-0 shrink-0 p-1 pb-0",
|
|
294
|
+
children: /* @__PURE__ */ jsx4(InputGroup, {
|
|
295
|
+
className: "border-input/30 bg-input/30 h-8 w-full min-w-0 shadow-none",
|
|
296
|
+
children: /* @__PURE__ */ jsx4(ComboboxPrimitive.Input, {
|
|
297
|
+
placeholder: labels.search,
|
|
298
|
+
render: /* @__PURE__ */ jsx4(InputGroupInput, {})
|
|
299
|
+
})
|
|
300
|
+
})
|
|
301
|
+
}),
|
|
302
|
+
/* @__PURE__ */ jsx4(VirtualizedComboboxList, {
|
|
303
|
+
emptyLabel,
|
|
304
|
+
groupSelections,
|
|
305
|
+
itemIndexRef,
|
|
306
|
+
locale,
|
|
307
|
+
messages: messageOverrides,
|
|
308
|
+
renderItem,
|
|
309
|
+
selectedValues,
|
|
310
|
+
virtualizerRef
|
|
311
|
+
})
|
|
312
|
+
]
|
|
313
|
+
})
|
|
314
|
+
})
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
var defaultTrigger = (ariaLabel, ariaInvalid, disabled, label, triggerId) => /* @__PURE__ */ jsxs(Button, {
|
|
318
|
+
"aria-label": ariaLabel,
|
|
319
|
+
"aria-invalid": ariaInvalid,
|
|
320
|
+
className: "w-full max-w-full min-w-0 justify-between overflow-hidden",
|
|
321
|
+
disabled,
|
|
322
|
+
id: triggerId,
|
|
323
|
+
type: "button",
|
|
324
|
+
variant: "outline",
|
|
325
|
+
children: [
|
|
326
|
+
/* @__PURE__ */ jsx4("span", {
|
|
327
|
+
className: "min-w-0 flex-1 truncate pr-7 text-left",
|
|
328
|
+
children: label
|
|
329
|
+
}),
|
|
330
|
+
/* @__PURE__ */ jsx4(ChevronsUpDown, {
|
|
331
|
+
className: "text-muted-foreground shrink-0 opacity-70"
|
|
332
|
+
})
|
|
333
|
+
]
|
|
334
|
+
});
|
|
335
|
+
var defaultClear = (label) => /* @__PURE__ */ jsx4(ComboboxPrimitive.Clear, {
|
|
336
|
+
"aria-label": label,
|
|
337
|
+
className: "text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-ring/50 absolute top-1/2 right-7 z-10 flex size-6 -translate-y-1/2 items-center justify-center rounded-sm outline-none focus-visible:border focus-visible:ring-3",
|
|
338
|
+
type: "button",
|
|
339
|
+
children: /* @__PURE__ */ jsx4(X, {
|
|
340
|
+
className: "size-3.5"
|
|
341
|
+
})
|
|
342
|
+
});
|
|
343
|
+
var selectedLabel = (value, placeholder) => {
|
|
344
|
+
if (Array.isArray(value)) {
|
|
345
|
+
return value.length > 0 ? value.map(({ label }) => label).join(", ") : placeholder;
|
|
346
|
+
}
|
|
347
|
+
return value && "label" in value ? value.label : placeholder;
|
|
348
|
+
};
|
|
349
|
+
var useComboboxVirtualizerRef = (providedRef) => {
|
|
350
|
+
const localRef = useRef(null);
|
|
351
|
+
return providedRef ?? localRef;
|
|
352
|
+
};
|
|
353
|
+
var VirtualizedComboboxSingle = (props) => {
|
|
354
|
+
const virtualizerRef = useComboboxVirtualizerRef(props.virtualizerRef);
|
|
355
|
+
const itemIndexRef = useRef(new Map);
|
|
356
|
+
const labels = virtualizedComboboxMessages(props.locale ?? "en", props.messages);
|
|
357
|
+
return /* @__PURE__ */ jsxs(ComboboxPrimitive.Root, {
|
|
358
|
+
disabled: props.disabled,
|
|
359
|
+
inputValue: props.searchValue,
|
|
360
|
+
items: [...props.items],
|
|
361
|
+
itemToStringLabel: (item) => item.label,
|
|
362
|
+
isItemEqualToValue: optionEquals,
|
|
363
|
+
onInputValueChange: (value) => props.onSearchValueChange?.(value),
|
|
364
|
+
onItemHighlighted: (_, { index, reason }) => {
|
|
365
|
+
if (reason === "keyboard" && index >= 0) {
|
|
366
|
+
virtualizerRef.current?.scrollToIndex(itemIndexRef.current.get(index) ?? index, { align: "auto" });
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
onOpenChange: (open) => props.onOpenChange?.(open),
|
|
370
|
+
onValueChange: (value) => props.onValueChange(value),
|
|
371
|
+
open: props.open,
|
|
372
|
+
value: props.value,
|
|
373
|
+
virtualized: true,
|
|
374
|
+
children: [
|
|
375
|
+
/* @__PURE__ */ jsxs("div", {
|
|
376
|
+
className: "relative",
|
|
377
|
+
children: [
|
|
378
|
+
/* @__PURE__ */ jsx4(ComboboxPrimitive.Trigger, {
|
|
379
|
+
render: props.trigger ?? defaultTrigger(props.ariaLabel, props.ariaInvalid, props.disabled, selectedLabel(props.value, props.placeholder), props.triggerId)
|
|
380
|
+
}),
|
|
381
|
+
props.trigger ? null : defaultClear(labels.clear)
|
|
382
|
+
]
|
|
383
|
+
}),
|
|
384
|
+
/* @__PURE__ */ jsx4(VirtualizedComboboxContent, {
|
|
385
|
+
...props,
|
|
386
|
+
groupSelections: false,
|
|
387
|
+
itemIndexRef,
|
|
388
|
+
selectedValues: new Set(props.value ? [props.value.value] : []),
|
|
389
|
+
virtualizerRef
|
|
390
|
+
})
|
|
391
|
+
]
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
var VirtualizedComboboxMultiple = (props) => {
|
|
395
|
+
const virtualizerRef = useComboboxVirtualizerRef(props.virtualizerRef);
|
|
396
|
+
const itemIndexRef = useRef(new Map);
|
|
397
|
+
const labels = virtualizedComboboxMessages(props.locale ?? "en", props.messages);
|
|
398
|
+
return /* @__PURE__ */ jsxs(ComboboxPrimitive.Root, {
|
|
399
|
+
disabled: props.disabled,
|
|
400
|
+
inputValue: props.searchValue,
|
|
401
|
+
items: [...props.items],
|
|
402
|
+
itemToStringLabel: (item) => item.label,
|
|
403
|
+
isItemEqualToValue: optionEquals,
|
|
404
|
+
multiple: true,
|
|
405
|
+
onInputValueChange: (value) => props.onSearchValueChange?.(value),
|
|
406
|
+
onItemHighlighted: (_, { index, reason }) => {
|
|
407
|
+
if (reason === "keyboard" && index >= 0) {
|
|
408
|
+
virtualizerRef.current?.scrollToIndex(itemIndexRef.current.get(index) ?? index, { align: "auto" });
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
onOpenChange: (open) => props.onOpenChange?.(open),
|
|
412
|
+
onValueChange: (value) => props.onValueChange(value),
|
|
413
|
+
open: props.open,
|
|
414
|
+
value: [...props.value],
|
|
415
|
+
virtualized: true,
|
|
416
|
+
children: [
|
|
417
|
+
/* @__PURE__ */ jsxs("div", {
|
|
418
|
+
className: "relative",
|
|
419
|
+
children: [
|
|
420
|
+
/* @__PURE__ */ jsx4(ComboboxPrimitive.Trigger, {
|
|
421
|
+
render: props.trigger ?? defaultTrigger(props.ariaLabel, props.ariaInvalid, props.disabled, selectedLabel(props.value, props.placeholder), props.triggerId)
|
|
422
|
+
}),
|
|
423
|
+
props.trigger ? null : defaultClear(labels.clear)
|
|
424
|
+
]
|
|
425
|
+
}),
|
|
426
|
+
/* @__PURE__ */ jsx4(VirtualizedComboboxContent, {
|
|
427
|
+
...props,
|
|
428
|
+
groupSelections: true,
|
|
429
|
+
itemIndexRef,
|
|
430
|
+
selectedValues: new Set(props.value.map(({ value }) => value)),
|
|
431
|
+
virtualizerRef
|
|
432
|
+
})
|
|
433
|
+
]
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
function VirtualizedCombobox(props) {
|
|
437
|
+
const locale = useKrakstackLocale(props.locale);
|
|
438
|
+
return props.multiple ? /* @__PURE__ */ jsx4(VirtualizedComboboxMultiple, {
|
|
439
|
+
...props,
|
|
440
|
+
locale
|
|
441
|
+
}) : /* @__PURE__ */ jsx4(VirtualizedComboboxSingle, {
|
|
442
|
+
...props,
|
|
443
|
+
locale
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// ../../src/components/ui/phone-number-input.tsx
|
|
448
|
+
import { ChevronsUpDown as ChevronsUpDown2 } from "lucide-react";
|
|
449
|
+
import { jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
450
|
+
var normalizePhoneNumber = (value, country) => {
|
|
451
|
+
const phone = parsePhoneNumberFromString(value, {
|
|
452
|
+
defaultCountry: country,
|
|
453
|
+
extract: false
|
|
454
|
+
});
|
|
455
|
+
return phone?.isValid() ? {
|
|
456
|
+
e164: phone.number,
|
|
457
|
+
extension: phone.ext ?? null,
|
|
458
|
+
country: phone.country ?? null
|
|
459
|
+
} : null;
|
|
460
|
+
};
|
|
461
|
+
var PhoneNumberInput = ({
|
|
462
|
+
value,
|
|
463
|
+
country,
|
|
464
|
+
onCountryChange,
|
|
465
|
+
onValueChange,
|
|
466
|
+
countries = getCountries(),
|
|
467
|
+
locale,
|
|
468
|
+
label,
|
|
469
|
+
id,
|
|
470
|
+
onBlur,
|
|
471
|
+
name,
|
|
472
|
+
withExtension = false,
|
|
473
|
+
onKeyDown,
|
|
474
|
+
...props
|
|
475
|
+
}) => {
|
|
476
|
+
const generatedId = useId();
|
|
477
|
+
const inputId = id ?? generatedId;
|
|
478
|
+
const language = useKrakstackLocale2(locale);
|
|
479
|
+
const french = language.startsWith("fr");
|
|
480
|
+
const names = new Intl.DisplayNames([language], { type: "region" });
|
|
481
|
+
const options = [...new Set([country, ...countries])].sort((a, b) => (names.of(a) ?? a).localeCompare(names.of(b) ?? b, language));
|
|
482
|
+
const valid = normalizePhoneNumber(value, country);
|
|
483
|
+
const parsed = parsePhoneNumberFromString(value, {
|
|
484
|
+
defaultCountry: country,
|
|
485
|
+
extract: false
|
|
486
|
+
});
|
|
487
|
+
const callingCode = value.trim().startsWith("+") ? parsed?.countryCallingCode ?? getCountryCallingCode(country) : getCountryCallingCode(country);
|
|
488
|
+
const nationalDraft = value.trim().startsWith(`+${callingCode}`) ? value.trim().slice(callingCode.length + 1).trimStart() : value;
|
|
489
|
+
const extension = parsed?.ext ?? "";
|
|
490
|
+
const nationalDigits = (extension ? nationalDraft.replace(/(?:ext\.?|x|#|;ext=)\s*\d+\s*$/i, "") : nationalDraft).replace(/\D/g, "");
|
|
491
|
+
const formatCountry = parsed?.country ?? country;
|
|
492
|
+
const nationalValue = parsed?.isValid() ? parsed.formatNational({ formatExtension: (number) => number }) : new AsYouType(formatCountry).input(nationalDigits);
|
|
493
|
+
const pendingCaret = useRef2(null);
|
|
494
|
+
useLayoutEffect(() => {
|
|
495
|
+
const pending = pendingCaret.current;
|
|
496
|
+
if (!pending)
|
|
497
|
+
return;
|
|
498
|
+
pendingCaret.current = null;
|
|
499
|
+
let remaining = pending.digits;
|
|
500
|
+
let position = 0;
|
|
501
|
+
while (position < pending.element.value.length && remaining > 0) {
|
|
502
|
+
if (/\d/.test(pending.element.value[position]))
|
|
503
|
+
remaining--;
|
|
504
|
+
position++;
|
|
505
|
+
}
|
|
506
|
+
pending.element.setSelectionRange(position, position);
|
|
507
|
+
});
|
|
508
|
+
const emitNational = (draft, nextExtension = extension) => {
|
|
509
|
+
const digits = draft.replace(/\D/g, "");
|
|
510
|
+
const next = digits ? `+${callingCode} ${digits}${nextExtension ? ` ext. ${nextExtension}` : ""}` : "";
|
|
511
|
+
onValueChange(next, normalizePhoneNumber(next, country));
|
|
512
|
+
};
|
|
513
|
+
const countryLabel = french ? "Pays du numéro" : "Phone country";
|
|
514
|
+
const items = options.map((code) => ({
|
|
515
|
+
value: code,
|
|
516
|
+
label: `${names.of(code)} (${code}) +${getCountryCallingCode(code)}`
|
|
517
|
+
}));
|
|
518
|
+
return /* @__PURE__ */ jsxs2("div", {
|
|
519
|
+
className: "grid gap-2",
|
|
520
|
+
children: [
|
|
521
|
+
/* @__PURE__ */ jsx5("label", {
|
|
522
|
+
htmlFor: inputId,
|
|
523
|
+
className: "text-sm font-medium",
|
|
524
|
+
children: label ?? (french ? "Numéro de téléphone" : "Phone number")
|
|
525
|
+
}),
|
|
526
|
+
name ? /* @__PURE__ */ jsx5("input", {
|
|
527
|
+
type: "hidden",
|
|
528
|
+
name,
|
|
529
|
+
value,
|
|
530
|
+
disabled: props.disabled
|
|
531
|
+
}) : null,
|
|
532
|
+
/* @__PURE__ */ jsxs2("div", {
|
|
533
|
+
className: "flex min-w-0 gap-2",
|
|
534
|
+
children: [
|
|
535
|
+
/* @__PURE__ */ jsx5(VirtualizedCombobox, {
|
|
536
|
+
ariaLabel: countryLabel,
|
|
537
|
+
locale: language,
|
|
538
|
+
items,
|
|
539
|
+
value: items.find((item) => item.value === country) ?? null,
|
|
540
|
+
placeholder: country,
|
|
541
|
+
emptyLabel: french ? "Aucun pays correspondant." : "No matching countries.",
|
|
542
|
+
contentClassName: "w-72! max-w-[calc(100vw-2rem)]!",
|
|
543
|
+
disabled: props.disabled || props.readOnly,
|
|
544
|
+
trigger: /* @__PURE__ */ jsxs2(Button, {
|
|
545
|
+
type: "button",
|
|
546
|
+
variant: "outline",
|
|
547
|
+
"aria-label": countryLabel,
|
|
548
|
+
disabled: props.disabled || props.readOnly,
|
|
549
|
+
className: "shrink-0 gap-1.5 px-2.5",
|
|
550
|
+
children: [
|
|
551
|
+
/* @__PURE__ */ jsx5("span", {
|
|
552
|
+
children: country
|
|
553
|
+
}),
|
|
554
|
+
/* @__PURE__ */ jsxs2("span", {
|
|
555
|
+
className: "text-muted-foreground",
|
|
556
|
+
children: [
|
|
557
|
+
"+",
|
|
558
|
+
callingCode
|
|
559
|
+
]
|
|
560
|
+
}),
|
|
561
|
+
/* @__PURE__ */ jsx5(ChevronsUpDown2, {
|
|
562
|
+
"aria-hidden": "true",
|
|
563
|
+
className: "size-3.5 opacity-60"
|
|
564
|
+
})
|
|
565
|
+
]
|
|
566
|
+
}),
|
|
567
|
+
onValueChange: (item) => {
|
|
568
|
+
const selected = options.find((option) => option === item?.value);
|
|
569
|
+
if (!selected || selected === country)
|
|
570
|
+
return;
|
|
571
|
+
onCountryChange(selected);
|
|
572
|
+
const next = nationalDigits ? `+${getCountryCallingCode(selected)} ${nationalDigits}${extension ? ` ext. ${extension}` : ""}` : "";
|
|
573
|
+
onValueChange(next, normalizePhoneNumber(next, selected));
|
|
574
|
+
}
|
|
575
|
+
}),
|
|
576
|
+
/* @__PURE__ */ jsx5(Input, {
|
|
577
|
+
...props,
|
|
578
|
+
id: inputId,
|
|
579
|
+
type: "tel",
|
|
580
|
+
inputMode: "numeric",
|
|
581
|
+
autoComplete: props.autoComplete ?? "tel-national",
|
|
582
|
+
className: "min-w-0 flex-1",
|
|
583
|
+
value: nationalValue,
|
|
584
|
+
"aria-invalid": Boolean(value) && !valid,
|
|
585
|
+
"aria-describedby": `${inputId}-status ${props["aria-describedby"] ?? ""}`,
|
|
586
|
+
onChange: (event) => {
|
|
587
|
+
const draft = event.target.value;
|
|
588
|
+
const international = draft.trimStart().startsWith("+");
|
|
589
|
+
const pasted = parsePhoneNumberFromString(draft, {
|
|
590
|
+
defaultCountry: country,
|
|
591
|
+
extract: false
|
|
592
|
+
});
|
|
593
|
+
if (!international && !pasted?.ext) {
|
|
594
|
+
pendingCaret.current = {
|
|
595
|
+
element: event.target,
|
|
596
|
+
digits: draft.slice(0, event.target.selectionStart ?? draft.length).replace(/\D/g, "").length
|
|
597
|
+
};
|
|
598
|
+
emitNational(draft);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
const digits = draft.replace(/\D/g, "");
|
|
602
|
+
const next = pasted?.isValid() ? `${pasted.number}${pasted.ext ? ` ext. ${pasted.ext}` : ""}` : digits ? `${international ? "+" : `+${callingCode} `}${digits}` : "";
|
|
603
|
+
const details = normalizePhoneNumber(next, country);
|
|
604
|
+
if (international && details?.country && options.includes(details.country) && details.country !== country)
|
|
605
|
+
onCountryChange(details.country);
|
|
606
|
+
onValueChange(next, details);
|
|
607
|
+
},
|
|
608
|
+
onKeyDown: (event) => {
|
|
609
|
+
onKeyDown?.(event);
|
|
610
|
+
if (event.defaultPrevented || props.readOnly || props.disabled || event.ctrlKey || event.metaKey || event.altKey)
|
|
611
|
+
return;
|
|
612
|
+
if (event.key.length === 1 && /\D/.test(event.key)) {
|
|
613
|
+
event.preventDefault();
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (event.key !== "Backspace" && event.key !== "Delete")
|
|
617
|
+
return;
|
|
618
|
+
const element = event.currentTarget;
|
|
619
|
+
const start = element.selectionStart ?? 0;
|
|
620
|
+
if (start !== element.selectionEnd)
|
|
621
|
+
return;
|
|
622
|
+
const direction = event.key === "Backspace" ? -1 : 1;
|
|
623
|
+
let index = direction === -1 ? start - 1 : start;
|
|
624
|
+
if (index < 0 || index >= nationalValue.length || /\d/.test(nationalValue[index]))
|
|
625
|
+
return;
|
|
626
|
+
while (index >= 0 && index < nationalValue.length && /\D/.test(nationalValue[index]))
|
|
627
|
+
index += direction;
|
|
628
|
+
if (index < 0 || index >= nationalValue.length)
|
|
629
|
+
return;
|
|
630
|
+
event.preventDefault();
|
|
631
|
+
const next = nationalValue.slice(0, index) + nationalValue.slice(index + 1);
|
|
632
|
+
pendingCaret.current = {
|
|
633
|
+
element,
|
|
634
|
+
digits: nationalValue.slice(0, direction === -1 ? index : start).replace(/\D/g, "").length
|
|
635
|
+
};
|
|
636
|
+
emitNational(next);
|
|
637
|
+
},
|
|
638
|
+
onBlur: (event) => {
|
|
639
|
+
if (!props.readOnly && !props.disabled && valid) {
|
|
640
|
+
const formatted = parsePhoneNumberFromString(value, {
|
|
641
|
+
defaultCountry: country,
|
|
642
|
+
extract: false
|
|
643
|
+
})?.formatInternational();
|
|
644
|
+
if (formatted && formatted !== value)
|
|
645
|
+
onValueChange(formatted, valid);
|
|
646
|
+
}
|
|
647
|
+
onBlur?.(event);
|
|
648
|
+
}
|
|
649
|
+
})
|
|
650
|
+
]
|
|
651
|
+
}),
|
|
652
|
+
withExtension || extension ? /* @__PURE__ */ jsxs2("div", {
|
|
653
|
+
className: "grid max-w-32 gap-1.5",
|
|
654
|
+
children: [
|
|
655
|
+
/* @__PURE__ */ jsx5("label", {
|
|
656
|
+
htmlFor: `${inputId}-extension`,
|
|
657
|
+
className: "text-muted-foreground text-xs",
|
|
658
|
+
children: french ? "Poste" : "Extension"
|
|
659
|
+
}),
|
|
660
|
+
/* @__PURE__ */ jsx5(Input, {
|
|
661
|
+
id: `${inputId}-extension`,
|
|
662
|
+
type: "text",
|
|
663
|
+
inputMode: "numeric",
|
|
664
|
+
value: extension,
|
|
665
|
+
disabled: props.disabled,
|
|
666
|
+
readOnly: props.readOnly,
|
|
667
|
+
onChange: (event) => emitNational(nationalDigits, event.target.value.replace(/\D/g, ""))
|
|
668
|
+
})
|
|
669
|
+
]
|
|
670
|
+
}) : null,
|
|
671
|
+
/* @__PURE__ */ jsx5("p", {
|
|
672
|
+
id: `${inputId}-status`,
|
|
673
|
+
className: "text-muted-foreground text-xs",
|
|
674
|
+
children: valid ? `${valid.e164}${valid.extension ? ` ext. ${valid.extension}` : ""}` : french ? "Saisissez un numéro complet. La validation ne vérifie pas son propriétaire." : "Enter a complete number. Validation does not verify ownership."
|
|
675
|
+
})
|
|
676
|
+
]
|
|
677
|
+
});
|
|
678
|
+
};
|
|
679
|
+
export {
|
|
680
|
+
PhoneNumberInput,
|
|
681
|
+
normalizePhoneNumber
|
|
682
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krak-stack/registry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Tree-shakable KrakStack components and Effect services.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,6 +17,14 @@
|
|
|
17
17
|
"type": "module",
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"exports": {
|
|
20
|
+
"./cron-expression-editor": {
|
|
21
|
+
"types": "./dist/components/ui/cron-expression-editor.d.ts",
|
|
22
|
+
"import": "./dist/components/ui/cron-expression-editor.js"
|
|
23
|
+
},
|
|
24
|
+
"./phone-number-input": {
|
|
25
|
+
"types": "./dist/components/ui/phone-number-input.d.ts",
|
|
26
|
+
"import": "./dist/components/ui/phone-number-input.js"
|
|
27
|
+
},
|
|
20
28
|
"./image-crop-editor": {
|
|
21
29
|
"types": "./dist/components/ui/image-crop-editor.d.ts",
|
|
22
30
|
"import": "./dist/components/ui/image-crop-editor.js"
|
|
@@ -370,6 +378,8 @@
|
|
|
370
378
|
"dependencies": {
|
|
371
379
|
"@oxlint/plugins": "1.78.0",
|
|
372
380
|
"bowser": "^2.14.1",
|
|
381
|
+
"cron-parser": "^5.10.4",
|
|
382
|
+
"libphonenumber-js": "^1.13.16",
|
|
373
383
|
"motion": "^14.12.0",
|
|
374
384
|
"react-colorful": "^5.8.1",
|
|
375
385
|
"web-vitals": "^5.1.0"
|