jfs-components 0.0.40 → 0.0.43
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/lib/commonjs/components/Form/Form.js +83 -0
- package/lib/commonjs/components/FormField/FormField.js +228 -0
- package/lib/commonjs/components/Toast/Toast.js +93 -0
- package/lib/commonjs/components/Toast/ToastProvider.js +61 -0
- package/lib/commonjs/components/Toast/useToast.js +61 -0
- package/lib/commonjs/components/index.js +60 -0
- package/lib/commonjs/design-tokens/JFS Variables-variables-full.json +1 -1
- package/lib/commonjs/icons/registry.js +1 -1
- package/lib/module/components/Form/Form.js +77 -0
- package/lib/module/components/FormField/FormField.js +222 -0
- package/lib/module/components/Toast/Toast.js +88 -0
- package/lib/module/components/Toast/ToastProvider.js +55 -0
- package/lib/module/components/Toast/useToast.js +54 -0
- package/lib/module/components/index.js +7 -1
- package/lib/module/design-tokens/JFS Variables-variables-full.json +1 -1
- package/lib/module/icons/registry.js +1 -1
- package/lib/typescript/App.d.ts +2 -0
- package/lib/typescript/babel.config.d.ts +6 -0
- package/lib/typescript/build/bundle.d.ts +2 -0
- package/lib/typescript/debug-gap.d.ts +2 -0
- package/lib/typescript/index.d.ts +1 -4
- package/lib/typescript/metro.config.d.ts +78 -0
- package/lib/typescript/react-native.config.d.ts +4 -0
- package/lib/typescript/scripts/extract-component-tokens.d.ts +9 -0
- package/lib/typescript/scripts/generate-component-docs.d.ts +9 -0
- package/lib/typescript/scripts/generate-icon-registry.d.ts +3 -0
- package/lib/typescript/src/components/Form/Form.d.ts +19 -0
- package/lib/typescript/src/components/FormField/FormField.d.ts +27 -0
- package/lib/typescript/src/components/Toast/Toast.d.ts +14 -0
- package/lib/typescript/src/components/Toast/ToastProvider.d.ts +11 -0
- package/lib/typescript/src/components/Toast/useToast.d.ts +24 -0
- package/lib/typescript/{components → src/components}/index.d.ts +6 -0
- package/lib/typescript/{icons → src/icons}/registry.d.ts +1 -1
- package/lib/typescript/src/index.d.ts +5 -0
- package/lib/typescript/webpack.config.d.ts +35 -0
- package/package.json +3 -1
- package/src/components/Form/Form.tsx +97 -0
- package/src/components/FormField/FormField.tsx +265 -0
- package/src/components/Toast/Toast.tsx +105 -0
- package/src/components/Toast/ToastProvider.tsx +75 -0
- package/src/components/Toast/useToast.ts +80 -0
- package/src/components/index.ts +6 -1
- package/src/design-tokens/JFS Variables-variables-full.json +1 -1
- package/src/icons/registry.ts +1 -1
- /package/lib/typescript/{Containers.d.ts → src/Containers.d.ts} +0 -0
- /package/lib/typescript/{components → src/components}/Accordion/Accordion.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ActionFooter/ActionFooter.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ActionTile/ActionTile.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ActionTile/index.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/AppBar/AppBar.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Avatar/Avatar.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/AvatarGroup/AvatarGroup.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Badge/Badge.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Balance/Balance.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/BottomNav/BottomNav.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/BottomNavItem/BottomNavItem.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Button/Button.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ButtonGroup/ButtonGroup.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Card/Card.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/CardFeedback/CardFeedback.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Carousel/Carousel.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ChipGroup/ChipGroup.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ChipSelect/ChipSelect.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/CtaCard/CtaCard.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/CtaCard/index.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Disclaimer/Disclaimer.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Divider/Divider.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Drawer/Drawer.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/EmptyState/EmptyState.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/FilterBar/FilterBar.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/HStack/HStack.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/IconButton/IconButton.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/IconCapsule/IconCapsule.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/InputSearch/InputSearch.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/LazyList/LazyList.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/LinearMeter/LinearMeter.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/LinearMeter/index.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ListGroup/ListGroup.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ListItem/ListItem.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/MediaCard/MediaCard.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/MerchantProfile/MerchantProfile.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/MoneyValue/MoneyValue.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/NavArrow/NavArrow.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/PageTitle/PageTitle.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/RadioButton/RadioButton.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/RechargeCard/RechargeCard.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Screen/Screen.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Section/Section.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Stepper/Step.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Stepper/StepLabel.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Stepper/Stepper.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/SupportText/SupportText.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/SupportText/SupportTextIcon.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/SupportText/index.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/TextInput/TextInput.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/ThreadHero/ThreadHero.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/Tooltip/Tooltip.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/TransactionDetails/TransactionDetails.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/TransactionStatus/TransactionStatus.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/UpiHandle/UpiHandle.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/VStack/VStack.d.ts +0 -0
- /package/lib/typescript/{components → src/components}/docs/DocSections.d.ts +0 -0
- /package/lib/typescript/{design-tokens → src/design-tokens}/JFSThemeProvider.d.ts +0 -0
- /package/lib/typescript/{design-tokens → src/design-tokens}/figma-variables-resolver.d.ts +0 -0
- /package/lib/typescript/{design-tokens → src/design-tokens}/index.d.ts +0 -0
- /package/lib/typescript/{icons → src/icons}/Icon.d.ts +0 -0
- /package/lib/typescript/{icons → src/icons}/index.d.ts +0 -0
- /package/lib/typescript/{utils → src/utils}/react-utils.d.ts +0 -0
- /package/lib/typescript/{utils → src/utils}/web-platform-utils.d.ts +0 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import React, { useState, useMemo, useCallback } from 'react'
|
|
2
|
+
import { View, Text, type StyleProp, type ViewStyle, type TextStyle } from 'react-native'
|
|
3
|
+
import { getVariableByName } from '../../design-tokens/figma-variables-resolver'
|
|
4
|
+
import { useTokens } from '../../design-tokens/JFSThemeProvider'
|
|
5
|
+
import TextInput from '../TextInput/TextInput'
|
|
6
|
+
import SupportText from '../SupportText/SupportText'
|
|
7
|
+
import type { SupportTextStatus } from '../SupportText/SupportTextIcon'
|
|
8
|
+
|
|
9
|
+
export type FormFieldType = 'text' | 'password' | 'email' | 'search'
|
|
10
|
+
|
|
11
|
+
export type FormFieldProps = {
|
|
12
|
+
label?: string | undefined;
|
|
13
|
+
placeholder?: string | undefined;
|
|
14
|
+
value?: string | undefined;
|
|
15
|
+
onChangeText?: ((text: string) => void) | undefined;
|
|
16
|
+
type?: FormFieldType | undefined;
|
|
17
|
+
|
|
18
|
+
leading?: React.ReactNode;
|
|
19
|
+
trailing?: React.ReactNode;
|
|
20
|
+
leadingIconName?: string | undefined;
|
|
21
|
+
|
|
22
|
+
isRequired?: boolean | undefined;
|
|
23
|
+
isDisabled?: boolean | undefined;
|
|
24
|
+
isInvalid?: boolean | undefined;
|
|
25
|
+
|
|
26
|
+
supportText?: string | undefined;
|
|
27
|
+
errorMessage?: string | undefined;
|
|
28
|
+
|
|
29
|
+
modes?: Record<string, any> | undefined;
|
|
30
|
+
style?: StyleProp<ViewStyle>;
|
|
31
|
+
|
|
32
|
+
onFocus?: ((e: any) => void) | undefined;
|
|
33
|
+
onBlur?: ((e: any) => void) | undefined;
|
|
34
|
+
accessibilityLabel?: string | undefined;
|
|
35
|
+
accessibilityHint?: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function useFormField(props: FormFieldProps) {
|
|
39
|
+
const {
|
|
40
|
+
type = 'text',
|
|
41
|
+
isDisabled = false,
|
|
42
|
+
isInvalid = false,
|
|
43
|
+
supportText,
|
|
44
|
+
errorMessage,
|
|
45
|
+
modes: propModes = {},
|
|
46
|
+
onFocus,
|
|
47
|
+
onBlur,
|
|
48
|
+
} = props
|
|
49
|
+
|
|
50
|
+
const { modes: globalModes } = useTokens()
|
|
51
|
+
const baseModes = useMemo(
|
|
52
|
+
() => ({ ...globalModes, ...propModes }),
|
|
53
|
+
[globalModes, propModes],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const [isFocused, setIsFocused] = useState(false)
|
|
57
|
+
|
|
58
|
+
// Merge FormField States collection based on focus
|
|
59
|
+
const modes = useMemo(() => ({
|
|
60
|
+
...baseModes,
|
|
61
|
+
'FormField States': isFocused ? 'Active' : 'Idle',
|
|
62
|
+
}), [baseModes, isFocused])
|
|
63
|
+
|
|
64
|
+
// -- Label tokens (from "FormField / Output" collection) --
|
|
65
|
+
const labelColor = getVariableByName('formField/label/color', modes) || '#0c0d10'
|
|
66
|
+
const labelFontFamily = getVariableByName('formField/label/fontFamily', modes) || 'JioType Var'
|
|
67
|
+
const labelFontSize = parseInt(getVariableByName('formField/label/fontSize', modes), 10) || 14
|
|
68
|
+
const labelLineHeight = parseInt(getVariableByName('formField/label/lineHeight', modes), 10) || 17
|
|
69
|
+
const labelFontWeight = getVariableByName('formField/label/fontWeight', modes) || '500'
|
|
70
|
+
const gap = parseInt(getVariableByName('formField/gap', modes), 10) || 8
|
|
71
|
+
|
|
72
|
+
// -- Input tokens (from "FormField / Output" + "FormField States" collections) --
|
|
73
|
+
const inputPaddingH = parseInt(getVariableByName('formField/input/padding/horizontal', modes), 10) || 12
|
|
74
|
+
const inputGap = parseInt(getVariableByName('formField/input/gap', modes), 10) || 8
|
|
75
|
+
const inputRadius = parseInt(getVariableByName('formField/input/radius', modes), 10) || 8
|
|
76
|
+
const inputBackground = getVariableByName('formField/input/background', modes) || '#ffffff'
|
|
77
|
+
const inputFontSize = parseInt(getVariableByName('formField/input/label/fontSize', modes), 10) || 16
|
|
78
|
+
const inputLineHeight = parseInt(getVariableByName('formField/input/label/lineHeight', modes), 10) || 45
|
|
79
|
+
const inputFontFamily = getVariableByName('formField/input/label/fontFamily', modes) || 'JioType Var'
|
|
80
|
+
const inputFontWeight = getVariableByName('formField/input/label/fontWeight', modes) || '400'
|
|
81
|
+
const inputTextColor = getVariableByName('states/formField/input/label/color', modes)
|
|
82
|
+
|| getVariableByName('formField/input/label/color', modes)
|
|
83
|
+
|| '#24262b'
|
|
84
|
+
const inputBorderColor = getVariableByName('states/formField/input/border/color', modes)
|
|
85
|
+
|| getVariableByName('formField/input/border/color', modes)
|
|
86
|
+
|| '#b5b6b7'
|
|
87
|
+
const inputBorderSize = parseInt(getVariableByName('formField/input/border/size', modes), 10) || 1
|
|
88
|
+
|
|
89
|
+
// -- Styles --
|
|
90
|
+
const labelStyle: TextStyle = useMemo(() => ({
|
|
91
|
+
color: labelColor,
|
|
92
|
+
fontFamily: labelFontFamily,
|
|
93
|
+
fontSize: labelFontSize,
|
|
94
|
+
lineHeight: labelLineHeight,
|
|
95
|
+
fontWeight: labelFontWeight as TextStyle['fontWeight'],
|
|
96
|
+
}), [labelColor, labelFontFamily, labelFontSize, labelLineHeight, labelFontWeight])
|
|
97
|
+
|
|
98
|
+
const wrapperStyle: ViewStyle = useMemo(() => ({
|
|
99
|
+
gap,
|
|
100
|
+
opacity: isDisabled ? 0.5 : 1,
|
|
101
|
+
}), [gap, isDisabled])
|
|
102
|
+
|
|
103
|
+
const requiredIndicatorStyle: TextStyle = useMemo(() => ({
|
|
104
|
+
color: '#d93d3d',
|
|
105
|
+
fontFamily: labelFontFamily,
|
|
106
|
+
fontSize: labelFontSize,
|
|
107
|
+
lineHeight: labelLineHeight,
|
|
108
|
+
fontWeight: labelFontWeight as TextStyle['fontWeight'],
|
|
109
|
+
}), [labelFontFamily, labelFontSize, labelLineHeight, labelFontWeight])
|
|
110
|
+
|
|
111
|
+
// Style overrides for the input row, sourced from formField/input/* tokens
|
|
112
|
+
const inputContainerStyle: ViewStyle = useMemo(() => ({
|
|
113
|
+
backgroundColor: inputBackground,
|
|
114
|
+
borderColor: inputBorderColor,
|
|
115
|
+
borderWidth: inputBorderSize,
|
|
116
|
+
borderRadius: inputRadius,
|
|
117
|
+
paddingHorizontal: inputPaddingH,
|
|
118
|
+
gap: inputGap,
|
|
119
|
+
}), [inputBackground, inputBorderColor, inputBorderSize, inputRadius, inputPaddingH, inputGap])
|
|
120
|
+
|
|
121
|
+
const inputTextStyle: TextStyle = useMemo(() => ({
|
|
122
|
+
color: inputTextColor,
|
|
123
|
+
fontSize: inputFontSize,
|
|
124
|
+
lineHeight: inputLineHeight,
|
|
125
|
+
fontFamily: inputFontFamily,
|
|
126
|
+
fontWeight: inputFontWeight as TextStyle['fontWeight'],
|
|
127
|
+
}), [inputTextColor, inputFontSize, inputLineHeight, inputFontFamily, inputFontWeight])
|
|
128
|
+
|
|
129
|
+
// -- Support text logic --
|
|
130
|
+
const supportStatus: SupportTextStatus = isInvalid ? 'Error' : 'Neutral'
|
|
131
|
+
const supportLabel = isInvalid && errorMessage ? errorMessage : supportText
|
|
132
|
+
|
|
133
|
+
// -- Input type derived props --
|
|
134
|
+
const secureTextEntry = type === 'password'
|
|
135
|
+
const keyboardType = type === 'email' ? 'email-address' as const : 'default' as const
|
|
136
|
+
const autoCapitalize = (type === 'email' || type === 'password') ? 'none' as const : 'sentences' as const
|
|
137
|
+
|
|
138
|
+
// -- Event handlers --
|
|
139
|
+
const handleFocus = useCallback((e: any) => {
|
|
140
|
+
setIsFocused(true)
|
|
141
|
+
onFocus?.(e)
|
|
142
|
+
}, [onFocus])
|
|
143
|
+
|
|
144
|
+
const handleBlur = useCallback((e: any) => {
|
|
145
|
+
setIsFocused(false)
|
|
146
|
+
onBlur?.(e)
|
|
147
|
+
}, [onBlur])
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
modes,
|
|
151
|
+
labelStyle,
|
|
152
|
+
wrapperStyle,
|
|
153
|
+
requiredIndicatorStyle,
|
|
154
|
+
inputContainerStyle,
|
|
155
|
+
inputTextStyle,
|
|
156
|
+
supportStatus,
|
|
157
|
+
supportLabel,
|
|
158
|
+
secureTextEntry,
|
|
159
|
+
keyboardType,
|
|
160
|
+
autoCapitalize,
|
|
161
|
+
handleFocus,
|
|
162
|
+
handleBlur,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function FormField({
|
|
167
|
+
label,
|
|
168
|
+
placeholder,
|
|
169
|
+
value = '',
|
|
170
|
+
onChangeText,
|
|
171
|
+
type = 'text',
|
|
172
|
+
leading,
|
|
173
|
+
trailing,
|
|
174
|
+
leadingIconName,
|
|
175
|
+
isRequired = false,
|
|
176
|
+
isDisabled = false,
|
|
177
|
+
isInvalid = false,
|
|
178
|
+
supportText,
|
|
179
|
+
errorMessage,
|
|
180
|
+
modes = {},
|
|
181
|
+
style,
|
|
182
|
+
onFocus,
|
|
183
|
+
onBlur,
|
|
184
|
+
accessibilityLabel,
|
|
185
|
+
accessibilityHint,
|
|
186
|
+
}: FormFieldProps) {
|
|
187
|
+
const {
|
|
188
|
+
modes: resolvedModes,
|
|
189
|
+
labelStyle,
|
|
190
|
+
wrapperStyle,
|
|
191
|
+
requiredIndicatorStyle,
|
|
192
|
+
inputContainerStyle,
|
|
193
|
+
inputTextStyle,
|
|
194
|
+
supportStatus,
|
|
195
|
+
supportLabel,
|
|
196
|
+
secureTextEntry,
|
|
197
|
+
keyboardType,
|
|
198
|
+
autoCapitalize,
|
|
199
|
+
handleFocus,
|
|
200
|
+
handleBlur,
|
|
201
|
+
} = useFormField({
|
|
202
|
+
type,
|
|
203
|
+
isDisabled,
|
|
204
|
+
isInvalid,
|
|
205
|
+
supportText,
|
|
206
|
+
errorMessage,
|
|
207
|
+
modes,
|
|
208
|
+
onFocus,
|
|
209
|
+
onBlur,
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
const resolvedA11yLabel = accessibilityLabel || label || placeholder || 'Form field'
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<View
|
|
216
|
+
style={[wrapperStyle, style]}
|
|
217
|
+
pointerEvents={isDisabled ? 'none' : 'auto'}
|
|
218
|
+
accessible={true}
|
|
219
|
+
accessibilityRole="none"
|
|
220
|
+
accessibilityLabel={resolvedA11yLabel}
|
|
221
|
+
accessibilityState={{
|
|
222
|
+
disabled: isDisabled,
|
|
223
|
+
}}
|
|
224
|
+
>
|
|
225
|
+
{label != null && (
|
|
226
|
+
<View style={{ flexDirection: 'row', alignItems: 'baseline' }}>
|
|
227
|
+
<Text style={labelStyle}>{label}</Text>
|
|
228
|
+
{isRequired && (
|
|
229
|
+
<Text style={requiredIndicatorStyle}> *</Text>
|
|
230
|
+
)}
|
|
231
|
+
</View>
|
|
232
|
+
)}
|
|
233
|
+
|
|
234
|
+
<TextInput
|
|
235
|
+
placeholder={placeholder || ''}
|
|
236
|
+
value={value}
|
|
237
|
+
{...(onChangeText ? { onChangeText } : {})}
|
|
238
|
+
leading={leading}
|
|
239
|
+
trailing={trailing}
|
|
240
|
+
leadingIconName={leadingIconName || 'ic_search'}
|
|
241
|
+
modes={resolvedModes}
|
|
242
|
+
style={inputContainerStyle}
|
|
243
|
+
inputStyle={inputTextStyle}
|
|
244
|
+
onFocus={handleFocus}
|
|
245
|
+
onBlur={handleBlur}
|
|
246
|
+
secureTextEntry={secureTextEntry}
|
|
247
|
+
keyboardType={keyboardType}
|
|
248
|
+
autoCapitalize={autoCapitalize}
|
|
249
|
+
editable={!isDisabled}
|
|
250
|
+
accessibilityLabel={resolvedA11yLabel}
|
|
251
|
+
accessibilityHint={accessibilityHint || ''}
|
|
252
|
+
/>
|
|
253
|
+
|
|
254
|
+
{supportLabel != null && (
|
|
255
|
+
<SupportText
|
|
256
|
+
label={supportLabel}
|
|
257
|
+
status={supportStatus}
|
|
258
|
+
modes={resolvedModes}
|
|
259
|
+
/>
|
|
260
|
+
)}
|
|
261
|
+
</View>
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export default FormField
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react'
|
|
2
|
+
import { StyleSheet, Text, type StyleProp, type ViewStyle } from 'react-native'
|
|
3
|
+
import Animated, { FadeIn, FadeOut, SlideInDown, SlideInUp } from 'react-native-reanimated'
|
|
4
|
+
import { getVariableByName } from '../../design-tokens/figma-variables-resolver'
|
|
5
|
+
import { closeToast, type ToastPlacement } from './useToast'
|
|
6
|
+
|
|
7
|
+
export interface ToastProps {
|
|
8
|
+
id: string
|
|
9
|
+
title: string
|
|
10
|
+
timeout?: number | undefined
|
|
11
|
+
onClose?: (() => void) | undefined
|
|
12
|
+
modes?: Record<string, any> | undefined
|
|
13
|
+
placement?: ToastPlacement | undefined
|
|
14
|
+
style?: StyleProp<ViewStyle> | undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ANIMATION_DURATION = 250
|
|
18
|
+
|
|
19
|
+
function Toast({
|
|
20
|
+
id,
|
|
21
|
+
title,
|
|
22
|
+
timeout = 4000,
|
|
23
|
+
onClose,
|
|
24
|
+
modes = {},
|
|
25
|
+
placement = 'bottom',
|
|
26
|
+
style,
|
|
27
|
+
}: ToastProps) {
|
|
28
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (timeout <= 0) return
|
|
32
|
+
timerRef.current = setTimeout(() => closeToast(id), timeout)
|
|
33
|
+
return () => {
|
|
34
|
+
if (timerRef.current) clearTimeout(timerRef.current)
|
|
35
|
+
}
|
|
36
|
+
}, [id, timeout])
|
|
37
|
+
|
|
38
|
+
const backgroundColor = getVariableByName('toast/background', modes) || '#303338'
|
|
39
|
+
const foreground = getVariableByName('toast/foreground', modes) || '#ffffff'
|
|
40
|
+
const fontSize = getVariableByName('toast/fontSize', modes) || 14
|
|
41
|
+
const fontFamily = getVariableByName('toast/fontFamily', modes) || undefined
|
|
42
|
+
const fontWeight = getVariableByName('toast/fontWeight', modes) || '500'
|
|
43
|
+
const lineHeight = getVariableByName('toast/lineHeight', modes) || 18
|
|
44
|
+
const radius = getVariableByName('toast/radius', modes) || 14
|
|
45
|
+
const paddingHorizontal = getVariableByName('toast/padding/horizontal', modes) || 16
|
|
46
|
+
const paddingVertical = getVariableByName('toast/padding/vertical', modes) || 14
|
|
47
|
+
const gap = getVariableByName('toast/gap', modes) || 8
|
|
48
|
+
const borderWidth = getVariableByName('toast/border/size', modes) || 1
|
|
49
|
+
const borderColor = getVariableByName('toast/border/color', modes) || 'rgba(255,255,255,0.1)'
|
|
50
|
+
const shadowBlurPrimary = getVariableByName('toast/shadow/primary/blur', modes) || 48
|
|
51
|
+
const shadowOffsetYPrimary = getVariableByName('toast/shadow/primary/offsetY', modes) || 16
|
|
52
|
+
const shadowColorPrimary = getVariableByName('toast/shadow/primary/color', modes) || 'rgba(13,13,15,0.16)'
|
|
53
|
+
|
|
54
|
+
const enterAnimation = placement === 'top'
|
|
55
|
+
? SlideInUp.duration(ANIMATION_DURATION)
|
|
56
|
+
: SlideInDown.duration(ANIMATION_DURATION)
|
|
57
|
+
|
|
58
|
+
const containerStyle: ViewStyle = {
|
|
59
|
+
backgroundColor,
|
|
60
|
+
borderRadius: radius,
|
|
61
|
+
paddingHorizontal,
|
|
62
|
+
paddingVertical,
|
|
63
|
+
gap,
|
|
64
|
+
borderWidth,
|
|
65
|
+
borderColor,
|
|
66
|
+
shadowColor: shadowColorPrimary,
|
|
67
|
+
shadowOffset: { width: 0, height: shadowOffsetYPrimary },
|
|
68
|
+
shadowOpacity: 1,
|
|
69
|
+
shadowRadius: shadowBlurPrimary / 2,
|
|
70
|
+
elevation: 8,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const textStyle = {
|
|
74
|
+
color: foreground,
|
|
75
|
+
fontSize,
|
|
76
|
+
fontFamily,
|
|
77
|
+
fontWeight: String(fontWeight) as any,
|
|
78
|
+
lineHeight,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<Animated.View
|
|
83
|
+
entering={enterAnimation}
|
|
84
|
+
exiting={FadeOut.duration(ANIMATION_DURATION)}
|
|
85
|
+
style={[styles.toast, containerStyle, style]}
|
|
86
|
+
accessibilityRole="alert"
|
|
87
|
+
accessibilityLiveRegion="assertive"
|
|
88
|
+
>
|
|
89
|
+
<Text style={textStyle} numberOfLines={2}>
|
|
90
|
+
{title}
|
|
91
|
+
</Text>
|
|
92
|
+
</Animated.View>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const styles = StyleSheet.create({
|
|
97
|
+
toast: {
|
|
98
|
+
alignSelf: 'center',
|
|
99
|
+
maxWidth: '90%',
|
|
100
|
+
minWidth: 200,
|
|
101
|
+
overflow: 'hidden',
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
export default Toast
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import React, { useMemo } from 'react'
|
|
2
|
+
import { StyleSheet, View } from 'react-native'
|
|
3
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context'
|
|
4
|
+
import { useToast, type ToastPlacement } from './useToast'
|
|
5
|
+
import Toast from './Toast'
|
|
6
|
+
|
|
7
|
+
export interface ToastProviderProps {
|
|
8
|
+
children: React.ReactNode
|
|
9
|
+
maxVisibleToasts?: number
|
|
10
|
+
placement?: ToastPlacement
|
|
11
|
+
modes?: Record<string, any>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function ToastProvider({
|
|
15
|
+
children,
|
|
16
|
+
maxVisibleToasts = 3,
|
|
17
|
+
placement = 'bottom',
|
|
18
|
+
modes,
|
|
19
|
+
}: ToastProviderProps) {
|
|
20
|
+
const { toasts } = useToast()
|
|
21
|
+
const insets = useSafeAreaInsets()
|
|
22
|
+
|
|
23
|
+
const visibleToasts = useMemo(
|
|
24
|
+
() => toasts.slice(-maxVisibleToasts),
|
|
25
|
+
[toasts, maxVisibleToasts],
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const regionStyle = useMemo(
|
|
29
|
+
() => [
|
|
30
|
+
styles.region,
|
|
31
|
+
placement === 'top'
|
|
32
|
+
? { top: insets.top + 8 }
|
|
33
|
+
: { bottom: insets.bottom + 8 },
|
|
34
|
+
],
|
|
35
|
+
[placement, insets.top, insets.bottom],
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<View style={styles.container}>
|
|
40
|
+
{children}
|
|
41
|
+
{visibleToasts.length > 0 && (
|
|
42
|
+
<View style={regionStyle} pointerEvents="box-none">
|
|
43
|
+
{visibleToasts.map((entry) => (
|
|
44
|
+
<Toast
|
|
45
|
+
key={entry.id}
|
|
46
|
+
id={entry.id}
|
|
47
|
+
title={entry.title}
|
|
48
|
+
timeout={entry.timeout}
|
|
49
|
+
onClose={entry.onClose}
|
|
50
|
+
modes={entry.modes ?? modes}
|
|
51
|
+
placement={placement}
|
|
52
|
+
/>
|
|
53
|
+
))}
|
|
54
|
+
</View>
|
|
55
|
+
)}
|
|
56
|
+
</View>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const styles = StyleSheet.create({
|
|
61
|
+
container: {
|
|
62
|
+
flex: 1,
|
|
63
|
+
},
|
|
64
|
+
region: {
|
|
65
|
+
position: 'absolute',
|
|
66
|
+
left: 0,
|
|
67
|
+
right: 0,
|
|
68
|
+
alignItems: 'center',
|
|
69
|
+
gap: 8,
|
|
70
|
+
zIndex: 9999,
|
|
71
|
+
pointerEvents: 'box-none',
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
export default ToastProvider
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react'
|
|
2
|
+
|
|
3
|
+
export type ToastPlacement = 'top' | 'bottom'
|
|
4
|
+
|
|
5
|
+
export interface ToastOptions {
|
|
6
|
+
title: string
|
|
7
|
+
timeout?: number
|
|
8
|
+
onClose?: () => void
|
|
9
|
+
modes?: Record<string, any>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ToastEntry {
|
|
13
|
+
id: string
|
|
14
|
+
title: string
|
|
15
|
+
timeout: number
|
|
16
|
+
onClose?: (() => void) | undefined
|
|
17
|
+
modes?: Record<string, any> | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Listener = () => void
|
|
21
|
+
|
|
22
|
+
let idCounter = 0
|
|
23
|
+
let toasts: ToastEntry[] = []
|
|
24
|
+
const listeners = new Set<Listener>()
|
|
25
|
+
|
|
26
|
+
function emit() {
|
|
27
|
+
for (const listener of listeners) {
|
|
28
|
+
listener()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getSnapshot(): ToastEntry[] {
|
|
33
|
+
return toasts
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function subscribe(listener: Listener): () => void {
|
|
37
|
+
listeners.add(listener)
|
|
38
|
+
return () => {
|
|
39
|
+
listeners.delete(listener)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function addToast(options: ToastOptions): string {
|
|
44
|
+
const id = `toast-${++idCounter}`
|
|
45
|
+
const entry: ToastEntry = {
|
|
46
|
+
id,
|
|
47
|
+
title: options.title,
|
|
48
|
+
timeout: options.timeout ?? 4000,
|
|
49
|
+
onClose: options.onClose,
|
|
50
|
+
modes: options.modes,
|
|
51
|
+
}
|
|
52
|
+
toasts = [...toasts, entry]
|
|
53
|
+
emit()
|
|
54
|
+
return id
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function closeToast(id: string): void {
|
|
58
|
+
const entry = toasts.find((t) => t.id === id)
|
|
59
|
+
toasts = toasts.filter((t) => t.id !== id)
|
|
60
|
+
emit()
|
|
61
|
+
entry?.onClose?.()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function closeAll(): void {
|
|
65
|
+
const prev = toasts
|
|
66
|
+
toasts = []
|
|
67
|
+
emit()
|
|
68
|
+
prev.forEach((t) => t.onClose?.())
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function useToast() {
|
|
72
|
+
const queue = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
toasts: queue,
|
|
76
|
+
addToast: useCallback(addToast, []),
|
|
77
|
+
closeToast: useCallback(closeToast, []),
|
|
78
|
+
closeAll: useCallback(closeAll, []),
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -13,6 +13,9 @@ export { default as Divider, type DividerProps, type DividerDirection } from './
|
|
|
13
13
|
export { default as Drawer } from './Drawer/Drawer';
|
|
14
14
|
export { default as CtaCard } from './CtaCard/CtaCard';
|
|
15
15
|
export { default as FilterBar } from './FilterBar/FilterBar';
|
|
16
|
+
export { default as Form, type FormProps } from './Form/Form';
|
|
17
|
+
export { useFormContext } from './Form/Form';
|
|
18
|
+
export { default as FormField, type FormFieldProps } from './FormField/FormField';
|
|
16
19
|
export { default as HStack, type HStackProps } from './HStack/HStack';
|
|
17
20
|
export { default as IconButton } from './IconButton/IconButton';
|
|
18
21
|
export { default as IconCapsule } from './IconCapsule/IconCapsule';
|
|
@@ -48,4 +51,6 @@ export { default as InputSearch, type InputSearchProps } from './InputSearch/Inp
|
|
|
48
51
|
export { default as SupportText, type SupportTextProps } from './SupportText/SupportText';
|
|
49
52
|
export { default as SupportTextIcon, type SupportTextIconProps } from './SupportText/SupportTextIcon';
|
|
50
53
|
export { default as RadioButton, type RadioButtonProps } from './RadioButton/RadioButton';
|
|
51
|
-
|
|
54
|
+
export { default as Toast, type ToastProps } from './Toast/Toast';
|
|
55
|
+
export { default as ToastProvider, type ToastProviderProps } from './Toast/ToastProvider';
|
|
56
|
+
export { useToast, addToast, closeToast, closeAll, type ToastOptions, type ToastEntry, type ToastPlacement } from './Toast/useToast';
|