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