@expressive-ui/native 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/Button.d.ts +48 -0
- package/dist/Button.d.ts.map +1 -0
- package/dist/Button.js +122 -0
- package/dist/Button.js.map +1 -0
- package/dist/Carousel.d.ts +13 -0
- package/dist/Carousel.d.ts.map +1 -0
- package/dist/Carousel.js +52 -0
- package/dist/Carousel.js.map +1 -0
- package/dist/Chips.d.ts +35 -0
- package/dist/Chips.d.ts.map +1 -0
- package/dist/Chips.js +138 -0
- package/dist/Chips.js.map +1 -0
- package/dist/Dialogs.d.ts +42 -0
- package/dist/Dialogs.d.ts.map +1 -0
- package/dist/Dialogs.js +115 -0
- package/dist/Dialogs.js.map +1 -0
- package/dist/ExpressiveCard.d.ts +20 -0
- package/dist/ExpressiveCard.d.ts.map +1 -0
- package/dist/ExpressiveCard.js +71 -0
- package/dist/ExpressiveCard.js.map +1 -0
- package/dist/Fab.d.ts +16 -0
- package/dist/Fab.d.ts.map +1 -0
- package/dist/Fab.js +104 -0
- package/dist/Fab.js.map +1 -0
- package/dist/Fields.d.ts +90 -0
- package/dist/Fields.d.ts.map +1 -0
- package/dist/Fields.js +211 -0
- package/dist/Fields.js.map +1 -0
- package/dist/Grid.d.ts +8 -0
- package/dist/Grid.d.ts.map +1 -0
- package/dist/Grid.js +16 -0
- package/dist/Grid.js.map +1 -0
- package/dist/Inputs.d.ts +33 -0
- package/dist/Inputs.d.ts.map +1 -0
- package/dist/Inputs.js +172 -0
- package/dist/Inputs.js.map +1 -0
- package/dist/List.d.ts +20 -0
- package/dist/List.d.ts.map +1 -0
- package/dist/List.js +222 -0
- package/dist/List.js.map +1 -0
- package/dist/Menu.d.ts +26 -0
- package/dist/Menu.d.ts.map +1 -0
- package/dist/Menu.js +117 -0
- package/dist/Menu.js.map +1 -0
- package/dist/Navigation.d.ts +34 -0
- package/dist/Navigation.d.ts.map +1 -0
- package/dist/Navigation.js +80 -0
- package/dist/Navigation.js.map +1 -0
- package/dist/Pickers.d.ts +16 -0
- package/dist/Pickers.d.ts.map +1 -0
- package/dist/Pickers.js +100 -0
- package/dist/Pickers.js.map +1 -0
- package/dist/Progress.d.ts +20 -0
- package/dist/Progress.d.ts.map +1 -0
- package/dist/Progress.js +93 -0
- package/dist/Progress.js.map +1 -0
- package/dist/Search.d.ts +50 -0
- package/dist/Search.d.ts.map +1 -0
- package/dist/Search.js +121 -0
- package/dist/Search.js.map +1 -0
- package/dist/Sheets.d.ts +44 -0
- package/dist/Sheets.d.ts.map +1 -0
- package/dist/Sheets.js +128 -0
- package/dist/Sheets.js.map +1 -0
- package/dist/Toolbar.d.ts +13 -0
- package/dist/Toolbar.d.ts.map +1 -0
- package/dist/Toolbar.js +38 -0
- package/dist/Toolbar.js.map +1 -0
- package/dist/Transition.d.ts +34 -0
- package/dist/Transition.d.ts.map +1 -0
- package/dist/Transition.js +93 -0
- package/dist/Transition.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +41 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +55 -0
- package/dist/primitives.js.map +1 -0
- package/dist/theme.d.ts +57 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +89 -0
- package/dist/theme.js.map +1 -0
- package/package.json +53 -4
- package/src/Button.tsx +347 -0
- package/src/Carousel.tsx +108 -0
- package/src/Chips.tsx +292 -0
- package/src/Dialogs.tsx +252 -0
- package/src/ExpressiveCard.tsx +205 -0
- package/src/Fab.tsx +187 -0
- package/src/Fields.tsx +556 -0
- package/src/Grid.tsx +29 -0
- package/src/Inputs.tsx +290 -0
- package/src/List.tsx +295 -0
- package/src/Menu.tsx +238 -0
- package/src/Navigation.tsx +198 -0
- package/src/Pickers.tsx +151 -0
- package/src/Progress.tsx +192 -0
- package/src/Search.tsx +351 -0
- package/src/Sheets.tsx +273 -0
- package/src/Toolbar.tsx +88 -0
- package/src/Transition.tsx +155 -0
- package/src/index.ts +20 -0
- package/src/primitives.tsx +110 -0
- package/src/theme.tsx +125 -0
package/src/Fields.tsx
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { Animated, Pressable, StyleSheet, TextInput, View, type KeyboardTypeOptions, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import Svg, { Path } from 'react-native-svg';
|
|
4
|
+
import {
|
|
5
|
+
CHECK_PATH,
|
|
6
|
+
MIXED_PATH,
|
|
7
|
+
parentCheckState,
|
|
8
|
+
resolveCheckbox,
|
|
9
|
+
resolveField,
|
|
10
|
+
resolveRadio,
|
|
11
|
+
resolveTooltip,
|
|
12
|
+
useField,
|
|
13
|
+
type FieldVariant,
|
|
14
|
+
type RadioGroupVariant,
|
|
15
|
+
type RadioOption,
|
|
16
|
+
type TooltipPlacement,
|
|
17
|
+
} from '@expressive-ui/core';
|
|
18
|
+
import { tokens, type TypeScaleToken } from '@expressive-ui/tokens';
|
|
19
|
+
import { Button } from './Button.js';
|
|
20
|
+
import { Icon, T, typeStyle, withAlpha } from './primitives.js';
|
|
21
|
+
import { useExpressiveTheme, useSpringValue } from './theme.js';
|
|
22
|
+
|
|
23
|
+
// --- TextField ------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
export type TextFieldProps = {
|
|
26
|
+
label: string;
|
|
27
|
+
value?: string;
|
|
28
|
+
defaultValue?: string;
|
|
29
|
+
onChange?: (value: string) => void;
|
|
30
|
+
onSubmit?: (value: string) => void;
|
|
31
|
+
variant?: FieldVariant;
|
|
32
|
+
placeholder?: string;
|
|
33
|
+
leadingIcon?: string;
|
|
34
|
+
trailingIcon?: string;
|
|
35
|
+
trailing?: ReactNode;
|
|
36
|
+
clearable?: boolean;
|
|
37
|
+
password?: boolean;
|
|
38
|
+
prefix?: string;
|
|
39
|
+
suffix?: string;
|
|
40
|
+
supportingText?: string;
|
|
41
|
+
error?: string | boolean;
|
|
42
|
+
maxLength?: number;
|
|
43
|
+
required?: boolean;
|
|
44
|
+
disabled?: boolean;
|
|
45
|
+
multiline?: boolean;
|
|
46
|
+
keyboardType?: KeyboardTypeOptions;
|
|
47
|
+
autoComplete?: React.ComponentProps<typeof TextInput>['autoComplete'];
|
|
48
|
+
style?: StyleProp<ViewStyle>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Text field do M3 (filled = TextField do RM/Elo; outlined com a label sobre o contorno). */
|
|
52
|
+
export function TextField({
|
|
53
|
+
label,
|
|
54
|
+
value,
|
|
55
|
+
defaultValue,
|
|
56
|
+
onChange,
|
|
57
|
+
onSubmit,
|
|
58
|
+
variant = 'filled',
|
|
59
|
+
placeholder,
|
|
60
|
+
leadingIcon,
|
|
61
|
+
trailingIcon,
|
|
62
|
+
trailing,
|
|
63
|
+
clearable = false,
|
|
64
|
+
password = false,
|
|
65
|
+
prefix,
|
|
66
|
+
suffix,
|
|
67
|
+
supportingText,
|
|
68
|
+
error,
|
|
69
|
+
maxLength,
|
|
70
|
+
required,
|
|
71
|
+
disabled = false,
|
|
72
|
+
multiline = false,
|
|
73
|
+
keyboardType,
|
|
74
|
+
autoComplete,
|
|
75
|
+
style,
|
|
76
|
+
}: TextFieldProps) {
|
|
77
|
+
const { labels } = useExpressiveTheme();
|
|
78
|
+
const { colors, mode } = useExpressiveTheme();
|
|
79
|
+
const spec = resolveField(variant);
|
|
80
|
+
const [inner, setInner] = useState(defaultValue ?? '');
|
|
81
|
+
const text = value ?? inner;
|
|
82
|
+
const field = useField({ value: text, error, supportingText, maxLength, required, label });
|
|
83
|
+
const [focused, setFocused] = useState(false);
|
|
84
|
+
const [hidden, setHidden] = useState(true);
|
|
85
|
+
const floating = focused || field.filled || Boolean(prefix || suffix);
|
|
86
|
+
const float = useSpringValue(floating ? 1 : 0, 'spatialFast');
|
|
87
|
+
const focus = useSpringValue(focused || field.invalid ? 1 : 0, 'spatialDefault');
|
|
88
|
+
const outlined = variant === 'outlined';
|
|
89
|
+
const accent = field.invalid ? spec.indicator.error : focused ? spec.indicator.focus : null;
|
|
90
|
+
const labelColor = colors[field.invalid ? spec.label.error : focused ? spec.label.focus : spec.label.color];
|
|
91
|
+
const set = (next: string) => {
|
|
92
|
+
if (value === undefined) setInner(next);
|
|
93
|
+
onChange?.(next);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const rest = typeStyle(spec.label.rest);
|
|
97
|
+
const small = typeStyle(spec.label.floating);
|
|
98
|
+
const leadOffset = leadingIcon ? 36 : 0;
|
|
99
|
+
// Label: centro do campo → topo (filled) ou sobre o contorno (outlined, alinhada ao ícone).
|
|
100
|
+
const labelTop = float.interpolate({ inputRange: [0, 1], outputRange: [(spec.height - (rest.lineHeight ?? 24)) / 2, outlined ? -(small.lineHeight ?? 16) / 2 : 8] });
|
|
101
|
+
const labelScale = float.interpolate({ inputRange: [0, 1], outputRange: [1, (small.fontSize ?? 12) / (rest.fontSize ?? 16)] });
|
|
102
|
+
const labelShift = float.interpolate({ inputRange: [0, 1], outputRange: [0, outlined ? -leadOffset : 0] });
|
|
103
|
+
|
|
104
|
+
let trail: ReactNode = null;
|
|
105
|
+
if (field.invalid) trail = <Icon name="error" color="error" filled />;
|
|
106
|
+
else if (password)
|
|
107
|
+
trail = (
|
|
108
|
+
<Pressable accessibilityRole="button" accessibilityLabel={hidden ? labels.showPassword : labels.hidePassword} onPress={() => setHidden(!hidden)} style={styles.action}>
|
|
109
|
+
<Icon name={hidden ? 'visibility' : 'visibility_off'} color={spec.icon.color} />
|
|
110
|
+
</Pressable>
|
|
111
|
+
);
|
|
112
|
+
else if (clearable && text)
|
|
113
|
+
trail = (
|
|
114
|
+
<Pressable accessibilityRole="button" accessibilityLabel={`Limpar ${label}`} onPress={() => set('')} style={styles.action}>
|
|
115
|
+
<Icon name="cancel" color={spec.icon.color} />
|
|
116
|
+
</Pressable>
|
|
117
|
+
);
|
|
118
|
+
else if (trailing) trail = trailing;
|
|
119
|
+
else if (trailingIcon) trail = <Icon name={trailingIcon} color={spec.icon.color} />;
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<View style={[{ opacity: disabled ? spec.disabledOpacity : 1 }, style]}>
|
|
123
|
+
<View
|
|
124
|
+
style={[
|
|
125
|
+
styles.box,
|
|
126
|
+
{
|
|
127
|
+
minHeight: spec.height,
|
|
128
|
+
paddingLeft: leadingIcon ? 12 : spec.paddingX,
|
|
129
|
+
paddingRight: trail ? 4 : spec.paddingX,
|
|
130
|
+
backgroundColor: spec.container ? colors[spec.container] : 'transparent',
|
|
131
|
+
borderRadius: spec.radius,
|
|
132
|
+
},
|
|
133
|
+
!outlined && { borderBottomLeftRadius: 0, borderBottomRightRadius: 0 },
|
|
134
|
+
outlined && {
|
|
135
|
+
borderWidth: accent ? spec.indicator.focusWidth : spec.indicator.width,
|
|
136
|
+
borderColor: colors[accent ?? spec.indicator.rest],
|
|
137
|
+
},
|
|
138
|
+
]}
|
|
139
|
+
>
|
|
140
|
+
{leadingIcon ? <Icon name={leadingIcon} color={field.invalid ? spec.icon.error : spec.icon.color} /> : null}
|
|
141
|
+
<View style={styles.control}>
|
|
142
|
+
{prefix && floating ? <T variant="bodyLarge" color={spec.supporting.color} style={styles.affix}>{prefix}</T> : null}
|
|
143
|
+
<TextInput
|
|
144
|
+
accessibilityLabel={field.labelText}
|
|
145
|
+
accessibilityHint={field.help ?? undefined}
|
|
146
|
+
value={text}
|
|
147
|
+
onChangeText={set}
|
|
148
|
+
onSubmitEditing={() => onSubmit?.(text)}
|
|
149
|
+
placeholder={focused ? placeholder : undefined}
|
|
150
|
+
placeholderTextColor={colors[spec.input.placeholder]}
|
|
151
|
+
secureTextEntry={password && hidden}
|
|
152
|
+
editable={!disabled}
|
|
153
|
+
maxLength={maxLength}
|
|
154
|
+
multiline={multiline}
|
|
155
|
+
keyboardType={keyboardType}
|
|
156
|
+
autoComplete={autoComplete}
|
|
157
|
+
keyboardAppearance={mode}
|
|
158
|
+
selectionColor={colors[spec.indicator.focus]}
|
|
159
|
+
onFocus={() => setFocused(true)}
|
|
160
|
+
onBlur={() => setFocused(false)}
|
|
161
|
+
style={[
|
|
162
|
+
typeStyle(spec.input.type),
|
|
163
|
+
styles.input,
|
|
164
|
+
{ color: colors[spec.input.color], paddingTop: outlined ? 16 : 24, paddingBottom: outlined ? 16 : 8 },
|
|
165
|
+
multiline && { minHeight: 96, textAlignVertical: 'top' },
|
|
166
|
+
]}
|
|
167
|
+
/>
|
|
168
|
+
{suffix && floating ? <T variant="bodyLarge" color={spec.supporting.color} style={styles.affix}>{suffix}</T> : null}
|
|
169
|
+
<Animated.Text
|
|
170
|
+
pointerEvents="none"
|
|
171
|
+
numberOfLines={1}
|
|
172
|
+
style={[
|
|
173
|
+
rest,
|
|
174
|
+
styles.label,
|
|
175
|
+
{
|
|
176
|
+
color: labelColor,
|
|
177
|
+
top: labelTop,
|
|
178
|
+
transform: [{ translateX: labelShift }, { scale: labelScale }],
|
|
179
|
+
transformOrigin: 'left top',
|
|
180
|
+
},
|
|
181
|
+
outlined && floating && { backgroundColor: colors.surface, paddingHorizontal: 4, marginLeft: -4 },
|
|
182
|
+
]}
|
|
183
|
+
>
|
|
184
|
+
{field.labelText}
|
|
185
|
+
</Animated.Text>
|
|
186
|
+
</View>
|
|
187
|
+
{trail ? <View style={styles.trail}>{trail}</View> : null}
|
|
188
|
+
{!outlined ? (
|
|
189
|
+
<>
|
|
190
|
+
<View pointerEvents="none" style={[styles.line, { height: 1, backgroundColor: colors[spec.indicator.rest] }]} />
|
|
191
|
+
<Animated.View
|
|
192
|
+
pointerEvents="none"
|
|
193
|
+
style={[styles.line, { height: 2, backgroundColor: colors[field.invalid ? spec.indicator.error : spec.indicator.focus], transform: [{ scaleX: focus }] }]}
|
|
194
|
+
/>
|
|
195
|
+
</>
|
|
196
|
+
) : null}
|
|
197
|
+
</View>
|
|
198
|
+
{field.help || field.counter ? (
|
|
199
|
+
<View style={styles.support}>
|
|
200
|
+
<T variant={spec.supporting.type} color={field.invalid ? spec.supporting.error : spec.supporting.color} accessibilityLiveRegion={field.invalid ? 'polite' : undefined} style={{ flex: 1 }}>
|
|
201
|
+
{field.help ?? ''}
|
|
202
|
+
</T>
|
|
203
|
+
{field.counter ? (
|
|
204
|
+
<T variant={spec.supporting.type} color={spec.supporting.color} style={{ fontVariant: ['tabular-nums'] }}>
|
|
205
|
+
{field.counter}
|
|
206
|
+
</T>
|
|
207
|
+
) : null}
|
|
208
|
+
</View>
|
|
209
|
+
) : null}
|
|
210
|
+
</View>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// --- Radio e Checkbox -----------------------------------------------------------------------
|
|
215
|
+
|
|
216
|
+
export type RadioProps = { checked?: boolean; onChange?: (checked: boolean) => void; label?: string; disabled?: boolean; invalid?: boolean; style?: StyleProp<ViewStyle> };
|
|
217
|
+
|
|
218
|
+
/** Radio do M3; o ponto entra com a mola spatial. */
|
|
219
|
+
export function Radio({ checked = false, onChange, label, disabled, invalid, style }: RadioProps) {
|
|
220
|
+
const { colors } = useExpressiveTheme();
|
|
221
|
+
const spec = resolveRadio();
|
|
222
|
+
const dot = useSpringValue(checked ? 1 : 0, 'spatialFast');
|
|
223
|
+
const color = colors[invalid ? spec.error : checked ? spec.selected : spec.unselected];
|
|
224
|
+
return (
|
|
225
|
+
<Pressable
|
|
226
|
+
accessibilityRole="radio"
|
|
227
|
+
accessibilityLabel={label}
|
|
228
|
+
accessibilityState={{ checked, disabled }}
|
|
229
|
+
disabled={disabled}
|
|
230
|
+
onPress={() => onChange?.(true)}
|
|
231
|
+
style={({ pressed }) => [
|
|
232
|
+
styles.control40,
|
|
233
|
+
{ width: spec.stateLayer, height: spec.stateLayer, borderRadius: spec.stateLayer / 2, opacity: disabled ? spec.disabledOpacity : 1 },
|
|
234
|
+
pressed && { backgroundColor: withAlpha(color, tokens.state.pressed) },
|
|
235
|
+
style,
|
|
236
|
+
]}
|
|
237
|
+
>
|
|
238
|
+
<View style={{ width: spec.size, height: spec.size, borderRadius: spec.size / 2, borderWidth: spec.border, borderColor: color, alignItems: 'center', justifyContent: 'center' }}>
|
|
239
|
+
<Animated.View style={{ width: spec.dot, height: spec.dot, borderRadius: spec.dot / 2, backgroundColor: color, transform: [{ scale: dot }] }} />
|
|
240
|
+
</View>
|
|
241
|
+
</Pressable>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export type CheckboxProps = { checked?: boolean | 'mixed'; onChange?: (checked: boolean) => void; label?: string; disabled?: boolean; invalid?: boolean; style?: StyleProp<ViewStyle> };
|
|
246
|
+
|
|
247
|
+
/** Checkbox do M3: caixa de 18px que preenche com ✓ ou − (indeterminado). */
|
|
248
|
+
export function Checkbox({ checked = false, onChange, label, disabled, invalid, style }: CheckboxProps) {
|
|
249
|
+
const { colors } = useExpressiveTheme();
|
|
250
|
+
const spec = resolveCheckbox();
|
|
251
|
+
const on = checked !== false;
|
|
252
|
+
const fill = useSpringValue(on ? 1 : 0, 'effectsFast');
|
|
253
|
+
const color = colors[invalid ? spec.error : on ? spec.checked.container : spec.unchecked];
|
|
254
|
+
return (
|
|
255
|
+
<Pressable
|
|
256
|
+
accessibilityRole="checkbox"
|
|
257
|
+
accessibilityLabel={label}
|
|
258
|
+
accessibilityState={{ checked: checked === 'mixed' ? 'mixed' : checked, disabled }}
|
|
259
|
+
disabled={disabled}
|
|
260
|
+
onPress={() => onChange?.(checked !== true)}
|
|
261
|
+
style={({ pressed }) => [
|
|
262
|
+
styles.control40,
|
|
263
|
+
{ width: spec.stateLayer, height: spec.stateLayer, borderRadius: spec.stateLayer / 2, opacity: disabled ? spec.disabledOpacity : 1 },
|
|
264
|
+
pressed && { backgroundColor: withAlpha(color, tokens.state.pressed) },
|
|
265
|
+
style,
|
|
266
|
+
]}
|
|
267
|
+
>
|
|
268
|
+
<View style={{ width: spec.size, height: spec.size, borderRadius: spec.radius, borderWidth: spec.border, borderColor: color, overflow: 'hidden' }}>
|
|
269
|
+
<Animated.View style={[StyleSheet.absoluteFill, { backgroundColor: color, opacity: fill }]} />
|
|
270
|
+
{on ? (
|
|
271
|
+
<Svg width={spec.size} height={spec.size} viewBox="0 0 18 18" style={{ position: 'absolute', left: -spec.border, top: -spec.border }}>
|
|
272
|
+
<Path d={checked === 'mixed' ? MIXED_PATH : CHECK_PATH} stroke={colors[spec.checked.content]} strokeWidth={2} fill="none" strokeLinecap="square" />
|
|
273
|
+
</Svg>
|
|
274
|
+
) : null}
|
|
275
|
+
</View>
|
|
276
|
+
</Pressable>
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// --- Grupos ---------------------------------------------------------------------------------
|
|
281
|
+
|
|
282
|
+
function OptionRow({
|
|
283
|
+
option,
|
|
284
|
+
checked,
|
|
285
|
+
variant,
|
|
286
|
+
first,
|
|
287
|
+
last,
|
|
288
|
+
control,
|
|
289
|
+
onPress,
|
|
290
|
+
}: {
|
|
291
|
+
option: RadioOption;
|
|
292
|
+
checked: boolean;
|
|
293
|
+
variant: RadioGroupVariant;
|
|
294
|
+
first: boolean;
|
|
295
|
+
last: boolean;
|
|
296
|
+
control: ReactNode;
|
|
297
|
+
onPress: () => void;
|
|
298
|
+
}) {
|
|
299
|
+
const { colors } = useExpressiveTheme();
|
|
300
|
+
const segmented = variant === 'segmented';
|
|
301
|
+
const top = useSpringValue(segmented && (first || checked) ? 20 : 4, 'spatialDefault');
|
|
302
|
+
const bottom = useSpringValue(segmented && (last || checked) ? 20 : 4, 'spatialDefault');
|
|
303
|
+
const fg = segmented && checked ? 'onSecondaryContainer' : 'onSurface';
|
|
304
|
+
return (
|
|
305
|
+
<Pressable accessible={false} disabled={option.disabled} onPress={onPress} style={variant === 'inline' ? undefined : { alignSelf: 'stretch' }}>
|
|
306
|
+
<Animated.View
|
|
307
|
+
style={[
|
|
308
|
+
styles.option,
|
|
309
|
+
{ opacity: option.disabled ? tokens.state.disabledContent : 1 },
|
|
310
|
+
segmented && {
|
|
311
|
+
minHeight: 56,
|
|
312
|
+
paddingLeft: 4,
|
|
313
|
+
paddingRight: 16,
|
|
314
|
+
backgroundColor: colors[checked ? 'secondaryContainer' : 'surfaceContainerLowest'],
|
|
315
|
+
borderTopLeftRadius: top,
|
|
316
|
+
borderTopRightRadius: top,
|
|
317
|
+
borderBottomLeftRadius: bottom,
|
|
318
|
+
borderBottomRightRadius: bottom,
|
|
319
|
+
},
|
|
320
|
+
]}
|
|
321
|
+
>
|
|
322
|
+
{control}
|
|
323
|
+
{option.icon ? <Icon name={option.icon} color="onSurfaceVariant" /> : null}
|
|
324
|
+
<View style={styles.optionText}>
|
|
325
|
+
<T variant="bodyLarge" color={fg}>
|
|
326
|
+
{option.label}
|
|
327
|
+
</T>
|
|
328
|
+
{option.supporting ? (
|
|
329
|
+
<T variant="bodyMedium" color={segmented && checked ? fg : 'onSurfaceVariant'}>
|
|
330
|
+
{option.supporting}
|
|
331
|
+
</T>
|
|
332
|
+
) : null}
|
|
333
|
+
</View>
|
|
334
|
+
</Animated.View>
|
|
335
|
+
</Pressable>
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function GroupFrame({ label, variant, help, invalid, children }: { label: string; variant: RadioGroupVariant; help?: string; invalid: boolean; children: ReactNode }) {
|
|
340
|
+
return (
|
|
341
|
+
<View>
|
|
342
|
+
<T variant="titleSmall" accessibilityRole="header" style={{ marginBottom: 8 }}>
|
|
343
|
+
{label}
|
|
344
|
+
</T>
|
|
345
|
+
<View style={variant === 'inline' ? styles.inline : variant === 'segmented' ? { gap: 2 } : undefined}>{children}</View>
|
|
346
|
+
{help ? (
|
|
347
|
+
<T variant="bodySmall" color={invalid ? 'error' : 'onSurfaceVariant'} style={{ marginTop: 4 }}>
|
|
348
|
+
{help}
|
|
349
|
+
</T>
|
|
350
|
+
) : null}
|
|
351
|
+
</View>
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export type RadioGroupProps<V extends string> = {
|
|
356
|
+
options: readonly RadioOption<V>[];
|
|
357
|
+
value: V | null;
|
|
358
|
+
onChange?: (value: V) => void;
|
|
359
|
+
label: string;
|
|
360
|
+
variant?: RadioGroupVariant;
|
|
361
|
+
supportingText?: string;
|
|
362
|
+
error?: string | boolean;
|
|
363
|
+
style?: StyleProp<ViewStyle>;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
export function RadioGroup<V extends string>({ options, value, onChange, label, variant = 'list', supportingText, error, style }: RadioGroupProps<V>) {
|
|
367
|
+
const invalid = Boolean(error);
|
|
368
|
+
return (
|
|
369
|
+
<View accessibilityRole="radiogroup" accessibilityLabel={label} style={style}>
|
|
370
|
+
<GroupFrame label={label} variant={variant} help={typeof error === 'string' ? error : supportingText} invalid={invalid}>
|
|
371
|
+
{options.map((option, i) => (
|
|
372
|
+
<OptionRow
|
|
373
|
+
key={option.value}
|
|
374
|
+
option={option}
|
|
375
|
+
checked={option.value === value}
|
|
376
|
+
variant={variant}
|
|
377
|
+
first={i === 0}
|
|
378
|
+
last={i === options.length - 1}
|
|
379
|
+
onPress={() => onChange?.(option.value)}
|
|
380
|
+
control={<Radio checked={option.value === value} label={option.label} disabled={option.disabled} invalid={invalid} onChange={() => onChange?.(option.value)} />}
|
|
381
|
+
/>
|
|
382
|
+
))}
|
|
383
|
+
</GroupFrame>
|
|
384
|
+
</View>
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export type CheckboxGroupProps<V extends string> = {
|
|
389
|
+
options: readonly RadioOption<V>[];
|
|
390
|
+
value: readonly V[];
|
|
391
|
+
onChange?: (value: V[]) => void;
|
|
392
|
+
label: string;
|
|
393
|
+
variant?: RadioGroupVariant;
|
|
394
|
+
selectAll?: string;
|
|
395
|
+
supportingText?: string;
|
|
396
|
+
error?: string | boolean;
|
|
397
|
+
style?: StyleProp<ViewStyle>;
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
export function CheckboxGroup<V extends string>({ options, value, onChange, label, variant = 'list', selectAll, supportingText, error, style }: CheckboxGroupProps<V>) {
|
|
401
|
+
const invalid = Boolean(error);
|
|
402
|
+
const enabled = options.filter((option) => !option.disabled).map((option) => option.value);
|
|
403
|
+
const parent = parentCheckState(enabled.filter((v) => value.includes(v)).length, enabled.length);
|
|
404
|
+
const toggle = (option: V) => onChange?.(value.includes(option) ? value.filter((v) => v !== option) : [...value, option]);
|
|
405
|
+
const toggleAll = () => onChange?.(parent === true ? value.filter((v) => !enabled.includes(v)) : [...new Set([...value, ...enabled])]);
|
|
406
|
+
return (
|
|
407
|
+
<View accessibilityLabel={label} style={style}>
|
|
408
|
+
<GroupFrame label={label} variant={variant} help={typeof error === 'string' ? error : supportingText} invalid={invalid}>
|
|
409
|
+
{selectAll ? (
|
|
410
|
+
<OptionRow
|
|
411
|
+
option={{ value: '__all', label: selectAll }}
|
|
412
|
+
checked={parent === true}
|
|
413
|
+
variant={variant}
|
|
414
|
+
first
|
|
415
|
+
last={false}
|
|
416
|
+
onPress={toggleAll}
|
|
417
|
+
control={<Checkbox checked={parent} label={selectAll} invalid={invalid} onChange={toggleAll} />}
|
|
418
|
+
/>
|
|
419
|
+
) : null}
|
|
420
|
+
{options.map((option, i) => (
|
|
421
|
+
<View key={option.value} style={variant === 'list' && selectAll ? { paddingLeft: 32 } : undefined}>
|
|
422
|
+
<OptionRow
|
|
423
|
+
option={option}
|
|
424
|
+
checked={value.includes(option.value)}
|
|
425
|
+
variant={variant}
|
|
426
|
+
first={!selectAll && i === 0}
|
|
427
|
+
last={i === options.length - 1}
|
|
428
|
+
onPress={() => toggle(option.value)}
|
|
429
|
+
control={<Checkbox checked={value.includes(option.value)} label={option.label} disabled={option.disabled} invalid={invalid} onChange={() => toggle(option.value)} />}
|
|
430
|
+
/>
|
|
431
|
+
</View>
|
|
432
|
+
))}
|
|
433
|
+
</GroupFrame>
|
|
434
|
+
</View>
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// --- Tooltip --------------------------------------------------------------------------------
|
|
439
|
+
|
|
440
|
+
export type TooltipProps = {
|
|
441
|
+
text: string;
|
|
442
|
+
title?: string;
|
|
443
|
+
actions?: readonly { label: string; onPress?: () => void }[];
|
|
444
|
+
placement?: TooltipPlacement;
|
|
445
|
+
/** Controlado; sem isso abre com toque longo (plain some sozinho; rich fecha ao tocar fora/numa ação). */
|
|
446
|
+
open?: boolean;
|
|
447
|
+
onOpenChange?: (open: boolean) => void;
|
|
448
|
+
children: ReactNode;
|
|
449
|
+
style?: StyleProp<ViewStyle>;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
/** Tooltip do M3 no celular: toque longo no gatilho mostra; o plain some após ~1,5 s. */
|
|
453
|
+
export function Tooltip({ text, title, actions, placement = 'top', open: controlled, onOpenChange, children, style }: TooltipProps) {
|
|
454
|
+
const { colors, mode } = useExpressiveTheme();
|
|
455
|
+
const spec = resolveTooltip();
|
|
456
|
+
const rich = Boolean(title || actions?.length);
|
|
457
|
+
const [inner, setInner] = useState(false);
|
|
458
|
+
const open = controlled ?? inner;
|
|
459
|
+
const setOpen = (next: boolean) => {
|
|
460
|
+
if (controlled === undefined) setInner(next);
|
|
461
|
+
onOpenChange?.(next);
|
|
462
|
+
};
|
|
463
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
464
|
+
const shown = useSpringValue(open ? 1 : 0, 'spatialFast');
|
|
465
|
+
useEffect(() => () => void (timer.current && clearTimeout(timer.current)), []);
|
|
466
|
+
|
|
467
|
+
const clear = () => timer.current && clearTimeout(timer.current);
|
|
468
|
+
const position: ViewStyle =
|
|
469
|
+
placement === 'top'
|
|
470
|
+
? { bottom: '100%', marginBottom: spec.offset, ...(rich ? { left: 0 } : { alignSelf: 'center' }) }
|
|
471
|
+
: placement === 'bottom'
|
|
472
|
+
? { top: '100%', marginTop: spec.offset, ...(rich ? { left: 0 } : { alignSelf: 'center' }) }
|
|
473
|
+
: placement === 'start'
|
|
474
|
+
? { right: '100%', marginRight: spec.offset, alignSelf: 'center' }
|
|
475
|
+
: { left: '100%', marginLeft: spec.offset, alignSelf: 'center' };
|
|
476
|
+
const p = rich ? spec.rich : spec.plain;
|
|
477
|
+
return (
|
|
478
|
+
<View
|
|
479
|
+
style={[styles.tipAnchor, style]}
|
|
480
|
+
onTouchStart={() => {
|
|
481
|
+
clear();
|
|
482
|
+
timer.current = setTimeout(() => setOpen(true), spec.showDelay);
|
|
483
|
+
}}
|
|
484
|
+
onTouchEnd={() => {
|
|
485
|
+
clear();
|
|
486
|
+
if (!rich && open) timer.current = setTimeout(() => setOpen(false), spec.touchDuration);
|
|
487
|
+
}}
|
|
488
|
+
>
|
|
489
|
+
{children}
|
|
490
|
+
{open ? (
|
|
491
|
+
<Animated.View
|
|
492
|
+
accessibilityLiveRegion="polite"
|
|
493
|
+
accessibilityRole={rich ? undefined : 'text'}
|
|
494
|
+
style={[
|
|
495
|
+
styles.tip,
|
|
496
|
+
position,
|
|
497
|
+
{
|
|
498
|
+
maxWidth: p.maxWidth,
|
|
499
|
+
backgroundColor: colors[p.container],
|
|
500
|
+
borderRadius: p.radius,
|
|
501
|
+
opacity: shown,
|
|
502
|
+
transform: [{ scale: shown.interpolate({ inputRange: [0, 1], outputRange: [0.9, 1] }) }],
|
|
503
|
+
},
|
|
504
|
+
rich
|
|
505
|
+
? { padding: spec.rich.padding, paddingBottom: actions?.length ? 8 : spec.rich.padding, gap: 4, boxShadow: tokens.elevation[mode][2], width: p.maxWidth }
|
|
506
|
+
: { minHeight: spec.plain.minHeight, paddingHorizontal: spec.plain.paddingX, paddingVertical: 4, justifyContent: 'center' },
|
|
507
|
+
]}
|
|
508
|
+
>
|
|
509
|
+
{title ? (
|
|
510
|
+
<T variant={spec.rich.titleType as TypeScaleToken} emphasized color={spec.rich.title}>
|
|
511
|
+
{title}
|
|
512
|
+
</T>
|
|
513
|
+
) : null}
|
|
514
|
+
<T variant={p.type} color={rich ? spec.rich.content : spec.plain.content}>
|
|
515
|
+
{text}
|
|
516
|
+
</T>
|
|
517
|
+
{actions?.length ? (
|
|
518
|
+
<View style={styles.tipActions}>
|
|
519
|
+
{actions.map((action) => (
|
|
520
|
+
<Button
|
|
521
|
+
key={action.label}
|
|
522
|
+
action={{ id: action.label, label: action.label }}
|
|
523
|
+
variant="text"
|
|
524
|
+
size="sm"
|
|
525
|
+
onPress={() => {
|
|
526
|
+
action.onPress?.();
|
|
527
|
+
setOpen(false);
|
|
528
|
+
}}
|
|
529
|
+
/>
|
|
530
|
+
))}
|
|
531
|
+
</View>
|
|
532
|
+
) : null}
|
|
533
|
+
</Animated.View>
|
|
534
|
+
) : null}
|
|
535
|
+
</View>
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const styles = StyleSheet.create({
|
|
540
|
+
box: { flexDirection: 'row', alignItems: 'center', gap: 12 },
|
|
541
|
+
control: { flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'center', alignSelf: 'stretch' },
|
|
542
|
+
input: { flex: 1, paddingHorizontal: 0 },
|
|
543
|
+
affix: { paddingTop: 16 },
|
|
544
|
+
label: { position: 'absolute', left: 0 },
|
|
545
|
+
trail: { flexDirection: 'row', alignItems: 'center' },
|
|
546
|
+
action: { width: 48, height: 48, borderRadius: 24, alignItems: 'center', justifyContent: 'center' },
|
|
547
|
+
line: { position: 'absolute', left: 0, right: 0, bottom: 0 },
|
|
548
|
+
support: { flexDirection: 'row', gap: 16, paddingHorizontal: 16, paddingTop: 4 },
|
|
549
|
+
control40: { alignItems: 'center', justifyContent: 'center' },
|
|
550
|
+
option: { flexDirection: 'row', alignItems: 'center', gap: 8, minHeight: 48 },
|
|
551
|
+
optionText: { flex: 1, paddingVertical: 8 },
|
|
552
|
+
inline: { flexDirection: 'row', flexWrap: 'wrap', columnGap: 16 },
|
|
553
|
+
tipAnchor: { alignSelf: 'flex-start' },
|
|
554
|
+
tip: { position: 'absolute', zIndex: 70 },
|
|
555
|
+
tipActions: { flexDirection: 'row', gap: 8, marginHorizontal: -12, marginTop: 4 },
|
|
556
|
+
});
|
package/src/Grid.tsx
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Children, useState } from 'react';
|
|
2
|
+
import { View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { SiblingIndexContext, type RuntimeProps } from '@expressive-ui/core';
|
|
4
|
+
import { GRID_DEFAULTS, type GridData } from '@expressive-ui/schema';
|
|
5
|
+
|
|
6
|
+
const GAPS = { sm: 8, md: 12, lg: 16 } as const;
|
|
7
|
+
// Mesma regra do web: no máximo `columns`, sem deixar item abaixo de ~160dp.
|
|
8
|
+
const MIN_ITEM = 160;
|
|
9
|
+
|
|
10
|
+
export type GridProps = GridData & RuntimeProps & { style?: StyleProp<ViewStyle> };
|
|
11
|
+
|
|
12
|
+
export function Grid({ columns = GRID_DEFAULTS.columns, gap = GRID_DEFAULTS.gap, style, children }: GridProps) {
|
|
13
|
+
const [width, setWidth] = useState(0);
|
|
14
|
+
const space = GAPS[gap];
|
|
15
|
+
const fit = Math.max(1, Math.min(columns, Math.floor((width + space) / (MIN_ITEM + space))));
|
|
16
|
+
const itemWidth = width ? (width - space * (fit - 1)) / fit : undefined;
|
|
17
|
+
return (
|
|
18
|
+
<View
|
|
19
|
+
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
|
|
20
|
+
style={[{ flexDirection: 'row', flexWrap: 'wrap', gap: space }, style]}
|
|
21
|
+
>
|
|
22
|
+
{Children.map(children, (child, i) => (
|
|
23
|
+
<View style={{ width: itemWidth ?? '100%' }}>
|
|
24
|
+
<SiblingIndexContext.Provider value={i}>{child}</SiblingIndexContext.Provider>
|
|
25
|
+
</View>
|
|
26
|
+
))}
|
|
27
|
+
</View>
|
|
28
|
+
);
|
|
29
|
+
}
|