@momotombodevs/istmo 0.0.0-stage → 0.2.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 +26 -0
- package/LICENSE +21 -0
- package/README.md +458 -2
- package/dist/dates.cjs +314 -0
- package/dist/dates.d.cts +127 -0
- package/dist/dates.d.ts +127 -0
- package/dist/dates.js +302 -0
- package/dist/formatted-input.cjs +51 -0
- package/dist/formatted-input.js +49 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/metadata.cjs +3 -0
- package/dist/metadata.js +4 -0
- package/dist/money.cjs +172 -0
- package/dist/money.d.cts +69 -0
- package/dist/money.d.ts +69 -0
- package/dist/money.js +166 -0
- package/dist/phone-examples.json.cjs +11 -0
- package/dist/phone-examples.json.js +11 -0
- package/dist/phone-input.cjs +34 -0
- package/dist/phone-input.d.cts +18 -0
- package/dist/phone-input.d.ts +18 -0
- package/dist/phone-input.js +33 -0
- package/dist/phone-metadata.json.cjs +224 -0
- package/dist/phone-metadata.json.js +224 -0
- package/dist/phone.cjs +121 -0
- package/dist/phone.d.cts +78 -0
- package/dist/phone.d.ts +78 -0
- package/dist/phone.js +117 -0
- package/dist/react.cjs +274 -0
- package/dist/react.d.cts +148 -0
- package/dist/react.d.ts +148 -0
- package/dist/react.js +268 -0
- package/dist/regions.cjs +94 -0
- package/dist/regions.d.cts +77 -0
- package/dist/regions.d.ts +77 -0
- package/dist/regions.js +85 -0
- package/dist/regions.json.cjs +136 -0
- package/dist/regions.json.js +125 -0
- package/dist/require.cjs +14 -0
- package/dist/require.js +13 -0
- package/flags/LICENSE +21 -0
- package/flags/bz.svg +145 -0
- package/flags/cr.svg +7 -0
- package/flags/gt.svg +204 -0
- package/flags/hn.svg +18 -0
- package/flags/ni.svg +129 -0
- package/flags/pa.svg +7 -0
- package/flags/sv.svg +593 -0
- package/package.json +201 -4
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
const require_dates = require("./dates.cjs");
|
|
4
|
+
const require_formatted_input = require("./formatted-input.cjs");
|
|
5
|
+
const require_money = require("./money.cjs");
|
|
6
|
+
const require_phone = require("./phone.cjs");
|
|
7
|
+
const require_phone_input = require("./phone-input.cjs");
|
|
8
|
+
const require_regions = require("./regions.cjs");
|
|
9
|
+
const require_require = require("./require.cjs");
|
|
10
|
+
// src/react.ts
|
|
11
|
+
const FRESH = {
|
|
12
|
+
touched: false,
|
|
13
|
+
caret: null
|
|
14
|
+
};
|
|
15
|
+
const DIGIT = /\d/;
|
|
16
|
+
const useIsomorphicLayoutEffect = "window" in globalThis ? react.useLayoutEffect : react.useEffect;
|
|
17
|
+
const digitsOf = (text) => text.replace(/\D/g, "");
|
|
18
|
+
function initialPhone(defaultCountry, initialValue, countries) {
|
|
19
|
+
const stored = initialValue ? require_phone.parsePhone(initialValue, defaultCountry, { countries }) : null;
|
|
20
|
+
return stored ? {
|
|
21
|
+
value: stored.national,
|
|
22
|
+
country: stored.country
|
|
23
|
+
} : {
|
|
24
|
+
value: "",
|
|
25
|
+
country: require_require.requireCountry(defaultCountry).code
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function useFieldValue(controlled, onChange, initial) {
|
|
29
|
+
const [own, setOwn] = (0, react.useState)(initial);
|
|
30
|
+
const value = controlled === void 0 ? own : controlled;
|
|
31
|
+
const latest = (0, react.useRef)({
|
|
32
|
+
value,
|
|
33
|
+
onChange,
|
|
34
|
+
controlled: controlled !== void 0
|
|
35
|
+
});
|
|
36
|
+
useIsomorphicLayoutEffect(() => {
|
|
37
|
+
latest.current = {
|
|
38
|
+
value,
|
|
39
|
+
onChange,
|
|
40
|
+
controlled: controlled !== void 0
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
return [value, (0, react.useCallback)((change) => {
|
|
44
|
+
const current = latest.current;
|
|
45
|
+
const next = change(current.value);
|
|
46
|
+
current.value = next;
|
|
47
|
+
if (!current.controlled) setOwn(next);
|
|
48
|
+
current.onChange?.(next);
|
|
49
|
+
}, [])];
|
|
50
|
+
}
|
|
51
|
+
function useCaret(caret, text) {
|
|
52
|
+
const element = (0, react.useRef)(null);
|
|
53
|
+
useIsomorphicLayoutEffect(() => {
|
|
54
|
+
if (caret === null || caret >= text.length) return;
|
|
55
|
+
element.current?.setSelectionRange?.(caret, caret);
|
|
56
|
+
element.current?.setSelection?.(caret, caret);
|
|
57
|
+
}, [caret, text]);
|
|
58
|
+
return (0, react.useCallback)((input) => {
|
|
59
|
+
element.current = input;
|
|
60
|
+
}, []);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
64
|
+
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
65
|
+
* flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
|
|
66
|
+
* The handlers keep the same identity while the options do not change. Pass `value`, `country` and `onChange` to
|
|
67
|
+
* control the field from a store or a form library; otherwise it keeps its own state.
|
|
68
|
+
*
|
|
69
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
70
|
+
* @example
|
|
71
|
+
* const field = usePhoneInput({ defaultCountry: 'NI', type: 'mobile' });
|
|
72
|
+
* <TextInput ref={field.ref} value={field.value} onChangeText={field.onChangeText} onBlur={field.onBlur} placeholder={field.placeholder} />
|
|
73
|
+
*/
|
|
74
|
+
function usePhoneInput(options) {
|
|
75
|
+
const { defaultCountry, initialValue, type } = options;
|
|
76
|
+
const allowedKey = (options.countries ?? require_regions.countryCodes).join(",");
|
|
77
|
+
const [field, update] = useFieldValue(options.value === void 0 ? void 0 : {
|
|
78
|
+
value: options.value,
|
|
79
|
+
country: options.country ?? defaultCountry
|
|
80
|
+
}, options.onChange, () => initialPhone(defaultCountry, initialValue, options.countries ?? require_regions.countryCodes));
|
|
81
|
+
const [interaction, setInteraction] = (0, react.useState)(FRESH);
|
|
82
|
+
const ref = useCaret(interaction.caret, field.value);
|
|
83
|
+
const handlers = (0, react.useMemo)(() => {
|
|
84
|
+
const allowed = allowedKey.split(",");
|
|
85
|
+
return {
|
|
86
|
+
allowed,
|
|
87
|
+
onChangeText: (next) => update((current) => {
|
|
88
|
+
let detected;
|
|
89
|
+
const edit = require_formatted_input.applyEdit(current.value, next, (typed) => {
|
|
90
|
+
const formatted = require_phone_input.formatPhoneInput(typed, current.country);
|
|
91
|
+
detected = formatted.country;
|
|
92
|
+
return formatted.text;
|
|
93
|
+
}, DIGIT);
|
|
94
|
+
setInteraction((state) => ({
|
|
95
|
+
...state,
|
|
96
|
+
caret: edit.caret
|
|
97
|
+
}));
|
|
98
|
+
return {
|
|
99
|
+
value: edit.text,
|
|
100
|
+
country: detected && allowed.includes(detected) ? detected : current.country
|
|
101
|
+
};
|
|
102
|
+
}),
|
|
103
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
104
|
+
...state,
|
|
105
|
+
touched: true
|
|
106
|
+
}),
|
|
107
|
+
setCountry: (code) => {
|
|
108
|
+
if (!allowed.includes(code)) throw new RangeError("País fuera de la lista countries del campo.");
|
|
109
|
+
setInteraction((state) => ({
|
|
110
|
+
...state,
|
|
111
|
+
caret: null
|
|
112
|
+
}));
|
|
113
|
+
update((current) => ({
|
|
114
|
+
value: require_phone_input.formatPhoneInput(current.value, code).text,
|
|
115
|
+
country: code
|
|
116
|
+
}));
|
|
117
|
+
},
|
|
118
|
+
reset: () => {
|
|
119
|
+
setInteraction(FRESH);
|
|
120
|
+
update(() => initialPhone(defaultCountry, initialValue, allowed));
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}, [
|
|
124
|
+
allowedKey,
|
|
125
|
+
defaultCountry,
|
|
126
|
+
initialValue,
|
|
127
|
+
update
|
|
128
|
+
]);
|
|
129
|
+
const validation = digitsOf(field.value) ? require_phone.validatePhone(field.value, field.country, {
|
|
130
|
+
type,
|
|
131
|
+
countries: handlers.allowed
|
|
132
|
+
}) : null;
|
|
133
|
+
return {
|
|
134
|
+
ref,
|
|
135
|
+
value: field.value,
|
|
136
|
+
country: require_require.requireCountry(field.country),
|
|
137
|
+
countries: require_regions.countries.filter(({ code }) => handlers.allowed.includes(code)),
|
|
138
|
+
placeholder: require_phone.phoneExample(field.country).national,
|
|
139
|
+
phone: validation?.valid ? validation.phone : null,
|
|
140
|
+
error: interaction.touched && validation && !validation.valid ? validation.error : null,
|
|
141
|
+
onChangeText: handlers.onChangeText,
|
|
142
|
+
onBlur: handlers.onBlur,
|
|
143
|
+
setCountry: handlers.setCountry,
|
|
144
|
+
reset: handlers.reset
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const AMOUNT_CHARACTER = /[\d.]/;
|
|
148
|
+
function initialMoney(currency, initialValue) {
|
|
149
|
+
return initialValue === null || initialValue === void 0 ? "" : require_money.formatMoney(initialValue, currency, { display: "none" });
|
|
150
|
+
}
|
|
151
|
+
const FIELD_FORMAT = /^[\d,]*(?:\.\d*)?$/;
|
|
152
|
+
function typedAmount(previous, next, currency) {
|
|
153
|
+
const prefix = require_formatted_input.commonPrefix(previous, next);
|
|
154
|
+
const suffix = require_formatted_input.commonSuffix(previous, next, prefix);
|
|
155
|
+
if (next.slice(prefix, next.length - suffix) === ",") return `${next.slice(0, prefix)}.${next.slice(next.length - suffix)}`;
|
|
156
|
+
return FIELD_FORMAT.test(next) ? next : require_money.parseMoney(next, currency) ?? next;
|
|
157
|
+
}
|
|
158
|
+
function amountError(amount, text, currency, min, max) {
|
|
159
|
+
if (amount === null) return text ? "format" : null;
|
|
160
|
+
const minor = require_money.toMinorUnits(amount, currency);
|
|
161
|
+
if (min !== void 0 && minor < require_money.toMinorUnits(min, currency)) return "min";
|
|
162
|
+
if (max !== void 0 && minor > require_money.toMinorUnits(max, currency)) return "max";
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* An amount field that groups thousands while typing, keeps the caret where the person is typing, takes `,` from
|
|
167
|
+
* a Spanish decimal keypad as the decimal point, reads pasted amounts in any of the region's formats, and completes
|
|
168
|
+
* the decimals on blur. It renders nothing: show `currency.symbol` and the input with your own components.
|
|
169
|
+
*
|
|
170
|
+
* @throws {RangeError} When the currency is not supported or `initialValue`, `min` or `max` is not an amount.
|
|
171
|
+
* @example
|
|
172
|
+
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
173
|
+
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
174
|
+
*/
|
|
175
|
+
function useMoneyInput({ currency, initialValue, min, max, value, onChange }) {
|
|
176
|
+
const target = require_require.requireCurrency(currency);
|
|
177
|
+
const [text, update] = useFieldValue(value, onChange, () => initialMoney(currency, initialValue));
|
|
178
|
+
const [interaction, setInteraction] = (0, react.useState)(FRESH);
|
|
179
|
+
const ref = useCaret(interaction.caret, text);
|
|
180
|
+
const handlers = (0, react.useMemo)(() => ({
|
|
181
|
+
onChangeText: (next) => update((current) => {
|
|
182
|
+
const edit = require_formatted_input.applyEdit(current, typedAmount(current, next, currency), (typed) => require_money.formatMoneyInput(typed, currency).text, AMOUNT_CHARACTER);
|
|
183
|
+
setInteraction((state) => ({
|
|
184
|
+
...state,
|
|
185
|
+
caret: edit.caret
|
|
186
|
+
}));
|
|
187
|
+
return edit.text;
|
|
188
|
+
}),
|
|
189
|
+
onBlur: () => {
|
|
190
|
+
setInteraction({
|
|
191
|
+
touched: true,
|
|
192
|
+
caret: null
|
|
193
|
+
});
|
|
194
|
+
update((current) => {
|
|
195
|
+
const { amount } = require_money.formatMoneyInput(current, currency);
|
|
196
|
+
return amount === null ? current : require_money.formatMoney(amount, currency, { display: "none" });
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
reset: () => {
|
|
200
|
+
setInteraction(FRESH);
|
|
201
|
+
update(() => initialMoney(currency, initialValue));
|
|
202
|
+
}
|
|
203
|
+
}), [
|
|
204
|
+
currency,
|
|
205
|
+
initialValue,
|
|
206
|
+
update
|
|
207
|
+
]);
|
|
208
|
+
const { amount } = require_money.formatMoneyInput(text, currency);
|
|
209
|
+
const error = amountError(amount, text, currency, min, max);
|
|
210
|
+
return {
|
|
211
|
+
ref,
|
|
212
|
+
value: text,
|
|
213
|
+
amount: error === null ? amount : null,
|
|
214
|
+
error: interaction.touched ? error : null,
|
|
215
|
+
currency: target,
|
|
216
|
+
onChangeText: handlers.onChangeText,
|
|
217
|
+
onBlur: handlers.onBlur,
|
|
218
|
+
reset: handlers.reset
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const DATE_DIGIT = /\d/;
|
|
222
|
+
function initialDate(initialValue) {
|
|
223
|
+
return initialValue ? require_dates.formatDate(initialValue) : "";
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
227
|
+
* that do not exist or fall outside `min` and `max`. It renders nothing.
|
|
228
|
+
*
|
|
229
|
+
* @throws {RangeError} When `initialValue` is not a valid `YYYY-MM-DD` date.
|
|
230
|
+
* @example
|
|
231
|
+
* const delivery = useDateInput({ min: today('NI') });
|
|
232
|
+
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
233
|
+
*/
|
|
234
|
+
function useDateInput({ initialValue, min, max, value, onChange } = {}) {
|
|
235
|
+
const [text, update] = useFieldValue(value, onChange, () => initialDate(initialValue));
|
|
236
|
+
const [interaction, setInteraction] = (0, react.useState)(FRESH);
|
|
237
|
+
const ref = useCaret(interaction.caret, text);
|
|
238
|
+
const handlers = (0, react.useMemo)(() => ({
|
|
239
|
+
onChangeText: (next) => update((current) => {
|
|
240
|
+
const edit = require_formatted_input.applyEdit(current, next, (typed) => require_dates.formatDateInput(typed).text, DATE_DIGIT);
|
|
241
|
+
setInteraction((state) => ({
|
|
242
|
+
...state,
|
|
243
|
+
caret: edit.caret
|
|
244
|
+
}));
|
|
245
|
+
return edit.text;
|
|
246
|
+
}),
|
|
247
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
248
|
+
...state,
|
|
249
|
+
touched: true
|
|
250
|
+
}),
|
|
251
|
+
reset: () => {
|
|
252
|
+
setInteraction(FRESH);
|
|
253
|
+
update(() => initialDate(initialValue));
|
|
254
|
+
}
|
|
255
|
+
}), [initialValue, update]);
|
|
256
|
+
const { date } = require_dates.formatDateInput(text);
|
|
257
|
+
const error = date === null ? text ? "format" : null : min && date < min ? "min" : max && date > max ? "max" : null;
|
|
258
|
+
return {
|
|
259
|
+
ref,
|
|
260
|
+
value: text,
|
|
261
|
+
date: error === null ? date : null,
|
|
262
|
+
error: interaction.touched ? error : null,
|
|
263
|
+
placeholder: "dd/mm/aaaa",
|
|
264
|
+
onChangeText: handlers.onChangeText,
|
|
265
|
+
onBlur: handlers.onBlur,
|
|
266
|
+
reset: handlers.reset
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
exports.formatDateInput = require_dates.formatDateInput;
|
|
270
|
+
exports.formatMoneyInput = require_money.formatMoneyInput;
|
|
271
|
+
exports.formatPhoneInput = require_phone_input.formatPhoneInput;
|
|
272
|
+
exports.useDateInput = useDateInput;
|
|
273
|
+
exports.useMoneyInput = useMoneyInput;
|
|
274
|
+
exports.usePhoneInput = usePhoneInput;
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { DateInputText, IsoDate, formatDateInput } from "./dates.cjs";
|
|
2
|
+
import { Amount, MoneyInputText, formatMoneyInput } from "./money.cjs";
|
|
3
|
+
import { Phone, PhoneError, PhoneOptions } from "./phone.cjs";
|
|
4
|
+
import { Country, CountryCode, Currency, CurrencyCode } from "./regions.cjs";
|
|
5
|
+
import { PhoneInputText, formatPhoneInput } from "./phone-input.cjs";
|
|
6
|
+
export interface UsePhoneInputOptions extends PhoneOptions {
|
|
7
|
+
/** Country selected at first, and used for numbers typed without `+`. */
|
|
8
|
+
readonly defaultCountry: CountryCode;
|
|
9
|
+
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. Ignored when `value` is set. */
|
|
10
|
+
readonly initialValue?: string | null;
|
|
11
|
+
/** Controlled text, for a field whose value lives in a store or a form library. Pass it with `country` and `onChange`. */
|
|
12
|
+
readonly value?: string;
|
|
13
|
+
/** Controlled country, used with `value`. Falls back to `defaultCountry`. */
|
|
14
|
+
readonly country?: CountryCode | null;
|
|
15
|
+
/** Called with the new text and country after every change, controlled or not. */
|
|
16
|
+
readonly onChange?: (change: PhoneInputChange) => void;
|
|
17
|
+
}
|
|
18
|
+
/** What {@link UsePhoneInputOptions.onChange} receives. */
|
|
19
|
+
export interface PhoneInputChange {
|
|
20
|
+
readonly value: string;
|
|
21
|
+
readonly country: CountryCode;
|
|
22
|
+
}
|
|
23
|
+
/** State and handlers for a phone field. Wire them to a `TextInput` (React Native) or an `<input>` (web). */
|
|
24
|
+
export interface PhoneInput {
|
|
25
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing after each formatting change. */
|
|
26
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
27
|
+
/** Text to show in the field, formatted while typing. */
|
|
28
|
+
readonly value: string;
|
|
29
|
+
/** Selected country. Typing `+` and a calling code selects that country. */
|
|
30
|
+
readonly country: Country;
|
|
31
|
+
/** Countries to offer in the selector, with their flag. */
|
|
32
|
+
readonly countries: readonly Country[];
|
|
33
|
+
/** Example number of the selected country, for the placeholder. */
|
|
34
|
+
readonly placeholder: string;
|
|
35
|
+
/** The valid number, or `null` while the field is empty or invalid. Send `phone.e164`. */
|
|
36
|
+
readonly phone: Phone | null;
|
|
37
|
+
/** Why the number is invalid, shown only after the field lost focus once. `null` when empty or valid. */
|
|
38
|
+
readonly error: PhoneError | null;
|
|
39
|
+
readonly onChangeText: (text: string) => void;
|
|
40
|
+
readonly onBlur: () => void;
|
|
41
|
+
/** Selects a country from {@link PhoneInput.countries}. */
|
|
42
|
+
readonly setCountry: (code: CountryCode) => void;
|
|
43
|
+
/** Goes back to the initial value and hides the error. */
|
|
44
|
+
readonly reset: () => void;
|
|
45
|
+
}
|
|
46
|
+
/** The part of a web `<input>` or a React Native `TextInput` the hooks use to move the caret. */
|
|
47
|
+
export interface SelectableInput {
|
|
48
|
+
setSelectionRange?: (start: number, end: number) => void;
|
|
49
|
+
setSelection?: (start: number, end: number) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
53
|
+
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
54
|
+
* flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
|
|
55
|
+
* The handlers keep the same identity while the options do not change. Pass `value`, `country` and `onChange` to
|
|
56
|
+
* control the field from a store or a form library; otherwise it keeps its own state.
|
|
57
|
+
*
|
|
58
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
59
|
+
* @example
|
|
60
|
+
* const field = usePhoneInput({ defaultCountry: 'NI', type: 'mobile' });
|
|
61
|
+
* <TextInput ref={field.ref} value={field.value} onChangeText={field.onChangeText} onBlur={field.onBlur} placeholder={field.placeholder} />
|
|
62
|
+
*/
|
|
63
|
+
export declare function usePhoneInput(options: UsePhoneInputOptions): PhoneInput;
|
|
64
|
+
/** Why an amount in {@link useMoneyInput} is not accepted. */
|
|
65
|
+
export type MoneyInputError = "format" | "min" | "max";
|
|
66
|
+
export interface UseMoneyInputOptions {
|
|
67
|
+
readonly currency: CurrencyCode;
|
|
68
|
+
/** A stored amount to edit, as decimal text (`"1250.5"`) or a number. */
|
|
69
|
+
readonly initialValue?: Amount | null;
|
|
70
|
+
/** Smallest accepted amount, inclusive. */
|
|
71
|
+
readonly min?: Amount;
|
|
72
|
+
/** Largest accepted amount, inclusive. */
|
|
73
|
+
readonly max?: Amount;
|
|
74
|
+
/** Controlled text shown in the field (`1,250.5`), for a value that lives in a store or a form library. */
|
|
75
|
+
readonly value?: string;
|
|
76
|
+
/** Called with the new text after every change, controlled or not. Read the amount with {@link formatMoneyInput}. */
|
|
77
|
+
readonly onChange?: (value: string) => void;
|
|
78
|
+
}
|
|
79
|
+
/** State and handlers for an amount field. Wire them like {@link PhoneInput}. */
|
|
80
|
+
export interface MoneyInput {
|
|
81
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing. */
|
|
82
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
83
|
+
/** Text to show in the field, grouped while typing (`1,250.5`) and completed on blur (`1,250.50`). */
|
|
84
|
+
readonly value: string;
|
|
85
|
+
/** The amount as decimal text with every decimal of the currency, ready to send, or `null` while empty or not accepted. */
|
|
86
|
+
readonly amount: string | null;
|
|
87
|
+
/** Why the amount is not accepted, shown only after the field lost focus once. `null` when empty or accepted. */
|
|
88
|
+
readonly error: MoneyInputError | null;
|
|
89
|
+
/** The currency, with its `symbol` to show next to the field. */
|
|
90
|
+
readonly currency: Currency;
|
|
91
|
+
readonly onChangeText: (text: string) => void;
|
|
92
|
+
readonly onBlur: () => void;
|
|
93
|
+
/** Goes back to the initial value and hides the error. */
|
|
94
|
+
readonly reset: () => void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* An amount field that groups thousands while typing, keeps the caret where the person is typing, takes `,` from
|
|
98
|
+
* a Spanish decimal keypad as the decimal point, reads pasted amounts in any of the region's formats, and completes
|
|
99
|
+
* the decimals on blur. It renders nothing: show `currency.symbol` and the input with your own components.
|
|
100
|
+
*
|
|
101
|
+
* @throws {RangeError} When the currency is not supported or `initialValue`, `min` or `max` is not an amount.
|
|
102
|
+
* @example
|
|
103
|
+
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
104
|
+
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
105
|
+
*/
|
|
106
|
+
export declare function useMoneyInput({ currency, initialValue, min, max, value, onChange }: UseMoneyInputOptions): MoneyInput;
|
|
107
|
+
/** Why a date in {@link useDateInput} is not accepted. */
|
|
108
|
+
export type DateInputError = "format" | "min" | "max";
|
|
109
|
+
export interface UseDateInputOptions {
|
|
110
|
+
/** A stored date to edit, as `YYYY-MM-DD`. */
|
|
111
|
+
readonly initialValue?: string | null;
|
|
112
|
+
/** Earliest accepted date, inclusive, as `YYYY-MM-DD`; for example {@link today} to refuse past dates. */
|
|
113
|
+
readonly min?: string;
|
|
114
|
+
/** Latest accepted date, inclusive, as `YYYY-MM-DD`. */
|
|
115
|
+
readonly max?: string;
|
|
116
|
+
/** Controlled text shown in the field (`24/11/2026`), for a value that lives in a store or a form library. */
|
|
117
|
+
readonly value?: string;
|
|
118
|
+
/** Called with the new text after every change, controlled or not. Read the date with {@link formatDateInput}. */
|
|
119
|
+
readonly onChange?: (value: string) => void;
|
|
120
|
+
}
|
|
121
|
+
/** State and handlers for a `dd/mm/aaaa` field. Wire them like {@link PhoneInput}. */
|
|
122
|
+
export interface DateInput {
|
|
123
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing. */
|
|
124
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
125
|
+
/** Text to show in the field, with `/` added while typing. */
|
|
126
|
+
readonly value: string;
|
|
127
|
+
/** The date as `YYYY-MM-DD`, ready to send, or `null` while empty, incomplete or not accepted. */
|
|
128
|
+
readonly date: IsoDate | null;
|
|
129
|
+
/** Why the date is not accepted, shown only after the field lost focus once. `null` when empty or accepted. */
|
|
130
|
+
readonly error: DateInputError | null;
|
|
131
|
+
/** `dd/mm/aaaa`, for the placeholder. */
|
|
132
|
+
readonly placeholder: string;
|
|
133
|
+
readonly onChangeText: (text: string) => void;
|
|
134
|
+
readonly onBlur: () => void;
|
|
135
|
+
/** Goes back to the initial value and hides the error. */
|
|
136
|
+
readonly reset: () => void;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
140
|
+
* that do not exist or fall outside `min` and `max`. It renders nothing.
|
|
141
|
+
*
|
|
142
|
+
* @throws {RangeError} When `initialValue` is not a valid `YYYY-MM-DD` date.
|
|
143
|
+
* @example
|
|
144
|
+
* const delivery = useDateInput({ min: today('NI') });
|
|
145
|
+
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
146
|
+
*/
|
|
147
|
+
export declare function useDateInput({ initialValue, min, max, value, onChange }?: UseDateInputOptions): DateInput;
|
|
148
|
+
export { type DateInputText, type MoneyInputText, type PhoneInputText, formatDateInput, formatMoneyInput, formatPhoneInput };
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { DateInputText, IsoDate, formatDateInput } from "./dates.js";
|
|
2
|
+
import { Amount, MoneyInputText, formatMoneyInput } from "./money.js";
|
|
3
|
+
import { Phone, PhoneError, PhoneOptions } from "./phone.js";
|
|
4
|
+
import { Country, CountryCode, Currency, CurrencyCode } from "./regions.js";
|
|
5
|
+
import { PhoneInputText, formatPhoneInput } from "./phone-input.js";
|
|
6
|
+
export interface UsePhoneInputOptions extends PhoneOptions {
|
|
7
|
+
/** Country selected at first, and used for numbers typed without `+`. */
|
|
8
|
+
readonly defaultCountry: CountryCode;
|
|
9
|
+
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. Ignored when `value` is set. */
|
|
10
|
+
readonly initialValue?: string | null;
|
|
11
|
+
/** Controlled text, for a field whose value lives in a store or a form library. Pass it with `country` and `onChange`. */
|
|
12
|
+
readonly value?: string;
|
|
13
|
+
/** Controlled country, used with `value`. Falls back to `defaultCountry`. */
|
|
14
|
+
readonly country?: CountryCode | null;
|
|
15
|
+
/** Called with the new text and country after every change, controlled or not. */
|
|
16
|
+
readonly onChange?: (change: PhoneInputChange) => void;
|
|
17
|
+
}
|
|
18
|
+
/** What {@link UsePhoneInputOptions.onChange} receives. */
|
|
19
|
+
export interface PhoneInputChange {
|
|
20
|
+
readonly value: string;
|
|
21
|
+
readonly country: CountryCode;
|
|
22
|
+
}
|
|
23
|
+
/** State and handlers for a phone field. Wire them to a `TextInput` (React Native) or an `<input>` (web). */
|
|
24
|
+
export interface PhoneInput {
|
|
25
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing after each formatting change. */
|
|
26
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
27
|
+
/** Text to show in the field, formatted while typing. */
|
|
28
|
+
readonly value: string;
|
|
29
|
+
/** Selected country. Typing `+` and a calling code selects that country. */
|
|
30
|
+
readonly country: Country;
|
|
31
|
+
/** Countries to offer in the selector, with their flag. */
|
|
32
|
+
readonly countries: readonly Country[];
|
|
33
|
+
/** Example number of the selected country, for the placeholder. */
|
|
34
|
+
readonly placeholder: string;
|
|
35
|
+
/** The valid number, or `null` while the field is empty or invalid. Send `phone.e164`. */
|
|
36
|
+
readonly phone: Phone | null;
|
|
37
|
+
/** Why the number is invalid, shown only after the field lost focus once. `null` when empty or valid. */
|
|
38
|
+
readonly error: PhoneError | null;
|
|
39
|
+
readonly onChangeText: (text: string) => void;
|
|
40
|
+
readonly onBlur: () => void;
|
|
41
|
+
/** Selects a country from {@link PhoneInput.countries}. */
|
|
42
|
+
readonly setCountry: (code: CountryCode) => void;
|
|
43
|
+
/** Goes back to the initial value and hides the error. */
|
|
44
|
+
readonly reset: () => void;
|
|
45
|
+
}
|
|
46
|
+
/** The part of a web `<input>` or a React Native `TextInput` the hooks use to move the caret. */
|
|
47
|
+
export interface SelectableInput {
|
|
48
|
+
setSelectionRange?: (start: number, end: number) => void;
|
|
49
|
+
setSelection?: (start: number, end: number) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
53
|
+
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
54
|
+
* flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
|
|
55
|
+
* The handlers keep the same identity while the options do not change. Pass `value`, `country` and `onChange` to
|
|
56
|
+
* control the field from a store or a form library; otherwise it keeps its own state.
|
|
57
|
+
*
|
|
58
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
59
|
+
* @example
|
|
60
|
+
* const field = usePhoneInput({ defaultCountry: 'NI', type: 'mobile' });
|
|
61
|
+
* <TextInput ref={field.ref} value={field.value} onChangeText={field.onChangeText} onBlur={field.onBlur} placeholder={field.placeholder} />
|
|
62
|
+
*/
|
|
63
|
+
export declare function usePhoneInput(options: UsePhoneInputOptions): PhoneInput;
|
|
64
|
+
/** Why an amount in {@link useMoneyInput} is not accepted. */
|
|
65
|
+
export type MoneyInputError = "format" | "min" | "max";
|
|
66
|
+
export interface UseMoneyInputOptions {
|
|
67
|
+
readonly currency: CurrencyCode;
|
|
68
|
+
/** A stored amount to edit, as decimal text (`"1250.5"`) or a number. */
|
|
69
|
+
readonly initialValue?: Amount | null;
|
|
70
|
+
/** Smallest accepted amount, inclusive. */
|
|
71
|
+
readonly min?: Amount;
|
|
72
|
+
/** Largest accepted amount, inclusive. */
|
|
73
|
+
readonly max?: Amount;
|
|
74
|
+
/** Controlled text shown in the field (`1,250.5`), for a value that lives in a store or a form library. */
|
|
75
|
+
readonly value?: string;
|
|
76
|
+
/** Called with the new text after every change, controlled or not. Read the amount with {@link formatMoneyInput}. */
|
|
77
|
+
readonly onChange?: (value: string) => void;
|
|
78
|
+
}
|
|
79
|
+
/** State and handlers for an amount field. Wire them like {@link PhoneInput}. */
|
|
80
|
+
export interface MoneyInput {
|
|
81
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing. */
|
|
82
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
83
|
+
/** Text to show in the field, grouped while typing (`1,250.5`) and completed on blur (`1,250.50`). */
|
|
84
|
+
readonly value: string;
|
|
85
|
+
/** The amount as decimal text with every decimal of the currency, ready to send, or `null` while empty or not accepted. */
|
|
86
|
+
readonly amount: string | null;
|
|
87
|
+
/** Why the amount is not accepted, shown only after the field lost focus once. `null` when empty or accepted. */
|
|
88
|
+
readonly error: MoneyInputError | null;
|
|
89
|
+
/** The currency, with its `symbol` to show next to the field. */
|
|
90
|
+
readonly currency: Currency;
|
|
91
|
+
readonly onChangeText: (text: string) => void;
|
|
92
|
+
readonly onBlur: () => void;
|
|
93
|
+
/** Goes back to the initial value and hides the error. */
|
|
94
|
+
readonly reset: () => void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* An amount field that groups thousands while typing, keeps the caret where the person is typing, takes `,` from
|
|
98
|
+
* a Spanish decimal keypad as the decimal point, reads pasted amounts in any of the region's formats, and completes
|
|
99
|
+
* the decimals on blur. It renders nothing: show `currency.symbol` and the input with your own components.
|
|
100
|
+
*
|
|
101
|
+
* @throws {RangeError} When the currency is not supported or `initialValue`, `min` or `max` is not an amount.
|
|
102
|
+
* @example
|
|
103
|
+
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
104
|
+
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
105
|
+
*/
|
|
106
|
+
export declare function useMoneyInput({ currency, initialValue, min, max, value, onChange }: UseMoneyInputOptions): MoneyInput;
|
|
107
|
+
/** Why a date in {@link useDateInput} is not accepted. */
|
|
108
|
+
export type DateInputError = "format" | "min" | "max";
|
|
109
|
+
export interface UseDateInputOptions {
|
|
110
|
+
/** A stored date to edit, as `YYYY-MM-DD`. */
|
|
111
|
+
readonly initialValue?: string | null;
|
|
112
|
+
/** Earliest accepted date, inclusive, as `YYYY-MM-DD`; for example {@link today} to refuse past dates. */
|
|
113
|
+
readonly min?: string;
|
|
114
|
+
/** Latest accepted date, inclusive, as `YYYY-MM-DD`. */
|
|
115
|
+
readonly max?: string;
|
|
116
|
+
/** Controlled text shown in the field (`24/11/2026`), for a value that lives in a store or a form library. */
|
|
117
|
+
readonly value?: string;
|
|
118
|
+
/** Called with the new text after every change, controlled or not. Read the date with {@link formatDateInput}. */
|
|
119
|
+
readonly onChange?: (value: string) => void;
|
|
120
|
+
}
|
|
121
|
+
/** State and handlers for a `dd/mm/aaaa` field. Wire them like {@link PhoneInput}. */
|
|
122
|
+
export interface DateInput {
|
|
123
|
+
/** Pass to the input's `ref` so the caret stays where the person is typing. */
|
|
124
|
+
readonly ref: (element: SelectableInput | null) => void;
|
|
125
|
+
/** Text to show in the field, with `/` added while typing. */
|
|
126
|
+
readonly value: string;
|
|
127
|
+
/** The date as `YYYY-MM-DD`, ready to send, or `null` while empty, incomplete or not accepted. */
|
|
128
|
+
readonly date: IsoDate | null;
|
|
129
|
+
/** Why the date is not accepted, shown only after the field lost focus once. `null` when empty or accepted. */
|
|
130
|
+
readonly error: DateInputError | null;
|
|
131
|
+
/** `dd/mm/aaaa`, for the placeholder. */
|
|
132
|
+
readonly placeholder: string;
|
|
133
|
+
readonly onChangeText: (text: string) => void;
|
|
134
|
+
readonly onBlur: () => void;
|
|
135
|
+
/** Goes back to the initial value and hides the error. */
|
|
136
|
+
readonly reset: () => void;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
140
|
+
* that do not exist or fall outside `min` and `max`. It renders nothing.
|
|
141
|
+
*
|
|
142
|
+
* @throws {RangeError} When `initialValue` is not a valid `YYYY-MM-DD` date.
|
|
143
|
+
* @example
|
|
144
|
+
* const delivery = useDateInput({ min: today('NI') });
|
|
145
|
+
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
146
|
+
*/
|
|
147
|
+
export declare function useDateInput({ initialValue, min, max, value, onChange }?: UseDateInputOptions): DateInput;
|
|
148
|
+
export { type DateInputText, type MoneyInputText, type PhoneInputText, formatDateInput, formatMoneyInput, formatPhoneInput };
|