@expressive-ui/native 0.2.1 → 0.4.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.
Files changed (53) hide show
  1. package/README.md +11 -1
  2. package/dist/Feedback.d.ts +3 -3
  3. package/dist/Feedback.d.ts.map +1 -1
  4. package/dist/Feedback.js +73 -14
  5. package/dist/Feedback.js.map +1 -1
  6. package/dist/Fields.d.ts +10 -6
  7. package/dist/Fields.d.ts.map +1 -1
  8. package/dist/Fields.js +125 -29
  9. package/dist/Fields.js.map +1 -1
  10. package/dist/FileUpload.d.ts.map +1 -1
  11. package/dist/FileUpload.js +61 -19
  12. package/dist/FileUpload.js.map +1 -1
  13. package/dist/NumberField.d.ts +6 -2
  14. package/dist/NumberField.d.ts.map +1 -1
  15. package/dist/NumberField.js +71 -11
  16. package/dist/NumberField.js.map +1 -1
  17. package/dist/OtpField.d.ts +34 -0
  18. package/dist/OtpField.d.ts.map +1 -0
  19. package/dist/OtpField.js +156 -0
  20. package/dist/OtpField.js.map +1 -0
  21. package/dist/Pickers.d.ts.map +1 -1
  22. package/dist/Pickers.js +227 -36
  23. package/dist/Pickers.js.map +1 -1
  24. package/dist/RenderUI.d.ts +3 -0
  25. package/dist/RenderUI.d.ts.map +1 -0
  26. package/dist/RenderUI.js +45 -0
  27. package/dist/RenderUI.js.map +1 -0
  28. package/dist/Search.js +1 -1
  29. package/dist/Search.js.map +1 -1
  30. package/dist/Select.d.ts +5 -1
  31. package/dist/Select.d.ts.map +1 -1
  32. package/dist/Select.js +52 -10
  33. package/dist/Select.js.map +1 -1
  34. package/dist/index.d.ts +2 -0
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +2 -0
  37. package/dist/index.js.map +1 -1
  38. package/dist/theme.d.ts +3 -1
  39. package/dist/theme.d.ts.map +1 -1
  40. package/dist/theme.js +36 -11
  41. package/dist/theme.js.map +1 -1
  42. package/package.json +4 -4
  43. package/src/Feedback.tsx +211 -16
  44. package/src/Fields.tsx +341 -58
  45. package/src/FileUpload.tsx +169 -7
  46. package/src/NumberField.tsx +187 -9
  47. package/src/OtpField.tsx +353 -0
  48. package/src/Pickers.tsx +783 -86
  49. package/src/RenderUI.tsx +47 -0
  50. package/src/Search.tsx +1 -1
  51. package/src/Select.tsx +209 -27
  52. package/src/index.ts +3 -0
  53. package/src/theme.tsx +77 -14
@@ -0,0 +1,353 @@
1
+ import { useEffect, useRef, useState, type Ref } from 'react';
2
+ import {
3
+ Animated,
4
+ StyleSheet,
5
+ TextInput,
6
+ View,
7
+ type StyleProp,
8
+ type ViewStyle,
9
+ } from 'react-native';
10
+ import Svg, { Path } from 'react-native-svg';
11
+ import {
12
+ otpCellRadius,
13
+ otpShapePoints,
14
+ resolveOtp,
15
+ useOtp,
16
+ type OtpCell,
17
+ type OtpCellState,
18
+ type OtpSpec,
19
+ type OtpStatus,
20
+ type OtpVariant,
21
+ } from '@expressive-ui/core';
22
+ import { T } from './primitives.js';
23
+ import { useExpressiveTheme, useSpringValue } from './theme.js';
24
+
25
+ export type OtpFieldProps = {
26
+ id?: string;
27
+ ref?: Ref<TextInput>;
28
+ inputProps?: Omit<
29
+ React.ComponentProps<typeof TextInput>,
30
+ 'value' | 'defaultValue' | 'onChangeText' | 'ref' | 'maxLength'
31
+ >;
32
+ label: string;
33
+ /** Só dígitos. Controlado; sem value o campo guarda o próprio código (use defaultValue). */
34
+ value?: string;
35
+ defaultValue?: string;
36
+ onChange?: (value: string) => void;
37
+ /** Chamado uma vez quando o último dígito entra (digitado, colado ou autopreenchido). */
38
+ onComplete?: (value: string) => void;
39
+ /** Quantidade de dígitos (1–12). Padrão 6. */
40
+ length?: number;
41
+ /** Dígitos por grupo na variante capsule (0 = sem grupos). Padrão 3 para 6 e 4 para 8 dígitos. */
42
+ groupSize?: number;
43
+ variant?: OtpVariant;
44
+ /** verifying e success bloqueiam a edição. */
45
+ status?: OtpStatus;
46
+ supportingText?: string;
47
+ /** true ou a mensagem. Com o campo cheio, o próximo dígito recomeça o código. */
48
+ error?: string | boolean;
49
+ required?: boolean;
50
+ disabled?: boolean;
51
+ readOnly?: boolean;
52
+ autoFocus?: boolean;
53
+ autoComplete?: React.ComponentProps<typeof TextInput>['autoComplete'];
54
+ style?: StyleProp<ViewStyle>;
55
+ };
56
+
57
+ const paths = Object.fromEntries(
58
+ (['empty', 'active', 'filled', 'verifying', 'success', 'error'] as const).map((state) => [
59
+ state,
60
+ `M${otpShapePoints(state)
61
+ .map(([x, y]) => `${x} ${y}`)
62
+ .join('L')}Z`,
63
+ ]),
64
+ ) as Record<OtpCellState, string>;
65
+
66
+ function Caret({ color }: { color: string }) {
67
+ const { reduceMotion } = useExpressiveTheme();
68
+ const opacity = useRef(new Animated.Value(1)).current;
69
+ useEffect(() => {
70
+ if (reduceMotion) return;
71
+ const blink = Animated.loop(
72
+ Animated.sequence([
73
+ Animated.timing(opacity, { toValue: 1, duration: 550, useNativeDriver: true }),
74
+ Animated.timing(opacity, { toValue: 0, duration: 1, useNativeDriver: true }),
75
+ Animated.timing(opacity, { toValue: 0, duration: 550, useNativeDriver: true }),
76
+ Animated.timing(opacity, { toValue: 1, duration: 1, useNativeDriver: true }),
77
+ ]),
78
+ );
79
+ blink.start();
80
+ return () => {
81
+ blink.stop();
82
+ opacity.setValue(1);
83
+ };
84
+ }, [opacity, reduceMotion]);
85
+ return <Animated.View style={[styles.caret, { backgroundColor: color, opacity }]} />;
86
+ }
87
+
88
+ /** Dígito que entra crescendo com a mola (remontado a cada troca pela key). */
89
+ function Digit({ char, color, spec }: { char: string; color: string; spec: OtpSpec }) {
90
+ const [target, setTarget] = useState(0.4);
91
+ useEffect(() => setTarget(1), []);
92
+ const scale = useSpringValue(target, 'spatialFast');
93
+ return (
94
+ <Animated.View style={{ transform: [{ scale }] }}>
95
+ <T
96
+ variant={spec.type}
97
+ emphasized
98
+ maxFontSizeMultiplier={1.3}
99
+ style={[styles.digit, { color }]}
100
+ >
101
+ {char}
102
+ </T>
103
+ </Animated.View>
104
+ );
105
+ }
106
+
107
+ function Cell({
108
+ cell,
109
+ length,
110
+ variant,
111
+ status,
112
+ invalid,
113
+ spec,
114
+ }: {
115
+ cell: OtpCell;
116
+ length: number;
117
+ variant: OtpVariant;
118
+ status: OtpStatus;
119
+ invalid: boolean;
120
+ spec: OtpSpec;
121
+ }) {
122
+ const { colors, reduceMotion } = useExpressiveTheme();
123
+ const [startTarget, endTarget] = otpCellRadius(variant, cell.state, cell.index, length);
124
+ const start = useSpringValue(startTarget, 'spatialFast');
125
+ const end = useSpringValue(endTarget, 'spatialFast');
126
+ const lift = useRef(new Animated.Value(0)).current;
127
+ const verifying = status === 'verifying' && !reduceMotion;
128
+ useEffect(() => {
129
+ if (!verifying) return;
130
+ const delay = cell.index * 80;
131
+ const step = (toValue: number) =>
132
+ Animated.timing(lift, { toValue, duration: 280, useNativeDriver: false });
133
+ const wave = Animated.loop(
134
+ Animated.sequence([
135
+ Animated.delay(delay),
136
+ step(-6),
137
+ step(0),
138
+ Animated.delay(Math.max(0, length * 80 - delay)),
139
+ ]),
140
+ );
141
+ wave.start();
142
+ return () => {
143
+ wave.stop();
144
+ lift.setValue(0);
145
+ };
146
+ }, [verifying, cell.index, length, lift]);
147
+
148
+ const focus = colors[invalid ? spec.indicator.error : spec.indicator.color];
149
+ const { container, content } = spec.colors[cell.state];
150
+ const shapes = variant === 'shapes';
151
+ const active = cell.state === 'active';
152
+ return (
153
+ <Animated.View
154
+ style={[
155
+ styles.cell,
156
+ {
157
+ transform: [{ translateY: lift }],
158
+ marginLeft: cell.index === 0 ? 0 : cell.groupStart ? spec.groupGap : spec.gap,
159
+ },
160
+ !shapes && {
161
+ backgroundColor: colors[container],
162
+ borderWidth: spec.indicator.width,
163
+ borderColor: cell.focused ? focus : 'transparent',
164
+ borderTopLeftRadius: start,
165
+ borderBottomLeftRadius: start,
166
+ borderTopRightRadius: end,
167
+ borderBottomRightRadius: end,
168
+ },
169
+ ]}
170
+ >
171
+ {shapes ? (
172
+ <Svg width="100%" height="100%" viewBox="0 0 100 100" style={StyleSheet.absoluteFill}>
173
+ <Path
174
+ d={paths[cell.state]}
175
+ fill={colors[container]}
176
+ stroke={active ? focus : undefined}
177
+ strokeWidth={active ? 4 : 0}
178
+ />
179
+ </Svg>
180
+ ) : null}
181
+ {shapes && cell.focused && !active ? (
182
+ <View style={[styles.underline, { backgroundColor: focus }]} />
183
+ ) : null}
184
+ {cell.char ? (
185
+ <Digit key={cell.char} char={cell.char} color={colors[content]} spec={spec} />
186
+ ) : cell.caret ? (
187
+ <Caret color={focus} />
188
+ ) : null}
189
+ </Animated.View>
190
+ );
191
+ }
192
+
193
+ /** Código de uso único num único TextInput: colar, autopreenchimento e leitores de tela funcionam. */
194
+ export function OtpField({
195
+ id,
196
+ ref,
197
+ inputProps,
198
+ label,
199
+ value,
200
+ defaultValue,
201
+ onChange,
202
+ onComplete,
203
+ length,
204
+ groupSize,
205
+ variant = 'capsule',
206
+ status = 'idle',
207
+ supportingText,
208
+ error,
209
+ required,
210
+ disabled = false,
211
+ readOnly = false,
212
+ autoFocus,
213
+ autoComplete = 'one-time-code',
214
+ style,
215
+ }: OtpFieldProps) {
216
+ const { colors, mode, reduceMotion } = useExpressiveTheme();
217
+ const spec = resolveOtp(variant);
218
+ const otp = useOtp({
219
+ value,
220
+ defaultValue,
221
+ onChange,
222
+ onComplete,
223
+ length,
224
+ groupSize,
225
+ variant,
226
+ status,
227
+ disabled,
228
+ readOnly,
229
+ error,
230
+ supportingText,
231
+ required,
232
+ label,
233
+ id,
234
+ });
235
+ const { field } = otp;
236
+ const shake = useRef(new Animated.Value(0)).current;
237
+ useEffect(() => {
238
+ if (!field.invalid || reduceMotion) return;
239
+ const animation = Animated.sequence(
240
+ [-6, 6, -6, 6, 0].map((toValue) =>
241
+ Animated.timing(shake, { toValue, duration: 80, useNativeDriver: true }),
242
+ ),
243
+ );
244
+ animation.start();
245
+ return () => {
246
+ animation.stop();
247
+ shake.setValue(0);
248
+ };
249
+ }, [field.invalid, reduceMotion, shake]);
250
+
251
+ return (
252
+ <View
253
+ style={[
254
+ styles.root,
255
+ {
256
+ maxWidth: otp.length * spec.maxCellWidth + (otp.length - 1) * spec.gap,
257
+ opacity: disabled ? spec.disabledOpacity : 1,
258
+ },
259
+ style,
260
+ ]}
261
+ >
262
+ <View accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
263
+ <T variant="labelLarge" color="onSurfaceVariant">
264
+ {field.labelText}
265
+ </T>
266
+ </View>
267
+ <View style={{ height: spec.height }}>
268
+ <Animated.View
269
+ pointerEvents="none"
270
+ accessibilityElementsHidden
271
+ importantForAccessibility="no-hide-descendants"
272
+ style={[styles.cells, { transform: [{ translateX: shake }] }]}
273
+ >
274
+ {otp.cells.map((cell) => (
275
+ <Cell
276
+ key={cell.index}
277
+ cell={cell}
278
+ length={otp.length}
279
+ variant={variant}
280
+ status={status}
281
+ invalid={field.invalid}
282
+ spec={spec}
283
+ />
284
+ ))}
285
+ </Animated.View>
286
+ <TextInput
287
+ {...inputProps}
288
+ ref={ref}
289
+ nativeID={field.ids.input}
290
+ accessibilityLabel={field.labelText}
291
+ accessibilityHint={field.help ?? undefined}
292
+ accessibilityState={{ disabled, busy: status === 'verifying' }}
293
+ value={otp.text}
294
+ onChangeText={otp.change}
295
+ editable={!otp.locked && inputProps?.editable !== false}
296
+ autoFocus={autoFocus}
297
+ keyboardType="number-pad"
298
+ textContentType={autoComplete === 'one-time-code' ? 'oneTimeCode' : undefined}
299
+ autoComplete={autoComplete}
300
+ autoCorrect={false}
301
+ keyboardAppearance={mode}
302
+ underlineColorAndroid="transparent"
303
+ selectionColor="transparent"
304
+ caretHidden
305
+ onFocus={(event) => {
306
+ otp.setFocused(true);
307
+ inputProps?.onFocus?.(event);
308
+ }}
309
+ onBlur={(event) => {
310
+ otp.setFocused(false);
311
+ inputProps?.onBlur?.(event);
312
+ }}
313
+ style={[StyleSheet.absoluteFill, styles.input]}
314
+ />
315
+ </View>
316
+ {field.help ? (
317
+ <T
318
+ variant={spec.supporting.type}
319
+ color={
320
+ field.invalid
321
+ ? spec.supporting.error
322
+ : status === 'success'
323
+ ? spec.supporting.success
324
+ : spec.supporting.color
325
+ }
326
+ accessibilityLiveRegion="polite"
327
+ style={{ minHeight: 16 }}
328
+ >
329
+ {field.help}
330
+ </T>
331
+ ) : null}
332
+ </View>
333
+ );
334
+ }
335
+
336
+ const styles = StyleSheet.create({
337
+ root: { width: '100%', gap: 8 },
338
+ // Códigos são lidos da esquerda para a direita também em RTL.
339
+ cells: { flex: 1, flexDirection: 'row', direction: 'ltr' },
340
+ cell: { flex: 1, minWidth: 0, alignItems: 'center', justifyContent: 'center' },
341
+ caret: { width: 2, height: 24, borderRadius: 1 },
342
+ digit: { fontVariant: ['tabular-nums'], textAlign: 'center' },
343
+ underline: {
344
+ position: 'absolute',
345
+ bottom: -5,
346
+ left: '25%',
347
+ right: '25%',
348
+ height: 3,
349
+ borderRadius: 2,
350
+ },
351
+ // O texto do input fica invisível; os espaços acima desenham o código.
352
+ input: { color: 'transparent', backgroundColor: 'transparent', padding: 0, fontSize: 16 },
353
+ });