@momotombodevs/istmo 0.1.0 → 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 +8 -1
- package/README.md +19 -3
- package/dist/react.cjs +121 -85
- package/dist/react.d.cts +24 -4
- package/dist/react.d.ts +24 -4
- package/dist/react.js +121 -85
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,12 @@ Formato basado en [Keep a Changelog](https://keepachangelog.com/es-ES/1.1.0/); v
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.2.0]
|
|
8
|
+
|
|
9
|
+
### Añadido
|
|
10
|
+
|
|
11
|
+
- Modo controlado en `usePhoneInput`, `useMoneyInput` y `useDateInput`: `value` y `onChange` (y `country` en el teléfono) para campos cuyo valor vive en un store o una librería de formularios. Sin `value` siguen guardando su propio estado, y `onChange` avisa de cada cambio en los dos modos.
|
|
12
|
+
|
|
7
13
|
## [0.1.0]
|
|
8
14
|
|
|
9
15
|
### Añadido
|
|
@@ -15,5 +21,6 @@ Formato basado en [Keep a Changelog](https://keepachangelog.com/es-ES/1.1.0/); v
|
|
|
15
21
|
- Campos con React en `@momotombodevs/istmo/react`: `usePhoneInput`, `useMoneyInput` y `useDateInput`, con formato mientras se escribe y el cursor en su lugar. `formatPhoneInput` en `/phone-input`, `phoneExample` y la bandera de cada país (`country.flag`).
|
|
16
22
|
- Subrutas `@momotombodevs/istmo/phone`, `/phone-input`, `/money`, `/dates`, `/regions` y `/react` para no cargar lo que no se usa en Metro.
|
|
17
23
|
|
|
18
|
-
[Unreleased]: https://github.com/momotombodevs/istmo-js/compare/v0.
|
|
24
|
+
[Unreleased]: https://github.com/momotombodevs/istmo-js/compare/v0.2.0...HEAD
|
|
25
|
+
[0.2.0]: https://github.com/momotombodevs/istmo-js/compare/v0.1.0...v0.2.0
|
|
19
26
|
[0.1.0]: https://github.com/momotombodevs/istmo-js/releases/tag/v0.1.0
|
package/README.md
CHANGED
|
@@ -215,7 +215,23 @@ export function PhoneField() {
|
|
|
215
215
|
| `error` | el motivo, solo después de que el campo perdió el foco una vez; `null` si está vacío o es válido |
|
|
216
216
|
| `reset()` | vuelve al valor inicial y oculta el error |
|
|
217
217
|
|
|
218
|
-
Opciones: `defaultCountry` (obligatoria), `type` y `countries` como en `validatePhone`,
|
|
218
|
+
Opciones: `defaultCountry` (obligatoria), `type` y `countries` como en `validatePhone`, `initialValue` con un número E.164 guardado para editarlo (`'+50581234567'`), y `value`, `country` y `onChange` para controlarlo desde afuera. El campo vacío no es un error: si el teléfono es obligatorio, compruebe `phone !== null` al enviar.
|
|
219
|
+
|
|
220
|
+
### Con un store o una librería de formularios
|
|
221
|
+
|
|
222
|
+
Si el valor vive en otro lado (zustand, Redux, React Hook Form), pase `value` y `onChange`, y en el teléfono también `country`. El hook sigue formateando y cuidando el cursor, pero el valor es el suyo:
|
|
223
|
+
|
|
224
|
+
```tsx
|
|
225
|
+
const { phone, phoneCountry, setPhone } = useCustomerStore();
|
|
226
|
+
const field = usePhoneInput({
|
|
227
|
+
defaultCountry: 'NI',
|
|
228
|
+
value: phone,
|
|
229
|
+
country: phoneCountry,
|
|
230
|
+
onChange: ({ value, country }) => setPhone(value, country),
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
`useMoneyInput` y `useDateInput` aceptan `value` y `onChange` con el texto del campo; el monto y la fecha se leen con `formatMoneyInput` y `formatDateInput`. Sin `value`, el hook guarda su propio estado y `onChange` solo avisa de cada cambio.
|
|
219
235
|
|
|
220
236
|
Las banderas son emoji (`country.flag`). Se ven en iOS, Android y macOS; los navegadores de Windows muestran las dos letras del código en su lugar. Para la web, el paquete trae las 7 banderas como SVG en `@momotombodevs/istmo/flags/<código>.svg`, tomadas de [flag-icons](https://github.com/lipis/flag-icons) (licencia MIT, incluida en `flags/LICENSE`). Son archivos, no JavaScript: Vite los importa como una URL y el navegador los carga aparte.
|
|
221
237
|
|
|
@@ -408,8 +424,8 @@ export function DeliveryFields() {
|
|
|
408
424
|
|
|
409
425
|
| Hook | Opciones | Devuelve |
|
|
410
426
|
|------|----------|----------|
|
|
411
|
-
| `useMoneyInput` | `currency`, `initialValue`, `min`, `max` | `amount` (`"1250.50"`, listo para enviar), `currency` (con `symbol`), `error`: `format`, `min` o `max` |
|
|
412
|
-
| `useDateInput` | `initialValue` (`YYYY-MM-DD`), `min`, `max` | `date` (`YYYY-MM-DD`), `placeholder` (`dd/mm/aaaa`), `error`: `format`, `min` o `max` |
|
|
427
|
+
| `useMoneyInput` | `currency`, `initialValue`, `min`, `max`, `value`, `onChange` | `amount` (`"1250.50"`, listo para enviar), `currency` (con `symbol`), `error`: `format`, `min` o `max` |
|
|
428
|
+
| `useDateInput` | `initialValue` (`YYYY-MM-DD`), `min`, `max`, `value`, `onChange` | `date` (`YYYY-MM-DD`), `placeholder` (`dd/mm/aaaa`), `error`: `format`, `min` o `max` |
|
|
413
429
|
|
|
414
430
|
`useMoneyInput` toma la `,` del teclado decimal en español como punto decimal (en Android, el teclado `decimal-pad` muestra `,` según el idioma), lee un monto pegado en cualquiera de las tres formas y completa los decimales al salir del campo (`1,250.5` → `1,250.50`).
|
|
415
431
|
|
package/dist/react.cjs
CHANGED
|
@@ -8,23 +8,46 @@ const require_phone_input = require("./phone-input.cjs");
|
|
|
8
8
|
const require_regions = require("./regions.cjs");
|
|
9
9
|
const require_require = require("./require.cjs");
|
|
10
10
|
// src/react.ts
|
|
11
|
+
const FRESH = {
|
|
12
|
+
touched: false,
|
|
13
|
+
caret: null
|
|
14
|
+
};
|
|
11
15
|
const DIGIT = /\d/;
|
|
12
16
|
const useIsomorphicLayoutEffect = "window" in globalThis ? react.useLayoutEffect : react.useEffect;
|
|
13
17
|
const digitsOf = (text) => text.replace(/\D/g, "");
|
|
14
|
-
function
|
|
18
|
+
function initialPhone(defaultCountry, initialValue, countries) {
|
|
15
19
|
const stored = initialValue ? require_phone.parsePhone(initialValue, defaultCountry, { countries }) : null;
|
|
16
20
|
return stored ? {
|
|
17
|
-
|
|
18
|
-
country: stored.country
|
|
19
|
-
touched: false,
|
|
20
|
-
caret: null
|
|
21
|
+
value: stored.national,
|
|
22
|
+
country: stored.country
|
|
21
23
|
} : {
|
|
22
|
-
|
|
23
|
-
country: require_require.requireCountry(defaultCountry).code
|
|
24
|
-
touched: false,
|
|
25
|
-
caret: null
|
|
24
|
+
value: "",
|
|
25
|
+
country: require_require.requireCountry(defaultCountry).code
|
|
26
26
|
};
|
|
27
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
|
+
}
|
|
28
51
|
function useCaret(caret, text) {
|
|
29
52
|
const element = (0, react.useRef)(null);
|
|
30
53
|
useIsomorphicLayoutEffect(() => {
|
|
@@ -40,7 +63,8 @@ function useCaret(caret, text) {
|
|
|
40
63
|
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
41
64
|
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
42
65
|
* 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.
|
|
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.
|
|
44
68
|
*
|
|
45
69
|
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
46
70
|
* @example
|
|
@@ -50,64 +74,70 @@ function useCaret(caret, text) {
|
|
|
50
74
|
function usePhoneInput(options) {
|
|
51
75
|
const { defaultCountry, initialValue, type } = options;
|
|
52
76
|
const allowedKey = (options.countries ?? require_regions.countryCodes).join(",");
|
|
53
|
-
const [
|
|
54
|
-
|
|
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);
|
|
55
83
|
const handlers = (0, react.useMemo)(() => {
|
|
56
84
|
const allowed = allowedKey.split(",");
|
|
57
|
-
const initial = () => initialState({
|
|
58
|
-
defaultCountry,
|
|
59
|
-
initialValue,
|
|
60
|
-
countries: allowed
|
|
61
|
-
});
|
|
62
85
|
return {
|
|
63
86
|
allowed,
|
|
64
|
-
onChangeText: (next) =>
|
|
87
|
+
onChangeText: (next) => update((current) => {
|
|
65
88
|
let detected;
|
|
66
|
-
const edit = require_formatted_input.applyEdit(current.
|
|
89
|
+
const edit = require_formatted_input.applyEdit(current.value, next, (typed) => {
|
|
67
90
|
const formatted = require_phone_input.formatPhoneInput(typed, current.country);
|
|
68
91
|
detected = formatted.country;
|
|
69
92
|
return formatted.text;
|
|
70
93
|
}, DIGIT);
|
|
71
|
-
|
|
94
|
+
setInteraction((state) => ({
|
|
95
|
+
...state,
|
|
96
|
+
caret: edit.caret
|
|
97
|
+
}));
|
|
72
98
|
return {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
caret: edit.caret,
|
|
76
|
-
country
|
|
99
|
+
value: edit.text,
|
|
100
|
+
country: detected && allowed.includes(detected) ? detected : current.country
|
|
77
101
|
};
|
|
78
102
|
}),
|
|
79
|
-
onBlur: () =>
|
|
80
|
-
...
|
|
103
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
104
|
+
...state,
|
|
81
105
|
touched: true
|
|
82
106
|
}),
|
|
83
107
|
setCountry: (code) => {
|
|
84
108
|
if (!allowed.includes(code)) throw new RangeError("País fuera de la lista countries del campo.");
|
|
85
|
-
|
|
86
|
-
...
|
|
87
|
-
country: code,
|
|
88
|
-
text: require_phone_input.formatPhoneInput(current.text, code).text,
|
|
109
|
+
setInteraction((state) => ({
|
|
110
|
+
...state,
|
|
89
111
|
caret: null
|
|
90
112
|
}));
|
|
113
|
+
update((current) => ({
|
|
114
|
+
value: require_phone_input.formatPhoneInput(current.value, code).text,
|
|
115
|
+
country: code
|
|
116
|
+
}));
|
|
91
117
|
},
|
|
92
|
-
reset: () =>
|
|
118
|
+
reset: () => {
|
|
119
|
+
setInteraction(FRESH);
|
|
120
|
+
update(() => initialPhone(defaultCountry, initialValue, allowed));
|
|
121
|
+
}
|
|
93
122
|
};
|
|
94
123
|
}, [
|
|
95
124
|
allowedKey,
|
|
96
125
|
defaultCountry,
|
|
97
|
-
initialValue
|
|
126
|
+
initialValue,
|
|
127
|
+
update
|
|
98
128
|
]);
|
|
99
|
-
const validation = digitsOf(
|
|
129
|
+
const validation = digitsOf(field.value) ? require_phone.validatePhone(field.value, field.country, {
|
|
100
130
|
type,
|
|
101
131
|
countries: handlers.allowed
|
|
102
132
|
}) : null;
|
|
103
133
|
return {
|
|
104
134
|
ref,
|
|
105
|
-
value:
|
|
106
|
-
country: require_require.requireCountry(
|
|
135
|
+
value: field.value,
|
|
136
|
+
country: require_require.requireCountry(field.country),
|
|
107
137
|
countries: require_regions.countries.filter(({ code }) => handlers.allowed.includes(code)),
|
|
108
|
-
placeholder: require_phone.phoneExample(
|
|
138
|
+
placeholder: require_phone.phoneExample(field.country).national,
|
|
109
139
|
phone: validation?.valid ? validation.phone : null,
|
|
110
|
-
error:
|
|
140
|
+
error: interaction.touched && validation && !validation.valid ? validation.error : null,
|
|
111
141
|
onChangeText: handlers.onChangeText,
|
|
112
142
|
onBlur: handlers.onBlur,
|
|
113
143
|
setCountry: handlers.setCountry,
|
|
@@ -116,11 +146,7 @@ function usePhoneInput(options) {
|
|
|
116
146
|
}
|
|
117
147
|
const AMOUNT_CHARACTER = /[\d.]/;
|
|
118
148
|
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
|
-
};
|
|
149
|
+
return initialValue === null || initialValue === void 0 ? "" : require_money.formatMoney(initialValue, currency, { display: "none" });
|
|
124
150
|
}
|
|
125
151
|
const FIELD_FORMAT = /^[\d,]*(?:\.\d*)?$/;
|
|
126
152
|
function typedAmount(previous, next, currency) {
|
|
@@ -146,36 +172,46 @@ function amountError(amount, text, currency, min, max) {
|
|
|
146
172
|
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
147
173
|
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
148
174
|
*/
|
|
149
|
-
function useMoneyInput({ currency, initialValue, min, max }) {
|
|
175
|
+
function useMoneyInput({ currency, initialValue, min, max, value, onChange }) {
|
|
150
176
|
const target = require_require.requireCurrency(currency);
|
|
151
|
-
const [
|
|
152
|
-
const
|
|
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);
|
|
153
180
|
const handlers = (0, react.useMemo)(() => ({
|
|
154
|
-
onChangeText: (next) =>
|
|
155
|
-
const edit = require_formatted_input.applyEdit(current
|
|
156
|
-
|
|
157
|
-
...
|
|
158
|
-
text: edit.text,
|
|
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,
|
|
159
185
|
caret: edit.caret
|
|
160
|
-
};
|
|
186
|
+
}));
|
|
187
|
+
return edit.text;
|
|
161
188
|
}),
|
|
162
|
-
onBlur: () =>
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
text: amount === null ? current.text : require_money.formatMoney(amount, currency, { display: "none" }),
|
|
189
|
+
onBlur: () => {
|
|
190
|
+
setInteraction({
|
|
166
191
|
touched: true,
|
|
167
192
|
caret: null
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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);
|
|
174
210
|
return {
|
|
175
211
|
ref,
|
|
176
|
-
value:
|
|
212
|
+
value: text,
|
|
177
213
|
amount: error === null ? amount : null,
|
|
178
|
-
error:
|
|
214
|
+
error: interaction.touched ? error : null,
|
|
179
215
|
currency: target,
|
|
180
216
|
onChangeText: handlers.onChangeText,
|
|
181
217
|
onBlur: handlers.onBlur,
|
|
@@ -184,11 +220,7 @@ function useMoneyInput({ currency, initialValue, min, max }) {
|
|
|
184
220
|
}
|
|
185
221
|
const DATE_DIGIT = /\d/;
|
|
186
222
|
function initialDate(initialValue) {
|
|
187
|
-
return
|
|
188
|
-
text: initialValue ? require_dates.formatDate(initialValue) : "",
|
|
189
|
-
touched: false,
|
|
190
|
-
caret: null
|
|
191
|
-
};
|
|
223
|
+
return initialValue ? require_dates.formatDate(initialValue) : "";
|
|
192
224
|
}
|
|
193
225
|
/**
|
|
194
226
|
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
@@ -199,31 +231,35 @@ function initialDate(initialValue) {
|
|
|
199
231
|
* const delivery = useDateInput({ min: today('NI') });
|
|
200
232
|
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
201
233
|
*/
|
|
202
|
-
function useDateInput({ initialValue, min, max } = {}) {
|
|
203
|
-
const [
|
|
204
|
-
const
|
|
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);
|
|
205
238
|
const handlers = (0, react.useMemo)(() => ({
|
|
206
|
-
onChangeText: (next) =>
|
|
207
|
-
const edit = require_formatted_input.applyEdit(current
|
|
208
|
-
|
|
209
|
-
...
|
|
210
|
-
text: edit.text,
|
|
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,
|
|
211
243
|
caret: edit.caret
|
|
212
|
-
};
|
|
244
|
+
}));
|
|
245
|
+
return edit.text;
|
|
213
246
|
}),
|
|
214
|
-
onBlur: () =>
|
|
215
|
-
...
|
|
247
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
248
|
+
...state,
|
|
216
249
|
touched: true
|
|
217
250
|
}),
|
|
218
|
-
reset: () =>
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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;
|
|
222
258
|
return {
|
|
223
259
|
ref,
|
|
224
|
-
value:
|
|
260
|
+
value: text,
|
|
225
261
|
date: error === null ? date : null,
|
|
226
|
-
error:
|
|
262
|
+
error: interaction.touched ? error : null,
|
|
227
263
|
placeholder: "dd/mm/aaaa",
|
|
228
264
|
onChangeText: handlers.onChangeText,
|
|
229
265
|
onBlur: handlers.onBlur,
|
package/dist/react.d.cts
CHANGED
|
@@ -6,8 +6,19 @@ import { PhoneInputText, formatPhoneInput } from "./phone-input.cjs";
|
|
|
6
6
|
export interface UsePhoneInputOptions extends PhoneOptions {
|
|
7
7
|
/** Country selected at first, and used for numbers typed without `+`. */
|
|
8
8
|
readonly defaultCountry: CountryCode;
|
|
9
|
-
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. */
|
|
9
|
+
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. Ignored when `value` is set. */
|
|
10
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;
|
|
11
22
|
}
|
|
12
23
|
/** State and handlers for a phone field. Wire them to a `TextInput` (React Native) or an `<input>` (web). */
|
|
13
24
|
export interface PhoneInput {
|
|
@@ -41,7 +52,8 @@ export interface SelectableInput {
|
|
|
41
52
|
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
42
53
|
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
43
54
|
* 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.
|
|
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.
|
|
45
57
|
*
|
|
46
58
|
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
47
59
|
* @example
|
|
@@ -59,6 +71,10 @@ export interface UseMoneyInputOptions {
|
|
|
59
71
|
readonly min?: Amount;
|
|
60
72
|
/** Largest accepted amount, inclusive. */
|
|
61
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;
|
|
62
78
|
}
|
|
63
79
|
/** State and handlers for an amount field. Wire them like {@link PhoneInput}. */
|
|
64
80
|
export interface MoneyInput {
|
|
@@ -87,7 +103,7 @@ export interface MoneyInput {
|
|
|
87
103
|
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
88
104
|
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
89
105
|
*/
|
|
90
|
-
export declare function useMoneyInput({ currency, initialValue, min, max }: UseMoneyInputOptions): MoneyInput;
|
|
106
|
+
export declare function useMoneyInput({ currency, initialValue, min, max, value, onChange }: UseMoneyInputOptions): MoneyInput;
|
|
91
107
|
/** Why a date in {@link useDateInput} is not accepted. */
|
|
92
108
|
export type DateInputError = "format" | "min" | "max";
|
|
93
109
|
export interface UseDateInputOptions {
|
|
@@ -97,6 +113,10 @@ export interface UseDateInputOptions {
|
|
|
97
113
|
readonly min?: string;
|
|
98
114
|
/** Latest accepted date, inclusive, as `YYYY-MM-DD`. */
|
|
99
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;
|
|
100
120
|
}
|
|
101
121
|
/** State and handlers for a `dd/mm/aaaa` field. Wire them like {@link PhoneInput}. */
|
|
102
122
|
export interface DateInput {
|
|
@@ -124,5 +144,5 @@ export interface DateInput {
|
|
|
124
144
|
* const delivery = useDateInput({ min: today('NI') });
|
|
125
145
|
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
126
146
|
*/
|
|
127
|
-
export declare function useDateInput({ initialValue, min, max }?: UseDateInputOptions): DateInput;
|
|
147
|
+
export declare function useDateInput({ initialValue, min, max, value, onChange }?: UseDateInputOptions): DateInput;
|
|
128
148
|
export { type DateInputText, type MoneyInputText, type PhoneInputText, formatDateInput, formatMoneyInput, formatPhoneInput };
|
package/dist/react.d.ts
CHANGED
|
@@ -6,8 +6,19 @@ import { PhoneInputText, formatPhoneInput } from "./phone-input.js";
|
|
|
6
6
|
export interface UsePhoneInputOptions extends PhoneOptions {
|
|
7
7
|
/** Country selected at first, and used for numbers typed without `+`. */
|
|
8
8
|
readonly defaultCountry: CountryCode;
|
|
9
|
-
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. */
|
|
9
|
+
/** A stored number to edit, in E.164 (`+50581234567`). Its country becomes the selected one. Ignored when `value` is set. */
|
|
10
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;
|
|
11
22
|
}
|
|
12
23
|
/** State and handlers for a phone field. Wire them to a `TextInput` (React Native) or an `<input>` (web). */
|
|
13
24
|
export interface PhoneInput {
|
|
@@ -41,7 +52,8 @@ export interface SelectableInput {
|
|
|
41
52
|
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
42
53
|
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
43
54
|
* 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.
|
|
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.
|
|
45
57
|
*
|
|
46
58
|
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
47
59
|
* @example
|
|
@@ -59,6 +71,10 @@ export interface UseMoneyInputOptions {
|
|
|
59
71
|
readonly min?: Amount;
|
|
60
72
|
/** Largest accepted amount, inclusive. */
|
|
61
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;
|
|
62
78
|
}
|
|
63
79
|
/** State and handlers for an amount field. Wire them like {@link PhoneInput}. */
|
|
64
80
|
export interface MoneyInput {
|
|
@@ -87,7 +103,7 @@ export interface MoneyInput {
|
|
|
87
103
|
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
88
104
|
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
89
105
|
*/
|
|
90
|
-
export declare function useMoneyInput({ currency, initialValue, min, max }: UseMoneyInputOptions): MoneyInput;
|
|
106
|
+
export declare function useMoneyInput({ currency, initialValue, min, max, value, onChange }: UseMoneyInputOptions): MoneyInput;
|
|
91
107
|
/** Why a date in {@link useDateInput} is not accepted. */
|
|
92
108
|
export type DateInputError = "format" | "min" | "max";
|
|
93
109
|
export interface UseDateInputOptions {
|
|
@@ -97,6 +113,10 @@ export interface UseDateInputOptions {
|
|
|
97
113
|
readonly min?: string;
|
|
98
114
|
/** Latest accepted date, inclusive, as `YYYY-MM-DD`. */
|
|
99
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;
|
|
100
120
|
}
|
|
101
121
|
/** State and handlers for a `dd/mm/aaaa` field. Wire them like {@link PhoneInput}. */
|
|
102
122
|
export interface DateInput {
|
|
@@ -124,5 +144,5 @@ export interface DateInput {
|
|
|
124
144
|
* const delivery = useDateInput({ min: today('NI') });
|
|
125
145
|
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
126
146
|
*/
|
|
127
|
-
export declare function useDateInput({ initialValue, min, max }?: UseDateInputOptions): DateInput;
|
|
147
|
+
export declare function useDateInput({ initialValue, min, max, value, onChange }?: UseDateInputOptions): DateInput;
|
|
128
148
|
export { type DateInputText, type MoneyInputText, type PhoneInputText, formatDateInput, formatMoneyInput, formatPhoneInput };
|
package/dist/react.js
CHANGED
|
@@ -7,23 +7,46 @@ import { formatPhoneInput } from "./phone-input.js";
|
|
|
7
7
|
import { countries, countryCodes } from "./regions.js";
|
|
8
8
|
import { requireCountry, requireCurrency } from "./require.js";
|
|
9
9
|
// src/react.ts
|
|
10
|
+
const FRESH = {
|
|
11
|
+
touched: false,
|
|
12
|
+
caret: null
|
|
13
|
+
};
|
|
10
14
|
const DIGIT = /\d/;
|
|
11
15
|
const useIsomorphicLayoutEffect = "window" in globalThis ? useLayoutEffect : useEffect;
|
|
12
16
|
const digitsOf = (text) => text.replace(/\D/g, "");
|
|
13
|
-
function
|
|
17
|
+
function initialPhone(defaultCountry, initialValue, countries) {
|
|
14
18
|
const stored = initialValue ? parsePhone(initialValue, defaultCountry, { countries }) : null;
|
|
15
19
|
return stored ? {
|
|
16
|
-
|
|
17
|
-
country: stored.country
|
|
18
|
-
touched: false,
|
|
19
|
-
caret: null
|
|
20
|
+
value: stored.national,
|
|
21
|
+
country: stored.country
|
|
20
22
|
} : {
|
|
21
|
-
|
|
22
|
-
country: requireCountry(defaultCountry).code
|
|
23
|
-
touched: false,
|
|
24
|
-
caret: null
|
|
23
|
+
value: "",
|
|
24
|
+
country: requireCountry(defaultCountry).code
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
+
function useFieldValue(controlled, onChange, initial) {
|
|
28
|
+
const [own, setOwn] = useState(initial);
|
|
29
|
+
const value = controlled === void 0 ? own : controlled;
|
|
30
|
+
const latest = useRef({
|
|
31
|
+
value,
|
|
32
|
+
onChange,
|
|
33
|
+
controlled: controlled !== void 0
|
|
34
|
+
});
|
|
35
|
+
useIsomorphicLayoutEffect(() => {
|
|
36
|
+
latest.current = {
|
|
37
|
+
value,
|
|
38
|
+
onChange,
|
|
39
|
+
controlled: controlled !== void 0
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
return [value, useCallback((change) => {
|
|
43
|
+
const current = latest.current;
|
|
44
|
+
const next = change(current.value);
|
|
45
|
+
current.value = next;
|
|
46
|
+
if (!current.controlled) setOwn(next);
|
|
47
|
+
current.onChange?.(next);
|
|
48
|
+
}, [])];
|
|
49
|
+
}
|
|
27
50
|
function useCaret(caret, text) {
|
|
28
51
|
const element = useRef(null);
|
|
29
52
|
useIsomorphicLayoutEffect(() => {
|
|
@@ -39,7 +62,8 @@ function useCaret(caret, text) {
|
|
|
39
62
|
* A phone field that formats while typing, keeps the caret where the person is typing, follows the country of a
|
|
40
63
|
* number typed with `+`, and validates with the same rules as {@link validatePhone}. It renders nothing: draw the
|
|
41
64
|
* flag selector and the input with your own components, and pass `ref` to the input so the caret stays in place.
|
|
42
|
-
* The handlers keep the same identity while the options do not change.
|
|
65
|
+
* The handlers keep the same identity while the options do not change. Pass `value`, `country` and `onChange` to
|
|
66
|
+
* control the field from a store or a form library; otherwise it keeps its own state.
|
|
43
67
|
*
|
|
44
68
|
* @throws {RangeError} When `defaultCountry` or an option is not valid, or `setCountry` gets a country outside `countries`.
|
|
45
69
|
* @example
|
|
@@ -49,64 +73,70 @@ function useCaret(caret, text) {
|
|
|
49
73
|
function usePhoneInput(options) {
|
|
50
74
|
const { defaultCountry, initialValue, type } = options;
|
|
51
75
|
const allowedKey = (options.countries ?? countryCodes).join(",");
|
|
52
|
-
const [
|
|
53
|
-
|
|
76
|
+
const [field, update] = useFieldValue(options.value === void 0 ? void 0 : {
|
|
77
|
+
value: options.value,
|
|
78
|
+
country: options.country ?? defaultCountry
|
|
79
|
+
}, options.onChange, () => initialPhone(defaultCountry, initialValue, options.countries ?? countryCodes));
|
|
80
|
+
const [interaction, setInteraction] = useState(FRESH);
|
|
81
|
+
const ref = useCaret(interaction.caret, field.value);
|
|
54
82
|
const handlers = useMemo(() => {
|
|
55
83
|
const allowed = allowedKey.split(",");
|
|
56
|
-
const initial = () => initialState({
|
|
57
|
-
defaultCountry,
|
|
58
|
-
initialValue,
|
|
59
|
-
countries: allowed
|
|
60
|
-
});
|
|
61
84
|
return {
|
|
62
85
|
allowed,
|
|
63
|
-
onChangeText: (next) =>
|
|
86
|
+
onChangeText: (next) => update((current) => {
|
|
64
87
|
let detected;
|
|
65
|
-
const edit = applyEdit(current.
|
|
88
|
+
const edit = applyEdit(current.value, next, (typed) => {
|
|
66
89
|
const formatted = formatPhoneInput(typed, current.country);
|
|
67
90
|
detected = formatted.country;
|
|
68
91
|
return formatted.text;
|
|
69
92
|
}, DIGIT);
|
|
70
|
-
|
|
93
|
+
setInteraction((state) => ({
|
|
94
|
+
...state,
|
|
95
|
+
caret: edit.caret
|
|
96
|
+
}));
|
|
71
97
|
return {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
caret: edit.caret,
|
|
75
|
-
country
|
|
98
|
+
value: edit.text,
|
|
99
|
+
country: detected && allowed.includes(detected) ? detected : current.country
|
|
76
100
|
};
|
|
77
101
|
}),
|
|
78
|
-
onBlur: () =>
|
|
79
|
-
...
|
|
102
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
103
|
+
...state,
|
|
80
104
|
touched: true
|
|
81
105
|
}),
|
|
82
106
|
setCountry: (code) => {
|
|
83
107
|
if (!allowed.includes(code)) throw new RangeError("País fuera de la lista countries del campo.");
|
|
84
|
-
|
|
85
|
-
...
|
|
86
|
-
country: code,
|
|
87
|
-
text: formatPhoneInput(current.text, code).text,
|
|
108
|
+
setInteraction((state) => ({
|
|
109
|
+
...state,
|
|
88
110
|
caret: null
|
|
89
111
|
}));
|
|
112
|
+
update((current) => ({
|
|
113
|
+
value: formatPhoneInput(current.value, code).text,
|
|
114
|
+
country: code
|
|
115
|
+
}));
|
|
90
116
|
},
|
|
91
|
-
reset: () =>
|
|
117
|
+
reset: () => {
|
|
118
|
+
setInteraction(FRESH);
|
|
119
|
+
update(() => initialPhone(defaultCountry, initialValue, allowed));
|
|
120
|
+
}
|
|
92
121
|
};
|
|
93
122
|
}, [
|
|
94
123
|
allowedKey,
|
|
95
124
|
defaultCountry,
|
|
96
|
-
initialValue
|
|
125
|
+
initialValue,
|
|
126
|
+
update
|
|
97
127
|
]);
|
|
98
|
-
const validation = digitsOf(
|
|
128
|
+
const validation = digitsOf(field.value) ? validatePhone(field.value, field.country, {
|
|
99
129
|
type,
|
|
100
130
|
countries: handlers.allowed
|
|
101
131
|
}) : null;
|
|
102
132
|
return {
|
|
103
133
|
ref,
|
|
104
|
-
value:
|
|
105
|
-
country: requireCountry(
|
|
134
|
+
value: field.value,
|
|
135
|
+
country: requireCountry(field.country),
|
|
106
136
|
countries: countries.filter(({ code }) => handlers.allowed.includes(code)),
|
|
107
|
-
placeholder: phoneExample(
|
|
137
|
+
placeholder: phoneExample(field.country).national,
|
|
108
138
|
phone: validation?.valid ? validation.phone : null,
|
|
109
|
-
error:
|
|
139
|
+
error: interaction.touched && validation && !validation.valid ? validation.error : null,
|
|
110
140
|
onChangeText: handlers.onChangeText,
|
|
111
141
|
onBlur: handlers.onBlur,
|
|
112
142
|
setCountry: handlers.setCountry,
|
|
@@ -115,11 +145,7 @@ function usePhoneInput(options) {
|
|
|
115
145
|
}
|
|
116
146
|
const AMOUNT_CHARACTER = /[\d.]/;
|
|
117
147
|
function initialMoney(currency, initialValue) {
|
|
118
|
-
return {
|
|
119
|
-
text: initialValue === null || initialValue === void 0 ? "" : formatMoney(initialValue, currency, { display: "none" }),
|
|
120
|
-
touched: false,
|
|
121
|
-
caret: null
|
|
122
|
-
};
|
|
148
|
+
return initialValue === null || initialValue === void 0 ? "" : formatMoney(initialValue, currency, { display: "none" });
|
|
123
149
|
}
|
|
124
150
|
const FIELD_FORMAT = /^[\d,]*(?:\.\d*)?$/;
|
|
125
151
|
function typedAmount(previous, next, currency) {
|
|
@@ -145,36 +171,46 @@ function amountError(amount, text, currency, min, max) {
|
|
|
145
171
|
* const price = useMoneyInput({ currency: 'NIO', min: '1' });
|
|
146
172
|
* <TextInput ref={price.ref} value={price.value} onChangeText={price.onChangeText} onBlur={price.onBlur} keyboardType="decimal-pad" />
|
|
147
173
|
*/
|
|
148
|
-
function useMoneyInput({ currency, initialValue, min, max }) {
|
|
174
|
+
function useMoneyInput({ currency, initialValue, min, max, value, onChange }) {
|
|
149
175
|
const target = requireCurrency(currency);
|
|
150
|
-
const [
|
|
151
|
-
const
|
|
176
|
+
const [text, update] = useFieldValue(value, onChange, () => initialMoney(currency, initialValue));
|
|
177
|
+
const [interaction, setInteraction] = useState(FRESH);
|
|
178
|
+
const ref = useCaret(interaction.caret, text);
|
|
152
179
|
const handlers = useMemo(() => ({
|
|
153
|
-
onChangeText: (next) =>
|
|
154
|
-
const edit = applyEdit(current
|
|
155
|
-
|
|
156
|
-
...
|
|
157
|
-
text: edit.text,
|
|
180
|
+
onChangeText: (next) => update((current) => {
|
|
181
|
+
const edit = applyEdit(current, typedAmount(current, next, currency), (typed) => formatMoneyInput(typed, currency).text, AMOUNT_CHARACTER);
|
|
182
|
+
setInteraction((state) => ({
|
|
183
|
+
...state,
|
|
158
184
|
caret: edit.caret
|
|
159
|
-
};
|
|
185
|
+
}));
|
|
186
|
+
return edit.text;
|
|
160
187
|
}),
|
|
161
|
-
onBlur: () =>
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
text: amount === null ? current.text : formatMoney(amount, currency, { display: "none" }),
|
|
188
|
+
onBlur: () => {
|
|
189
|
+
setInteraction({
|
|
165
190
|
touched: true,
|
|
166
191
|
caret: null
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
192
|
+
});
|
|
193
|
+
update((current) => {
|
|
194
|
+
const { amount } = formatMoneyInput(current, currency);
|
|
195
|
+
return amount === null ? current : formatMoney(amount, currency, { display: "none" });
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
reset: () => {
|
|
199
|
+
setInteraction(FRESH);
|
|
200
|
+
update(() => initialMoney(currency, initialValue));
|
|
201
|
+
}
|
|
202
|
+
}), [
|
|
203
|
+
currency,
|
|
204
|
+
initialValue,
|
|
205
|
+
update
|
|
206
|
+
]);
|
|
207
|
+
const { amount } = formatMoneyInput(text, currency);
|
|
208
|
+
const error = amountError(amount, text, currency, min, max);
|
|
173
209
|
return {
|
|
174
210
|
ref,
|
|
175
|
-
value:
|
|
211
|
+
value: text,
|
|
176
212
|
amount: error === null ? amount : null,
|
|
177
|
-
error:
|
|
213
|
+
error: interaction.touched ? error : null,
|
|
178
214
|
currency: target,
|
|
179
215
|
onChangeText: handlers.onChangeText,
|
|
180
216
|
onBlur: handlers.onBlur,
|
|
@@ -183,11 +219,7 @@ function useMoneyInput({ currency, initialValue, min, max }) {
|
|
|
183
219
|
}
|
|
184
220
|
const DATE_DIGIT = /\d/;
|
|
185
221
|
function initialDate(initialValue) {
|
|
186
|
-
return
|
|
187
|
-
text: initialValue ? formatDate(initialValue) : "",
|
|
188
|
-
touched: false,
|
|
189
|
-
caret: null
|
|
190
|
-
};
|
|
222
|
+
return initialValue ? formatDate(initialValue) : "";
|
|
191
223
|
}
|
|
192
224
|
/**
|
|
193
225
|
* A `dd/mm/aaaa` field that adds `/` while typing, keeps the caret where the person is typing, and refuses dates
|
|
@@ -198,31 +230,35 @@ function initialDate(initialValue) {
|
|
|
198
230
|
* const delivery = useDateInput({ min: today('NI') });
|
|
199
231
|
* <TextInput ref={delivery.ref} value={delivery.value} onChangeText={delivery.onChangeText} onBlur={delivery.onBlur} keyboardType="number-pad" />
|
|
200
232
|
*/
|
|
201
|
-
function useDateInput({ initialValue, min, max } = {}) {
|
|
202
|
-
const [
|
|
203
|
-
const
|
|
233
|
+
function useDateInput({ initialValue, min, max, value, onChange } = {}) {
|
|
234
|
+
const [text, update] = useFieldValue(value, onChange, () => initialDate(initialValue));
|
|
235
|
+
const [interaction, setInteraction] = useState(FRESH);
|
|
236
|
+
const ref = useCaret(interaction.caret, text);
|
|
204
237
|
const handlers = useMemo(() => ({
|
|
205
|
-
onChangeText: (next) =>
|
|
206
|
-
const edit = applyEdit(current
|
|
207
|
-
|
|
208
|
-
...
|
|
209
|
-
text: edit.text,
|
|
238
|
+
onChangeText: (next) => update((current) => {
|
|
239
|
+
const edit = applyEdit(current, next, (typed) => formatDateInput(typed).text, DATE_DIGIT);
|
|
240
|
+
setInteraction((state) => ({
|
|
241
|
+
...state,
|
|
210
242
|
caret: edit.caret
|
|
211
|
-
};
|
|
243
|
+
}));
|
|
244
|
+
return edit.text;
|
|
212
245
|
}),
|
|
213
|
-
onBlur: () =>
|
|
214
|
-
...
|
|
246
|
+
onBlur: () => setInteraction((state) => state.touched ? state : {
|
|
247
|
+
...state,
|
|
215
248
|
touched: true
|
|
216
249
|
}),
|
|
217
|
-
reset: () =>
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
250
|
+
reset: () => {
|
|
251
|
+
setInteraction(FRESH);
|
|
252
|
+
update(() => initialDate(initialValue));
|
|
253
|
+
}
|
|
254
|
+
}), [initialValue, update]);
|
|
255
|
+
const { date } = formatDateInput(text);
|
|
256
|
+
const error = date === null ? text ? "format" : null : min && date < min ? "min" : max && date > max ? "max" : null;
|
|
221
257
|
return {
|
|
222
258
|
ref,
|
|
223
|
-
value:
|
|
259
|
+
value: text,
|
|
224
260
|
date: error === null ? date : null,
|
|
225
|
-
error:
|
|
261
|
+
error: interaction.touched ? error : null,
|
|
226
262
|
placeholder: "dd/mm/aaaa",
|
|
227
263
|
onChangeText: handlers.onChangeText,
|
|
228
264
|
onBlur: handlers.onBlur,
|