@expressive-ui/native 0.2.0 → 0.3.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 (48) hide show
  1. package/README.md +10 -0
  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/Pickers.d.ts.map +1 -1
  18. package/dist/Pickers.js +227 -36
  19. package/dist/Pickers.js.map +1 -1
  20. package/dist/RenderUI.d.ts +3 -0
  21. package/dist/RenderUI.d.ts.map +1 -0
  22. package/dist/RenderUI.js +45 -0
  23. package/dist/RenderUI.js.map +1 -0
  24. package/dist/Search.js +1 -1
  25. package/dist/Search.js.map +1 -1
  26. package/dist/Select.d.ts +5 -1
  27. package/dist/Select.d.ts.map +1 -1
  28. package/dist/Select.js +52 -10
  29. package/dist/Select.js.map +1 -1
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1 -0
  33. package/dist/index.js.map +1 -1
  34. package/dist/theme.d.ts +3 -1
  35. package/dist/theme.d.ts.map +1 -1
  36. package/dist/theme.js +36 -11
  37. package/dist/theme.js.map +1 -1
  38. package/package.json +4 -4
  39. package/src/Feedback.tsx +211 -16
  40. package/src/Fields.tsx +341 -58
  41. package/src/FileUpload.tsx +169 -7
  42. package/src/NumberField.tsx +187 -9
  43. package/src/Pickers.tsx +783 -86
  44. package/src/RenderUI.tsx +47 -0
  45. package/src/Search.tsx +1 -1
  46. package/src/Select.tsx +209 -27
  47. package/src/index.ts +2 -0
  48. package/src/theme.tsx +77 -14
@@ -6,11 +6,173 @@ import { Icon, T } from './primitives.js';
6
6
  import { useExpressiveTheme } from './theme.js';
7
7
  export type { NativeUploadFile } from '@expressive-ui/core';
8
8
  /** Inject an Expo DocumentPicker or platform picker adapter; this package does not install a picker dependency. */
9
- export type FileUploadProps=FileUploadOptions<NativeUploadFile>&{label:string;pickFiles:()=>Promise<readonly NativeUploadFile[]>;supportingText?:string;disabled?:boolean;style?:StyleProp<ViewStyle>};
10
- export function FileUpload({label,pickFiles,supportingText,disabled,style,...options}:FileUploadProps){
11
- const {colors,locale}=useExpressiveTheme(),pt=locale.startsWith('pt'),[picking,setPicking]=useState(false),[error,setError]=useState(''),mounted=useRef(true),inflight=useRef(false);
12
- useEffect(()=>{mounted.current=true;return()=>{mounted.current=false;};},[]);
13
- const queue=useFileUpload({...options,onReject:rejected=>{setError(rejected.map(item=>`${item.file.name}: ${item.reason}`).join(', '));options.onReject?.(rejected);}});
14
- const choose=async()=>{if(inflight.current||disabled)return;inflight.current=true;setPicking(true);setError('');try{const files=await pickFiles();if(mounted.current)queue.add(files);}catch(error){if(mounted.current)setError(error instanceof Error?error.message:String(error));}finally{inflight.current=false;if(mounted.current)setPicking(false);}};
15
- return <View style={[{gap:14},style]}><T variant="titleSmall">{label}</T>{supportingText?<T color="onSurfaceVariant">{supportingText}</T>:null}<View style={{padding:24,borderWidth:2,borderStyle:'dashed',borderColor:colors.outlineVariant,borderRadius:20,alignItems:'center',gap:12}}><Icon name="upload" size={32}/><Button action={{id:'upload.choose',label:pt?'Escolher arquivos':'Choose files'}} loading={picking} disabled={disabled||picking} onPress={choose}/></View>{error?<T accessibilityRole="alert" color="error">{error}</T>:null}{queue.entries.map(entry=><View key={entry.id} style={{padding:12,borderRadius:16,backgroundColor:colors.surfaceContainer,gap:8}}><View style={{flexDirection:'row',alignItems:'center',gap:12}}>{entry.file.type.startsWith('image/')?<Image source={{uri:entry.file.uri}} accessibilityIgnoresInvertColors style={{width:48,height:48,borderRadius:8}}/>:<Icon name="description"/>}<View style={{flex:1}}><T>{entry.file.name}</T><T variant="bodySmall" color="onSurfaceVariant">{Math.ceil(entry.file.size/1024)} KB · {entry.status}</T></View></View>{entry.status==='uploading'?<View accessibilityRole="progressbar" accessibilityLabel={entry.file.name} accessibilityValue={{min:0,max:100,now:entry.progress}} style={{height:4,borderRadius:2,backgroundColor:colors.outlineVariant}}><View style={{height:4,width:`${entry.progress}%`,backgroundColor:colors.primary}}/></View>:null}{entry.error?<T accessibilityRole="alert" color="error">{entry.error}</T>:null}<View style={{flexDirection:'row',flexWrap:'wrap',gap:8}}>{entry.status==='uploading'?<Button variant="text" action={{id:'upload.cancel',label:pt?'Cancelar':'Cancel'}} onPress={()=>queue.cancel(entry.id)}/>:null}{options.onUpload&&['error','cancelled'].includes(entry.status)?<Button variant="text" disabled={disabled} action={{id:'upload.retry',label:pt?'Tentar novamente':'Retry'}} onPress={()=>queue.retry(entry.id)}/>:null}<Button variant="text" disabled={disabled} action={{id:'upload.remove',label:pt?'Remover':'Remove'}} onPress={()=>queue.remove(entry.id)}/></View></View>)}</View>;
9
+ export type FileUploadProps = FileUploadOptions<NativeUploadFile> & {
10
+ label: string;
11
+ pickFiles: () => Promise<readonly NativeUploadFile[]>;
12
+ supportingText?: string;
13
+ disabled?: boolean;
14
+ style?: StyleProp<ViewStyle>;
15
+ };
16
+ export function FileUpload({
17
+ label,
18
+ pickFiles,
19
+ supportingText,
20
+ disabled,
21
+ style,
22
+ ...options
23
+ }: FileUploadProps) {
24
+ const { colors, labels } = useExpressiveTheme(),
25
+ [picking, setPicking] = useState(false),
26
+ [error, setError] = useState(''),
27
+ mounted = useRef(true),
28
+ inflight = useRef(false);
29
+ useEffect(() => {
30
+ mounted.current = true;
31
+ return () => {
32
+ mounted.current = false;
33
+ };
34
+ }, []);
35
+ const queue = useFileUpload({
36
+ ...options,
37
+ onReject: (rejected) => {
38
+ setError(
39
+ rejected
40
+ .map(
41
+ (item) =>
42
+ `${item.file.name}: ${
43
+ item.reason === 'type'
44
+ ? labels.uploadTypeError
45
+ : item.reason === 'size'
46
+ ? labels.uploadSizeError
47
+ : labels.uploadCountError
48
+ }`,
49
+ )
50
+ .join(', '),
51
+ );
52
+ options.onReject?.(rejected);
53
+ },
54
+ });
55
+ const choose = async () => {
56
+ if (inflight.current || disabled) return;
57
+ inflight.current = true;
58
+ setPicking(true);
59
+ setError('');
60
+ try {
61
+ const files = await pickFiles();
62
+ if (mounted.current) queue.add(files);
63
+ } catch (error) {
64
+ if (mounted.current) setError(error instanceof Error ? error.message : String(error));
65
+ } finally {
66
+ inflight.current = false;
67
+ if (mounted.current) setPicking(false);
68
+ }
69
+ };
70
+ return (
71
+ <View style={[{ gap: 14 }, style]}>
72
+ <T variant="titleSmall">{label}</T>
73
+ {supportingText ? <T color="onSurfaceVariant">{supportingText}</T> : null}
74
+ <View
75
+ style={{
76
+ padding: 24,
77
+ borderWidth: 2,
78
+ borderStyle: 'dashed',
79
+ borderColor: colors.outlineVariant,
80
+ borderRadius: 20,
81
+ alignItems: 'center',
82
+ gap: 12,
83
+ }}
84
+ >
85
+ <Icon name="upload" size={32} />
86
+ <Button
87
+ action={{ id: 'upload.choose', label: labels.chooseFiles }}
88
+ loading={picking}
89
+ disabled={disabled || picking}
90
+ onPress={choose}
91
+ />
92
+ </View>
93
+ {error ? (
94
+ <T accessibilityRole="alert" color="error">
95
+ {error}
96
+ </T>
97
+ ) : null}
98
+ {queue.entries.map((entry) => (
99
+ <View
100
+ key={entry.id}
101
+ style={{
102
+ padding: 12,
103
+ borderRadius: 16,
104
+ backgroundColor: colors.surfaceContainer,
105
+ gap: 8,
106
+ }}
107
+ >
108
+ <View style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
109
+ {entry.file.type.startsWith('image/') ? (
110
+ <Image
111
+ source={{ uri: entry.file.uri }}
112
+ accessibilityIgnoresInvertColors
113
+ style={{ width: 48, height: 48, borderRadius: 8 }}
114
+ />
115
+ ) : (
116
+ <Icon name="description" />
117
+ )}
118
+ <View style={{ flex: 1 }}>
119
+ <T>{entry.file.name}</T>
120
+ <T variant="bodySmall" color="onSurfaceVariant">
121
+ {Math.ceil(entry.file.size / 1024)} KB ·{' '}
122
+ {
123
+ {
124
+ ready: labels.uploadReady,
125
+ uploading: labels.uploading,
126
+ complete: labels.uploadComplete,
127
+ error: labels.uploadFailed,
128
+ cancelled: labels.uploadCancelled,
129
+ }[entry.status]
130
+ }
131
+ </T>
132
+ </View>
133
+ </View>
134
+ {entry.status === 'uploading' ? (
135
+ <View
136
+ accessibilityRole="progressbar"
137
+ accessibilityLabel={entry.file.name}
138
+ accessibilityValue={{ min: 0, max: 100, now: entry.progress }}
139
+ style={{ height: 4, borderRadius: 2, backgroundColor: colors.outlineVariant }}
140
+ >
141
+ <View
142
+ style={{ height: 4, width: `${entry.progress}%`, backgroundColor: colors.primary }}
143
+ />
144
+ </View>
145
+ ) : null}
146
+ {entry.error ? (
147
+ <T accessibilityRole="alert" color="error">
148
+ {entry.error}
149
+ </T>
150
+ ) : null}
151
+ <View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 8 }}>
152
+ {entry.status === 'uploading' ? (
153
+ <Button
154
+ variant="text"
155
+ action={{ id: 'upload.cancel', label: labels.cancel }}
156
+ onPress={() => queue.cancel(entry.id)}
157
+ />
158
+ ) : null}
159
+ {options.onUpload && ['error', 'cancelled'].includes(entry.status) ? (
160
+ <Button
161
+ variant="text"
162
+ disabled={disabled}
163
+ action={{ id: 'upload.retry', label: labels.retry }}
164
+ onPress={() => queue.retry(entry.id)}
165
+ />
166
+ ) : null}
167
+ <Button
168
+ variant="text"
169
+ disabled={disabled}
170
+ action={{ id: 'upload.remove', label: labels.remove }}
171
+ onPress={() => queue.remove(entry.id)}
172
+ />
173
+ </View>
174
+ </View>
175
+ ))}
176
+ </View>
177
+ );
16
178
  }
@@ -1,13 +1,191 @@
1
- import { useId, useState } from 'react';
2
- import { Pressable, TextInput, View, type StyleProp, type ViewStyle } from 'react-native';
1
+ import { useId, type Ref, useState } from 'react';
2
+ import {
3
+ Pressable,
4
+ TextInput,
5
+ View,
6
+ type TextInputProps,
7
+ type StyleProp,
8
+ type ViewStyle,
9
+ } from 'react-native';
3
10
  import { useNumberField, type NumberFieldOptions } from '@expressive-ui/core';
4
11
  import { Icon, T } from './primitives.js';
5
12
  import { useExpressiveTheme } from './theme.js';
6
- export type NumberFieldProps=NumberFieldOptions&{label:string;disabled?:boolean;required?:boolean;readOnly?:boolean;variant?:'filled'|'outlined';controlsPosition?:'split'|'end';controlsOrientation?:'horizontal'|'vertical';prefix?:string;suffix?:string;supportingText?:string;error?:string|boolean;style?:StyleProp<ViewStyle>};
7
- export function NumberField({label,disabled,required,readOnly,variant='outlined',controlsPosition='split',controlsOrientation='horizontal',prefix,suffix,supportingText,error,style,...options}:NumberFieldProps){
8
- const {colors,locale}=useExpressiveTheme(),state=useNumberField({locale,...options}),pt=locale.startsWith('pt'),id=useId();
9
- const [selection,setSelection]=useState<{start:number;end:number}|undefined>();
10
- const vertical=controlsPosition==='end'&&controlsOrientation==='vertical';
11
- const stepButton=(direction:1|-1)=><Pressable accessibilityRole="button" accessibilityLabel={`${direction===1?(pt?'Aumentar':'Increase'):(pt?'Diminuir':'Decrease')} ${label}`} disabled={disabled||readOnly||options.value!==null&&(direction===1?options.value>=(options.max??Infinity):options.value<=(options.min??-Infinity))} onPress={()=>state.increment(direction)} style={({pressed})=>({width:vertical?48:44,height:vertical?48:44,borderRadius:vertical?0:22,borderTopWidth:vertical&&direction===-1?1:0,borderTopColor:colors.outlineVariant,alignItems:'center',justifyContent:'center',backgroundColor:vertical?'transparent':colors.secondaryContainer,opacity:disabled||readOnly? .38 :pressed? .7 :1})}><Icon name={direction===1?'add':'remove'}/></Pressable>;
12
- return <View style={[{gap:6,opacity:disabled? .38 :1},style]}><T variant="labelMedium" nativeID={`${id}-label`}>{label}{required?' *':''}</T><View style={{flexDirection:'row',alignItems:'center',gap:8,minHeight:56,padding:6,borderWidth:variant==='outlined'?1:0,borderBottomWidth:1,borderColor:error?colors.error:colors.outline,borderRadius:12,backgroundColor:variant==='filled'?colors.surfaceContainerHighest:colors.surface}}>{controlsPosition==='split'?stepButton(-1):null}{prefix??state.currencySymbol?<T>{prefix??state.currencySymbol}</T>:null}<TextInput accessibilityLabel={label} accessibilityRole="adjustable" accessibilityValue={{...(options.min!==undefined?{min:options.min}:{}),...(options.max!==undefined?{max:options.max}:{}),...(options.value!==null?{now:options.value}:{}),text:state.text}} accessibilityState={{disabled:!!disabled}} accessibilityActions={[{name:'increment'},{name:'decrement'}]} onAccessibilityAction={event=>{if(!disabled&&!readOnly)state.increment(event.nativeEvent.actionName==='increment'?1:-1);}} editable={!disabled&&!readOnly} value={state.text} selection={options.format==='currency'?selection:undefined} onSelectionChange={event=>{if(options.format==='currency')setSelection(event.nativeEvent.selection);}} onFocus={()=>{if(options.format==='currency'&&(options.value===null||options.value===0)){const point=state.text.indexOf(state.decimalSeparator);setSelection({start:0,end:point<0?state.text.length:point});}}} onChangeText={text=>{const result=state.editText(text);if(options.format==='currency')setSelection({start:result.caret,end:result.caret});}} onEndEditing={state.commit} keyboardType="numbers-and-punctuation" style={{flex:1,minWidth:0,padding:10,textAlign:controlsPosition==='end'?'auto':'center',color:colors.onSurface,fontSize:16}}/>{suffix?<T>{suffix}</T>:null}{controlsPosition==='end'?<View style={{flexDirection:vertical?'column':'row',flexShrink:0,gap:vertical?0:6,borderWidth:vertical?1:0,borderColor:colors.outlineVariant,borderRadius:vertical?16:0,overflow:'hidden',backgroundColor:vertical?colors.secondaryContainer:undefined}}>{vertical?<>{stepButton(1)}{stepButton(-1)}</>:<>{stepButton(-1)}{stepButton(1)}</>}</View>:stepButton(1)}</View>{error||supportingText?<T variant="bodySmall" color={error?'error':'onSurfaceVariant'}>{typeof error==='string'?error:supportingText}</T>:null}</View>;
13
+ export type NumberFieldProps = NumberFieldOptions & {
14
+ ref?: Ref<TextInput>;
15
+ id?: string;
16
+ inputProps?: Omit<TextInputProps, 'value' | 'defaultValue' | 'onChangeText'>;
17
+ label: string;
18
+ disabled?: boolean;
19
+ required?: boolean;
20
+ readOnly?: boolean;
21
+ variant?: 'filled' | 'outlined';
22
+ controlsPosition?: 'split' | 'end';
23
+ controlsOrientation?: 'horizontal' | 'vertical';
24
+ prefix?: string;
25
+ suffix?: string;
26
+ supportingText?: string;
27
+ error?: string | boolean;
28
+ style?: StyleProp<ViewStyle>;
29
+ };
30
+ export function NumberField({
31
+ ref,
32
+ id: providedId,
33
+ inputProps,
34
+ label,
35
+ disabled,
36
+ required,
37
+ readOnly,
38
+ variant = 'outlined',
39
+ controlsPosition = 'split',
40
+ controlsOrientation = 'horizontal',
41
+ prefix,
42
+ suffix,
43
+ supportingText,
44
+ error,
45
+ style,
46
+ ...options
47
+ }: NumberFieldProps) {
48
+ const { colors, locale, labels } = useExpressiveTheme(),
49
+ state = useNumberField({ locale, ...options }),
50
+ generatedId = useId(),
51
+ id = providedId ?? generatedId;
52
+ const [selection, setSelection] = useState<{ start: number; end: number } | undefined>();
53
+ const vertical = controlsPosition === 'end' && controlsOrientation === 'vertical';
54
+ const stepButton = (direction: 1 | -1) => (
55
+ <Pressable
56
+ accessibilityRole="button"
57
+ accessibilityLabel={`${direction === 1 ? labels.increase : labels.decrease} ${label}`}
58
+ disabled={
59
+ disabled ||
60
+ readOnly ||
61
+ (options.value !== null &&
62
+ (direction === 1
63
+ ? options.value >= (options.max ?? Infinity)
64
+ : options.value <= (options.min ?? -Infinity)))
65
+ }
66
+ onPress={() => state.increment(direction)}
67
+ style={({ pressed }) => ({
68
+ width: vertical ? 48 : 44,
69
+ height: vertical ? 48 : 44,
70
+ borderRadius: vertical ? 0 : 22,
71
+ borderTopWidth: vertical && direction === -1 ? 1 : 0,
72
+ borderTopColor: colors.outlineVariant,
73
+ alignItems: 'center',
74
+ justifyContent: 'center',
75
+ backgroundColor: vertical ? 'transparent' : colors.secondaryContainer,
76
+ opacity: disabled || readOnly ? 0.38 : pressed ? 0.7 : 1,
77
+ })}
78
+ >
79
+ <Icon name={direction === 1 ? 'add' : 'remove'} />
80
+ </Pressable>
81
+ );
82
+ return (
83
+ <View style={[{ gap: 6, opacity: disabled ? 0.38 : 1 }, style]}>
84
+ <T variant="labelMedium" nativeID={`${id}-label`}>
85
+ {label}
86
+ {required ? ' *' : ''}
87
+ </T>
88
+ <View
89
+ style={{
90
+ flexDirection: 'row',
91
+ alignItems: 'center',
92
+ gap: 8,
93
+ minHeight: 56,
94
+ padding: 6,
95
+ borderWidth: variant === 'outlined' ? 1 : 0,
96
+ borderBottomWidth: 1,
97
+ borderColor: error ? colors.error : colors.outline,
98
+ borderRadius: 12,
99
+ backgroundColor: variant === 'filled' ? colors.surfaceContainerHighest : colors.surface,
100
+ }}
101
+ >
102
+ {controlsPosition === 'split' ? stepButton(-1) : null}
103
+ {prefix ?? state.currencySymbol ? <T>{prefix ?? state.currencySymbol}</T> : null}
104
+ <TextInput
105
+ {...inputProps}
106
+ ref={ref}
107
+ nativeID={id}
108
+ accessibilityLabel={label}
109
+ accessibilityRole="adjustable"
110
+ accessibilityValue={{
111
+ ...(options.min !== undefined ? { min: options.min } : {}),
112
+ ...(options.max !== undefined ? { max: options.max } : {}),
113
+ ...(options.value !== null ? { now: options.value } : {}),
114
+ text: state.text,
115
+ }}
116
+ accessibilityState={{ disabled: !!disabled }}
117
+ accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
118
+ onAccessibilityAction={(event) => {
119
+ if (!disabled && !readOnly)
120
+ state.increment(event.nativeEvent.actionName === 'increment' ? 1 : -1);
121
+ }}
122
+ editable={!disabled && !readOnly}
123
+ value={state.text}
124
+ selection={options.format === 'currency' ? selection : undefined}
125
+ onSelectionChange={(event) => {
126
+ if (options.format === 'currency') setSelection(event.nativeEvent.selection);
127
+ }}
128
+ onFocus={(event) => {
129
+ inputProps?.onFocus?.(event);
130
+ if (options.format === 'currency' && (options.value === null || options.value === 0)) {
131
+ const point = state.text.indexOf(state.decimalSeparator);
132
+ setSelection({ start: 0, end: point < 0 ? state.text.length : point });
133
+ }
134
+ }}
135
+ onChangeText={(text) => {
136
+ const result = state.editText(text);
137
+ if (options.format === 'currency')
138
+ setSelection({ start: result.caret, end: result.caret });
139
+ }}
140
+ onEndEditing={(event) => {
141
+ state.commit();
142
+ inputProps?.onEndEditing?.(event);
143
+ }}
144
+ keyboardType="numbers-and-punctuation"
145
+ style={{
146
+ flex: 1,
147
+ minWidth: 0,
148
+ padding: 10,
149
+ textAlign: controlsPosition === 'end' ? 'auto' : 'center',
150
+ color: colors.onSurface,
151
+ fontSize: 16,
152
+ }}
153
+ />
154
+ {suffix ? <T>{suffix}</T> : null}
155
+ {controlsPosition === 'end' ? (
156
+ <View
157
+ style={{
158
+ flexDirection: vertical ? 'column' : 'row',
159
+ flexShrink: 0,
160
+ gap: vertical ? 0 : 6,
161
+ borderWidth: vertical ? 1 : 0,
162
+ borderColor: colors.outlineVariant,
163
+ borderRadius: vertical ? 16 : 0,
164
+ overflow: 'hidden',
165
+ backgroundColor: vertical ? colors.secondaryContainer : undefined,
166
+ }}
167
+ >
168
+ {vertical ? (
169
+ <>
170
+ {stepButton(1)}
171
+ {stepButton(-1)}
172
+ </>
173
+ ) : (
174
+ <>
175
+ {stepButton(-1)}
176
+ {stepButton(1)}
177
+ </>
178
+ )}
179
+ </View>
180
+ ) : (
181
+ stepButton(1)
182
+ )}
183
+ </View>
184
+ {error || supportingText ? (
185
+ <T variant="bodySmall" color={error ? 'error' : 'onSurfaceVariant'}>
186
+ {typeof error === 'string' ? error : supportingText}
187
+ </T>
188
+ ) : null}
189
+ </View>
190
+ );
13
191
  }